

Public Member Functions | |
| BaseRecognizer () | |
| BaseRecognizer (RecognizerSharedState state) | |
| void | reset () |
| Object | match (IntStream input, int ttype, BitSet follow) throws RecognitionException |
| void | matchAny (IntStream input) |
| boolean | mismatchIsUnwantedToken (IntStream input, int ttype) |
| boolean | mismatchIsMissingToken (IntStream input, BitSet follow) |
| void | reportError (RecognitionException e) |
| void | displayRecognitionError (String[] tokenNames, RecognitionException e) |
| String | getErrorMessage (RecognitionException e, String[] tokenNames) |
| int | getNumberOfSyntaxErrors () |
| String | getErrorHeader (RecognitionException e) |
| String | getTokenErrorDisplay (Token t) |
| void | emitErrorMessage (String msg) |
| void | recover (IntStream input, RecognitionException re) |
| void | beginResync () |
| void | endResync () |
| Object | recoverFromMismatchedSet (IntStream input, RecognitionException e, BitSet follow) throws RecognitionException |
| void | consumeUntil (IntStream input, int tokenType) |
| void | consumeUntil (IntStream input, BitSet set) |
| List | getRuleInvocationStack () |
| int | getBacktrackingLevel () |
| void | setBacktrackingLevel (int n) |
| boolean | failed () |
| String[] | getTokenNames () |
| String | getGrammarFileName () |
| abstract String | getSourceName () |
| List | toStrings (List tokens) |
| int | getRuleMemoization (int ruleIndex, int ruleStartIndex) |
| boolean | alreadyParsedRule (IntStream input, int ruleIndex) |
| void | memoize (IntStream input, int ruleIndex, int ruleStartIndex) |
| int | getRuleMemoizationCacheSize () |
| void | traceIn (String ruleName, int ruleIndex, Object inputSymbol) |
| void | traceOut (String ruleName, int ruleIndex, Object inputSymbol) |
Static Public Member Functions | |
| static List | getRuleInvocationStack (Throwable e, String recognizerClassName) |
Static Public Attributes | |
| static final int | MEMO_RULE_FAILED = -2 |
| static final int | MEMO_RULE_UNKNOWN = -1 |
| static final int | INITIAL_FOLLOW_STACK_SIZE = 100 |
| static final int | DEFAULT_TOKEN_CHANNEL = Token.DEFAULT_CHANNEL |
| static final int | HIDDEN = Token.HIDDEN_CHANNEL |
| static final String | NEXT_TOKEN_RULE_NAME = "nextToken" |
Protected Member Functions | |
| BitSet | computeErrorRecoverySet () |
| BitSet | computeContextSensitiveRuleFOLLOW () |
| BitSet | combineFollows (boolean exact) |
| Object | recoverFromMismatchedToken (IntStream input, int ttype, BitSet follow) throws RecognitionException |
| Object | getCurrentInputSymbol (IntStream input) |
| Object | getMissingSymbol (IntStream input, RecognitionException e, int expectedTokenType, BitSet follow) |
| void | pushFollow (BitSet fset) |
Protected Attributes | |
| RecognizerSharedState | state |
Definition at line 40 of file BaseRecognizer.java.
| org.antlr.runtime.BaseRecognizer.BaseRecognizer | ( | ) |
Definition at line 59 of file BaseRecognizer.java.
| org.antlr.runtime.BaseRecognizer.BaseRecognizer | ( | RecognizerSharedState | state | ) |
Definition at line 63 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.reset | ( | ) |
reset the parser's state; subclasses must rewinds the input stream
Reimplemented in org.antlr.runtime.Lexer, org.antlr.runtime.Parser, and org.antlr.runtime.tree.TreeParser.
Definition at line 71 of file BaseRecognizer.java.
| Object org.antlr.runtime.BaseRecognizer.match | ( | IntStream | input, | |
| int | ttype, | |||
| BitSet | follow | |||
| ) | throws RecognitionException |
Match current input symbol against ttype. Attempt single token insertion or deletion error recovery. If that fails, throw MismatchedTokenException.
To turn off single token insertion or deletion error recovery, override recoverFromMismatchedToken() and have it throw an exception. See TreeParser.recoverFromMismatchedToken(). This way any error in a rule will cause an exception and immediate exit from rule. Rule would recover by resynchronizing to the set of symbols that can follow rule ref.
Definition at line 100 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.matchAny | ( | IntStream | input | ) |
Match the wildcard: in a symbol
Reimplemented in org.antlr.runtime.tree.TreeParser.
Definition at line 120 of file BaseRecognizer.java.
| boolean org.antlr.runtime.BaseRecognizer.mismatchIsUnwantedToken | ( | IntStream | input, | |
| int | ttype | |||
| ) |
Definition at line 126 of file BaseRecognizer.java.
Definition at line 130 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.reportError | ( | RecognitionException | e | ) |
Report a recognition problem.
This method sets errorRecovery to indicate the parser is recovering not parsing. Once in recovery mode, no errors are generated. To get out of recovery mode, the parser must successfully match a token (after a resync). So it will go:
1. error occurs 2. enter recovery mode, report error 3. consume until token found in resynch set 4. try to resume parsing 5. next match() will reset errorRecovery mode
If you override, make sure to update syntaxErrors if you care about that.
Reimplemented in org.antlr.runtime.debug.DebugParser, org.antlr.runtime.debug.DebugTreeParser, and org.antlr.runtime.Lexer.
Definition at line 176 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.displayRecognitionError | ( | String[] | tokenNames, | |
| RecognitionException | e | |||
| ) |
Definition at line 189 of file BaseRecognizer.java.
| String org.antlr.runtime.BaseRecognizer.getErrorMessage | ( | RecognitionException | e, | |
| String[] | tokenNames | |||
| ) |
What error message should be generated for the various exception types?
Not very object-oriented code, but I like having all error message generation within one method rather than spread among all of the exception classes. This also makes it much easier for the exception handling because the exception classes do not have to have pointers back to this object to access utility routines and so on. Also, changing the message for an exception type would be difficult because you would have to subclassing exception, but then somehow get ANTLR to make those kinds of exception objects instead of the default. This looks weird, but trust me--it makes the most sense in terms of flexibility.
For grammar debugging, you will want to override this to add more information such as the stack frame with getRuleInvocationStack(e, this.getClass().getName()) and, for no viable alts, the decision description and state etc...
Override this to change the message generated for one or more exception types.
Reimplemented in org.antlr.runtime.Lexer, and org.antlr.runtime.tree.TreeParser.
Definition at line 219 of file BaseRecognizer.java.
| int org.antlr.runtime.BaseRecognizer.getNumberOfSyntaxErrors | ( | ) |
Get number of recognition errors (lexer, parser, tree parser). Each recognizer tracks its own number. So parser and lexer each have separate count. Does not count the spurious errors found between an error and next valid token match
See also reportError()
Definition at line 306 of file BaseRecognizer.java.
| String org.antlr.runtime.BaseRecognizer.getErrorHeader | ( | RecognitionException | e | ) |
What is the error header, normally line/character position information?
Reimplemented in org.antlr.runtime.tree.TreeParser.
Definition at line 311 of file BaseRecognizer.java.
| String org.antlr.runtime.BaseRecognizer.getTokenErrorDisplay | ( | Token | t | ) |
How should a token be displayed in an error message? The default is to display just the text, but during development you might want to have a lot of information spit out. Override in that case to use t.toString() (which, for CommonToken, dumps everything about the token). This is better than forcing you to override a method in your token objects because you don't have to go modify your lexer so that it creates a new Java type.
Definition at line 326 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.emitErrorMessage | ( | String | msg | ) |
Override this method to change where error messages go
Definition at line 343 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.recover | ( | IntStream | input, | |
| RecognitionException | re | |||
| ) |
Recover from an error found on the input stream. This is for NoViableAlt and mismatched symbol exceptions. If you enable single token insertion and deletion, this will usually not handle mismatched symbol exceptions but there could be a mismatched token that the match() routine could not recover from.
Definition at line 353 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.beginResync | ( | ) |
A hook to listen in on the token consumption during error recovery. The DebugParser subclasses this to fire events to the listenter.
Reimplemented in org.antlr.runtime.debug.DebugParser, and org.antlr.runtime.debug.DebugTreeParser.
Definition at line 371 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.endResync | ( | ) |
Reimplemented in org.antlr.runtime.debug.DebugParser, and org.antlr.runtime.debug.DebugTreeParser.
Definition at line 374 of file BaseRecognizer.java.
| BitSet org.antlr.runtime.BaseRecognizer.computeErrorRecoverySet | ( | ) | [protected] |
Definition at line 468 of file BaseRecognizer.java.
| BitSet org.antlr.runtime.BaseRecognizer.computeContextSensitiveRuleFOLLOW | ( | ) | [protected] |
Compute the context-sensitive FOLLOW set for current rule. This is set of token types that can follow a specific rule reference given a specific call chain. You get the set of viable tokens that can possibly come next (lookahead depth 1) given the current call chain. Contrast this with the definition of plain FOLLOW for rule r:
FOLLOW(r)={x | S=>*alpha r beta in G and x in FIRST(beta)}
where x in T* and alpha, beta in V*; T is set of terminals and V is the set of terminals and nonterminals. In other words, FOLLOW(r) is the set of all tokens that can possibly follow references to r in *any* sentential form (context). At runtime, however, we know precisely which context applies as we have the call chain. We may compute the exact (rather than covering superset) set of following tokens.
For example, consider grammar:
stat : ID '=' expr ';' // FOLLOW(stat)=={EOF} | "return" expr '.' ; expr : atom ('+' atom)* ; // FOLLOW(expr)=={';','.',')'} atom : INT // FOLLOW(atom)=={'+',')',';','.'} | '(' expr ')' ;
The FOLLOW sets are all inclusive whereas context-sensitive FOLLOW sets are precisely what could follow a rule reference. For input input "i=(3);", here is the derivation:
stat => ID '=' expr ';' => ID '=' atom ('+' atom)* ';' => ID '=' '(' expr ')' ('+' atom)* ';' => ID '=' '(' atom ')' ('+' atom)* ';' => ID '=' '(' INT ')' ('+' atom)* ';' => ID '=' '(' INT ')' ';'
At the "3" token, you'd have a call chain of
stat -> expr -> atom -> expr -> atom
What can follow that specific nested ref to atom? Exactly ')' as you can see by looking at the derivation of this specific input. Contrast this with the FOLLOW(atom)={'+',')',';','.'}.
You want the exact viable token set when recovering from a token mismatch. Upon token mismatch, if LA(1) is member of the viable next token set, then you know there is most likely a missing token in the input stream. "Insert" one by just not throwing an exception.
Definition at line 524 of file BaseRecognizer.java.
| BitSet org.antlr.runtime.BaseRecognizer.combineFollows | ( | boolean | exact | ) | [protected] |
Definition at line 532 of file BaseRecognizer.java.
| Object org.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken | ( | IntStream | input, | |
| int | ttype, | |||
| BitSet | follow | |||
| ) | throws RecognitionException [protected] |
Attempt to recover from a single missing or extra token.
EXTRA TOKEN
LA(1) is not what we are looking for. If LA(2) has the right token, however, then assume LA(1) is some extra spurious token. Delete it and LA(2) as if we were doing a normal match(), which advances the input.
MISSING TOKEN
If current token is consistent with what could come after ttype then it is ok to "insert" the missing token, else throw exception For example, Input "i=(3;" is clearly missing the ')'. When the parser returns from the nested call to expr, it will have call chain:
stat -> expr -> atom
and it will be trying to match the ')' at this point in the derivation:
=> ID '=' '(' INT ')' ('+' atom)* ';' ^ match() will see that ';' doesn't match ')' and report a mismatched token error. To recover, it sees that LA(1)==';' is in the set of tokens that can follow the ')' token reference in rule atom. It can assume that you forgot the ')'.
Reimplemented in org.antlr.runtime.tree.TreeParser.
Definition at line 588 of file BaseRecognizer.java.
| Object org.antlr.runtime.BaseRecognizer.recoverFromMismatchedSet | ( | IntStream | input, | |
| RecognitionException | e, | |||
| BitSet | follow | |||
| ) | throws RecognitionException |
Not currently used
Definition at line 622 of file BaseRecognizer.java.
| Object org.antlr.runtime.BaseRecognizer.getCurrentInputSymbol | ( | IntStream | input | ) | [protected] |
Match needs to return the current input symbol, which gets put into the label for the associated token ref; e.g., x=ID. Token and tree parsers need to return different objects. Rather than test for input stream type or change the IntStream interface, I use a simple method to ask the recognizer to tell me what the current input symbol is.
This is ignored for lexers.
Reimplemented in org.antlr.runtime.Parser, and org.antlr.runtime.tree.TreeParser.
Definition at line 646 of file BaseRecognizer.java.
| Object org.antlr.runtime.BaseRecognizer.getMissingSymbol | ( | IntStream | input, | |
| RecognitionException | e, | |||
| int | expectedTokenType, | |||
| BitSet | follow | |||
| ) | [protected] |
Conjure up a missing token during error recovery.
The recognizer attempts to recover from single missing symbols. But, actions might refer to that missing symbol. For example, x=ID {f($x);}. The action clearly assumes that there has been an identifier matched previously and that $x points at that token. If that token is missing, but the next token in the stream is what we want we assume that this token is missing and we keep going. Because we have to return some token to replace the missing token, we have to conjure one up. This method gives the user control over the tokens returned for missing tokens. Mostly, you will want to create something special for identifier tokens. For literals such as '{' and ',', the default action in the parser or tree parser works. It simply creates a CommonToken of the appropriate type. The text will be the token. If you change what tokens must be created by the lexer, override this method to create the appropriate tokens.
Reimplemented in org.antlr.runtime.debug.DebugTreeParser, org.antlr.runtime.Parser, and org.antlr.runtime.tree.TreeParser.
Definition at line 667 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.consumeUntil | ( | IntStream | input, | |
| int | tokenType | |||
| ) |
Definition at line 675 of file BaseRecognizer.java.
Consume tokens until one matches the given token set
Definition at line 685 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.pushFollow | ( | BitSet | fset | ) | [protected] |
Push a rule's follow set using our own hardcoded stack
Definition at line 696 of file BaseRecognizer.java.
| List org.antlr.runtime.BaseRecognizer.getRuleInvocationStack | ( | ) |
Return List<String> of the rules in your parser instance leading up to a call to this method. You could override if you want more details such as the file/line info of where in the parser java code a rule is invoked.
This is very useful for error messages and for context-sensitive error recovery.
Definition at line 713 of file BaseRecognizer.java.
| static List org.antlr.runtime.BaseRecognizer.getRuleInvocationStack | ( | Throwable | e, | |
| String | recognizerClassName | |||
| ) | [static] |
A more general version of getRuleInvocationStack where you can pass in, for example, a RecognitionException to get it's rule stack trace. This routine is shared with all recognizers, hence, static.
TODO: move to a utility class or something; weird having lexer call this
Definition at line 725 of file BaseRecognizer.java.
| int org.antlr.runtime.BaseRecognizer.getBacktrackingLevel | ( | ) |
Definition at line 747 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.setBacktrackingLevel | ( | int | n | ) |
Definition at line 749 of file BaseRecognizer.java.
| boolean org.antlr.runtime.BaseRecognizer.failed | ( | ) |
Return whether or not a backtracking attempt failed.
Definition at line 752 of file BaseRecognizer.java.
| String [] org.antlr.runtime.BaseRecognizer.getTokenNames | ( | ) |
Used to print out token names like ID during debugging and error reporting. The generated parsers implement a method that overrides this to point to their String[] tokenNames.
Definition at line 758 of file BaseRecognizer.java.
| String org.antlr.runtime.BaseRecognizer.getGrammarFileName | ( | ) |
For debugging and other purposes, might want the grammar name. Have ANTLR generate an implementation for this method.
Definition at line 765 of file BaseRecognizer.java.
| abstract String org.antlr.runtime.BaseRecognizer.getSourceName | ( | ) | [pure virtual] |
Implemented in org.antlr.runtime.Lexer, org.antlr.runtime.Parser, and org.antlr.runtime.tree.TreeParser.
| List org.antlr.runtime.BaseRecognizer.toStrings | ( | List | tokens | ) |
A convenience method for use most often with template rewrites. Convert a List<Token> to List<String>
Definition at line 774 of file BaseRecognizer.java.
| int org.antlr.runtime.BaseRecognizer.getRuleMemoization | ( | int | ruleIndex, | |
| int | ruleStartIndex | |||
| ) |
Given a rule number and a start token index number, return MEMO_RULE_UNKNOWN if the rule has not parsed input starting from start index. If this rule has parsed input starting from the start index before, then return where the rule stopped parsing. It returns the index of the last token matched by the rule.
For now we use a hashtable and just the slow Object-based one. Later, we can make a special one for ints and also one that tosses out data after we commit past input position i.
Definition at line 793 of file BaseRecognizer.java.
| boolean org.antlr.runtime.BaseRecognizer.alreadyParsedRule | ( | IntStream | input, | |
| int | ruleIndex | |||
| ) |
Has this rule already parsed input at the current index in the input stream? Return the stop token index or MEMO_RULE_UNKNOWN. If we attempted but failed to parse properly before, return MEMO_RULE_FAILED.
This method has a side-effect: if we have seen this input for this rule and successfully parsed before, then seek ahead to 1 past the stop token matched for this rule last time.
Definition at line 814 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.memoize | ( | IntStream | input, | |
| int | ruleIndex, | |||
| int | ruleStartIndex | |||
| ) |
Record whether or not this rule parsed the input at this position successfully. Use a standard java hashtable for now.
Definition at line 833 of file BaseRecognizer.java.
| int org.antlr.runtime.BaseRecognizer.getRuleMemoizationCacheSize | ( | ) |
return how many rule/input-index pairs there are in total. TODO: this includes synpreds. :(
Definition at line 854 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.traceIn | ( | String | ruleName, | |
| int | ruleIndex, | |||
| Object | inputSymbol | |||
| ) |
Definition at line 865 of file BaseRecognizer.java.
| void org.antlr.runtime.BaseRecognizer.traceOut | ( | String | ruleName, | |
| int | ruleIndex, | |||
| Object | inputSymbol | |||
| ) |
Definition at line 873 of file BaseRecognizer.java.
final int org.antlr.runtime.BaseRecognizer.MEMO_RULE_FAILED = -2 [static] |
Definition at line 41 of file BaseRecognizer.java.
final int org.antlr.runtime.BaseRecognizer.MEMO_RULE_UNKNOWN = -1 [static] |
Definition at line 42 of file BaseRecognizer.java.
final int org.antlr.runtime.BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE = 100 [static] |
Definition at line 43 of file BaseRecognizer.java.
final int org.antlr.runtime.BaseRecognizer.DEFAULT_TOKEN_CHANNEL = Token.DEFAULT_CHANNEL [static] |
Definition at line 46 of file BaseRecognizer.java.
final int org.antlr.runtime.BaseRecognizer.HIDDEN = Token.HIDDEN_CHANNEL [static] |
Definition at line 47 of file BaseRecognizer.java.
final String org.antlr.runtime.BaseRecognizer.NEXT_TOKEN_RULE_NAME = "nextToken" [static] |
Definition at line 49 of file BaseRecognizer.java.
State of a lexer, parser, or tree parser are collected into a state object so the state can be shared. This sharing is needed to have one grammar import others and share same error variables and other state variables. It's a kind of explicit multiple inheritance via delegation of methods and shared state.
Definition at line 57 of file BaseRecognizer.java.
1.5.5