source: trunk/yao/share/antlr-2.7.7/lib/csharp/antlr.runtime/antlr/TreeParser.cs @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 5.2 KB
Line 
1using System;
2using AST = antlr.collections.AST;
3using BitSet = antlr.collections.impl.BitSet;
4       
5namespace antlr
6{
7        /*ANTLR Translator Generator
8        * Project led by Terence Parr at http://www.jGuru.com
9        * Software rights: http://www.antlr.org/license.html
10        *
11        * $Id:$
12        */
13
14        //
15        // ANTLR C# Code Generator by Micheal Jordan
16        //                            Kunle Odutola       : kunle UNDERSCORE odutola AT hotmail DOT com
17        //                            Anthony Oguntimehin
18        //
19        // With many thanks to Eric V. Smith from the ANTLR list.
20        //
21
22        public class TreeParser
23        {
24                /*The AST Null object; the parsing cursor is set to this when
25                *  it is found to be null.  This way, we can test the
26                *  token type of a node without having to have tests for null
27                *  everywhere.
28                */
29                public static ASTNULLType ASTNULL = new ASTNULLType();
30               
31                /*Where did this rule leave off parsing; avoids a return parameter */
32                protected internal AST retTree_;
33               
34                /*guessing nesting level; guessing==0 implies not guessing */
35                // protected int guessing = 0;
36               
37                /*Nesting level of registered handlers */
38                // protected int exceptionLevel = 0;
39               
40                protected internal TreeParserSharedInputState inputState;
41               
42                /*Table of token type to token names */
43                protected internal string[] tokenNames;
44               
45                /*AST return value for a rule is squirreled away here */
46                protected internal AST returnAST;
47               
48                /*AST support code; parser and treeparser delegate to this object */
49                protected internal ASTFactory astFactory = new ASTFactory();
50               
51                /*Used to keep track of indentdepth for traceIn/Out */
52                protected internal int traceDepth = 0;
53               
54                public TreeParser()
55                {
56                        inputState = new TreeParserSharedInputState();
57                }
58                /*Get the AST return value squirreled away in the parser */
59                public virtual AST getAST()
60                {
61                        return returnAST;
62                }
63                public virtual ASTFactory getASTFactory()
64                {
65                        return astFactory;
66                }
67                public virtual void resetState()
68                {
69                        traceDepth  = 0;
70                        returnAST       = null;
71                        retTree_        = null;
72                        inputState.reset();
73                }
74                public virtual string getTokenName(int num)
75                {
76                        return tokenNames[num];
77                }
78                public virtual string[] getTokenNames()
79                {
80                        return tokenNames;
81                }
82                protected internal virtual void  match(AST t, int ttype)
83                {
84                        //System.out.println("match("+ttype+"); cursor is "+t);
85                        if (t == null || t == ASTNULL || t.Type != ttype)
86                        {
87                                throw new MismatchedTokenException(getTokenNames(), t, ttype, false);
88                        }
89                }
90                /*Make sure current lookahead symbol matches the given set
91                * Throw an exception upon mismatch, which is catch by either the
92                * error handler or by the syntactic predicate.
93                */
94                public virtual void  match(AST t, BitSet b)
95                {
96                        if (t == null || t == ASTNULL || !b.member(t.Type))
97                        {
98                                throw new MismatchedTokenException(getTokenNames(), t, b, false);
99                        }
100                }
101                protected internal virtual void  matchNot(AST t, int ttype)
102                {
103                        //System.out.println("match("+ttype+"); cursor is "+t);
104                        if (t == null || t == ASTNULL || t.Type == ttype)
105                        {
106                                throw new MismatchedTokenException(getTokenNames(), t, ttype, true);
107                        }
108                }
109
110                /// <summary>
111                /// @deprecated as of 2.7.2. This method calls System.exit() and writes
112                /// directly to stderr, which is usually not appropriate when
113                /// a parser is embedded into a larger application. Since the method is
114                /// <code>static</code>, it cannot be overridden to avoid these problems.
115                /// ANTLR no longer uses this method internally or in generated code.
116                /// </summary>
117                ///
118                [Obsolete("De-activated since version 2.7.2.6 as it cannot be overidden.", true)]
119                public static void panic()
120                {
121                        Console.Error.WriteLine("TreeWalker: panic");
122                        System.Environment.Exit(1);
123                }
124                /*Parser error-reporting function can be overridden in subclass */
125                public virtual void  reportError(RecognitionException ex)
126                {
127                        Console.Error.WriteLine(ex.ToString());
128                }
129                /*Parser error-reporting function can be overridden in subclass */
130                public virtual void  reportError(string s)
131                {
132                        Console.Error.WriteLine("error: " + s);
133                }
134                /*Parser warning-reporting function can be overridden in subclass */
135                public virtual void  reportWarning(string s)
136                {
137                        Console.Error.WriteLine("warning: " + s);
138                }
139                /*Specify an object with support code (shared by
140                *  Parser and TreeParser.  Normally, the programmer
141                *  does not play with this, using setASTNodeType instead.
142                */
143                public virtual void  setASTFactory(ASTFactory f)
144                {
145                        astFactory = f;
146                }
147               
148                /*Specify the type of node to create during tree building */
149                public virtual void  setASTNodeType(string nodeType)
150                {
151                        setASTNodeClass(nodeType);
152                }
153               
154                /*Specify the type of node to create during tree building */
155                public virtual void  setASTNodeClass(string nodeType)
156                {
157                        astFactory.setASTNodeType(nodeType);
158                }
159               
160                public virtual void  traceIndent()
161                {
162                         for (int i = 0; i < traceDepth; i++)
163                                Console.Out.Write(" ");
164                }
165                public virtual void  traceIn(string rname, AST t)
166                {
167                        traceDepth += 1;
168                        traceIndent();
169                        Console.Out.WriteLine("> " + rname + "(" + ((t != null) ? t.ToString() : "null") + ")" + ((inputState.guessing > 0) ? " [guessing]" : ""));
170                }
171                public virtual void  traceOut(string rname, AST t)
172                {
173                        traceIndent();
174                        Console.Out.WriteLine("< " + rname + "(" + ((t != null) ? t.ToString() : "null") + ")" + ((inputState.guessing > 0) ? " [guessing]" : ""));
175                        traceDepth--;
176                }
177        }
178}
Note: See TracBrowser for help on using the repository browser.