Home | History | Annotate | Download | only in runtime
      1 /*
      2  [The "BSD license"]
      3  Copyright (c) 2005-2009 Terence Parr
      4  All rights reserved.
      5 
      6  Redistribution and use in source and binary forms, with or without
      7  modification, are permitted provided that the following conditions
      8  are met:
      9  1. Redistributions of source code must retain the above copyright
     10      notice, this list of conditions and the following disclaimer.
     11  2. Redistributions in binary form must reproduce the above copyright
     12      notice, this list of conditions and the following disclaimer in the
     13      documentation and/or other materials provided with the distribution.
     14  3. The name of the author may not be used to endorse or promote products
     15      derived from this software without specific prior written permission.
     16 
     17  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */package org.antlr.runtime;
     28 
     29 import java.util.Map;
     30 
     31 /** The set of fields needed by an abstract recognizer to recognize input
     32  *  and recover from errors etc...  As a separate state object, it can be
     33  *  shared among multiple grammars; e.g., when one grammar imports another.
     34  *
     35  *  These fields are publically visible but the actual state pointer per
     36  *  parser is protected.
     37  */
     38 public class RecognizerSharedState {
     39 	/** Track the set of token types that can follow any rule invocation.
     40 	 *  Stack grows upwards.  When it hits the max, it grows 2x in size
     41 	 *  and keeps going.
     42 	 */
     43 	public BitSet[] following = new BitSet[BaseRecognizer.INITIAL_FOLLOW_STACK_SIZE];
     44 	public int _fsp = -1;
     45 
     46 	/** This is true when we see an error and before having successfully
     47 	 *  matched a token.  Prevents generation of more than one error message
     48 	 *  per error.
     49 	 */
     50 	public boolean errorRecovery = false;
     51 
     52 	/** The index into the input stream where the last error occurred.
     53 	 * 	This is used to prevent infinite loops where an error is found
     54 	 *  but no token is consumed during recovery...another error is found,
     55 	 *  ad naseum.  This is a failsafe mechanism to guarantee that at least
     56 	 *  one token/tree node is consumed for two errors.
     57 	 */
     58 	public int lastErrorIndex = -1;
     59 
     60 	/** In lieu of a return value, this indicates that a rule or token
     61 	 *  has failed to match.  Reset to false upon valid token match.
     62 	 */
     63 	public boolean failed = false;
     64 
     65 	/** Did the recognizer encounter a syntax error?  Track how many. */
     66 	public int syntaxErrors = 0;
     67 
     68 	/** If 0, no backtracking is going on.  Safe to exec actions etc...
     69 	 *  If >0 then it's the level of backtracking.
     70 	 */
     71 	public int backtracking = 0;
     72 
     73 	/** An array[size num rules] of Map<Integer,Integer> that tracks
     74 	 *  the stop token index for each rule.  ruleMemo[ruleIndex] is
     75 	 *  the memoization table for ruleIndex.  For key ruleStartIndex, you
     76 	 *  get back the stop token for associated rule or MEMO_RULE_FAILED.
     77 	 *
     78 	 *  This is only used if rule memoization is on (which it is by default).
     79 	 */
     80 	public Map[] ruleMemo;
     81 
     82 
     83 	// LEXER FIELDS (must be in same state object to avoid casting
     84 	//               constantly in generated code and Lexer object) :(
     85 
     86 
     87 	/** The goal of all lexer rules/methods is to create a token object.
     88 	 *  This is an instance variable as multiple rules may collaborate to
     89 	 *  create a single token.  nextToken will return this object after
     90 	 *  matching lexer rule(s).  If you subclass to allow multiple token
     91 	 *  emissions, then set this to the last token to be matched or
     92 	 *  something nonnull so that the auto token emit mechanism will not
     93 	 *  emit another token.
     94 	 */
     95     public Token token;
     96 
     97 	/** What character index in the stream did the current token start at?
     98 	 *  Needed, for example, to get the text for current token.  Set at
     99 	 *  the start of nextToken.
    100  	 */
    101 	public int tokenStartCharIndex = -1;
    102 
    103 	/** The line on which the first character of the token resides */
    104 	public int tokenStartLine;
    105 
    106 	/** The character position of first character within the line */
    107 	public int tokenStartCharPositionInLine;
    108 
    109 	/** The channel number for the current token */
    110 	public int channel;
    111 
    112 	/** The token type for the current token */
    113 	public int type;
    114 
    115 	/** You can set the text for the current token to override what is in
    116 	 *  the input char buffer.  Use setText() or can set this instance var.
    117  	 */
    118 	public String text;
    119 
    120     public RecognizerSharedState() {;}
    121 
    122     public RecognizerSharedState(RecognizerSharedState state) {
    123         if ( this.following.length < state.following.length ) {
    124             this.following = new BitSet[state.following.length];
    125         }
    126         System.arraycopy(state.following, 0, this.following, 0, state.following.length);
    127         this._fsp = state._fsp;
    128         this.errorRecovery = state.errorRecovery;
    129         this.lastErrorIndex = state.lastErrorIndex;
    130         this.failed = state.failed;
    131         this.syntaxErrors = state.syntaxErrors;
    132         this.backtracking = state.backtracking;
    133         if ( state.ruleMemo!=null ) {
    134             this.ruleMemo = new Map[state.ruleMemo.length];
    135             System.arraycopy(state.ruleMemo, 0, this.ruleMemo, 0, state.ruleMemo.length);
    136         }
    137         this.token = state.token;
    138         this.tokenStartCharIndex = state.tokenStartCharIndex;
    139         this.tokenStartCharPositionInLine = state.tokenStartCharPositionInLine;
    140         this.channel = state.channel;
    141         this.type = state.type;
    142         this.text = state.text;
    143     }
    144 }
    145