Class Grammar

java.lang.Object
org.antlr.v4.tool.Grammar
All Implemented Interfaces:
AttributeResolver
Direct Known Subclasses:
LexerGrammar

public class Grammar extends Object implements AttributeResolver
  • Field Details

    • GRAMMAR_FROM_STRING_NAME

      public static final String GRAMMAR_FROM_STRING_NAME
      See Also:
    • INVALID_TOKEN_NAME

      public static final String INVALID_TOKEN_NAME
      This value is used in the following situations to indicate that a token type does not have an associated name which can be directly referenced in a grammar.
      • This value is the name and display name for the token with type Token.INVALID_TYPE.
      • This value is the name for tokens with a type not represented by a named token. The display name for these tokens is simply the string representation of the token type as an integer.
      See Also:
    • INVALID_RULE_NAME

      public static final String INVALID_RULE_NAME
      This value is used as the name for elements in the array returned by getRuleNames() for indexes not associated with a rule.
      See Also:
    • parserOptions

      public static final Set<String> parserOptions
    • lexerOptions

      public static final Set<String> lexerOptions
    • ruleOptions

      public static final Set<String> ruleOptions
    • ParserBlockOptions

      public static final Set<String> ParserBlockOptions
    • LexerBlockOptions

      public static final Set<String> LexerBlockOptions
    • ruleRefOptions

      public static final Set<String> ruleRefOptions
      Legal options for rule refs like id<key=value>
    • tokenOptions

      public static final Set<String> tokenOptions
      Legal options for terminal refs like ID<assoc=right>
    • actionOptions

      public static final Set<String> actionOptions
    • semPredOptions

      public static final Set<String> semPredOptions
    • doNotCopyOptionsToLexer

      public static final Set<String> doNotCopyOptionsToLexer
    • grammarAndLabelRefTypeToScope

      public static final Map<String,AttributeDict> grammarAndLabelRefTypeToScope
    • name

      public String name
    • ast

      public GrammarRootAST ast
    • tokenStream

      public final org.antlr.runtime.TokenStream tokenStream
      Track token stream used to create this grammar
    • originalTokenStream

      public org.antlr.runtime.TokenStream originalTokenStream
      If we transform grammar, track original unaltered token stream. This is set to the same value as tokenStream when tokenStream is initially set. If this field differs from tokenStream, then we have transformed the grammar.
    • text

      public String text
    • fileName

      public String fileName
    • implicitLexer

      public LexerGrammar implicitLexer
      Was this parser grammar created from a COMBINED grammar? If so, this is what we extracted.
    • originalGrammar

      public Grammar originalGrammar
      If this is an extracted/implicit lexer, we point at original grammar
    • parent

      public Grammar parent
      If we're imported, who imported us? If null, implies grammar is root
    • importedGrammars

      public List<Grammar> importedGrammars
    • rules

      public OrderedHashMap<String,Rule> rules
      All rules defined in this specific grammar, not imported. Also does not include lexical rules if combined.
    • indexToRule

      public List<Rule> indexToRule
    • atn

      public ATN atn
      The ATN that represents the grammar with edges labelled with tokens or epsilon. It is more suitable to analysis than an AST representation.
    • stateToGrammarRegionMap

      public Map<Integer,Interval> stateToGrammarRegionMap
    • decisionDFAs

      public Map<Integer,DFA> decisionDFAs
    • decisionLOOK

      public List<IntervalSet[]> decisionLOOK
    • tool

      public final Tool tool
    • tokenNameToTypeMap

      public final Map<String,Integer> tokenNameToTypeMap
      Map token like ID (but not literals like 'while') to its token type.
    • stringLiteralToTypeMap

      public final Map<String,Integer> stringLiteralToTypeMap
      Map token literals like 'while' to its token type. It may be that WHILE="while"=35, in which case both tokenNameToTypeMap and this field will have entries both mapped to 35.
    • typeToStringLiteralList

      public final List<String> typeToStringLiteralList
      Reverse index for stringLiteralToTypeMap. Indexed with raw token type. 0 is invalid.
    • typeToTokenList

      public final List<String> typeToTokenList
      Map a token type to its token name. Indexed with raw token type. 0 is invalid.
    • channelNameToValueMap

      public final Map<String,Integer> channelNameToValueMap
      Map channel like COMMENTS_CHANNEL to its constant channel value. Only user-defined channels are defined in this map.
    • channelValueToNameList

      public final List<String> channelValueToNameList
      Map a constant channel value to its name. Indexed with raw channel value. The predefined channels Token.DEFAULT_CHANNEL and Token.HIDDEN_CHANNEL are not stored in this list, so the values at the corresponding indexes is null.
    • namedActions

      public Map<String,ActionAST> namedActions
      Map a name to an action. The code generator will use this to fill holes in the output files. I track the AST node for the action in case I need the line number for errors.
    • lexerActions

      public LinkedHashMap<ActionAST,Integer> lexerActions
      Tracks all user lexer actions in all alternatives of all rules. Doesn't track sempreds. maps tree node to action index (alt number 1..n).
    • sempreds

      public LinkedHashMap<PredAST,Integer> sempreds
      All sempreds found in grammar; maps tree node to sempred index; sempred index is 0..n-1
    • indexToPredMap

      public LinkedHashMap<Integer,PredAST> indexToPredMap
      Map the other direction upon demand
    • AUTO_GENERATED_TOKEN_NAME_PREFIX

      public static final String AUTO_GENERATED_TOKEN_NAME_PREFIX
      See Also:
  • Constructor Details

    • Grammar

      public Grammar(Tool tool, GrammarRootAST ast)
    • Grammar

      public Grammar(String grammarText) throws org.antlr.runtime.RecognitionException
      For testing
      Throws:
      org.antlr.runtime.RecognitionException
    • Grammar

      public Grammar(String grammarText, LexerGrammar tokenVocabSource) throws org.antlr.runtime.RecognitionException
      Throws:
      org.antlr.runtime.RecognitionException
    • Grammar

      public Grammar(String grammarText, ANTLRToolListener listener) throws org.antlr.runtime.RecognitionException
      For testing
      Throws:
      org.antlr.runtime.RecognitionException
    • Grammar

      public Grammar(String fileName, String grammarText) throws org.antlr.runtime.RecognitionException
      For testing; builds trees, does sem anal
      Throws:
      org.antlr.runtime.RecognitionException
    • Grammar

      public Grammar(String fileName, String grammarText, ANTLRToolListener listener) throws org.antlr.runtime.RecognitionException
      For testing; builds trees, does sem anal
      Throws:
      org.antlr.runtime.RecognitionException
    • Grammar

      public Grammar(String fileName, String grammarText, Grammar tokenVocabSource, ANTLRToolListener listener) throws org.antlr.runtime.RecognitionException
      For testing; builds trees, does sem anal
      Throws:
      org.antlr.runtime.RecognitionException
  • Method Details

    • initTokenSymbolTables

      protected void initTokenSymbolTables()
    • loadImportedGrammars

      public void loadImportedGrammars()
    • defineAction

      public void defineAction(GrammarAST atAST)
    • defineRule

      public boolean defineRule(Rule r)
      Define the specified rule in the grammar. This method assigns the rule's Rule.index according to the ruleNumber field, and adds the Rule instance to rules and indexToRule.
      Parameters:
      r - The rule to define in the grammar.
      Returns:
      true if the rule was added to the Grammar instance; otherwise, false if a rule with this name already existed in the grammar instance.
    • undefineRule

      public boolean undefineRule(Rule r)
      Undefine the specified rule from this Grammar instance. The instance r is removed from rules and indexToRule. This method updates the Rule.index field for all rules defined after r, and decrements ruleNumber in preparation for adding new rules.

      This method does nothing if the current Grammar does not contain the instance r at index r.index in indexToRule.

      Parameters:
      r -
      Returns:
      true if the rule was removed from the Grammar instance; otherwise, false if the specified rule was not defined in the grammar.
    • getRule

      public Rule getRule(String name)
    • getATN

      public ATN getATN()
    • getRule

      public Rule getRule(int index)
    • getRule

      public Rule getRule(String grammarName, String ruleName)
    • getAllImportedGrammars

      public List<Grammar> getAllImportedGrammars()
      Get list of all imports from all grammars in the delegate subtree of g. The grammars are in import tree preorder. Don't include ourselves in list as we're not a delegate of ourselves.
    • getImportedGrammars

      public List<Grammar> getImportedGrammars()
    • getImplicitLexer

      public LexerGrammar getImplicitLexer()
    • load

      public static Grammar load(String fileName)
      convenience method for Tool.loadGrammar()
    • getGrammarAncestors

      public List<Grammar> getGrammarAncestors()
      Return list of imported grammars from root down to our parent. Order is [root, ..., this.parent]. (us not included).
    • getOutermostGrammar

      public Grammar getOutermostGrammar()
      Return the grammar that imported us and our parents. Return this if we're root.
    • getRecognizerName

      public String getRecognizerName()
      Get the name of the generated recognizer; may or may not be same as grammar name. Recognizer is TParser and TLexer from T if combined, else just use T regardless of grammar type.
    • getStringLiteralLexerRuleName

      public String getStringLiteralLexerRuleName(String lit)
    • getImportedGrammar

      public Grammar getImportedGrammar(String name)
      Return grammar directly imported by this grammar
    • getTokenType

      public int getTokenType(String token)
    • getTokenName

      public String getTokenName(String literal)
    • getTokenDisplayName

      public String getTokenDisplayName(int ttype)
      Given a token type, get a meaningful name for it such as the ID or string literal. If this is a lexer and the ttype is in the char vocabulary, compute an ANTLR-valid (possibly escaped) char literal.
    • getTokenName

      public String getTokenName(int ttype)
      Gets the name by which a token can be referenced in the generated code. For tokens defined in a tokens{} block or via a lexer rule, this is the declared name of the token. For token types generated by the use of a string literal within a parser rule of a combined grammar, this is the automatically generated token type which includes the AUTO_GENERATED_TOKEN_NAME_PREFIX prefix. For types which are not associated with a defined token, this method returns INVALID_TOKEN_NAME.
      Parameters:
      ttype - The token type.
      Returns:
      The name of the token with the specified type.
    • getChannelValue

      public int getChannelValue(String channel)
      Gets the constant channel value for a user-defined channel.

      This method only returns channel values for user-defined channels. All other channels, including the predefined channels Token.DEFAULT_CHANNEL and Token.HIDDEN_CHANNEL along with any channel defined in code (e.g. in a @members{} block), are ignored.

      Parameters:
      channel - The channel name.
      Returns:
      The channel value, if channel is the name of a known user-defined token channel; otherwise, -1.
    • getRuleNames

      public String[] getRuleNames()
      Gets an array of rule names for rules defined or imported by the grammar. The array index is the rule index, and the value is the name of the rule with the corresponding Rule.index.

      If no rule is defined with an index for an element of the resulting array, the value of that element is INVALID_RULE_NAME.

      Returns:
      The names of all rules defined in the grammar.
    • getTokenNames

      public String[] getTokenNames()
      Gets an array of token names for tokens defined or imported by the grammar. The array index is the token type, and the value is the result of getTokenName(java.lang.String) for the corresponding token type.
      Returns:
      The token names of all tokens defined in the grammar.
      See Also:
    • getTokenDisplayNames

      public String[] getTokenDisplayNames()
      Gets an array of display names for tokens defined or imported by the grammar. The array index is the token type, and the value is the result of getTokenDisplayName(int) for the corresponding token type.
      Returns:
      The display names of all tokens defined in the grammar.
      See Also:
    • getTokenLiteralNames

      public String[] getTokenLiteralNames()
      Gets the literal names assigned to tokens in the grammar.
    • getTokenSymbolicNames

      public String[] getTokenSymbolicNames()
      Gets the symbolic names assigned to tokens in the grammar.
    • getVocabulary

      public Vocabulary getVocabulary()
      Gets a Vocabulary instance describing the vocabulary used by the grammar.
    • getSemanticContextDisplayString

      public String getSemanticContextDisplayString(SemanticContext semctx)
      Given an arbitrarily complex SemanticContext, walk the "tree" and get display string. Pull predicates from grammar text.
    • joinPredicateOperands

      public String joinPredicateOperands(SemanticContext.Operator op, String separator)
    • getIndexToPredicateMap

      public LinkedHashMap<Integer,PredAST> getIndexToPredicateMap()
    • getPredicateDisplayString

      public String getPredicateDisplayString(SemanticContext.Predicate pred)
    • getMaxCharValue

      public int getMaxCharValue()
      What is the max char value possible for this grammar's target? Use unicode max if no target defined.
    • getTokenTypes

      public IntSet getTokenTypes()
      Return a set of all possible token or char types for this grammar
    • getAllCharValues

      public IntSet getAllCharValues()
      Return min to max char as defined by the target. If no target, use max unicode char value.
    • getMaxTokenType

      public int getMaxTokenType()
      How many token types have been allocated so far?
    • getNewTokenType

      public int getNewTokenType()
      Return a new unique integer in the token type space
    • getNewChannelNumber

      public int getNewChannelNumber()
      Return a new unique integer in the channel value space.
    • importTokensFromTokensFile

      public void importTokensFromTokensFile()
    • importVocab

      public void importVocab(Grammar importG)
    • defineTokenName

      public int defineTokenName(String name)
    • defineTokenName

      public int defineTokenName(String name, int ttype)
    • defineStringLiteral

      public int defineStringLiteral(String lit)
    • defineStringLiteral

      public int defineStringLiteral(String lit, int ttype)
    • defineTokenAlias

      public int defineTokenAlias(String name, String lit)
    • setTokenForType

      public void setTokenForType(int ttype, String text)
    • defineChannelName

      public int defineChannelName(String name)
      Define a token channel with a specified name.

      If a channel with the specified name already exists, the previously assigned channel value is returned.

      Parameters:
      name - The channel name.
      Returns:
      The constant channel value assigned to the channel.
    • defineChannelName

      public int defineChannelName(String name, int value)
      Define a token channel with a specified name.

      If a channel with the specified name already exists, the previously assigned channel value is not altered.

      Parameters:
      name - The channel name.
      Returns:
      The constant channel value assigned to the channel.
    • setChannelNameForValue

      public void setChannelNameForValue(int channelValue, String name)
      Sets the channel name associated with a particular channel value.

      If a name has already been assigned to the channel with constant value channelValue, this method does nothing.

      Parameters:
      channelValue - The constant value for the channel.
      name - The channel name.
    • resolveToAttribute

      public Attribute resolveToAttribute(String x, ActionAST node)
      Specified by:
      resolveToAttribute in interface AttributeResolver
    • resolveToAttribute

      public Attribute resolveToAttribute(String x, String y, ActionAST node)
      Specified by:
      resolveToAttribute in interface AttributeResolver
    • resolvesToLabel

      public boolean resolvesToLabel(String x, ActionAST node)
      Specified by:
      resolvesToLabel in interface AttributeResolver
    • resolvesToListLabel

      public boolean resolvesToListLabel(String x, ActionAST node)
      Specified by:
      resolvesToListLabel in interface AttributeResolver
    • resolvesToToken

      public boolean resolvesToToken(String x, ActionAST node)
      Specified by:
      resolvesToToken in interface AttributeResolver
    • resolvesToAttributeDict

      public boolean resolvesToAttributeDict(String x, ActionAST node)
      Specified by:
      resolvesToAttributeDict in interface AttributeResolver
    • getDefaultActionScope

      public String getDefaultActionScope()
      Given a grammar type, what should be the default action scope? If I say @members in a COMBINED grammar, for example, the default scope should be "parser".
    • getType

      public int getType()
    • getTokenStream

      public org.antlr.runtime.TokenStream getTokenStream()
    • isLexer

      public boolean isLexer()
    • isParser

      public boolean isParser()
    • isCombined

      public boolean isCombined()
    • isTokenName

      public static boolean isTokenName(String id)
      Is id a valid token name? Does id start with an uppercase letter?
    • getTypeString

      public String getTypeString()
    • getGrammarTypeToFileNameSuffix

      public static String getGrammarTypeToFileNameSuffix(int type)
    • getOptionString

      public String getOptionString(String key)
    • setNodeOptions

      public static void setNodeOptions(GrammarAST node, GrammarAST options)
      Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) set option assoc=right in TOKEN_REF.
    • getStringLiteralAliasesFromLexerRules

      public static List<Pair<GrammarAST,GrammarAST>> getStringLiteralAliasesFromLexerRules(GrammarRootAST ast)
      Return list of (TOKEN_NAME node, 'literal' node) pairs
    • defAlias

      protected static boolean defAlias(GrammarAST r, String pattern, org.antlr.runtime.tree.TreeWizard wiz, List<Pair<GrammarAST,GrammarAST>> lexerRuleToStringLiteral)
    • getStringLiterals

      public Set<String> getStringLiterals()
    • setLookaheadDFA

      public void setLookaheadDFA(int decision, DFA lookaheadDFA)
    • getStateToGrammarRegionMap

      public static Map<Integer,Interval> getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes)
    • getStateToGrammarRegion

      public Interval getStateToGrammarRegion(int atnStateNumber)
      Given an ATN state number, return the token index range within the grammar from which that ATN state was derived.
    • createLexerInterpreter

      public LexerInterpreter createLexerInterpreter(CharStream input)
    • createGrammarParserInterpreter

      public GrammarParserInterpreter createGrammarParserInterpreter(TokenStream tokenStream)
      Since:
      4.5.1
    • createParserInterpreter

      public ParserInterpreter createParserInterpreter(TokenStream tokenStream)