1 18 19 package org.codehaus.groovy.antlr; 20 21 28 29 import antlr.collections.AST; 30 import java.util.*; 31 32 public class AntlrASTProcessSnippets implements AntlrASTProcessor{ 33 private SourceBuffer sourceBuffer; 34 35 public AntlrASTProcessSnippets(SourceBuffer sourceBuffer) { 36 this.sourceBuffer = sourceBuffer; 37 } 38 39 45 public AST process(AST t) { 46 List l = new ArrayList(); 48 t = traverse((GroovySourceAST)t,l,null); 49 50 Iterator itr = l.iterator(); 53 if (itr.hasNext()) { itr.next(); } 54 t = traverse((GroovySourceAST)t,null,itr); 55 return t; 56 } 57 58 65 private AST traverse(GroovySourceAST t,List l,Iterator itr) { 66 if (t == null) { return t; } 67 68 if (l != null) { 70 l.add(new LineColumn(t.getLine(),t.getColumn())); 71 } 72 73 if (itr != null && itr.hasNext()) { 75 LineColumn lc = (LineColumn)itr.next(); 76 if (t.getLineLast() == 0) { 77 int nextLine = lc.getLine(); 78 int nextColumn = lc.getColumn(); 79 if (nextLine < t.getLine() || (nextLine == t.getLine() && nextColumn < t.getColumn())) { 80 nextLine = t.getLine(); 81 nextColumn = t.getColumn(); 82 } 83 t.setLineLast(nextLine); 84 t.setColumnLast(nextColumn); 85 t.setSnippet(sourceBuffer.getSnippet(new LineColumn(t.getLine(),t.getColumn()), 86 new LineColumn(t.getLineLast(),t.getColumnLast()))); 87 } 88 } 89 90 GroovySourceAST child = (GroovySourceAST)t.getFirstChild(); 91 if (child != null) { 92 traverse(child,l,itr); 93 } 94 95 GroovySourceAST sibling = (GroovySourceAST)t.getNextSibling(); 96 if (sibling != null) { 97 traverse(sibling,l,itr); 98 } 99 100 return t; 101 } 102 } 103 | Popular Tags |