Package org.antlr.tool
Class Grammar
java.lang.Object
org.antlr.tool.Grammar
Represents a grammar in memory.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionMap a scope to a map of name:action pairs.protected boolean
static String[]
Given a char, we need to be able to show as an ANTLR literal.static int[]
When converting ANTLR char and string literals, here is the value set of escape chars.static final String
boolean
At least one backtrack=true in rule or decision or grammar.boolean
At least one rule has memoize=truestatic final String
Track decisions with syn preds specified for reporting.Track decisions with syn preds specified for reporting.protected boolean
We need a way to detect when a lexer grammar is autogenerated from another grammar or we are just sending in a string representing a grammar.static final int
protected IntSet
TODO: hook this to the charVocabulary optionstatic final int
If this grammar is part of a larger composite grammar via delegate statement, then this points at the composite.A pointer back into grammar tree.protected int
Be able to assign a number to every decision in grammar; decisions in 1..nTrack decisions that actually use the syn preds in the DFA.Track decisions that actually use the syn preds in the DFA.What are the default options for a subrule?static final String
The list of all rules referenced in this grammar, not defined here, and defined in a delegate grammar.long
How long in ms did it take to build DFAs for this grammar? If this grammar is a combined grammar, it only records time for the parser grammar component.protected boolean
An external tool requests that DFA analysis abort prematurely.protected NFAFactory
protected String
What file name holds this grammar?static final String
protected CodeGenerator
If non-null, this is the code generator we will use to generate recognizers in the target language.protected int
Is there a global fixed lookahead set for this grammar? If 0, nothing specified.static final String
protected GrammarAST
An AST that records entire input grammar with all rules.static final String[]
static final String[]
static final String
boolean
Was this created from a COMBINED grammar?protected Vector
<Grammar.Decision> Each subrule/rule is a decision point and we must track them so we can go back later and build DFA predictors for them.static final int
static final int
If this is a delegate of another grammar, this is the label used as an instance var by that grammar to point at this grammar.static String[]
A list of all rules that are in any left-recursive cycle.Legal options for terminal refs like ID<node=MyVarNode>static final int
static final String
used for generating lexer temp filesprotected org.stringtemplate.v4.ST
protected String
For merged lexer/parsers, we must construct a separate lexer spec.The ANTLRParser tracks lexer rules when reading combined grammars so we can build the Tokens rule.For ANTLRWorks, we want to be able to map a line:col to a specific decision DFA so it can display DFA.What name did the user provide for this grammar?protected LinkedHashMap
<String, Rule> Map a rule to it's Rule objectprotected LinkedHashMap
<String, GrammarAST> When we read in a grammar, we track the list of syntactic predicates and build faux rules for them later.The NFA that represents the grammar with edges labelled with tokens or epsilon.int
int
int
A list of options specified at the grammar level such as language=Java.If this rule is a delegate, some rules might be overridden; don't want to gen code for them.static final int
Each left-recursive precedence rule must define precedence array for binary operators like: static int[] e_prec = new int[tokenNames.length]; static { e_prec[75] = 1; } Track and we push into parser later; this is computed early when we look for prec rules.static final int
static final int
protected Set
<GrammarAST> The unique set of all rule references in any rule; set of tree node objects so two refs to same rule can exist but at different line/position.(package private) GrammarSanity
Factored out the sanity checking code; delegate to it.protected Set
<GrammarAST> protected Map
<String, AttributeScope> Track the scopes defined outside of rules and the scopes associated with all rules (even if empty).static final String
static final String
Track names of preds so we can avoid generating preds that aren't used Computed during NFA to DFA conversion.(package private) Target
Useful for asking questions about target during analysisstatic final int
static final int
This is the buffer of *all* tokens found in the grammar file including whitespace tokens etc...The unique set of all token ID references in any rulestatic final int
int
What type of grammar is this: lexer, parser, tree walkerSet of valid imports.static final int
static final int
-
Constructor Summary
ConstructorsConstructorDescriptionGrammar()
Useful for when you are sure that you are not part of a composite already.Used for testing; only useful on noncomposite grammars.Used for testing and Interp/RandomPhrase.Grammar
(Tool tool, String fileName, CompositeGrammar composite) Create a grammar from file name. -
Method Summary
Modifier and TypeMethodDescriptionaddArtificialMatchTokensRule
(GrammarAST grammarAST, List<String> ruleNames, List<String> delegateNames, boolean filterMode) Parse a rule we add artificially that is a list of the other lexer rules like this: "Tokens : ID | INT | SEMI ;" nextToken() will invoke this to set the current token.protected void
addDelegateGrammar
(Grammar delegateGrammar) add new delegate to composite treevoid
addRule
(GrammarAST grammarTree, GrammarAST t) void
boolean
void
altReferencesRule
(String enclosingRuleName, GrammarAST refScopeAST, GrammarAST refAST, int outerAltNum) Track a rule reference within an outermost alt of a rule.void
altReferencesTokenID
(String ruleName, GrammarAST refAST, int outerAltNum) Track a token reference within an outermost alt of a rule.int
assignDecisionNumber
(NFAState state) boolean
buildAST()
void
buildNFA()
boolean
List
<? extends Collection<? extends Rule>> void
Remove all labels on rule refs whose target rules have no return value.void
ANALYZE ACTIONS, LOOKING FOR LABEL AND ATTR REFS, sanity checkvoid
checkRuleReference
(GrammarAST scopeAST, GrammarAST refAST, GrammarAST argsAST, String currentRuleName) complement
(int atom) complement
(IntSet set) For lexer grammars, return everything in unicode not in set.computeTokenNameFromLiteral
(int tokenType, String literal) given a token type and the text of the literal, come up with a decent token type label.protected Grammar.Decision
createDecision
(int decision) createLL_1_LookaheadDFA
(int decision) createLookaheadDFA
(int decision, boolean wackTempStructures) void
For each decision in this grammar, compute a single DFA using the NFA states associated with the decision.void
createLookaheadDFAs
(boolean wackTempStructures) createParameterScope
(String ruleName, Token argAction) createReturnScope
(String ruleName, Token retAction) createRuleScope
(String ruleName, Token scopeAction) void
Define all the rule begin/end NFAStates to solve forward reference issues.protected void
defineGlobalScope
(String name, Token scopeAction) void
protected void
defineLabel
(Rule r, Token label, GrammarAST element, int type) Define a label defined in a rule r; check the validity then ask the Rule object to actually define it.void
defineLexerRuleForAliasedStringLiteral
(String tokenID, String literal, int tokenType) If someone does PLUS='+' in the parser, must make sure we get "PLUS : '+' ;" in lexer not "T73 : '+';"void
defineLexerRuleForStringLiteral
(String literal, int tokenType) void
defineLexerRuleFoundInParser
(Token ruleToken, GrammarAST ruleAST) void
defineNamedAction
(GrammarAST ampersandAST, String scope, GrammarAST nameAST, GrammarAST actionAST) Given @scope::name {action} define it for this grammar.void
defineRule
(Token ruleToken, String modifier, Map<String, Object> options, GrammarAST tree, GrammarAST argActionAST, int numAlts) Define a new rule.void
defineRuleListLabel
(String ruleName, Token label, GrammarAST element) void
defineRuleRefLabel
(String ruleName, Token label, GrammarAST ruleRef) defineSyntacticPredicate
(GrammarAST blockAST, String currentRuleName) Define a new predicate and get back its name for use in building a semantic predicate reference to the syn pred.void
defineToken
(String text, int tokenType) Define a token at a particular token type value.void
defineTokenListLabel
(String ruleName, Token label, GrammarAST element) void
defineTokenRefLabel
(String ruleName, Token label, GrammarAST tokenRef) void
defineWildcardTreeLabel
(String ruleName, Token label, GrammarAST tokenRef) void
defineWildcardTreeListLabel
(String ruleName, Token label, GrammarAST tokenRef) protected void
Before generating code, we examine all actions that can have $x.y and $y stuff in them because some code generation depends on Rule.referencedPredefinedRuleAttributes.void
Terminate DFA creation (grammar analysis).protected GrammarAST
findFirstTypeOutsideRewrite
(GrammarAST block, BitSet types) boolean
generateMethodForRule
(String ruleName) Should codegen.g gen rule for ruleName? If synpred, only gen if used in a DFA.If there is a char vocabulary, use it; else return min to max char as defined by the target.Get set of all rules imported from all delegate grammars even if indirectly delegated.static String
getANTLRCharLiteralForChar
(int c) Return a string representing the escaped char for code c.protected List
<? extends GrammarAST> getArtificialRulesForSyntacticPredicates
(LinkedHashMap<String, GrammarAST> nameToSynpredASTMap) for any syntactic predicates, we need to define rules for them; they will get defined automatically like any other rule.boolean
getAutoBacktrackMode
(int decision) getBlockOption
(GrammarAST blockAST, String key) static int
getCharValueFromGrammarCharLiteral
(String literal) Given a literal like (the 3 char sequence with single quotes) 'a', return the int value of 'a'.protected Grammar.Decision
getDecision
(int decision) getDecisionBlockAST
(int decision) getDecisionNFAStartState
(int decision) getDefaultActionScope
(int grammarType) 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".Get the set of Rules that need to have manual delegations like "void rule() { importedGrammar.rule(); }" If this grammar is master, get list of all rule definitions from all delegate grammars.Get list of all delegates from all grammars directly or indirectly imported into this grammar.Who's my direct parent grammar?Get list of all delegators.getGlobalScope
(String name) Get a global scopeboolean
int
boolean
Get delegates below direct delegatesgetLabels
(Set<GrammarAST> rewriteElements, int labelType) Given a set of all rewrite elements on right of ->, filter for label types such as Grammar.TOKEN_LABEL, Grammar.TOKEN_LIST_LABEL, ...Return a list of left-recursive rules; no analysis can be done successfully on these.If the grammar is a combined grammar, return the text of the implicit lexer grammar.getLocallyDefinedRule
(String ruleName) getLookaheadDFA
(int decision) getLookaheadDFAColumnsForLineInFile
(int line) returns a list of column numbers for all decisions on a particular line so ANTLRWorks choose the decision depending on the location of the cursor (otherwise, ANTLRWorks has to give the *exact* location which is not easy from the user point of view).getLookaheadDFAFromPositionInFile
(int line, int col) Useful for ANTLRWorks to map position in file to the DFA for displayint
What is the max char value possible for this grammar's target? Use unicode max if no target defined.int
How many token types have been allocated so far?int
Return a new unique integer in the token type spacegetNFAStateForAltOfDecision
(NFAState decisionState, int alt) Get the ith alternative (1..n) from a decision; return null when an invalid alt is requested.int
getNumberOfAltsForDecisionNFA
(NFAState decisionState) Decisions are linked together with transition(1).int
int
Get the name of the generated recognizer; may or may not be same as grammar name.int
getRuleIndex
(String ruleName) int
getRuleIndex
(String scopeName, String ruleName) getRuleModifier
(String ruleName) getRuleName
(int ruleIndex) getRules()
getRuleStartState
(String ruleName) getRuleStartState
(String scopeName, String ruleName) getRuleStopState
(String ruleName) getSetFromRule
(TreeToNFAConverter nfabuilder, String ruleName) Get the set equivalent (if any) of the indicated rule from this grammar.Get the list of ANTLR String literalsgetSyntacticPredicate
(String name) getTokenDisplayName
(int ttype) Given a token type, get a meaningful name for it such as the ID or string literal.Get a list of all token IDs and literals that have an associated token type.Get the list of tokens that are IDs like BLOCK and LPARENint
getTokenType
(String tokenName) Return a set of all possible token or char types for this grammarReturn an ordered integer list of token types that have no corresponding token ID like INT or KEYWORD_BEGIN; for stuff like 'begin'.getTool()
static StringBuffer
ANTLR does not convert escape sequences during the parse phase because it could not know how to print String/char literals back out when printing grammars etc...int
getUserMaxLookahead
(int decision) grammarTreeToString
(GrammarAST t, boolean showActions) void
importGrammar
(GrammarAST grammarNameAST, String label) Import the rules/tokens of a delegate grammar.int
importTokenVocabulary
(Grammar importFromGr) Pull your token definitions from an existing grammar in memory.int
importTokenVocabulary
(GrammarAST tokenVocabOptionAST, String vocabName) Load a vocab file <vocabName>.tokens and return max token type found.boolean
isAtomTokenType
(int ttype) boolean
boolean
isEmptyRule
(GrammarAST block) Rules like "a : ;" and "a : {...} ;" should not generate try/catch blocks for RecognitionException.boolean
isValidSet
(TreeToNFAConverter nfabuilder, GrammarAST t) Given set tree like ( SET A B ), check that A and B are both valid sets themselves, else we must tree like a BLOCKboolean
legalOption
(String key) protected List
<IntervalSet> makeEdgeSetsDisjoint
(List<IntervalSet> edges) boolean
boolean
optionIsValid
(String key, Object value) void
void
parseArtificialRule
(String ruleText) void
printGrammar
(PrintStream output) void
referenceRuleLabelPredefinedAttribute
(String ruleName) To yield smaller, more readable code, track which rules have their predefined attributes accessed.protected void
removeUselessLabels
(Map<String, Grammar.LabelElementPair> ruleToElementLabelPairMap) A label on a rule is useless if the rule has no return value, no tree or template output, and it is not referenced in an action.boolean
void
setCodeGenerator
(CodeGenerator generator) void
setDecisionBlockAST
(int decision, GrammarAST blockAST) void
setDecisionNFA
(int decision, NFAState state) void
setFileName
(String fileName) void
setGrammarContent
(String grammarString) void
setGrammarTree
(GrammarAST value) void
setLookaheadDFA
(int decision, DFA lookaheadDFA) Set the lookahead DFA for a particular decision.void
Save the option key/value pair and process it; return the key or null if invalid option.void
setOptions
(Map<String, Object> options, Token optionsStartToken) void
setRuleAST
(String ruleName, GrammarAST t) void
setSynPredGateIfNotAlready
(org.stringtemplate.v4.ST gateST) void
void
synPredUsedInDFA
(DFA dfa, SemanticContext semCtx) toString()
void
translateLeftRecursiveRule
(GrammarAST ruleAST) private void
updateLineColumnToLookaheadDFAMap
(DFA lookaheadDFA) boolean
validImport
(Grammar delegate) Many imports are illegal such as lexer into a tree grammar
-
Field Details
-
SYNPRED_RULE_PREFIX
- See Also:
-
GRAMMAR_FILE_EXTENSION
- See Also:
-
LEXER_GRAMMAR_FILE_EXTENSION
used for generating lexer temp files- See Also:
-
INITIAL_DECISION_LIST_SIZE
public static final int INITIAL_DECISION_LIST_SIZE- See Also:
-
INVALID_RULE_INDEX
public static final int INVALID_RULE_INDEX- See Also:
-
RULE_LABEL
public static final int RULE_LABEL- See Also:
-
TOKEN_LABEL
public static final int TOKEN_LABEL- See Also:
-
RULE_LIST_LABEL
public static final int RULE_LIST_LABEL- See Also:
-
TOKEN_LIST_LABEL
public static final int TOKEN_LIST_LABEL- See Also:
-
CHAR_LABEL
public static final int CHAR_LABEL- See Also:
-
WILDCARD_TREE_LABEL
public static final int WILDCARD_TREE_LABEL- See Also:
-
WILDCARD_TREE_LIST_LABEL
public static final int WILDCARD_TREE_LIST_LABEL- See Also:
-
LabelTypeToString
-
ARTIFICIAL_TOKENS_RULENAME
- See Also:
-
FRAGMENT_RULE_MODIFIER
- See Also:
-
SYNPREDGATE_ACTION_NAME
- See Also:
-
ANTLRLiteralEscapedCharValue
public static int[] ANTLRLiteralEscapedCharValueWhen converting ANTLR char and string literals, here is the value set of escape chars. -
ANTLRLiteralCharValueEscape
Given a char, we need to be able to show as an ANTLR literal. -
LEXER
public static final int LEXER- See Also:
-
PARSER
public static final int PARSER- See Also:
-
TREE_PARSER
public static final int TREE_PARSER- See Also:
-
COMBINED
public static final int COMBINED- See Also:
-
grammarTypeToString
-
grammarTypeToFileNameSuffix
-
validDelegations
Set of valid imports. E.g., can only import a tree parser into another tree parser. Maps delegate to set of delegator grammar types. validDelegations.get(LEXER) gives list of the kinds of delegators that can import lexers. -
tokenBuffer
This is the buffer of *all* tokens found in the grammar file including whitespace tokens etc... I use this to extract lexer rules from combined grammars. -
IGNORE_STRING_IN_GRAMMAR_FILE_NAME
- See Also:
-
AUTO_GENERATED_TOKEN_NAME_PREFIX
- See Also:
-
name
What name did the user provide for this grammar? -
type
public int typeWhat type of grammar is this: lexer, parser, tree walker -
options
A list of options specified at the grammar level such as language=Java. The value can be an AST for complicated values such as character sets. There may be code generator specific options in here. I do no interpretation of the key/value pairs...they are simply available for who wants them. -
legalLexerOptions
-
legalParserOptions
-
legalTreeParserOptions
-
doNotCopyOptionsToLexer
-
defaultOptions
-
legalBlockOptions
-
defaultBlockOptions
What are the default options for a subrule? -
defaultLexerBlockOptions
-
legalTokenOptions
Legal options for terminal refs like ID<node=MyVarNode> -
defaultTokenOption
- See Also:
-
global_k
protected int global_kIs there a global fixed lookahead set for this grammar? If 0, nothing specified. -1 implies we have not looked at the options table yet to set k. -
actions
Map a scope to a map of name:action pairs. Map<String, Map<String,GrammarAST>> 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. -
nfa
The NFA that represents the grammar with edges labelled with tokens or epsilon. It is more suitable to analysis than an AST representation. -
factory
-
composite
If this grammar is part of a larger composite grammar via delegate statement, then this points at the composite. The composite holds a global list of rules, token types, decision numbers, etc... -
compositeTreeNode
A pointer back into grammar tree. Needed so we can add delegates. -
label
If this is a delegate of another grammar, this is the label used as an instance var by that grammar to point at this grammar. null if no label was specified in the delegate statement. -
charVocabulary
TODO: hook this to the charVocabulary option -
lineColumnToLookaheadDFAMap
For ANTLRWorks, we want to be able to map a line:col to a specific decision DFA so it can display DFA. -
tool
-
ruleRefs
The unique set of all rule references in any rule; set of tree node objects so two refs to same rule can exist but at different line/position. -
scopedRuleRefs
-
tokenIDRefs
The unique set of all token ID references in any rule -
decisionCount
protected int decisionCountBe able to assign a number to every decision in grammar; decisions in 1..n -
leftRecursiveRules
A list of all rules that are in any left-recursive cycle. There could be multiple cycles, but this is a flat list of all problematic rules. This is stuff we couldn't refactor to precedence rule. -
externalAnalysisAbort
protected boolean externalAnalysisAbortAn external tool requests that DFA analysis abort prematurely. Stops at DFA granularity, which are limited to a DFA size and time computation as failsafe. -
numNonLLStar
public int numNonLLStar -
nameToSynpredASTMap
When we read in a grammar, we track the list of syntactic predicates and build faux rules for them later. See my blog entry Dec 2, 2005: http://www.antlr.org/blog/antlr3/lookahead.tml This maps the name (we make up) for a pred to the AST grammar fragment. -
precRuleInitCodeBlocks
Each left-recursive precedence rule must define precedence array for binary operators like: static int[] e_prec = new int[tokenNames.length]; static { e_prec[75] = 1; } Track and we push into parser later; this is computed early when we look for prec rules. -
atLeastOneRuleMemoizes
public boolean atLeastOneRuleMemoizesAt least one rule has memoize=true -
atLeastOneBacktrackOption
public boolean atLeastOneBacktrackOptionAt least one backtrack=true in rule or decision or grammar. -
implicitLexer
public boolean implicitLexerWas this created from a COMBINED grammar? -
nameToRuleMap
Map a rule to it's Rule object -
overriddenRules
If this rule is a delegate, some rules might be overridden; don't want to gen code for them. -
delegatedRuleReferences
The list of all rules referenced in this grammar, not defined here, and defined in a delegate grammar. Not all of these will be generated in the recognizer for this file; only those that are affected by rule definitions in this grammar. I am not sure the Java target will need this but I'm leaving in case other targets need it. see NameSpaceChecker.lookForReferencesToUndefinedSymbols() -
lexerRuleNamesInCombined
The ANTLRParser tracks lexer rules when reading combined grammars so we can build the Tokens rule. -
scopes
Track the scopes defined outside of rules and the scopes associated with all rules (even if empty). -
grammarTree
An AST that records entire input grammar with all rules. A simple grammar with one rule, "grammar t; a : A | B ;", looks like: ( grammar t ( rule a ( BLOCK ( ALT A ) ( ALT B ) ) <end-of-rule> ) ) -
indexToDecision
Each subrule/rule is a decision point and we must track them so we can go back later and build DFA predictors for them. This includes all the rules, subrules, optional blocks, ()+, ()* etc... -
generator
If non-null, this is the code generator we will use to generate recognizers in the target language. -
nameSpaceChecker
-
ll1Analyzer
-
lexerGrammarTemplate
For merged lexer/parsers, we must construct a separate lexer spec. This is the template for lexer; put the literals first then the regular rules. We don't need to specify a token vocab import as I make the new grammar import from the old all in memory; don't want to force it to read from the disk. Lexer grammar will have same name as original grammar but will be in different filename. Foo.g with combined grammar will have FooParser.java generated and Foo__.g with again Foo inside. It will however generate FooLexer.java as it's a lexer grammar. A bit odd, but autogenerated. Can tweak later if we want. -
lexerGrammarST
protected org.stringtemplate.v4.ST lexerGrammarST -
fileName
What file name holds this grammar? -
DFACreationWallClockTimeInMS
public long DFACreationWallClockTimeInMSHow long in ms did it take to build DFAs for this grammar? If this grammar is a combined grammar, it only records time for the parser grammar component. This only records the time to do the LL(*) work; NFA→DFA conversion. -
numberOfSemanticPredicates
public int numberOfSemanticPredicates -
numberOfManualLookaheadOptions
public int numberOfManualLookaheadOptions -
setOfNondeterministicDecisionNumbers
-
setOfNondeterministicDecisionNumbersResolvedWithPredicates
-
blocksWithSynPreds
Track decisions with syn preds specified for reporting. This is the a set of BLOCK type AST nodes. -
decisionsWhoseDFAsUsesSynPreds
Track decisions that actually use the syn preds in the DFA. Computed during NFA to DFA conversion. -
synPredNamesUsedInDFA
Track names of preds so we can avoid generating preds that aren't used Computed during NFA to DFA conversion. Just walk accept states and look for synpreds because that is the only state target whose incident edges can have synpreds. Same is try for decisionsWhoseDFAsUsesSynPreds. -
blocksWithSemPreds
Track decisions with syn preds specified for reporting. This is the a set of BLOCK type AST nodes. -
decisionsWhoseDFAsUsesSemPreds
Track decisions that actually use the syn preds in the DFA. -
allDecisionDFACreated
protected boolean allDecisionDFACreated -
builtFromString
protected boolean builtFromStringWe need a way to detect when a lexer grammar is autogenerated from another grammar or we are just sending in a string representing a grammar. We don't want to generate a .tokens file, for example, in such cases. -
sanity
GrammarSanity sanityFactored out the sanity checking code; delegate to it. -
target
Target targetUseful for asking questions about target during analysis
-
-
Constructor Details
-
Grammar
Create a grammar from file name. -
Grammar
public Grammar()Useful for when you are sure that you are not part of a composite already. Used in Interp/RandomPhrase and testing. -
Grammar
-
Grammar
Used for testing; only useful on noncomposite grammars.- Throws:
RecognitionException
-
Grammar
Used for testing and Interp/RandomPhrase. Only useful on noncomposite grammars.- Throws:
RecognitionException
-
-
Method Details
-
setFileName
-
getFileName
-
setName
-
setGrammarContent
- Throws:
RecognitionException
-
parseAndBuildAST
- Throws:
IOException
-
parseAndBuildAST
-
dealWithTreeFilterMode
protected void dealWithTreeFilterMode() -
translateLeftRecursiveRule
-
defineGrammarSymbols
public void defineGrammarSymbols() -
checkNameSpaceAndActions
public void checkNameSpaceAndActions()ANALYZE ACTIONS, LOOKING FOR LABEL AND ATTR REFS, sanity check -
validImport
Many imports are illegal such as lexer into a tree grammar -
getLexerGrammar
If the grammar is a combined grammar, return the text of the implicit lexer grammar. -
getImplicitlyGeneratedLexerFileName
-
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. -
addArtificialMatchTokensRule
public GrammarAST addArtificialMatchTokensRule(GrammarAST grammarAST, List<String> ruleNames, List<String> delegateNames, boolean filterMode) Parse a rule we add artificially that is a list of the other lexer rules like this: "Tokens : ID | INT | SEMI ;" nextToken() will invoke this to set the current token. Add char literals before the rule references. If in filter mode, we want every alt to backtrack and we need to do k=1 to force the "first token def wins" rule. Otherwise, the longest-match rule comes into play with LL(*). The ANTLRParser antlr.g file now invokes this when parsing a lexer grammar, which I think is proper even though it peeks at the info that later phases will (re)compute. It gets a list of lexer rules and builds a string representing the rule; then it creates a parser and adds the resulting tree to the grammar's tree. -
parseArtificialRule
-
addRule
-
getArtificialRulesForSyntacticPredicates
protected List<? extends GrammarAST> getArtificialRulesForSyntacticPredicates(LinkedHashMap<String, GrammarAST> nameToSynpredASTMap) for any syntactic predicates, we need to define rules for them; they will get defined automatically like any other rule. :) -
addRulesForSyntacticPredicates
public void addRulesForSyntacticPredicates() -
createRuleStartAndStopNFAStates
public void createRuleStartAndStopNFAStates()Define all the rule begin/end NFAStates to solve forward reference issues. Critical for composite grammars too. This is normally called on all root/delegates manually and then buildNFA() is called afterwards because the NFA construction needs to see rule start/stop states from potentially every grammar. Has to be have these created a priori. Testing routines will often just call buildNFA(), which forces a call to this method if not done already. Works ONLY for single noncomposite grammars. -
buildNFA
public void buildNFA() -
createLookaheadDFAs
public void createLookaheadDFAs()For each decision in this grammar, compute a single DFA using the NFA states associated with the decision. The DFA construction determines whether or not the alternatives in the decision are separable using a regular lookahead language. Store the lookahead DFAs in the AST created from the user's grammar so the code generator or whoever can easily access it. This is a separate method because you might want to create a Grammar without doing the expensive analysis. -
createLookaheadDFAs
public void createLookaheadDFAs(boolean wackTempStructures) -
createLL_1_LookaheadDFA
-
updateLineColumnToLookaheadDFAMap
-
makeEdgeSetsDisjoint
-
createLookaheadDFA
-
externallyAbortNFAToDFAConversion
public void externallyAbortNFAToDFAConversion()Terminate DFA creation (grammar analysis). -
NFAToDFAConversionExternallyAborted
public boolean NFAToDFAConversionExternallyAborted() -
getNewTokenType
public int getNewTokenType()Return a new unique integer in the token type space -
defineToken
Define a token at a particular token type value. Blast an old value with a new one. This is called normal grammar processsing and during import vocab operations to set tokens with specific values. -
defineRule
public void defineRule(Token ruleToken, String modifier, Map<String, Object> options, GrammarAST tree, GrammarAST argActionAST, int numAlts) Define a new rule. A new rule index is created by incrementing ruleIndex. -
defineSyntacticPredicate
Define a new predicate and get back its name for use in building a semantic predicate reference to the syn pred. -
getSyntacticPredicates
-
getSyntacticPredicate
-
synPredUsedInDFA
-
defineNamedAction
public void defineNamedAction(GrammarAST ampersandAST, String scope, GrammarAST nameAST, GrammarAST actionAST) Given @scope::name {action} define it for this grammar. Later, the code generator will ask for the actions table. For composite grammars, make sure header action propogates down to all delegates. -
setSynPredGateIfNotAlready
public void setSynPredGateIfNotAlready(org.stringtemplate.v4.ST gateST) -
getActions
-
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". -
defineLexerRuleFoundInParser
-
defineLexerRuleForAliasedStringLiteral
If someone does PLUS='+' in the parser, must make sure we get "PLUS : '+' ;" in lexer not "T73 : '+';" -
defineLexerRuleForStringLiteral
-
getLocallyDefinedRule
-
getRule
-
getRule
-
getRuleIndex
-
getRuleIndex
-
getRuleName
-
generateMethodForRule
Should codegen.g gen rule for ruleName? If synpred, only gen if used in a DFA. If regular rule, only gen if not overridden in delegator Always gen Tokens rule though. -
defineGlobalScope
-
createReturnScope
-
createRuleScope
-
createParameterScope
-
getGlobalScope
Get a global scope -
getGlobalScopes
-
defineLabel
Define a label defined in a rule r; check the validity then ask the Rule object to actually define it. -
defineTokenRefLabel
-
defineWildcardTreeLabel
-
defineWildcardTreeListLabel
-
defineRuleRefLabel
-
defineTokenListLabel
-
defineRuleListLabel
-
getLabels
Given a set of all rewrite elements on right of ->, filter for label types such as Grammar.TOKEN_LABEL, Grammar.TOKEN_LIST_LABEL, ... Return a displayable token type name computed from the GrammarAST. -
examineAllExecutableActions
protected void examineAllExecutableActions()Before generating code, we examine all actions that can have $x.y and $y stuff in them because some code generation depends on Rule.referencedPredefinedRuleAttributes. I need to remove unused rule labels for example. -
checkAllRulesForUselessLabels
public void checkAllRulesForUselessLabels()Remove all labels on rule refs whose target rules have no return value. Do this for all rules in grammar. -
removeUselessLabels
A label on a rule is useless if the rule has no return value, no tree or template output, and it is not referenced in an action. -
altReferencesRule
public void altReferencesRule(String enclosingRuleName, GrammarAST refScopeAST, GrammarAST refAST, int outerAltNum) Track a rule reference within an outermost alt of a rule. Used at the moment to decide if $ruleref refers to a unique rule ref in the alt. Rewrite rules force tracking of all rule AST results. This data is also used to verify that all rules have been defined. -
altReferencesTokenID
Track a token reference within an outermost alt of a rule. Used to decide if $tokenref refers to a unique token ref in the alt. Does not track literals! Rewrite rules force tracking of all tokens. -
referenceRuleLabelPredefinedAttribute
To yield smaller, more readable code, track which rules have their predefined attributes accessed. If the rule has no user-defined return values, then don't generate the return value scope classes etc... Make the rule have void return value. Don't track for lexer rules. -
checkAllRulesForLeftRecursion
-
getLeftRecursiveRules
Return a list of left-recursive rules; no analysis can be done successfully on these. Useful to skip these rules then and also for ANTLRWorks to highlight them. -
checkRuleReference
public void checkRuleReference(GrammarAST scopeAST, GrammarAST refAST, GrammarAST argsAST, String currentRuleName) -
isEmptyRule
Rules like "a : ;" and "a : {...} ;" should not generate try/catch blocks for RecognitionException. To detect this it's probably ok to just look for any reference to an atom that can match some input. W/o that, the rule is unlikey to have any else. -
findFirstTypeOutsideRewrite
-
isAtomTokenType
public boolean isAtomTokenType(int ttype) -
getTokenType
-
getTokenIDs
Get the list of tokens that are IDs like BLOCK and LPAREN -
getTokenTypesWithoutID
Return an ordered integer list of token types that have no corresponding token ID like INT or KEYWORD_BEGIN; for stuff like 'begin'. -
getTokenDisplayNames
Get a list of all token IDs and literals that have an associated token type. -
getCharValueFromGrammarCharLiteral
Given a literal like (the 3 char sequence with single quotes) 'a', return the int value of 'a'. Convert escape sequences here also. ANTLR's antlr.g parser does not convert escape sequences. 11/26/2005: I changed literals to always be '...' even for strings. This routine still works though. -
getUnescapedStringFromGrammarStringLiteral
ANTLR does not convert escape sequences during the parse phase because it could not know how to print String/char literals back out when printing grammars etc... Someone in China might use the real unicode char in a literal as it will display on their screen; when printing back out, I could not know whether to display or use a unicode escape. This routine converts a string literal with possible escape sequences into a pure string of 16-bit char values. Escapes and unicode specs are converted to pure chars. return in a buffer; people may want to walk/manipulate further. The NFA construction routine must know the actual char values. -
importTokenVocabulary
Pull your token definitions from an existing grammar in memory. You must use Grammar() ctor then this method then setGrammarContent() to make this work. This was useful primarily for testing and interpreting grammars until I added import grammar functionality. When you import a grammar you implicitly import its vocabulary as well and keep the same token type values. Returns the max token type found. -
importGrammar
Import the rules/tokens of a delegate grammar. All delegate grammars are read during the ctor of first Grammar created. Do not create NFA here because NFA construction needs to hook up with overridden rules in delegation root grammar. -
addDelegateGrammar
add new delegate to composite tree -
importTokenVocabulary
Load a vocab file <vocabName>.tokens and return max token type found. -
getTokenDisplayName
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. -
getStringLiterals
Get the list of ANTLR String literals -
getGrammarTypeString
-
getGrammarMaxLookahead
public int getGrammarMaxLookahead() -
setOption
Save the option key/value pair and process it; return the key or null if invalid option. -
legalOption
-
setOptions
-
getOption
-
getLocallyDefinedOption
-
getBlockOption
-
getUserMaxLookahead
public int getUserMaxLookahead(int decision) -
getAutoBacktrackMode
public boolean getAutoBacktrackMode(int decision) -
optionIsValid
-
buildAST
public boolean buildAST() -
rewriteMode
public boolean rewriteMode() -
isBuiltFromString
public boolean isBuiltFromString() -
buildTemplate
public boolean buildTemplate() -
getRules
-
getDelegatedRules
Get the set of Rules that need to have manual delegations like "void rule() { importedGrammar.rule(); }" If this grammar is master, get list of all rule definitions from all delegate grammars. Only master has complete interface from combined grammars...we will generated delegates as helper objects. Composite grammars that are not the root/master do not have complete interfaces. It is not my intention that people use subcomposites. Only the outermost grammar should be used from outside code. The other grammar components are specifically generated to work only with the master/root. delegatedRules = imported - overridden -
getAllImportedRules
Get set of all rules imported from all delegate grammars even if indirectly delegated. -
getDelegates
Get list of all delegates from all grammars directly or indirectly imported into this grammar. -
getHasDelegates
public boolean getHasDelegates() -
getDelegateNames
-
getDirectDelegates
-
getIndirectDelegates
Get delegates below direct delegates -
getDelegators
Get list of all delegators. This amounts to the grammars on the path to the root of the delegation tree. -
getDelegator
Who's my direct parent grammar? -
getDelegatedRuleReferences
-
getGrammarIsRoot
public boolean getGrammarIsRoot() -
setRuleAST
-
getRuleStartState
-
getRuleStartState
-
getRuleModifier
-
getRuleStopState
-
assignDecisionNumber
-
getDecision
-
getDecisions
-
createDecision
-
getDecisionNFAStartStateList
-
getDecisionNFAStartState
-
getLookaheadDFA
-
getDecisionBlockAST
-
getLookaheadDFAColumnsForLineInFile
returns a list of column numbers for all decisions on a particular line so ANTLRWorks choose the decision depending on the location of the cursor (otherwise, ANTLRWorks has to give the *exact* location which is not easy from the user point of view). This is not particularly fast as it walks entire line:col→DFA map looking for a prefix of "line:". -
getLookaheadDFAFromPositionInFile
Useful for ANTLRWorks to map position in file to the DFA for display -
getLineColumnToLookaheadDFAMap
-
getNumberOfDecisions
public int getNumberOfDecisions() -
getNumberOfCyclicDecisions
public int getNumberOfCyclicDecisions() -
setLookaheadDFA
Set the lookahead DFA for a particular decision. This means that the appropriate AST node must updated to have the new lookahead DFA. This method could be used to properly set the DFAs without using the createLookaheadDFAs() method. You could do this Grammar g = new Grammar("..."); g.setLookahead(1, dfa1); g.setLookahead(2, dfa2); ... -
setDecisionNFA
-
setDecisionBlockAST
-
allDecisionDFAHaveBeenCreated
public boolean allDecisionDFAHaveBeenCreated() -
getMaxTokenType
public int getMaxTokenType()How many token types have been allocated so far? -
getMaxCharValue
public int getMaxCharValue()What is the max char value possible for this grammar's target? Use unicode max if no target defined. -
getTokenTypes
Return a set of all possible token or char types for this grammar -
getAllCharValues
If there is a char vocabulary, use it; else return min to max char as defined by the target. If no target, use max unicode char value. -
getANTLRCharLiteralForChar
Return a string representing the escaped char for code c. E.g., If c has value 0x100, you will get "Ā". ASCII gets the usual char (non-hex) representation. Control characters are spit out as unicode. While this is specially set up for returning Java strings, it can be used by any language target that has the same syntax. :) 11/26/2005: I changed this to use double quotes, consistent with antlr.g 12/09/2005: I changed so everything is single quotes -
complement
For lexer grammars, return everything in unicode not in set. For parser and tree grammars, return everything in token space from MIN_TOKEN_TYPE to last valid token type or char value. -
complement
-
isValidSet
Given set tree like ( SET A B ), check that A and B are both valid sets themselves, else we must tree like a BLOCK -
getSetFromRule
public IntSet getSetFromRule(TreeToNFAConverter nfabuilder, String ruleName) throws RecognitionException Get the set equivalent (if any) of the indicated rule from this grammar. Mostly used in the lexer to do ~T for some fragment rule T. If the rule AST has a SET use that. If the rule is a single char convert it to a set and return. If rule is not a simple set (w/o actions) then return null. Rules have AST form: ^( RULE ID modifier ARG RET SCOPE block EOR )- Throws:
RecognitionException
-
getNumberOfAltsForDecisionNFA
Decisions are linked together with transition(1). Count how many there are. This is here rather than in NFAState because a grammar decides how NFAs are put together to form a decision. -
getNFAStateForAltOfDecision
Get the ith alternative (1..n) from a decision; return null when an invalid alt is requested. I must count in to find the right alternative number. For (A|B), you get NFA structure (roughly): o->o-A->o | o->o-B->o This routine returns the leftmost state for each alt. So alt=1, returns the upperleft most state in this structure. -
FIRST
-
LOOK
-
setCodeGenerator
-
getCodeGenerator
-
getGrammarTree
-
setGrammarTree
-
getTool
-
setTool
-
computeTokenNameFromLiteral
given a token type and the text of the literal, come up with a decent token type label. For now it's just T<type>. Actually, if there is an aliased name from tokens like PLUS='+', use it. -
toString
-
grammarTreeToString
-
grammarTreeToString
-
printGrammar
-