]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ltk.antlr/src/org/simantics/ltk/antlr/ANTLRUtils.java
Removed extra debug/timing prints from graph compiler
[simantics/platform.git] / bundles / org.simantics.ltk.antlr / src / org / simantics / ltk / antlr / ANTLRUtils.java
1 package org.simantics.ltk.antlr;
2
3 import java.io.IOException;
4
5 import org.antlr.runtime.ANTLRInputStream;
6 import org.antlr.runtime.CharStream;
7 import org.antlr.runtime.CommonToken;
8 import org.antlr.runtime.CommonTokenStream;
9 import org.antlr.runtime.Lexer;
10 import org.antlr.runtime.Parser;
11 import org.antlr.runtime.RecognitionException;
12 import org.antlr.runtime.Token;
13 import org.antlr.runtime.tree.CommonTree;
14 import org.antlr.runtime.tree.Tree;
15 import org.simantics.ltk.ISource;
16 import org.simantics.ltk.Location;
17 import org.simantics.ltk.Problem;
18
19 public class ANTLRUtils {
20
21         public static void setUpParser(ISource source, Lexer lexer, Parser parser) throws IOException {
22                 CharStream stream = new ANTLRInputStream(source.open());
23                 lexer.setCharStream(stream);
24                 parser.setTokenStream(new CommonTokenStream(lexer));
25         }
26         
27         public static Location location(ISource source, Tree tree) {
28                 return new Location(source, 
29                                 getStartLine(tree), 
30                                 getStartIndex(tree), 
31                                 getStopIndex(tree)+1
32                         );
33         }
34         
35         public static int getStartLine(Tree tree) {
36                 Token payload = ((CommonTree)tree).token;
37                 int startLine = payload != null && payload.getTokenIndex() >= 0 ?
38                                 ((CommonToken)payload).getLine()
39                                 : Integer.MAX_VALUE;
40                 int count = tree.getChildCount();
41                 for(int i=0;i<count;++i) {
42                         Tree child = tree.getChild(i);
43                         int temp = getStartLine(child);
44                         if(temp < startLine)
45                                 startLine = temp;
46                 }
47                 return startLine;
48         }
49         
50         public static int getStartIndex(Tree tree) {
51                 Token payload = ((CommonTree)tree).token;
52                 int startIndex = payload != null && payload.getTokenIndex() >= 0 ?
53                                 ((CommonToken)payload).getStartIndex()
54                                 : Integer.MAX_VALUE;
55                 int count = tree.getChildCount();
56                 for(int i=0;i<count;++i) {
57                         Tree child = tree.getChild(i);
58                         int temp = getStartIndex(child);
59                         if(temp < startIndex)
60                                 startIndex = temp;
61                 }
62                 return startIndex;
63         }
64         
65         public static int getStopIndex(Tree tree) {
66                 Token payload = ((CommonTree)tree).token;
67                 int stopIndex =  payload != null && payload.getTokenIndex() >= 0 ?
68                                 ((CommonToken)payload).getStopIndex()
69                                 : 0;
70                 int count = tree.getChildCount();
71                 for(int i=0;i<count;++i) {
72                         Tree child = tree.getChild(i);
73                         int temp = getStopIndex(child);
74                         if(temp > stopIndex)
75                                 stopIndex = temp;
76                 }
77                 return stopIndex;
78         }
79         
80         public static Location location(ISource source, Token token) {
81                 return new Location(source, 
82                                 token.getLine(),
83                                 ((CommonToken) token).getStartIndex(), 
84                                 ((CommonToken) token).getStopIndex()+1
85                         );
86         }
87
88         public static Problem error(ISource source, RecognitionException e, Parser parser) {
89                 return new Problem(location(source, e.token),
90                                 parser.getErrorMessage(e, parser.getTokenNames()));
91         }
92
93         public static Problem error(ISource source, RecognitionException e,     Lexer lexer) {
94                 return new Problem(new Location(source, e.line, e.index, e.index+1),
95                                 lexer.getErrorMessage(e, lexer.getTokenNames()));
96         }       
97 }