Search in sources :

Example 81 with GrammarAST

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

the class ParserFactory method set.

@Override
public List<SrcOp> set(GrammarAST setAST, GrammarAST labelAST, boolean invert) {
    MatchSet matchOp;
    if (invert)
        matchOp = new MatchNotSet(this, setAST);
    else
        matchOp = new MatchSet(this, setAST);
    if (labelAST != null) {
        String label = labelAST.getText();
        RuleFunction rf = getCurrentRuleFunction();
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            defineImplicitLabel(setAST, matchOp);
            TokenListDecl l = getTokenListLabelDecl(label);
            rf.addContextDecl(setAST.getAltLabel(), l);
        } else {
            Decl d = getTokenLabelDecl(label);
            matchOp.labels.add(d);
            rf.addContextDecl(setAST.getAltLabel(), d);
        }
    }
    if (controller.needsImplicitLabel(setAST, matchOp))
        defineImplicitLabel(setAST, matchOp);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
    return list(matchOp, listLabelOp);
}
Also used : MatchSet(org.antlr.v4.codegen.model.MatchSet) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) 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) MatchNotSet(org.antlr.v4.codegen.model.MatchNotSet) AddToLabelList(org.antlr.v4.codegen.model.AddToLabelList)

Example 82 with GrammarAST

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

the class Tool method loadImportedGrammar.

/**
	 * Try current dir then dir of g then lib dir
	 * @param g
	 * @param nameNode The node associated with the imported grammar name.
	 */
public Grammar loadImportedGrammar(Grammar g, GrammarAST nameNode) throws IOException {
    String name = nameNode.getText();
    Grammar imported = importedGrammars.get(name);
    if (imported == null) {
        g.tool.log("grammar", "load " + name + " from " + g.fileName);
        File importedFile = null;
        for (String extension : ALL_GRAMMAR_EXTENSIONS) {
            importedFile = getImportedGrammarFile(g, name + extension);
            if (importedFile != null) {
                break;
            }
        }
        if (importedFile == null) {
            errMgr.grammarError(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, g.fileName, nameNode.getToken(), name);
            return null;
        }
        String absolutePath = importedFile.getAbsolutePath();
        ANTLRFileStream in = new ANTLRFileStream(absolutePath, grammarEncoding);
        GrammarRootAST root = parse(g.fileName, in);
        if (root == null) {
            return null;
        }
        imported = createGrammar(root);
        imported.fileName = absolutePath;
        importedGrammars.put(root.getGrammarName(), imported);
    }
    return imported;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) File(java.io.File)

Example 83 with GrammarAST

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

the class TokenVocabParser method load.

