Search in sources :

Example 16 with CommonToken

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

the class LexerATNFactory method action.

@Override
public Handle action(String action) {
    if (action.trim().isEmpty()) {
        ATNState left = newState(null);
        ATNState right = newState(null);
        epsilon(left, right);
        return new Handle(left, right);
    }
    // define action AST for this rule as if we had found in grammar
    ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action));
    currentRule.defineActionInAlt(currentOuterAlt, ast);
    return action(ast);
}
Also used : CommonToken(org.antlr.runtime.CommonToken) ATNState(org.antlr.v4.runtime.atn.ATNState) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 17 with CommonToken

use of org.antlr.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)

Example 18 with CommonToken

use of org.antlr.runtime.CommonToken in project smali by JesusFreke.

the class StringUtils method parseQuotedString.

@Nullable
public static String parseQuotedString(String str) {
    if (str.charAt(0) != '"') {
        return null;
    }
    smaliFlexLexer lexer = new smaliFlexLexer(new StringReader(str));
    lexer.setSuppressErrors(true);
    CommonToken token = (CommonToken) lexer.nextToken();
    if (token.getType() != smaliParser.STRING_LITERAL) {
        return null;
    }
    if (token.getStopIndex() != str.length() - 1) {
        return null;
    }
    String text = token.getText();
    return text.substring(1, text.length() - 1);
}
Also used : StringReader(java.io.StringReader) org.jf.smali.smaliFlexLexer(org.jf.smali.smaliFlexLexer) CommonToken(org.antlr.runtime.CommonToken) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with CommonToken

use of org.antlr.runtime.CommonToken in project ow by vtst.

the class CustomizedLessLexer method createSemiColon.

private Token createSemiColon(Token nextToken) {
    CommonToken newToken = new CommonToken(nextToken);
    newToken.setType(TOKEN_SEMI_COLON);
    newToken.setStartIndex(getStartIndex(nextToken));
    newToken.setStopIndex(newToken.getStopIndex() - 1);
    newToken.setText("");
    return newToken;
}
Also used : CommonToken(org.antlr.runtime.CommonToken)

Aggregations

CommonToken (org.antlr.runtime.CommonToken)19 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)3 InputStreamReader (java.io.InputStreamReader)2 Token (org.antlr.runtime.Token)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)2 PartitionExpression (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionExpression)2 PartitionSpec (org.apache.hadoop.hive.ql.parse.PTFInvocationSpec.PartitionSpec)2 CoffeeSymbol (com.aptana.editor.coffee.parsing.lexer.CoffeeSymbol)1 IElementType (com.intellij.psi.tree.IElementType)1 Encoder (java.beans.Encoder)1 Expression (java.beans.Expression)1 PersistenceDelegate (java.beans.PersistenceDelegate)1 Statement (java.beans.Statement)1 XMLEncoder (java.beans.XMLEncoder)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1