Search in sources :

Example 61 with Token

use of org.antlr.v4.runtime.Token in project incubator-systemml by apache.

the class CommonSyntacticValidator method unaryExpressionHelper.

protected void unaryExpressionHelper(ParserRuleContext ctx, ExpressionInfo left, ExpressionInfo me, String op) {
    if (left.expr != null) {
        Token start = ctx.start;
        String fileName = currentFile;
        int line = start.getLine();
        int col = start.getCharPositionInLine();
        if (left.expr instanceof IntIdentifier) {
            if (op.equals("-")) {
                ((IntIdentifier) left.expr).multiplyByMinusOne();
            }
            me.expr = left.expr;
        } else if (left.expr instanceof DoubleIdentifier) {
            if (op.equals("-")) {
                ((DoubleIdentifier) left.expr).multiplyByMinusOne();
            }
            me.expr = left.expr;
        } else {
            Expression right = new IntIdentifier(1, fileName, line, col, line, col);
            if (op.equals("-")) {
                right = new IntIdentifier(-1, fileName, line, col, line, col);
            }
            Expression.BinaryOp bop = Expression.getBinaryOp("*");
            BinaryExpression be = new BinaryExpression(bop);
            be.setLeft(left.expr);
            be.setRight(right);
            me.expr = be;
        }
        setFileLineColumn(me.expr, ctx);
    }
}
Also used : DoubleIdentifier(org.apache.sysml.parser.DoubleIdentifier) BinaryExpression(org.apache.sysml.parser.BinaryExpression) IntIdentifier(org.apache.sysml.parser.IntIdentifier) RelationalExpression(org.apache.sysml.parser.RelationalExpression) BooleanExpression(org.apache.sysml.parser.BooleanExpression) ParameterizedBuiltinFunctionExpression(org.apache.sysml.parser.ParameterizedBuiltinFunctionExpression) BuiltinFunctionExpression(org.apache.sysml.parser.BuiltinFunctionExpression) BinaryExpression(org.apache.sysml.parser.BinaryExpression) Expression(org.apache.sysml.parser.Expression) ParameterExpression(org.apache.sysml.parser.ParameterExpression) DataExpression(org.apache.sysml.parser.DataExpression) Token(org.antlr.v4.runtime.Token)

Example 62 with Token

use of org.antlr.v4.runtime.Token in project compiler by boalang.

the class BaseTest method lex.