/** Load a vocab file {@code <vocabName>.tokens} and return mapping. */
public Map<String, Integer> load() {
    Map<String, Integer> tokens = new LinkedHashMap<String, Integer>();
    int maxTokenType = -1;
    File fullFile = getImportedVocabFile();
    FileInputStream fis = null;
    BufferedReader br = null;
    Tool tool = g.tool;
    String vocabName = g.getOptionString("tokenVocab");
    try {
        Pattern tokenDefPattern = Pattern.compile("([^\n]+?)[ \\t]*?=[ \\t]*?([0-9]+)");
        fis = new FileInputStream(fullFile);
        InputStreamReader isr;
        if (tool.grammarEncoding != null) {
            isr = new InputStreamReader(fis, tool.grammarEncoding);
        } else {
            isr = new InputStreamReader(fis);
        }
        br = new BufferedReader(isr);
        String tokenDef = br.readLine();
        int lineNum = 1;
        while (tokenDef != null) {
            Matcher matcher = tokenDefPattern.matcher(tokenDef);
            if (matcher.find()) {
                String tokenID = matcher.group(1);
                String tokenTypeS = matcher.group(2);
                int tokenType;
                try {
                    tokenType = Integer.valueOf(tokenTypeS);
                } catch (NumberFormatException nfe) {
                    tool.errMgr.toolError(ErrorType.TOKENS_FILE_SYNTAX_ERROR, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION, " bad token type: " + tokenTypeS, lineNum);
                    tokenType = Token.INVALID_TOKEN_TYPE;
                }
                tool.log("grammar", "import " + tokenID + "=" + tokenType);
                tokens.put(tokenID, tokenType);
                maxTokenType = Math.max(maxTokenType, tokenType);
                lineNum++;
            } else {
                if (tokenDef.length() > 0) {
                    // ignore blank lines
                    tool.errMgr.toolError(ErrorType.TOKENS_FILE_SYNTAX_ERROR, vocabName + CodeGenerator.VOCAB_FILE_EXTENSION, " bad token def: " + tokenDef, lineNum);
                }
            }
            tokenDef = br.readLine();
        }
    } catch (FileNotFoundException fnfe) {
        GrammarAST inTree = g.ast.getOptionAST("tokenVocab");
        String inTreeValue = inTree.getToken().getText();
        if (vocabName.equals(inTreeValue)) {
            tool.errMgr.grammarError(ErrorType.CANNOT_FIND_TOKENS_FILE_REFD_IN_GRAMMAR, g.fileName, inTree.getToken(), fullFile);
        } else {
            // must be from -D option on cmd-line not token in tree
            tool.errMgr.toolError(ErrorType.CANNOT_FIND_TOKENS_FILE_GIVEN_ON_CMDLINE, fullFile, g.name);
        }
    } catch (Exception e) {
        tool.errMgr.toolError(ErrorType.ERROR_READING_TOKENS_FILE, e, fullFile, e.getMessage());
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ioe) {
            tool.errMgr.toolError(ErrorType.ERROR_READING_TOKENS_FILE, ioe, fullFile, ioe.getMessage());
        }
    }
    return tokens;
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LinkedHashMap(java.util.LinkedHashMap) BufferedReader(java.io.BufferedReader) File(java.io.File) Tool(org.antlr.v4.Tool)

Example 84 with GrammarAST

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

the class ParserFactory method wildcard.

@Override
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) {
    Wildcard wild = new Wildcard(this, ast);
    // TODO: dup with tokenRef
    if (labelAST != null) {
        String label = labelAST.getText();
        Decl d = getTokenLabelDecl(label);
        wild.labels.add(d);
        getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
        if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
            TokenListDecl l = getTokenListLabelDecl(label);
            getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), l);
        }
    }
    if (controller.needsImplicitLabel(ast, wild))
        defineImplicitLabel(ast, wild);
    AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(wild, labelAST);
    return list(wild, listLabelOp);
}
Also used : Wildcard(org.antlr.v4.codegen.model.Wildcard) TokenListDecl(org.antlr.v4.codegen.model.decl.TokenListDecl) 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 85 with GrammarAST

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

the class ParserATNFactory method epsilon.

/** From an empty alternative build {@code o-e->o}. */
@Override
public Handle epsilon(GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    epsilon(left, right);
    node.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState)

Aggregations

GrammarAST (org.antlr.v4.tool.ast.GrammarAST)55 Rule (org.antlr.v4.tool.Rule)20 ATNState (org.antlr.v4.runtime.atn.ATNState)15 ArrayList (java.util.ArrayList)12 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)12 Grammar (org.antlr.v4.tool.Grammar)8 Decl (org.antlr.v4.codegen.model.decl.Decl)7 ActionAST (org.antlr.v4.tool.ast.ActionAST)7 AltAST (org.antlr.v4.tool.ast.AltAST)7 TerminalAST (org.antlr.v4.tool.ast.TerminalAST)7 LinkedHashMap (java.util.LinkedHashMap)6 Token (org.antlr.runtime.Token)6 RuleAST (org.antlr.v4.tool.ast.RuleAST)6 HashMap (java.util.HashMap)5 AddToLabelList (org.antlr.v4.codegen.model.AddToLabelList)5 Pair (org.antlr.v4.runtime.misc.Pair)5 LexerGrammar (org.antlr.v4.tool.LexerGrammar)5 GrammarASTWithOptions (org.antlr.v4.tool.ast.GrammarASTWithOptions)5