Parser.as revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 [The "BSD licence"]
3 Copyright (c) 2005-2006 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15    derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28package org.antlr.runtime {
29    import org.antlr.runtime.tree.TreeAdaptor;
30
31	/** A parser for TokenStreams.  "parser grammars" result in a subclass
32	 *  of this.
33	 */
34	public class Parser extends BaseRecognizer {
35	    protected var input:TokenStream;
36
37		public function Parser(input:TokenStream, state:RecognizerSharedState = null) {
38			super(state);
39			tokenStream = input;
40	    }
41
42		public override function reset():void {
43			super.reset(); // reset all recognizer state variables
44			if ( input!=null ) {
45				input.seek(0); // rewind the input
46			}
47		}
48
49		protected override function getCurrentInputSymbol(input:IntStream):Object {
50			return TokenStream(input).LT(1);
51		}
52
53		protected override function getMissingSymbol(input:IntStream,
54										    e:RecognitionException,
55										    expectedTokenType:int,
56										    follow:BitSet):Object {
57		    var tokenText:String = null;
58            if ( expectedTokenType==TokenConstants.EOF ) tokenText = "<missing EOF>";
59            else tokenText = "<missing "+tokenNames[expectedTokenType]+">";
60			var t:CommonToken = new CommonToken(expectedTokenType, tokenText);
61			var current:Token = TokenStream(input).LT(1);
62			if ( current.type == TokenConstants.EOF ) {
63				current = TokenStream(input).LT(-1);
64			}
65			t.line = current.line;
66			t.charPositionInLine = current.charPositionInLine;
67			t.channel = DEFAULT_TOKEN_CHANNEL;
68			return t;
69		}
70
71		/** Set the token stream and reset the parser */
72		public function set tokenStream(input:TokenStream):void {
73			this.input = null;
74			reset();
75			this.input = input;
76		}
77
78	    public function get tokenStream():TokenStream {
79			return input;
80		}
81
82		public override function get sourceName():String {
83			return input.sourceName;
84		}
85
86		public function set treeAdaptor(adaptor:TreeAdaptor):void {
87		    // do nothing, implemented in generated code
88		}
89
90		public function get treeAdaptor():TreeAdaptor {
91		    // implementation provided in generated code
92		    return null;
93		}
94
95		public function traceIn(ruleName:String, ruleIndex:int):void  {
96			super.traceInSymbol(ruleName, ruleIndex, input.LT(1));
97		}
98
99		public function traceOut(ruleName:String, ruleIndex:int):void {
100			super.traceOutSymbol(ruleName, ruleIndex, input.LT(1));
101		}
102	}
103
104}