1/* 2 [The "BSD license"] 3 Copyright (c) 2005-2009 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 30import org.antlr.runtime.debug.DebugTokenStream; 31 32/** A parser for TokenStreams. "parser grammars" result in a subclass 33 * of this. 34 */ 35public class Parser extends BaseRecognizer { 36 public TokenStream input; 37 38 public Parser(TokenStream input) { 39 super(); // highlight that we go to super to set state object 40 setTokenStream(input); 41 } 42 43 public Parser(TokenStream input, RecognizerSharedState state) { 44 super(state); // share the state object with another parser 45 this.input = input; 46 } 47 48 public void reset() { 49 super.reset(); // reset all recognizer state variables 50 if ( input!=null ) { 51 input.seek(0); // rewind the input 52 } 53 } 54 55 protected Object getCurrentInputSymbol(IntStream input) { 56 return ((TokenStream)input).LT(1); 57 } 58 59 protected Object getMissingSymbol(IntStream input, 60 RecognitionException e, 61 int expectedTokenType, 62 BitSet follow) 63 { 64 String tokenText = null; 65 if ( expectedTokenType==Token.EOF ) tokenText = "<missing EOF>"; 66 else tokenText = "<missing "+getTokenNames()[expectedTokenType]+">"; 67 CommonToken t = new CommonToken(expectedTokenType, tokenText); 68 Token current = ((TokenStream)input).LT(1); 69 if ( current.getType() == Token.EOF ) { 70 current = ((TokenStream)input).LT(-1); 71 } 72 t.line = current.getLine(); 73 t.charPositionInLine = current.getCharPositionInLine(); 74 t.channel = DEFAULT_TOKEN_CHANNEL; 75 t.input = current.getInputStream(); 76 return t; 77 } 78 79 /** Set the token stream and reset the parser */ 80 public void setTokenStream(TokenStream input) { 81 this.input = null; 82 reset(); 83 this.input = input; 84 } 85 86 public TokenStream getTokenStream() { 87 return input; 88 } 89 90 public String getSourceName() { 91 return input.getSourceName(); 92 } 93 94 public void traceIn(String ruleName, int ruleIndex) { 95 super.traceIn(ruleName, ruleIndex, input.LT(1)); 96 } 97 98 public void traceOut(String ruleName, int ruleIndex) { 99 super.traceOut(ruleName, ruleIndex, input.LT(1)); 100 } 101} 102