Example usage for org.antlr.v4.runtime Token getLine

List of usage examples for org.antlr.v4.runtime Token getLine

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getLine.

Prototype

int getLine();

Source Link

Document

The line number on which the 1st character of this token was matched, line=1..n

Usage

From source file:com.facebook.presto.sql.parser.AstBuilder.java

License:Apache License

public static NodeLocation getLocation(Token token) {
    requireNonNull(token, "token is null");
    return new NodeLocation(token.getLine(), token.getCharPositionInLine());
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public Template visitBlock(final BlockContext ctx) {
    SexprContext sexpr = ctx.sexpr();//from w  ww  .  j ava  2 s .c  om
    Token nameStart = sexpr.QID().getSymbol();
    String name = nameStart.getText();
    String nameEnd = ctx.nameEnd.getText();
    if (!name.equals(nameEnd)) {
        reportError(null, ctx.nameEnd.getLine(), ctx.nameEnd.getCharPositionInLine(),
                String.format("found: '%s', expected: '%s'", nameEnd, name));
    }

    hasTag(true);
    Block block = new Block(handlebars, name, false, params(sexpr.param()), hash(sexpr.hash()));
    block.filename(source.filename());
    block.position(nameStart.getLine(), nameStart.getCharPositionInLine());
    String startDelim = ctx.start.getText();
    startDelim = startDelim.substring(0, startDelim.length() - 1);
    block.startDelimiter(startDelim);
    block.endDelimiter(ctx.stop.getText());

    Template body = visitBody(ctx.thenBody);
    if (body != null) {
        block.body(body);
    }
    ElseBlockContext elseBlock = ctx.elseBlock();
    if (elseBlock != null) {
        Template unless = visitBody(elseBlock.unlessBody);
        if (unless != null) {
            String inverseLabel = elseBlock.inverseToken.getText();
            if (inverseLabel.startsWith(startDelim)) {
                inverseLabel = inverseLabel.substring(startDelim.length());
            }
            block.inverse(inverseLabel, unless);
        }
    }
    hasTag(true);
    return block;
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public Object visitEscape(final EscapeContext ctx) {
    Token token = ctx.ESC_VAR().getSymbol();
    String text = token.getText().substring(1);
    line.append(text);/*from  w  ww.j av  a 2 s.  com*/
    return new Text(text, "\\").filename(source.filename()).position(token.getLine(),
            token.getCharPositionInLine());
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

/**
 * Build a new {@link Variable}./*from   ww  w  .  ja v  a2 s  .  c o m*/
 *
 * @param name The var's name.
 * @param varType The var's type.
 * @param params The var params.
 * @param hash The var hash.
 * @param startDelimiter The current start delimiter.
 * @param endDelimiter The current end delimiter.
 * @return A new {@link Variable}.
 */
private Template newVar(final Token name, final TagType varType, final List<Object> params,
        final Map<String, Object> hash, final String startDelimiter, final String endDelimiter) {
    String varName = name.getText();
    Helper<Object> helper = handlebars.helper(varName);
    if (helper == null && ((params.size() > 0 || hash.size() > 0) || varType == TagType.SUB_EXPRESSION)) {
        Helper<Object> helperMissing = handlebars.helper(HelperRegistry.HELPER_MISSING);
        if (helperMissing == null) {
            reportError(null, name.getLine(), name.getCharPositionInLine(),
                    "could not find helper: '" + varName + "'");
        }
    }
    return new Variable(handlebars, varName, varType, params, hash).startDelimiter(startDelimiter)
            .endDelimiter(endDelimiter).filename(source.filename())
            .position(name.getLine(), name.getCharPositionInLine());
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public Template visitPartial(final PartialContext ctx) {
    hasTag(true);/* w  w  w.j a v  a2 s  . co m*/
    Token pathToken = ctx.PATH().getSymbol();
    String uri = pathToken.getText();
    if (uri.startsWith("[") && uri.endsWith("]")) {
        uri = uri.substring(1, uri.length() - 1);
    }

    if (uri.startsWith("/")) {
        String message = "found: '/', partial shouldn't start with '/'";
        reportError(null, pathToken.getLine(), pathToken.getCharPositionInLine(), message);
    }

    String indent = line.toString();
    if (hasTag()) {
        if (isEmpty(indent) || !isEmpty(indent.trim())) {
            indent = null;
        }
    } else {
        indent = null;
    }

    TerminalNode partialContext = ctx.QID();
    String startDelim = ctx.start.getText();
    Template partial = new Partial(handlebars, uri, partialContext != null ? partialContext.getText() : null)
            .startDelimiter(startDelim.substring(0, startDelim.length() - 1)).endDelimiter(ctx.stop.getText())
            .indent(indent).filename(source.filename())
            .position(pathToken.getLine(), pathToken.getCharPositionInLine());

    return partial;
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public BaseTemplate visitNewline(final NewlineContext ctx) {
    Token newline = ctx.NL().getSymbol();
    if (newline.getChannel() == Token.HIDDEN_CHANNEL) {
        return null;
    }/*from  w w w.j a  v a  2s .  c om*/
    line.setLength(0);
    return new Text(newline.getText()).filename(source.filename()).position(newline.getLine(),
            newline.getCharPositionInLine());
}

From source file:com.ibm.bi.dml.parser.python.PydmlSyntacticValidator.java

License:Open Source License

private ConstIdentifier getConstIdFromString(String varValue, Token start) {
    // Both varName and varValue are correct
    int linePosition = start.getLine();
    int charPosition = start.getCharPositionInLine();
    try {//from   www  .j av a  2 s  .co m
        long val = Long.parseLong(varValue);
        return new IntIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition, linePosition,
                charPosition);
    } catch (Exception e) {
        try {
            double val = Double.parseDouble(varValue);
            return new DoubleIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition,
                    linePosition, charPosition);
        } catch (Exception e1) {
            try {
                if (varValue.compareTo("True") == 0 || varValue.compareTo("False") == 0) {
                    boolean val = false;
                    if (varValue.compareTo("True") == 0) {
                        val = true;
                    }
                    return new BooleanIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition,
                            linePosition, charPosition);
                } else {
                    String val = "";
                    String text = varValue;
                    if ((text.startsWith("\"") && text.endsWith("\""))
                            || (text.startsWith("\'") && text.endsWith("\'"))) {
                        if (text.length() > 2) {
                            val = text.substring(1, text.length() - 1);
                        }
                    } else {
                        val = text;
                        // the commandline parameters can be passed without any quotes
                        //                           helper.notifyErrorListeners("something wrong while parsing string ... strange", start);
                        //                           return null;
                    }
                    return new StringIdentifier(val, helper.getCurrentFileName(), linePosition, charPosition,
                            linePosition, charPosition);
                }
            } catch (Exception e3) {
                helper.notifyErrorListeners(
                        "unable to cast the commandline parameter into int/double/boolean/string", start);
                return null;
            }
        }
    }
}

From source file:com.ibm.bi.dml.parser.python.PydmlSyntacticValidatorHelper.java

License:Open Source License

public void notifyErrorListeners(String message, Token op) {
    this._errorListener.validationError(op.getLine(), op.getCharPositionInLine(), message);
}

From source file:com.microsoft.thrifty.schema.parser.ThriftListener.java

License:Open Source License

private Location locationOf(Token token) {
    int line = token.getLine();
    int col = token.getCharPositionInLine() + 1; // Location.col is 1-based, Token.col is 0-based
    return location.at(line, col);
}

From source file:com.oracle.truffle.sl.parser.SimpleLanguageParser.java

License:Open Source License

public void SemErr(Token token, String message) {
    assert token != null;
    throwParseError(source, token.getLine(), token.getCharPositionInLine(), token, message);
}