Search in sources :

Example 46 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project antlr4 by antlr.

the class TestRig method process.

protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, CharStream input) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
    lexer.setInputStream(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    if (showTokens) {
        for (Token tok : tokens.getTokens()) {
            if (tok instanceof CommonToken) {
                System.out.println(((CommonToken) tok).toString(lexer));
            } else {
                System.out.println(tok.toString());
            }
        }
    }
    if (startRuleName.equals(LEXER_START_RULE_NAME))
        return;
    if (diagnostics) {
        parser.addErrorListener(new DiagnosticErrorListener());
        parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    }
    if (printTree || gui || psFile != null) {
        parser.setBuildParseTree(true);
    }
    if (SLL) {
        // overrides diagnostics
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    }
    parser.setTokenStream(tokens);
    parser.setTrace(trace);
    try {
        Method startRule = parserClass.getMethod(startRuleName);
        ParserRuleContext tree = (ParserRuleContext) startRule.invoke(parser, (Object[]) null);
        if (printTree) {
            System.out.println(tree.toStringTree(parser));
        }
        if (gui) {
            Trees.inspect(tree, parser);
        }
        if (psFile != null) {
            // Generate postscript
            Trees.save(tree, parser, psFile);
        }
    } catch (NoSuchMethodException nsme) {
        System.err.println("No method for rule " + startRuleName + " or it has arguments");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DiagnosticErrorListener(org.antlr.v4.runtime.DiagnosticErrorListener) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token) Method(java.lang.reflect.Method) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 47 with CommonToken

use of org.antlr.v4.runtime.CommonToken in project antlr4 by antlr.

the class ScopeParser method parseAttributeDef.

/**
	 * For decls like "String foo" or "char *foo32[]" compute the ID
	 * and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
	 * but if the separator is ',' you cannot use ',' in the initvalue
	 * unless you escape use "\," escape.
	 */
public static Attribute parseAttributeDef(ActionAST action, Pair<String, Integer> decl, Grammar g) {
    if (decl.a == null)
        return null;
    Attribute attr = new Attribute();
    int rightEdgeOfDeclarator = decl.a.length() - 1;
    int equalsIndex = decl.a.indexOf('=');
    if (equalsIndex > 0) {
        // everything after the '=' is the init value
        attr.initValue = decl.a.substring(equalsIndex + 1, decl.a.length()).trim();
        rightEdgeOfDeclarator = equalsIndex - 1;
    }
    String declarator = decl.a.substring(0, rightEdgeOfDeclarator + 1);
    Pair<Integer, Integer> p;
    String text = decl.a;
    text = text.replaceAll("::", "");
    if (text.contains(":")) {
        // declarator has type appearing after the name like "x:T"
        p = _parsePostfixDecl(attr, declarator, action, g);
    } else {
        // declarator has type appearing before the name like "T x"
        p = _parsePrefixDecl(attr, declarator, action, g);
    }
    int idStart = p.a;
    int idStop = p.b;
    attr.decl = decl.a;
    if (action != null) {
        String actionText = action.getText();
        int[] lines = new int[actionText.length()];
        int[] charPositionInLines = new int[actionText.length()];
        for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
            lines[i] = line;
            charPositionInLines[i] = col;
            if (actionText.charAt(i) == '\n') {
                line++;
                col = -1;
            }
        }
        int[] charIndexes = new int[actionText.length()];
        for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
            charIndexes[j] = i;
            // skip comments
            if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
                while (i < actionText.length() && actionText.charAt(i) != '\n') {
                    i++;
                }
            }
        }
        int declOffset = charIndexes[decl.b];
        int declLine = lines[declOffset + idStart];
        int line = action.getToken().getLine() + declLine;
        int charPositionInLine = charPositionInLines[declOffset + idStart];
        if (declLine == 0) {
            /* offset for the start position of the ARG_ACTION token, plus 1
				 * since the ARG_ACTION text had the leading '[' stripped before
				 * reaching the scope parser.
				 */
            charPositionInLine += action.getToken().getCharPositionInLine() + 1;
        }
        int offset = ((CommonToken) action.getToken()).getStartIndex();
        attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
        attr.token.setLine(line);
        attr.token.setCharPositionInLine(charPositionInLine);
        assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
    }
    return attr;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) CommonToken(org.antlr.runtime.CommonToken)

Aggregations

CommonToken (org.antlr.v4.runtime.CommonToken)39 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 DescriptionContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.DescriptionContext)15 HtmlElementContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementContext)12 JavadocTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocTagContext)12 JavadocInlineTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocInlineTagContext)10 ReferenceContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ReferenceContext)10 HtmlElementCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementCloseContext)9 HtmlElementOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlElementOpenContext)9 HtmlTagContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.HtmlTagContext)9 AttributeContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.AttributeContext)5 PTagOpenContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.PTagOpenContext)5 CommonToken (org.antlr.runtime.CommonToken)5 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 ParametersContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.ParametersContext)4 TokenFactory (org.antlr.v4.runtime.TokenFactory)4 TokenSource (org.antlr.v4.runtime.TokenSource)4 Test (org.junit.Test)4 PTagCloseContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.PTagCloseContext)3