00001 /* 00002 [The "BSD license"] 00003 Copyright (c) 2005-2009 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 package org.antlr.runtime.tree; 00029 00030 import org.antlr.runtime.Token; 00031 import org.antlr.runtime.CommonToken; 00032 00033 public class TreePatternParser { 00034 protected TreePatternLexer tokenizer; 00035 protected int ttype; 00036 protected TreeWizard wizard; 00037 protected TreeAdaptor adaptor; 00038 00039 public TreePatternParser(TreePatternLexer tokenizer, TreeWizard wizard, TreeAdaptor adaptor) { 00040 this.tokenizer = tokenizer; 00041 this.wizard = wizard; 00042 this.adaptor = adaptor; 00043 ttype = tokenizer.nextToken(); // kickstart 00044 } 00045 00046 public Object pattern() { 00047 if ( ttype==TreePatternLexer.BEGIN ) { 00048 return parseTree(); 00049 } 00050 else if ( ttype==TreePatternLexer.ID ) { 00051 Object node = parseNode(); 00052 if ( ttype==TreePatternLexer.EOF ) { 00053 return node; 00054 } 00055 return null; // extra junk on end 00056 } 00057 return null; 00058 } 00059 00060 public Object parseTree() { 00061 if ( ttype != TreePatternLexer.BEGIN ) { 00062 throw new RuntimeException("no BEGIN"); 00063 } 00064 ttype = tokenizer.nextToken(); 00065 Object root = parseNode(); 00066 if ( root==null ) { 00067 return null; 00068 } 00069 while ( ttype==TreePatternLexer.BEGIN || 00070 ttype==TreePatternLexer.ID || 00071 ttype==TreePatternLexer.PERCENT || 00072 ttype==TreePatternLexer.DOT ) 00073 { 00074 if ( ttype==TreePatternLexer.BEGIN ) { 00075 Object subtree = parseTree(); 00076 adaptor.addChild(root, subtree); 00077 } 00078 else { 00079 Object child = parseNode(); 00080 if ( child==null ) { 00081 return null; 00082 } 00083 adaptor.addChild(root, child); 00084 } 00085 } 00086 if ( ttype != TreePatternLexer.END ) { 00087 throw new RuntimeException("no END"); 00088 } 00089 ttype = tokenizer.nextToken(); 00090 return root; 00091 } 00092 00093 public Object parseNode() { 00094 // "%label:" prefix 00095 String label = null; 00096 if ( ttype == TreePatternLexer.PERCENT ) { 00097 ttype = tokenizer.nextToken(); 00098 if ( ttype != TreePatternLexer.ID ) { 00099 return null; 00100 } 00101 label = tokenizer.sval.toString(); 00102 ttype = tokenizer.nextToken(); 00103 if ( ttype != TreePatternLexer.COLON ) { 00104 return null; 00105 } 00106 ttype = tokenizer.nextToken(); // move to ID following colon 00107 } 00108 00109 // Wildcard? 00110 if ( ttype == TreePatternLexer.DOT ) { 00111 ttype = tokenizer.nextToken(); 00112 Token wildcardPayload = new CommonToken(0, "."); 00113 TreeWizard.TreePattern node = 00114 new TreeWizard.WildcardTreePattern(wildcardPayload); 00115 if ( label!=null ) { 00116 node.label = label; 00117 } 00118 return node; 00119 } 00120 00121 // "ID" or "ID[arg]" 00122 if ( ttype != TreePatternLexer.ID ) { 00123 return null; 00124 } 00125 String tokenName = tokenizer.sval.toString(); 00126 ttype = tokenizer.nextToken(); 00127 if ( tokenName.equals("nil") ) { 00128 return adaptor.nil(); 00129 } 00130 String text = tokenName; 00131 // check for arg 00132 String arg = null; 00133 if ( ttype == TreePatternLexer.ARG ) { 00134 arg = tokenizer.sval.toString(); 00135 text = arg; 00136 ttype = tokenizer.nextToken(); 00137 } 00138 00139 // create node 00140 int treeNodeType = wizard.getTokenType(tokenName); 00141 if ( treeNodeType==Token.INVALID_TOKEN_TYPE ) { 00142 return null; 00143 } 00144 Object node; 00145 node = adaptor.create(treeNodeType, text); 00146 if ( label!=null && node.getClass()==TreeWizard.TreePattern.class ) { 00147 ((TreeWizard.TreePattern)node).label = label; 00148 } 00149 if ( arg!=null && node.getClass()==TreeWizard.TreePattern.class ) { 00150 ((TreeWizard.TreePattern)node).hasTextArg = true; 00151 } 00152 return node; 00153 } 00154 }
1.5.5