protected CommonTokenStream lex(final String input, final int[] ids, final String[] strings, final String[] errors) throws IOException {
    final List<String> foundErr = new ArrayList<String>();
    final BoaLexer lexer = new BoaLexer(new ANTLRInputStream(new StringReader(input)));
    lexer.removeErrorListeners();
    lexer.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
            foundErr.add(line + "," + charPositionInLine + ": " + msg);
        }
    });
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    if (ids.length > 0 && strings.length > 0)
        assertEquals("ids != strings", ids.length, strings.length);
    if (ids.length > 0) {
        final List<Token> t = tokens.getTokens();
        if (DEBUG) {
            for (int i = 0; i < t.size(); i++) {
                final Token token = t.get(i);
                System.out.print(token.getType() + ", ");
            }
            System.out.println();
            for (int i = 0; i < t.size(); i++) {
                final Token token = t.get(i);
                System.out.print(token.getText() + ", ");
            }
            System.out.println();
            System.out.println();
        }
        assertEquals("wrong number of tokens", ids.length, t.size());
        for (int i = 0; i < t.size(); i++) assertEquals("wrong token type", ids[i], t.get(i).getType());
    }
    if (strings.length > 0) {
        final List<Token> t = tokens.getTokens();
        assertEquals("wrong number of tokens", strings.length, t.size());
        for (int i = 0; i < t.size(); i++) assertEquals("wrong token type", strings[i], t.get(i).getText());
    }
    assertEquals("wrong number of errors: " + input, errors.length, foundErr.size());
    for (int i = 0; i < foundErr.size(); i++) assertEquals("wrong error", errors[i], foundErr.get(i));
    return tokens;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) StringReader(java.io.StringReader) JavaFileObject(javax.tools.JavaFileObject) BoaLexer(boa.parser.BoaLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 63 with Token

use of org.antlr.v4.runtime.Token in project CFLint by cflint.

the class CFLint method suppressed.

/*
     * Look for a suppress comment on the same line. cflint:line - suppresses
     * any messages on the same line cflint:MESSAGE_CODE - suppresses any
     * message matching that code
     */
protected boolean suppressed(final BugInfo bugInfo, final Token token, final Context context) {
    if (context == null || context.isSuppressed(bugInfo)) {
        return true;
    }
    if (token == null) {
        return false;
    }
    final Iterable<Token> tokens = context.afterTokens(token);
    for (final Token currentTok : tokens) {
        if (currentTok.getLine() != token.getLine()) {
            break;
        }
        if (currentTok.getChannel() == Token.HIDDEN_CHANNEL && currentTok.getType() == CFSCRIPTLexer.LINE_COMMENT) {
            final String commentText = currentTok.getText().replaceFirst("^//\\s*", "").trim();
            if (commentText.startsWith("cflint ")) {
                final Pattern pattern = Pattern.compile("cflint\\s+ignore:([\\w,]+).*");
                final Matcher matcher = pattern.matcher(commentText);
                if (matcher.matches() && matcher.groupCount() > 0) {
                    final String ignoreCodes = matcher.group(1);
                    if ("line".equalsIgnoreCase(ignoreCodes)) {
                        return true;
                    }
                    for (final String ignoreCode : ignoreCodes.split(",\\s*")) {
                        if (ignoreCode.equals(bugInfo.getMessageCode())) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Token(org.antlr.v4.runtime.Token)

Example 64 with Token

use of org.antlr.v4.runtime.Token in project CFLint by cflint.

the class CFLint method registerRuleOverrides.

/**
 * @param context
 *            The current context.
 * @param expression
 *            The expression statement to check
 */
protected void registerRuleOverrides(final Context context, final CFExpressionStatement expression) {
    if (expression.getTokens() == null) {
        return;
    }
    final Iterable<Token> tokens = expression.getTokens().getTokens();
    for (final Token currentTok : tokens) {
        if (currentTok.getLine() == expression.getExpression().getLine()) {
            if (currentTok.getChannel() == Token.HIDDEN_CHANNEL && currentTok.getType() == CFSCRIPTLexer.LINE_COMMENT) {
                final String commentText = currentTok.getText().replaceFirst("^//\\s*", "").trim();
                if (commentText.startsWith("cflint ")) {
                    final Pattern pattern = Pattern.compile("cflint\\s+ignore:([\\w,]+).*");
                    final Matcher matcher = pattern.matcher(commentText);
                    if (matcher.matches()) {
                        final String ignoreCodes = matcher.group(1);
                        context.ignore(Arrays.asList(ignoreCodes.split(",\\s*")));
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Token(org.antlr.v4.runtime.Token)

Example 65 with Token

use of org.antlr.v4.runtime.Token in project hive by apache.

the class Stmt method createTable.

/**
 * CREATE TABLE statement
 */
public Integer createTable(HplsqlParser.Create_table_stmtContext ctx) {
    trace(ctx, "CREATE TABLE");
    StringBuilder sql = new StringBuilder();
    exec.append(sql, ctx.T_CREATE(), ctx.T_TABLE());
    exec.append(sql, evalPop(ctx.table_name()).toString(), ctx.T_TABLE().getSymbol(), ctx.table_name().getStart());
    Token last = ctx.table_name().getStop();
    if (ctx.create_table_preoptions() != null) {
        String preopt = evalPop(ctx.create_table_preoptions()).toString();
        if (preopt != null) {
            sql.append(" " + preopt);
        }
        last = ctx.create_table_preoptions().stop;
    }
    sql.append(createTableDefinition(ctx.create_table_definition(), last));
    trace(ctx, sql.toString());
    Query query = exec.executeSql(ctx, sql.toString(), exec.conf.defaultConnection);
    if (query.error()) {
        exec.signal(query);
        return 1;
    }
    exec.setSqlSuccess();
    exec.closeQuery(query, exec.conf.defaultConnection);
    return 0;
}
Also used : Token(org.antlr.v4.runtime.Token)

Aggregations

Token (org.antlr.v4.runtime.Token)38 Test (org.junit.Test)26 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)18 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)16 ArrayList (java.util.ArrayList)15 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)14 Grammar (org.antlr.v4.tool.Grammar)12 LexerGrammar (org.antlr.v4.tool.LexerGrammar)12 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)11 Token (org.antlr.runtime.Token)10 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)10 CharStream (org.antlr.v4.runtime.CharStream)9 CommonToken (org.antlr.v4.runtime.CommonToken)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 Rule (org.antlr.v4.tool.Rule)8 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)7 StringReader (java.io.StringReader)6 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)6 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6