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