xref: /aosp_15_r20/external/antlr/runtime/C/include/antlr3recognizersharedstate.h (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robot /** \file
2*16467b97STreehugger Robot  * While the C runtime does not need to model the state of
3*16467b97STreehugger Robot  * multiple lexers and parsers in the same way as the Java runtime does
4*16467b97STreehugger Robot  * it is no overhead to reflect that model. In fact the
5*16467b97STreehugger Robot  * C runtime has always been able to share recognizer state.
6*16467b97STreehugger Robot  *
7*16467b97STreehugger Robot  * This 'class' therefore defines all the elements of a recognizer
8*16467b97STreehugger Robot  * (either lexer, parser or tree parser) that are need to
9*16467b97STreehugger Robot  * track the current recognition state. Multiple recognizers
10*16467b97STreehugger Robot  * may then share this state, for instance when one grammar
11*16467b97STreehugger Robot  * imports another.
12*16467b97STreehugger Robot  */
13*16467b97STreehugger Robot 
14*16467b97STreehugger Robot #ifndef	_ANTLR3_RECOGNIZER_SHARED_STATE_H
15*16467b97STreehugger Robot #define	_ANTLR3_RECOGNIZER_SHARED_STATE_H
16*16467b97STreehugger Robot 
17*16467b97STreehugger Robot // [The "BSD licence"]
18*16467b97STreehugger Robot // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
19*16467b97STreehugger Robot // http://www.temporal-wave.com
20*16467b97STreehugger Robot // http://www.linkedin.com/in/jimidle
21*16467b97STreehugger Robot //
22*16467b97STreehugger Robot // All rights reserved.
23*16467b97STreehugger Robot //
24*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without
25*16467b97STreehugger Robot // modification, are permitted provided that the following conditions
26*16467b97STreehugger Robot // are met:
27*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright
28*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer.
29*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright
30*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer in the
31*16467b97STreehugger Robot //    documentation and/or other materials provided with the distribution.
32*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products
33*16467b97STreehugger Robot //    derived from this software without specific prior written permission.
34*16467b97STreehugger Robot //
35*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
38*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
39*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*16467b97STreehugger Robot 
46*16467b97STreehugger Robot #include    <antlr3defs.h>
47*16467b97STreehugger Robot 
48*16467b97STreehugger Robot #ifdef __cplusplus
49*16467b97STreehugger Robot extern "C" {
50*16467b97STreehugger Robot #endif
51*16467b97STreehugger Robot 
52*16467b97STreehugger Robot /** All the data elements required to track the current state
53*16467b97STreehugger Robot  *  of any recognizer (lexer, parser, tree parser).
54*16467b97STreehugger Robot  * May be share between multiple recognizers such that
55*16467b97STreehugger Robot  * grammar inheritance is easily supported.
56*16467b97STreehugger Robot  */
57*16467b97STreehugger Robot typedef	struct ANTLR3_RECOGNIZER_SHARED_STATE_struct
58*16467b97STreehugger Robot {
59*16467b97STreehugger Robot     /** If set to ANTLR3_TRUE then the recognizer has an exception
60*16467b97STreehugger Robot      * condition (this is tested by the generated code for the rules of
61*16467b97STreehugger Robot      * the grammar).
62*16467b97STreehugger Robot      */
63*16467b97STreehugger Robot     ANTLR3_BOOLEAN	    error;
64*16467b97STreehugger Robot 
65*16467b97STreehugger Robot     /** Points to the first in a possible chain of exceptions that the
66*16467b97STreehugger Robot      *  recognizer has discovered.
67*16467b97STreehugger Robot      */
68*16467b97STreehugger Robot     pANTLR3_EXCEPTION	    exception;
69*16467b97STreehugger Robot 
70*16467b97STreehugger Robot     /** Track around a hint from the creator of the recognizer as to how big this
71*16467b97STreehugger Robot      *  thing is going to get, as the actress said to the bishop. This allows us
72*16467b97STreehugger Robot      *  to tune hash tables accordingly. This might not be the best place for this
73*16467b97STreehugger Robot      *  in the end but we will see.
74*16467b97STreehugger Robot      */
75*16467b97STreehugger Robot     ANTLR3_UINT32	sizeHint;
76*16467b97STreehugger Robot 
77*16467b97STreehugger Robot     /** Track the set of token types that can follow any rule invocation.
78*16467b97STreehugger Robot      *  Stack structure, to support: List<BitSet>.
79*16467b97STreehugger Robot      */
80*16467b97STreehugger Robot     pANTLR3_STACK	following;
81*16467b97STreehugger Robot 
82*16467b97STreehugger Robot 
83*16467b97STreehugger Robot     /** This is true when we see an error and before having successfully
84*16467b97STreehugger Robot      *  matched a token.  Prevents generation of more than one error message
85*16467b97STreehugger Robot      *  per error.
86*16467b97STreehugger Robot      */
87*16467b97STreehugger Robot     ANTLR3_BOOLEAN	errorRecovery;
88*16467b97STreehugger Robot 
89*16467b97STreehugger Robot     /** The index into the input stream where the last error occurred.
90*16467b97STreehugger Robot      * 	This is used to prevent infinite loops where an error is found
91*16467b97STreehugger Robot      *  but no token is consumed during recovery...another error is found,
92*16467b97STreehugger Robot      *  ad nauseam.  This is a failsafe mechanism to guarantee that at least
93*16467b97STreehugger Robot      *  one token/tree node is consumed for two errors.
94*16467b97STreehugger Robot      */
95*16467b97STreehugger Robot     ANTLR3_MARKER	lastErrorIndex;
96*16467b97STreehugger Robot 
97*16467b97STreehugger Robot     /** In lieu of a return value, this indicates that a rule or token
98*16467b97STreehugger Robot      *  has failed to match.  Reset to false upon valid token match.
99*16467b97STreehugger Robot      */
100*16467b97STreehugger Robot     ANTLR3_BOOLEAN	failed;
101*16467b97STreehugger Robot 
102*16467b97STreehugger Robot     /** When the recognizer terminates, the error handling functions
103*16467b97STreehugger Robot      *  will have incremented this value if any error occurred (that was displayed). It can then be
104*16467b97STreehugger Robot      *  used by the grammar programmer without having to use static globals.
105*16467b97STreehugger Robot      */
106*16467b97STreehugger Robot     ANTLR3_UINT32	errorCount;
107*16467b97STreehugger Robot 
108*16467b97STreehugger Robot     /** If 0, no backtracking is going on.  Safe to exec actions etc...
109*16467b97STreehugger Robot      *  If >0 then it's the level of backtracking.
110*16467b97STreehugger Robot      */
111*16467b97STreehugger Robot     ANTLR3_INT32	backtracking;
112*16467b97STreehugger Robot 
113*16467b97STreehugger Robot     /** ANTLR3_VECTOR of ANTLR3_LIST for rule memoizing.
114*16467b97STreehugger Robot      *  Tracks  the stop token index for each rule.  ruleMemo[ruleIndex] is
115*16467b97STreehugger Robot      *  the memoization table for ruleIndex.  For key ruleStartIndex, you
116*16467b97STreehugger Robot      *  get back the stop token for associated rule or MEMO_RULE_FAILED.
117*16467b97STreehugger Robot      *
118*16467b97STreehugger Robot      *  This is only used if rule memoization is on.
119*16467b97STreehugger Robot      */
120*16467b97STreehugger Robot     pANTLR3_INT_TRIE	ruleMemo;
121*16467b97STreehugger Robot 
122*16467b97STreehugger Robot     /** Pointer to an array of token names
123*16467b97STreehugger Robot      *  that are generally useful in error reporting. The generated parsers install
124*16467b97STreehugger Robot      *  this pointer. The table it points to is statically allocated as 8 bit ascii
125*16467b97STreehugger Robot      *  at parser compile time - grammar token names are thus restricted in character
126*16467b97STreehugger Robot      *  sets, which does not seem to terrible.
127*16467b97STreehugger Robot      */
128*16467b97STreehugger Robot     pANTLR3_UINT8	* tokenNames;
129*16467b97STreehugger Robot 
130*16467b97STreehugger Robot     /** User programmable pointer that can be used for instance as a place to
131*16467b97STreehugger Robot      *  store some tracking structure specific to the grammar that would not normally
132*16467b97STreehugger Robot      *  be available to the error handling functions.
133*16467b97STreehugger Robot      */
134*16467b97STreehugger Robot     void		* userp;
135*16467b97STreehugger Robot 
136*16467b97STreehugger Robot 	    /** The goal of all lexer rules/methods is to create a token object.
137*16467b97STreehugger Robot      *  This is an instance variable as multiple rules may collaborate to
138*16467b97STreehugger Robot      *  create a single token.  For example, NUM : INT | FLOAT ;
139*16467b97STreehugger Robot      *  In this case, you want the INT or FLOAT rule to set token and not
140*16467b97STreehugger Robot      *  have it reset to a NUM token in rule NUM.
141*16467b97STreehugger Robot      */
142*16467b97STreehugger Robot     pANTLR3_COMMON_TOKEN	token;
143*16467b97STreehugger Robot 
144*16467b97STreehugger Robot     /** The goal of all lexer rules being to create a token, then a lexer
145*16467b97STreehugger Robot      *  needs to build a token factory to create them.
146*16467b97STreehugger Robot      */
147*16467b97STreehugger Robot     pANTLR3_TOKEN_FACTORY	tokFactory;
148*16467b97STreehugger Robot 
149*16467b97STreehugger Robot     /** A lexer is a source of tokens, produced by all the generated (or
150*16467b97STreehugger Robot      *  hand crafted if you like) matching rules. As such it needs to provide
151*16467b97STreehugger Robot      *  a token source interface implementation.
152*16467b97STreehugger Robot      */
153*16467b97STreehugger Robot     pANTLR3_TOKEN_SOURCE	tokSource;
154*16467b97STreehugger Robot 
155*16467b97STreehugger Robot     /** The channel number for the current token
156*16467b97STreehugger Robot      */
157*16467b97STreehugger Robot     ANTLR3_UINT32		channel;
158*16467b97STreehugger Robot 
159*16467b97STreehugger Robot     /** The token type for the current token
160*16467b97STreehugger Robot      */
161*16467b97STreehugger Robot     ANTLR3_UINT32		type;
162*16467b97STreehugger Robot 
163*16467b97STreehugger Robot     /** The input line (where it makes sense) on which the first character of the current
164*16467b97STreehugger Robot      *  token resides.
165*16467b97STreehugger Robot      */
166*16467b97STreehugger Robot     ANTLR3_INT32		tokenStartLine;
167*16467b97STreehugger Robot 
168*16467b97STreehugger Robot     /** The character position of the first character of the current token
169*16467b97STreehugger Robot      *  within the line specified by tokenStartLine
170*16467b97STreehugger Robot      */
171*16467b97STreehugger Robot     ANTLR3_INT32		tokenStartCharPositionInLine;
172*16467b97STreehugger Robot 
173*16467b97STreehugger Robot     /** What character index in the stream did the current token start at?
174*16467b97STreehugger Robot      *  Needed, for example, to get the text for current token.  Set at
175*16467b97STreehugger Robot      *  the start of nextToken.
176*16467b97STreehugger Robot      */
177*16467b97STreehugger Robot     ANTLR3_MARKER		tokenStartCharIndex;
178*16467b97STreehugger Robot 
179*16467b97STreehugger Robot     /** Text for the current token. This can be overridden by setting this
180*16467b97STreehugger Robot      *  variable directly or by using the SETTEXT() macro (preferred) in your
181*16467b97STreehugger Robot      *  lexer rules.
182*16467b97STreehugger Robot      */
183*16467b97STreehugger Robot     pANTLR3_STRING		text;
184*16467b97STreehugger Robot 
185*16467b97STreehugger Robot 	/** User controlled variables that will be installed in a newly created
186*16467b97STreehugger Robot 	 * token.
187*16467b97STreehugger Robot 	 */
188*16467b97STreehugger Robot 	ANTLR3_UINT32		user1, user2, user3;
189*16467b97STreehugger Robot 	void				* custom;
190*16467b97STreehugger Robot 
191*16467b97STreehugger Robot     /** Input stream stack, which allows the C programmer to switch input streams
192*16467b97STreehugger Robot      *  easily and allow the standard nextToken() implementation to deal with it
193*16467b97STreehugger Robot      *  as this is a common requirement.
194*16467b97STreehugger Robot      */
195*16467b97STreehugger Robot     pANTLR3_STACK		streams;
196*16467b97STreehugger Robot 
197*16467b97STreehugger Robot 	/// A stack of token/tree rewrite streams that are available for use
198*16467b97STreehugger Robot 	/// by a parser or tree parser that is using rewrites to generate
199*16467b97STreehugger Robot 	/// an AST. This saves each rule in the recongizer from having to
200*16467b97STreehugger Robot 	/// allocate and deallocate rewtire streams on entry and exit. As
201*16467b97STreehugger Robot 	/// the parser recurses throgh the rules it will reach a steady state
202*16467b97STreehugger Robot 	/// of the maximum number of allocated streams, which instead of
203*16467b97STreehugger Robot 	/// deallocating them at rule exit, it will place on this stack for
204*16467b97STreehugger Robot 	/// reuse. The streams are then all finally freed when this stack
205*16467b97STreehugger Robot 	/// is freed.
206*16467b97STreehugger Robot 	///
207*16467b97STreehugger Robot 	pANTLR3_VECTOR		rStreams;
208*16467b97STreehugger Robot 
209*16467b97STreehugger Robot }
210*16467b97STreehugger Robot 	ANTLR3_RECOGNIZER_SHARED_STATE;
211*16467b97STreehugger Robot 
212*16467b97STreehugger Robot #ifdef __cplusplus
213*16467b97STreehugger Robot }
214*16467b97STreehugger Robot #endif
215*16467b97STreehugger Robot 
216*16467b97STreehugger Robot #endif
217*16467b97STreehugger Robot 
218*16467b97STreehugger Robot 
219