1/* 2 [The "BSD licence"] 3 Copyright (c) 2005-2007 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.tree { 29 import org.antlr.runtime.*; 30 31 /** A parser for a stream of tree nodes. "tree grammars" result in a subclass 32 * of this. All the error reporting and recovery is shared with Parser via 33 * the BaseRecognizer superclass. 34 */ 35 public class TreeParser extends BaseRecognizer { 36 public static const DOWN:int = TokenConstants.DOWN; 37 public static const UP:int = TokenConstants.UP; 38 39 protected var input:TreeNodeStream; 40 41 public function TreeParser(input:TreeNodeStream, state:RecognizerSharedState = null) { 42 super(state); 43 treeNodeStream = input; 44 } 45 46 public override function reset():void { 47 super.reset(); // reset all recognizer state variables 48 if ( input!=null ) { 49 input.seek(0); // rewind the input 50 } 51 } 52 53 /** Set the input stream */ 54 public function set treeNodeStream(input:TreeNodeStream):void { 55 this.input = input; 56 } 57 58 public function get treeNodeStream():TreeNodeStream { 59 return input; 60 } 61 62 public override function get sourceName():String { 63 return input.sourceName; 64 } 65 66 protected override function getCurrentInputSymbol(input:IntStream):Object { 67 return TreeNodeStream(input).LT(1); 68 } 69 70 protected override function getMissingSymbol(input:IntStream, 71 e:RecognitionException, 72 expectedTokenType:int, 73 follow:BitSet):Object { 74 var tokenText:String = 75 "<missing "+tokenNames[expectedTokenType]+">"; 76 return CommonTree.createFromToken(new CommonToken(expectedTokenType, tokenText)); 77 } 78 79 /** Match '.' in tree parser has special meaning. Skip node or 80 * entire tree if node has children. If children, scan until 81 * corresponding UP node. 82 */ 83 public function matchAny(ignore:IntStream):void { // ignore stream, copy of this.input 84 state.errorRecovery = false; 85 state.failed = false; 86 var look:Object = input.LT(1); 87 if ( input.treeAdaptor.getChildCount(look)==0 ) { 88 input.consume(); // not subtree, consume 1 node and return 89 return; 90 } 91 // current node is a subtree, skip to corresponding UP. 92 // must count nesting level to get right UP 93 var level:int=0; 94 var tokenType:int = input.treeAdaptor.getType(look); 95 while ( tokenType!=TokenConstants.EOF && !(tokenType==UP && level==0) ) { 96 input.consume(); 97 look = input.LT(1); 98 tokenType = input.treeAdaptor.getType(look); 99 if ( tokenType == DOWN ) { 100 level++; 101 } 102 else if ( tokenType == UP ) { 103 level--; 104 } 105 } 106 input.consume(); // consume UP 107 } 108 109 /** We have DOWN/UP nodes in the stream that have no line info; override. 110 * plus we want to alter the exception type. Don't try to recover 111 * from tree parser errors inline... 112 */ 113 protected override function mismatch(input:IntStream, ttype:int, follow:BitSet):void { 114 throw new MismatchedTreeNodeException(ttype, TreeNodeStream(input)); 115 } 116 117 /** Prefix error message with the grammar name because message is 118 * always intended for the programmer because the parser built 119 * the input tree not the user. 120 */ 121 public override function getErrorHeader(e:RecognitionException):String { 122 return grammarFileName+": node from "+ 123 (e.approximateLineInfo?"after ":"")+"line "+e.line+":"+e.charPositionInLine; 124 } 125 126 /** Tree parsers parse nodes they usually have a token object as 127 * payload. Set the exception token and do the default behavior. 128 */ 129 public override function getErrorMessage(e:RecognitionException, tokenNames:Array):String { 130 if ( this is TreeParser ) { 131 var adaptor:TreeAdaptor = TreeNodeStream(e.input).treeAdaptor; 132 e.token = adaptor.getToken(e.node); 133 if ( e.token==null ) { // could be an UP/DOWN node 134 e.token = new CommonToken(adaptor.getType(e.node), 135 adaptor.getText(e.node)); 136 } 137 } 138 return super.getErrorMessage(e, tokenNames); 139 } 140 141 public function set treeAdaptor(adaptor:TreeAdaptor):void { 142 // do nothing, implemented in generated code 143 } 144 145 public function get treeAdaptor():TreeAdaptor { 146 // implementation provided in generated code 147 return null; 148 } 149 150 public function traceIn(ruleName:String, ruleIndex:int):void { 151 super.traceInSymbol(ruleName, ruleIndex, input.LT(1)); 152 } 153 154 public function traceOut(ruleName:String, ruleIndex:int):void { 155 super.traceOutSymbol(ruleName, ruleIndex, input.LT(1)); 156 } 157 } 158 159}