Search in sources :

Example 1 with TextSource

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();
}
Also used : MarkerList(dyvilx.tools.parsing.marker.MarkerList) DyvilLexer(dyvilx.tools.parsing.lexer.DyvilLexer) IToken(dyvilx.tools.parsing.token.IToken) TextSource(dyvil.source.TextSource) TokenList(dyvilx.tools.parsing.TokenList)

Example 2 with TextSource

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);
}
Also used : TextSource(dyvil.source.TextSource) CodeClass(dyvilx.tools.compiler.ast.classes.CodeClass) ClassBody(dyvilx.tools.compiler.ast.classes.ClassBody)

Aggregations

TextSource (dyvil.source.TextSource)2 ClassBody (dyvilx.tools.compiler.ast.classes.ClassBody)1 CodeClass (dyvilx.tools.compiler.ast.classes.CodeClass)1 TokenList (dyvilx.tools.parsing.TokenList)1 DyvilLexer (dyvilx.tools.parsing.lexer.DyvilLexer)1 MarkerList (dyvilx.tools.parsing.marker.MarkerList)1 IToken (dyvilx.tools.parsing.token.IToken)1