use of dyvil.source.TextSource in project Dyvil by Dyvil.
the class Colorizer method colorize.
public static String colorize(String text, REPLContext context) {
final TextSource source = new TextSource(text);
final TokenList tokens = new DyvilLexer(new MarkerList(Markers.INSTANCE), DyvilSymbols.INSTANCE).tokenize(text);
// Split into lines
final int lineCount = source.lineCount();
final StringBuilder[] lines = new StringBuilder[lineCount];
for (int i = 0; i < lineCount; i++) {
lines[i] = new StringBuilder(source.line(i + 1));
}
// iterate, starting from the last token
for (IToken token = tokens.last(); token != null && token.type() != Tokens.EOF; token = token.prev()) {
final String color = tokenColor(token, context);
if (color != null) {
// insert ANSI color codes before and after the token
final StringBuilder line = lines[token.startLine() - 1];
line.insert(token.endColumn(), Console.ANSI_RESET);
line.insert(token.startColumn(), color);
}
}
// Merge back together
final StringBuilder first = lines[0];
for (int i = 1; i < lineCount; i++) {
first.append('\n').append(lines[i]);
}
return first.toString();
}
use of dyvil.source.TextSource in project Dyvil by Dyvil.
the class REPLContext method startEvaluation.
// Evaluation
public void startEvaluation(String text) {
final String className = CLASS_PREFIX + this.classIndex++;
this.currentClass = new CodeClass(this, Name.fromRaw(className));
this.currentClass.setBody(new ClassBody(this.currentClass));
this.currentSource = new TextSource(text);
}
Aggregations