Search in sources :

Example 6 with TerminalAST

use of org.antlr.v4.tool.ast.TerminalAST in project antlr4 by tunnelvisionlabs.

the class GrammarASTAdaptor method create.

@Override
public /**
 * Make sure even imaginary nodes know the input stream
 */
GrammarAST create(int tokenType, String text) {
    GrammarAST t;
    if (tokenType == ANTLRParser.RULE) {
        // needed by TreeWizard to make RULE tree
        t = new RuleAST(new CommonToken(tokenType, text));
    } else if (tokenType == ANTLRParser.STRING_LITERAL) {
        // implicit lexer construction done with wizard; needs this node type
        // whereas grammar ANTLRParser.g can use token option to spec node type
        t = new TerminalAST(new CommonToken(tokenType, text));
    } else {
        t = (GrammarAST) super.create(tokenType, text);
    }
    t.token.setInputStream(input);
    return t;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) CommonToken(org.antlr.runtime.CommonToken) TerminalAST(org.antlr.v4.tool.ast.TerminalAST)

Example 7 with TerminalAST

use of org.antlr.v4.tool.ast.TerminalAST in project antlr4 by antlr.

the class LexerATNFactory method tokenRef.

@Override
public Handle tokenRef(TerminalAST node) {
    // Ref to EOF in lexer yields char transition on -1
    if (node.getText().equals("EOF")) {
        ATNState left = newState(node);
        ATNState right = newState(node);
        left.addTransition(new AtomTransition(right, IntStream.EOF));
        return new Handle(left, right);
    }
    return _ruleRef(node);
}
Also used : AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 8 with TerminalAST

use of org.antlr.v4.tool.ast.TerminalAST in project antlr4 by antlr.

the class LexerATNFactory method stringLiteral.

/**
 * For a lexer, a string is a sequence of char to match.  That is,
 *  "fog" is treated as 'f' 'o' 'g' not as a single transition in
 *  the DFA.  Machine== o-'f'->o-'o'->o-'g'->o and has n+1 states
 *  for n characters.
 */
@Override
public Handle stringLiteral(TerminalAST stringLiteralAST) {
    String chars = stringLiteralAST.getText();
    ATNState left = newState(stringLiteralAST);
    ATNState right;
    String s = CharSupport.getStringFromGrammarStringLiteral(chars);
    if (s == null) {
        // the lexer will already have given an error
        return new Handle(left, left);
    }
    int n = s.length();
    ATNState prev = left;
    right = null;
    for (int i = 0; i < n; ) {
        right = newState(stringLiteralAST);
        int codePoint = s.codePointAt(i);
        prev.addTransition(CodePointTransitions.createWithCodePoint(right, codePoint));
        prev = right;
        i += Character.charCount(codePoint);
    }
    stringLiteralAST.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 9 with TerminalAST

use of org.antlr.v4.tool.ast.TerminalAST in project antlr4 by antlr.

the class ParserFactory method tokenRef.

@Override
public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) {
    MatchToken matchOp = new MatchToken(this, (TerminalAST) ID);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            // add Token _X and List<Token> X decls
            // adds _X
            defineImplicitLabel(ID, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(ID.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(ID.getAltLabel(), d);
        }
    // Decl d = getTokenLabelDecl(label);
    // ((MatchToken)matchOp).labels.add(d);
    // getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), d);
    // if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
    // TokenListDecl l = getTokenListLabelDecl(label);
    // getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), l);
    // }
    }
    if (controller.needsImplicitLabel(ID, matchOp))
        defineImplicitLabel(ID, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) MatchToken(org.antlr.v4.codegen.model.MatchToken) RuleFunction(org.antlr.v4.codegen.model.RuleFunction) LeftRecursiveRuleFunction(org.antlr.v4.codegen.model.LeftRecursiveRuleFunction) TokenDecl(org.antlr.v4.codegen.model.decl.TokenDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) RuleContextDecl(org.antlr.v4.codegen.model.decl.RuleContextDecl) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 10 with TerminalAST

use of org.antlr.v4.tool.ast.TerminalAST in project antlr4 by antlr.

the class Grammar method getStringLiterals.

public Set<String> getStringLiterals() {
    final Set<String> strings = new LinkedHashSet<String>();
    GrammarTreeVisitor collector = new GrammarTreeVisitor() {

        @Override
        public void stringRef(TerminalAST ref) {
            strings.add(ref.getText());
        }

        @Override
        public ErrorManager getErrorManager() {
            return tool.errMgr;
        }
    };
    collector.visitGrammar(ast);
    return strings;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GrammarTreeVisitor(org.antlr.v4.parse.GrammarTreeVisitor) TerminalAST(org.antlr.v4.tool.ast.TerminalAST)

Aggregations

TerminalAST (org.antlr.v4.tool.ast.TerminalAST)12 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)8 ArrayList (java.util.ArrayList)6 ATNState (org.antlr.v4.runtime.atn.ATNState)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 CommonToken (org.antlr.runtime.CommonToken)4 GrammarTreeVisitor (org.antlr.v4.parse.GrammarTreeVisitor)4 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)4 AltAST (org.antlr.v4.tool.ast.AltAST)4 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Token (org.antlr.runtime.Token)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 Tree (org.antlr.runtime.tree.Tree)2 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)2 LeftRecursiveRuleFunction (org.antlr.v4.codegen.model.LeftRecursiveRuleFunction)2 MatchToken (org.antlr.v4.codegen.model.MatchToken)2 RuleFunction (org.antlr.v4.codegen.model.RuleFunction)2 Decl (org.antlr.v4.codegen.model.decl.Decl)2