1/*
2 * [The "BSD license"]
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/** Template overrides to add debugging to normal Java output;
33 *  If ASTs are built, then you'll also get ASTDbg.stg loaded.
34 */
35
36@outputFile.imports() ::= <<
37<@super.imports()>
38using Antlr.Runtime.Debug;
39using IOException = System.IO.IOException;
40>>
41
42@genericParser.members() ::= <<
43<if(grammar.grammarIsRoot)>
44public static readonly string[] ruleNames =
45	new string[]
46	{
47		"invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n	", separator=", ">
48	};<\n>
49<endif>
50<if(grammar.grammarIsRoot)><! grammar imports other grammar(s) !>
51	int ruleLevel = 0;
52	public virtual int RuleLevel { get { return ruleLevel; } }
53	public virtual void IncRuleLevel() { ruleLevel++; }
54	public virtual void DecRuleLevel() { ruleLevel--; }
55<if(profile)>
56	<ctorForProfilingRootGrammar()>
57<else>
58	<ctorForRootGrammar()>
59<endif>
60<ctorForPredefinedListener()>
61<else><! imported grammar !>
62	public int RuleLevel { get { return <grammar.delegators:{g| <g:delegateName()>}>.RuleLevel; } }
63	public void IncRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.IncRuleLevel(); }
64	public void DecRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.DecRuleLevel(); }
65	<ctorForDelegateGrammar()>
66<endif>
67<if(profile)>
68public override bool AlreadyParsedRule( IIntStream input, int ruleIndex )
69{
70	int stopIndex = GetRuleMemoization(ruleIndex, input.Index);
71	((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, stopIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
72	return base.AlreadyParsedRule(input, ruleIndex);
73}<\n>
74public override void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex )
75{
76	((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
77	base.Memoize(input, ruleIndex, ruleStartIndex);
78}<\n>
79<endif>
80protected virtual bool EvalPredicate( bool result, string predicate )
81{
82	dbg.SemanticPredicate( result, predicate );
83	return result;
84}<\n>
85>>
86
87ctorForRootGrammar() ::= <<
88<! bug: can't use <@super.members()> cut-n-paste instead !>
89<! Same except we add port number and profile stuff if root grammar !>
90<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input )
91	: this( input, DebugEventSocketProxy.DefaultDebuggerPort, new RecognizerSharedState() )
92{
93}
94<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, int port, RecognizerSharedState state )
95	: base( input, state )
96{
97	<parserCtorBody()>
98	<createListenerAndHandshake()>
99	<grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>( input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n">
100	<@finally()>
101}<\n>
102>>
103
104ctorForProfilingRootGrammar() ::= <<
105<! bug: can't use <@super.members()> cut-n-paste instead !>
106<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input )
107	: this( input, new Profiler(null), new RecognizerSharedState() )
108{
109}
110<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state )
111	: base( input, dbg, state )
112{
113	Profiler p = (Profiler)dbg;
114	p.setParser(this);
115	<parserCtorBody()>
116	<grammar.directDelegates:
117	 {g|<g:delegateName()> = new <g.recognizerName>( input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n">
118	<@finally()>
119}
120<\n>
121>>
122
123/** Basically we don't want to set any dbg listeners are root will have it. */
124ctorForDelegateGrammar() ::= <<
125<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
126	: base( input, dbg, state )
127{
128	<parserCtorBody()>
129	<grammar.directDelegates:
130	 {g|<g:delegateName()> = new <g.recognizerName>( input, this, this.state<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n">
131}<\n>
132>>
133
134ctorForPredefinedListener() ::= <<
135<actions.(actionScope).ctorModifier; null="public"> <name>( <inputStreamType> input, IDebugEventListener dbg )
136	<@superClassRef>: base( input, dbg, new RecognizerSharedState() )<@end>
137{
138<if(profile)>
139	Profiler p = (Profiler)dbg;
140	p.setParser(this);
141<endif>
142	<parserCtorBody()>
143	<grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
144	<@finally()>
145}<\n>
146>>
147
148createListenerAndHandshake() ::= <<
149<if(TREE_PARSER)>
150DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, input.TreeAdaptor );<\n>
151<else>
152DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, null );<\n>
153<endif>
154DebugListener = proxy;
155try
156{
157	proxy.Handshake();
158}
159catch ( IOException ioe )
160{
161	ReportError( ioe );
162}
163>>
164
165@genericParser.superClassName() ::= "Debug<@super.superClassName()>"
166
167/*
168 * Much of the following rules were merged into CSharp3.stg.
169 */
170
171@rule.preamble() ::= <<
172if (RuleLevel == 0)
173	DebugListener.Commence();
174IncRuleLevel();
175>>
176//@rule.preamble() ::= <<
177//try
178//{
179//	dbg.EnterRule( GrammarFileName, "<ruleName>" );
180//	if ( RuleLevel == 0 )
181//	{
182//		dbg.Commence();
183//	}
184//	IncRuleLevel();
185//	dbg.Location( <ruleDescriptor.tree.line>, <ruleDescriptor.tree.charPositionInLine> );<\n>
186//>>
187
188@rule.postamble() ::= <<
189DecRuleLevel();
190if (RuleLevel == 0)
191	DebugListener.Terminate();
192>>
193//@rule.postamble() ::= <<
194//dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);<\n>
195//}
196//finally
197//{
198//	dbg.ExitRule( GrammarFileName, "<ruleName>" );
199//	DecRuleLevel();
200//	if ( RuleLevel == 0 )
201//	{
202//		dbg.Terminate();
203//	}
204//}<\n>
205//>>
206
207//@insertSynpreds.start() ::= "dbg.BeginBacktrack( state.backtracking );"
208//@insertSynpreds.stop() ::= "dbg.EndBacktrack( state.backtracking, success );"
209
210// Common debug event triggers used by region overrides below
211
212//enterSubRule() ::= <<
213//try
214//{
215//	dbg.EnterSubRule( <decisionNumber> );<\n>
216//>>
217
218//exitSubRule() ::= <<
219//}
220//finally
221//{
222//	dbg.ExitSubRule( <decisionNumber> );
223//}<\n>
224//>>
225
226//enterDecision() ::= <<
227//try
228//{
229//	dbg.EnterDecision( <decisionNumber> );<\n>
230//>>
231
232//exitDecision() ::= <<
233//}
234//finally
235//{
236//	dbg.ExitDecision( <decisionNumber> );
237//}<\n>
238//>>
239
240//enterAlt(n) ::= "dbg.EnterAlt( <n> );<\n>"
241
242// Region overrides that tell various constructs to add debugging triggers
243
244//@block.predecision() ::= "<enterSubRule()><enterDecision()>"
245
246//@block.postdecision() ::= "<exitDecision()>"
247
248//@block.postbranch() ::= "<exitSubRule()>"
249
250//@ruleBlock.predecision() ::= "<enterDecision()>"
251
252//@ruleBlock.postdecision() ::= "<exitDecision()>"
253
254//@ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
255
256//@blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
257
258//@positiveClosureBlock.preloop() ::= "<enterSubRule()>"
259
260//@positiveClosureBlock.postloop() ::= "<exitSubRule()>"
261
262//@positiveClosureBlock.predecision() ::= "<enterDecision()>"
263
264//@positiveClosureBlock.postdecision() ::= "<exitDecision()>"
265
266//@positiveClosureBlock.earlyExitException() ::=
267//	"dbg.RecognitionException( eee<decisionNumber> );<\n>"
268
269//@closureBlock.preloop() ::= "<enterSubRule()>"
270
271//@closureBlock.postloop() ::= "<exitSubRule()>"
272
273//@closureBlock.predecision() ::= "<enterDecision()>"
274
275//@closureBlock.postdecision() ::= "<exitDecision()>"
276
277//@altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
278
279//@element.prematch() ::=
280//	"dbg.Location( <it.line>, <it.pos> );"
281
282//@matchSet.mismatchedSetException() ::=
283//	"dbg.RecognitionException( mse );"
284
285//@dfaState.noViableAltException() ::= "dbg.RecognitionException( nvae );"
286
287//@dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException( nvae );"
288
289//dfaDecision(decisionNumber,description) ::= <<
290//try
291//{
292//	isCyclicDecision = true;
293//	<super.dfaDecision(...)>
294//}
295//catch ( NoViableAltException nvae )
296//{
297//	dbg.RecognitionException( nvae );
298//	throw nvae;
299//}
300//>>
301
302//@cyclicDFA.errorMethod() ::= <<
303//public override void Error( NoViableAltException nvae )
304//{
305//	((DebugParser)recognizer).dbg.RecognitionException( nvae );
306//}
307//>>
308
309/** Force predicate validation to trigger an event */
310evalPredicate(pred,description) ::= <<
311EvalPredicate(<pred>, "<description>")
312>>
313