00001 /* 00002 [The "BSD licence"] 00003 Copyright (c) 2005-2008 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 System.out.println("no BEGIN"); 00063 return null; 00064 } 00065 ttype = tokenizer.nextToken(); 00066 Object root = parseNode(); 00067 if ( root==null ) { 00068 return null; 00069 } 00070 while ( ttype==TreePatternLexer.BEGIN || 00071 ttype==TreePatternLexer.ID || 00072 ttype==TreePatternLexer.PERCENT || 00073 ttype==TreePatternLexer.DOT ) 00074 { 00075 if ( ttype==TreePatternLexer.BEGIN ) { 00076 Object subtree = parseTree(); 00077 adaptor.addChild(root, subtree); 00078 } 00079 else { 00080 Object child = parseNode(); 00081 if ( child==null ) { 00082 return null; 00083 } 00084 adaptor.addChild(root, child); 00085 } 00086 } 00087 if ( ttype != TreePatternLexer.END ) { 00088 System.out.println("no END"); 00089 return null; 00090 } 00091 ttype = tokenizer.nextToken(); 00092 return root; 00093 } 00094 00095 public Object parseNode() { 00096 // "%label:" prefix 00097 String label = null; 00098 if ( ttype == TreePatternLexer.PERCENT ) { 00099 ttype = tokenizer.nextToken(); 00100 if ( ttype != TreePatternLexer.ID ) { 00101 return null; 00102 } 00103 label = tokenizer.sval.toString(); 00104 ttype = tokenizer.nextToken(); 00105 if ( ttype != TreePatternLexer.COLON ) { 00106 return null; 00107 } 00108 ttype = tokenizer.nextToken(); // move to ID following colon 00109 } 00110 00111 // Wildcard? 00112 if ( ttype == TreePatternLexer.DOT ) { 00113 ttype = tokenizer.nextToken(); 00114 Token wildcardPayload = new CommonToken(0, "."); 00115 TreeWizard.TreePattern node = 00116 new TreeWizard.WildcardTreePattern(wildcardPayload); 00117 if ( label!=null ) { 00118 node.label = label; 00119 } 00120 return node; 00121 } 00122 00123 // "ID" or "ID[arg]" 00124 if ( ttype != TreePatternLexer.ID ) { 00125 return null; 00126 } 00127 String tokenName = tokenizer.sval.toString(); 00128 ttype = tokenizer.nextToken(); 00129 if ( tokenName.equals("nil") ) { 00130 return adaptor.nil(); 00131 } 00132 String text = tokenName; 00133 // check for arg 00134 String arg = null; 00135 if ( ttype == TreePatternLexer.ARG ) { 00136 arg = tokenizer.sval.toString(); 00137 text = arg; 00138 ttype = tokenizer.nextToken(); 00139 } 00140 00141 // create node 00142 int treeNodeType = wizard.getTokenType(tokenName); 00143 if ( treeNodeType==Token.INVALID_TOKEN_TYPE ) { 00144 return null; 00145 } 00146 Object node; 00147 node = adaptor.create(treeNodeType, text); 00148 if ( label!=null && node.getClass()==TreeWizard.TreePattern.class ) { 00149 ((TreeWizard.TreePattern)node).label = label; 00150 } 00151 if ( arg!=null && node.getClass()==TreeWizard.TreePattern.class ) { 00152 ((TreeWizard.TreePattern)node).hasTextArg = true; 00153 } 00154 return node; 00155 } 00156 }
1.5.5