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
33namespace Antlr.Runtime
34{
35    using System.Collections.Generic;
36    using CLSCompliant = System.CLSCompliantAttribute;
37    using ArgumentNullException = System.ArgumentNullException;
38
39    /** <summary>
40     *  The set of fields needed by an abstract recognizer to recognize input
41     *  and recover from errors etc...  As a separate state object, it can be
42     *  shared among multiple grammars; e.g., when one grammar imports another.
43     *  </summary>
44     *
45     *  <remarks>
46     *  These fields are publically visible but the actual state pointer per
47     *  parser is protected.
48     *  </remarks>
49     */
50    public class RecognizerSharedState
51    {
52        /** <summary>
53         *  Track the set of token types that can follow any rule invocation.
54         *  Stack grows upwards.  When it hits the max, it grows 2x in size
55         *  and keeps going.
56         *  </summary>
57         */
58        //public List<BitSet> following;
59        public BitSet[] following;
60        [CLSCompliant( false )]
61        public int _fsp;
62
63        /** <summary>
64         *  This is true when we see an error and before having successfully
65         *  matched a token.  Prevents generation of more than one error message
66         *  per error.
67         *  </summary>
68         */
69        public bool errorRecovery;
70
71        /** <summary>
72         *  The index into the input stream where the last error occurred.
73         * 	This is used to prevent infinite loops where an error is found
74         *  but no token is consumed during recovery...another error is found,
75         *  ad naseum.  This is a failsafe mechanism to guarantee that at least
76         *  one token/tree node is consumed for two errors.
77         *  </summary>
78         */
79        public int lastErrorIndex;
80
81        /** <summary>
82         *  In lieu of a return value, this indicates that a rule or token
83         *  has failed to match.  Reset to false upon valid token match.
84         *  </summary>
85         */
86        public bool failed;
87
88        /** <summary>Did the recognizer encounter a syntax error?  Track how many.</summary> */
89        public int syntaxErrors;
90
91        /** <summary>
92         *  If 0, no backtracking is going on.  Safe to exec actions etc...
93         *  If >0 then it's the level of backtracking.
94         *  </summary>
95         */
96        public int backtracking;
97
98        /** <summary>
99         *  An array[size num rules] of Map<Integer,Integer> that tracks
100         *  the stop token index for each rule.  ruleMemo[ruleIndex] is
101         *  the memoization table for ruleIndex.  For key ruleStartIndex, you
102         *  get back the stop token for associated rule or MEMO_RULE_FAILED.
103         *  </summary>
104         *
105         *  <remarks>This is only used if rule memoization is on (which it is by default).</remarks>
106         */
107        public IDictionary<int, int>[] ruleMemo;
108
109
110        // LEXER FIELDS (must be in same state object to avoid casting
111        //               constantly in generated code and Lexer object) :(
112
113
114        /** <summary>
115         *  The goal of all lexer rules/methods is to create a token object.
116         *  This is an instance variable as multiple rules may collaborate to
117         *  create a single token.  nextToken will return this object after
118         *  matching lexer rule(s).  If you subclass to allow multiple token
119         *  emissions, then set this to the last token to be matched or
120         *  something nonnull so that the auto token emit mechanism will not
121         *  emit another token.
122         *  </summary>
123         */
124        public IToken token;
125
126        /** <summary>
127         *  What character index in the stream did the current token start at?
128         *  Needed, for example, to get the text for current token.  Set at
129         *  the start of nextToken.
130         *  </summary>
131         */
132        public int tokenStartCharIndex;
133
134        /** <summary>The line on which the first character of the token resides</summary> */
135        public int tokenStartLine;
136
137        /** <summary>The character position of first character within the line</summary> */
138        public int tokenStartCharPositionInLine;
139
140        /** <summary>The channel number for the current token</summary> */
141        public int channel;
142
143        /** <summary>The token type for the current token</summary> */
144        public int type;
145
146        /** <summary>
147         *  You can set the text for the current token to override what is in
148         *  the input char buffer.  Use setText() or can set this instance var.
149         *  </summary>
150         */
151        public string text;
152
153        public RecognizerSharedState()
154        {
155            //following = new List<BitSet>( BaseRecognizer.InitialFollowStackSize );
156            following = new BitSet[BaseRecognizer.InitialFollowStackSize];
157            _fsp = -1;
158            lastErrorIndex = -1;
159            tokenStartCharIndex = -1;
160        }
161
162        public RecognizerSharedState( RecognizerSharedState state )
163        {
164            if (state == null)
165                throw new ArgumentNullException("state");
166
167            following = (BitSet[])state.following.Clone();
168            _fsp = state._fsp;
169            errorRecovery = state.errorRecovery;
170            lastErrorIndex = state.lastErrorIndex;
171            failed = state.failed;
172            syntaxErrors = state.syntaxErrors;
173            backtracking = state.backtracking;
174
175            if ( state.ruleMemo != null )
176                ruleMemo = (IDictionary<int, int>[])state.ruleMemo.Clone();
177
178            token = state.token;
179            tokenStartCharIndex = state.tokenStartCharIndex;
180            tokenStartCharPositionInLine = state.tokenStartCharPositionInLine;
181            channel = state.channel;
182            type = state.type;
183            text = state.text;
184        }
185    }
186}
187