PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeWizard.java

https://bitbucket.org/jwalton/antlr3
Java | 537 lines | 256 code | 34 blank | 247 comment | 39 complexity | 414c41ddf9f3ae308b214f17d9928bec MD5 | raw file
  1. /*
  2. [The "BSD license"]
  3. Copyright (c) 2005-2009 Terence Parr
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package org.antlr.runtime.tree;
  27. import org.antlr.runtime.Token;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. /** Build and navigate trees with this object. Must know about the names
  33. * of tokens so you have to pass in a map or array of token names (from which
  34. * this class can build the map). I.e., Token DECL means nothing unless the
  35. * class can translate it to a token type.
  36. *
  37. * In order to create nodes and navigate, this class needs a TreeAdaptor.
  38. *
  39. * This class can build a token type -> node index for repeated use or for
  40. * iterating over the various nodes with a particular type.
  41. *
  42. * This class works in conjunction with the TreeAdaptor rather than moving
  43. * all this functionality into the adaptor. An adaptor helps build and
  44. * navigate trees using methods. This class helps you do it with string
  45. * patterns like "(A B C)". You can create a tree from that pattern or
  46. * match subtrees against it.
  47. */
  48. public class TreeWizard {
  49. protected TreeAdaptor adaptor;
  50. protected Map<String, Integer> tokenNameToTypeMap;
  51. public interface ContextVisitor {
  52. // TODO: should this be called visit or something else?
  53. public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels);
  54. }
  55. public static abstract class Visitor implements ContextVisitor {
  56. @Override
  57. public void visit(Object t, Object parent, int childIndex, Map<String, Object> labels) {
  58. visit(t);
  59. }
  60. public abstract void visit(Object t);
  61. }
  62. /** When using %label:TOKENNAME in a tree for parse(), we must
  63. * track the label.
  64. */
  65. public static class TreePattern extends CommonTree {
  66. public String label;
  67. public boolean hasTextArg;
  68. public TreePattern(Token payload) {
  69. super(payload);
  70. }
  71. @Override
  72. public String toString() {
  73. if ( label!=null ) {
  74. return "%"+label+":"+super.toString();
  75. }
  76. else {
  77. return super.toString();
  78. }
  79. }
  80. }
  81. public static class WildcardTreePattern extends TreePattern {
  82. public WildcardTreePattern(Token payload) {
  83. super(payload);
  84. }
  85. }
  86. /** This adaptor creates TreePattern objects for use during scan() */
  87. public static class TreePatternTreeAdaptor extends CommonTreeAdaptor {
  88. @Override
  89. public Object create(Token payload) {
  90. return new TreePattern(payload);
  91. }
  92. }
  93. // TODO: build indexes for the wizard
  94. /** During fillBuffer(), we can make a reverse index from a set
  95. * of token types of interest to the list of indexes into the
  96. * node stream. This lets us convert a node pointer to a
  97. * stream index semi-efficiently for a list of interesting
  98. * nodes such as function definition nodes (you'll want to seek
  99. * to their bodies for an interpreter). Also useful for doing
  100. * dynamic searches; i.e., go find me all PLUS nodes.
  101. protected Map tokenTypeToStreamIndexesMap;
  102. /** If tokenTypesToReverseIndex set to INDEX_ALL then indexing
  103. * occurs for all token types.
  104. public static final Set INDEX_ALL = new HashSet();
  105. /** A set of token types user would like to index for faster lookup.
  106. * If this is INDEX_ALL, then all token types are tracked. If null,
  107. * then none are indexed.
  108. protected Set tokenTypesToReverseIndex = null;
  109. */
  110. public TreeWizard(TreeAdaptor adaptor) {
  111. this.adaptor = adaptor;
  112. }
  113. public TreeWizard(TreeAdaptor adaptor, Map<String, Integer> tokenNameToTypeMap) {
  114. this.adaptor = adaptor;
  115. this.tokenNameToTypeMap = tokenNameToTypeMap;
  116. }
  117. public TreeWizard(TreeAdaptor adaptor, String[] tokenNames) {
  118. this.adaptor = adaptor;
  119. this.tokenNameToTypeMap = computeTokenTypes(tokenNames);
  120. }
  121. public TreeWizard(String[] tokenNames) {
  122. this(new CommonTreeAdaptor(), tokenNames);
  123. }
  124. /** Compute a Map<String, Integer> that is an inverted index of
  125. * tokenNames (which maps int token types to names).
  126. */
  127. public Map<String, Integer> computeTokenTypes(String[] tokenNames) {
  128. Map<String, Integer> m = new HashMap<String, Integer>();
  129. if ( tokenNames==null ) {
  130. return m;
  131. }
  132. for (int ttype = Token.MIN_TOKEN_TYPE; ttype < tokenNames.length; ttype++) {
  133. String name = tokenNames[ttype];
  134. m.put(name, ttype);
  135. }
  136. return m;
  137. }
  138. /** Using the map of token names to token types, return the type. */
  139. public int getTokenType(String tokenName) {
  140. if ( tokenNameToTypeMap==null ) {
  141. return Token.INVALID_TOKEN_TYPE;
  142. }
  143. Integer ttypeI = tokenNameToTypeMap.get(tokenName);
  144. if ( ttypeI!=null ) {
  145. return ttypeI;
  146. }
  147. return Token.INVALID_TOKEN_TYPE;
  148. }
  149. /** Walk the entire tree and make a node name to nodes mapping.
  150. * For now, use recursion but later nonrecursive version may be
  151. * more efficient. Returns Map<Integer, List> where the List is
  152. * of your AST node type. The Integer is the token type of the node.
  153. *
  154. * TODO: save this index so that find and visit are faster
  155. */
  156. public Map<Integer, List<Object>> index(Object t) {
  157. Map<Integer, List<Object>> m = new HashMap<Integer, List<Object>>();
  158. _index(t, m);
  159. return m;
  160. }
  161. /** Do the work for index */
  162. protected void _index(Object t, Map<Integer, List<Object>> m) {
  163. if ( t==null ) {
  164. return;
  165. }
  166. int ttype = adaptor.getType(t);
  167. List<Object> elements = m.get(ttype);
  168. if ( elements==null ) {
  169. elements = new ArrayList<Object>();
  170. m.put(ttype, elements);
  171. }
  172. elements.add(t);
  173. int n = adaptor.getChildCount(t);
  174. for (int i=0; i<n; i++) {
  175. Object child = adaptor.getChild(t, i);
  176. _index(child, m);
  177. }
  178. }
  179. /** Return a List of tree nodes with token type ttype */
  180. public List<? extends Object> find(Object t, int ttype) {
  181. final List<Object> nodes = new ArrayList<Object>();
  182. visit(t, ttype, new TreeWizard.Visitor() {
  183. @Override
  184. public void visit(Object t) {
  185. nodes.add(t);
  186. }
  187. });
  188. return nodes;
  189. }
  190. /** Return a List of subtrees matching pattern. */
  191. public List<? extends Object> find(Object t, String pattern) {
  192. final List<Object> subtrees = new ArrayList<Object>();
  193. // Create a TreePattern from the pattern
  194. TreePatternLexer tokenizer = new TreePatternLexer(pattern);
  195. TreePatternParser parser =
  196. new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor());
  197. final TreePattern tpattern = (TreePattern)parser.pattern();
  198. // don't allow invalid patterns
  199. if ( tpattern==null ||
  200. tpattern.isNil() ||
  201. tpattern.getClass()==WildcardTreePattern.class )
  202. {
  203. return null;
  204. }
  205. int rootTokenType = tpattern.getType();
  206. visit(t, rootTokenType, new TreeWizard.ContextVisitor() {
  207. @Override
  208. public void visit(Object t, Object parent, int childIndex, Map labels) {
  209. if ( _parse(t, tpattern, null) ) {
  210. subtrees.add(t);
  211. }
  212. }
  213. });
  214. return subtrees;
  215. }
  216. public Object findFirst(Object t, int ttype) {
  217. return null;
  218. }
  219. public Object findFirst(Object t, String pattern) {
  220. return null;
  221. }
  222. /** Visit every ttype node in t, invoking the visitor. This is a quicker
  223. * version of the general visit(t, pattern) method. The labels arg
  224. * of the visitor action method is never set (it's null) since using
  225. * a token type rather than a pattern doesn't let us set a label.
  226. */
  227. public void visit(Object t, int ttype, ContextVisitor visitor) {
  228. _visit(t, null, 0, ttype, visitor);
  229. }
  230. /** Do the recursive work for visit */
  231. protected void _visit(Object t, Object parent, int childIndex, int ttype, ContextVisitor visitor) {
  232. if ( t==null ) {
  233. return;
  234. }
  235. if ( adaptor.getType(t)==ttype ) {
  236. visitor.visit(t, parent, childIndex, null);
  237. }
  238. int n = adaptor.getChildCount(t);
  239. for (int i=0; i<n; i++) {
  240. Object child = adaptor.getChild(t, i);
  241. _visit(child, t, i, ttype, visitor);
  242. }
  243. }
  244. /** For all subtrees that match the pattern, execute the visit action.
  245. * The implementation uses the root node of the pattern in combination
  246. * with visit(t, ttype, visitor) so nil-rooted patterns are not allowed.
  247. * Patterns with wildcard roots are also not allowed.
  248. */
  249. public void visit(Object t, final String pattern, final ContextVisitor visitor) {
  250. // Create a TreePattern from the pattern
  251. TreePatternLexer tokenizer = new TreePatternLexer(pattern);
  252. TreePatternParser parser =
  253. new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor());
  254. final TreePattern tpattern = (TreePattern)parser.pattern();
  255. // don't allow invalid patterns
  256. if ( tpattern==null ||
  257. tpattern.isNil() ||
  258. tpattern.getClass()==WildcardTreePattern.class )
  259. {
  260. return;
  261. }
  262. final Map<String, Object> labels = new HashMap<String, Object>(); // reused for each _parse
  263. int rootTokenType = tpattern.getType();
  264. visit(t, rootTokenType, new TreeWizard.ContextVisitor() {
  265. @Override
  266. public void visit(Object t, Object parent, int childIndex, Map<String, Object> unusedlabels) {
  267. // the unusedlabels arg is null as visit on token type doesn't set.
  268. labels.clear();
  269. if ( _parse(t, tpattern, labels) ) {
  270. visitor.visit(t, parent, childIndex, labels);
  271. }
  272. }
  273. });
  274. }
  275. /** Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels
  276. * on the various nodes and '.' (dot) as the node/subtree wildcard,
  277. * return true if the pattern matches and fill the labels Map with
  278. * the labels pointing at the appropriate nodes. Return false if
  279. * the pattern is malformed or the tree does not match.
  280. *
  281. * If a node specifies a text arg in pattern, then that must match
  282. * for that node in t.
  283. *
  284. * TODO: what's a better way to indicate bad pattern? Exceptions are a hassle
  285. */
  286. public boolean parse(Object t, String pattern, Map<String, Object> labels) {
  287. TreePatternLexer tokenizer = new TreePatternLexer(pattern);
  288. TreePatternParser parser =
  289. new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor());
  290. TreePattern tpattern = (TreePattern)parser.pattern();
  291. /*
  292. System.out.println("t="+((Tree)t).toStringTree());
  293. System.out.println("scant="+tpattern.toStringTree());
  294. */
  295. boolean matched = _parse(t, tpattern, labels);
  296. return matched;
  297. }
  298. public boolean parse(Object t, String pattern) {
  299. return parse(t, pattern, null);
  300. }
  301. /** Do the work for parse. Check to see if the t2 pattern fits the
  302. * structure and token types in t1. Check text if the pattern has
  303. * text arguments on nodes. Fill labels map with pointers to nodes
  304. * in tree matched against nodes in pattern with labels.
  305. */
  306. protected boolean _parse(Object t1, TreePattern tpattern, Map<String, Object> labels) {
  307. // make sure both are non-null
  308. if ( t1==null || tpattern==null ) {
  309. return false;
  310. }
  311. // check roots (wildcard matches anything)
  312. if ( tpattern.getClass() != WildcardTreePattern.class ) {
  313. if ( adaptor.getType(t1) != tpattern.getType() ) return false;
  314. // if pattern has text, check node text
  315. if ( tpattern.hasTextArg && !adaptor.getText(t1).equals(tpattern.getText()) ) {
  316. return false;
  317. }
  318. }
  319. if ( tpattern.label!=null && labels!=null ) {
  320. // map label in pattern to node in t1
  321. labels.put(tpattern.label, t1);
  322. }
  323. // check children
  324. int n1 = adaptor.getChildCount(t1);
  325. int n2 = tpattern.getChildCount();
  326. if ( n1 != n2 ) {
  327. return false;
  328. }
  329. for (int i=0; i<n1; i++) {
  330. Object child1 = adaptor.getChild(t1, i);
  331. TreePattern child2 = (TreePattern)tpattern.getChild(i);
  332. if ( !_parse(child1, child2, labels) ) {
  333. return false;
  334. }
  335. }
  336. return true;
  337. }
  338. /** Create a tree or node from the indicated tree pattern that closely
  339. * follows ANTLR tree grammar tree element syntax:
  340. *
  341. * (root child1 ... child2).
  342. *
  343. * You can also just pass in a node: ID
  344. *
  345. * Any node can have a text argument: ID[foo]
  346. * (notice there are no quotes around foo--it's clear it's a string).
  347. *
  348. * nil is a special name meaning "give me a nil node". Useful for
  349. * making lists: (nil A B C) is a list of A B C.
  350. */
  351. public Object create(String pattern) {
  352. TreePatternLexer tokenizer = new TreePatternLexer(pattern);
  353. TreePatternParser parser = new TreePatternParser(tokenizer, this, adaptor);
  354. Object t = parser.pattern();
  355. return t;
  356. }
  357. /** Compare t1 and t2; return true if token types/text, structure match exactly.
  358. * The trees are examined in their entirety so that (A B) does not match
  359. * (A B C) nor (A (B C)).
  360. // TODO: allow them to pass in a comparator
  361. * TODO: have a version that is nonstatic so it can use instance adaptor
  362. *
  363. * I cannot rely on the tree node's equals() implementation as I make
  364. * no constraints at all on the node types nor interface etc...
  365. */
  366. public static boolean equals(Object t1, Object t2, TreeAdaptor adaptor) {
  367. return _equals(t1, t2, adaptor);
  368. }
  369. /** Compare type, structure, and text of two trees, assuming adaptor in
  370. * this instance of a TreeWizard.
  371. */
  372. public boolean equals(Object t1, Object t2) {
  373. return _equals(t1, t2, adaptor);
  374. }
  375. protected static boolean _equals(Object t1, Object t2, TreeAdaptor adaptor) {
  376. // make sure both are non-null
  377. if ( t1==null || t2==null ) {
  378. return false;
  379. }
  380. // check roots
  381. if ( adaptor.getType(t1) != adaptor.getType(t2) ) {
  382. return false;
  383. }
  384. if ( !adaptor.getText(t1).equals(adaptor.getText(t2)) ) {
  385. return false;
  386. }
  387. // check children
  388. int n1 = adaptor.getChildCount(t1);
  389. int n2 = adaptor.getChildCount(t2);
  390. if ( n1 != n2 ) {
  391. return false;
  392. }
  393. for (int i=0; i<n1; i++) {
  394. Object child1 = adaptor.getChild(t1, i);
  395. Object child2 = adaptor.getChild(t2, i);
  396. if ( !_equals(child1, child2, adaptor) ) {
  397. return false;
  398. }
  399. }
  400. return true;
  401. }
  402. // TODO: next stuff taken from CommonTreeNodeStream
  403. /** Given a node, add this to the reverse index tokenTypeToStreamIndexesMap.
  404. * You can override this method to alter how indexing occurs. The
  405. * default is to create a
  406. *
  407. * Map<Integer token type,ArrayList<Integer stream index>>
  408. *
  409. * This data structure allows you to find all nodes with type INT in order.
  410. *
  411. * If you really need to find a node of type, say, FUNC quickly then perhaps
  412. *
  413. * Map<Integertoken type,Map<Object tree node,Integer stream index>>
  414. *
  415. * would be better for you. The interior maps map a tree node to
  416. * the index so you don't have to search linearly for a specific node.
  417. *
  418. * If you change this method, you will likely need to change
  419. * getNodeIndex(), which extracts information.
  420. protected void fillReverseIndex(Object node, int streamIndex) {
  421. //System.out.println("revIndex "+node+"@"+streamIndex);
  422. if ( tokenTypesToReverseIndex==null ) {
  423. return; // no indexing if this is empty (nothing of interest)
  424. }
  425. if ( tokenTypeToStreamIndexesMap==null ) {
  426. tokenTypeToStreamIndexesMap = new HashMap(); // first indexing op
  427. }
  428. int tokenType = adaptor.getType(node);
  429. Integer tokenTypeI = new Integer(tokenType);
  430. if ( !(tokenTypesToReverseIndex==INDEX_ALL ||
  431. tokenTypesToReverseIndex.contains(tokenTypeI)) )
  432. {
  433. return; // tokenType not of interest
  434. }
  435. Integer streamIndexI = new Integer(streamIndex);
  436. ArrayList indexes = (ArrayList)tokenTypeToStreamIndexesMap.get(tokenTypeI);
  437. if ( indexes==null ) {
  438. indexes = new ArrayList(); // no list yet for this token type
  439. indexes.add(streamIndexI); // not there yet, add
  440. tokenTypeToStreamIndexesMap.put(tokenTypeI, indexes);
  441. }
  442. else {
  443. if ( !indexes.contains(streamIndexI) ) {
  444. indexes.add(streamIndexI); // not there yet, add
  445. }
  446. }
  447. }
  448. /** Track the indicated token type in the reverse index. Call this
  449. * repeatedly for each type or use variant with Set argument to
  450. * set all at once.
  451. * @param tokenType
  452. public void reverseIndex(int tokenType) {
  453. if ( tokenTypesToReverseIndex==null ) {
  454. tokenTypesToReverseIndex = new HashSet();
  455. }
  456. else if ( tokenTypesToReverseIndex==INDEX_ALL ) {
  457. return;
  458. }
  459. tokenTypesToReverseIndex.add(new Integer(tokenType));
  460. }
  461. /** Track the indicated token types in the reverse index. Set
  462. * to INDEX_ALL to track all token types.
  463. public void reverseIndex(Set tokenTypes) {
  464. tokenTypesToReverseIndex = tokenTypes;
  465. }
  466. /** Given a node pointer, return its index into the node stream.
  467. * This is not its Token stream index. If there is no reverse map
  468. * from node to stream index or the map does not contain entries
  469. * for node's token type, a linear search of entire stream is used.
  470. *
  471. * Return -1 if exact node pointer not in stream.
  472. public int getNodeIndex(Object node) {
  473. //System.out.println("get "+node);
  474. if ( tokenTypeToStreamIndexesMap==null ) {
  475. return getNodeIndexLinearly(node);
  476. }
  477. int tokenType = adaptor.getType(node);
  478. Integer tokenTypeI = new Integer(tokenType);
  479. ArrayList indexes = (ArrayList)tokenTypeToStreamIndexesMap.get(tokenTypeI);
  480. if ( indexes==null ) {
  481. //System.out.println("found linearly; stream index = "+getNodeIndexLinearly(node));
  482. return getNodeIndexLinearly(node);
  483. }
  484. for (int i = 0; i < indexes.size(); i++) {
  485. Integer streamIndexI = (Integer)indexes.get(i);
  486. Object n = get(streamIndexI.intValue());
  487. if ( n==node ) {
  488. //System.out.println("found in index; stream index = "+streamIndexI);
  489. return streamIndexI.intValue(); // found it!
  490. }
  491. }
  492. return -1;
  493. }
  494. */
  495. }