-package org.simantics.ltk.antlr;\r
-\r
-import java.io.IOException;\r
-\r
-import org.antlr.runtime.ANTLRInputStream;\r
-import org.antlr.runtime.CharStream;\r
-import org.antlr.runtime.CommonToken;\r
-import org.antlr.runtime.CommonTokenStream;\r
-import org.antlr.runtime.Lexer;\r
-import org.antlr.runtime.Parser;\r
-import org.antlr.runtime.RecognitionException;\r
-import org.antlr.runtime.Token;\r
-import org.antlr.runtime.tree.CommonTree;\r
-import org.antlr.runtime.tree.Tree;\r
-import org.simantics.ltk.ISource;\r
-import org.simantics.ltk.Location;\r
-import org.simantics.ltk.Problem;\r
-\r
-public class ANTLRUtils {\r
-\r
- public static void setUpParser(ISource source, Lexer lexer, Parser parser) throws IOException {\r
- CharStream stream = new ANTLRInputStream(source.open());\r
- lexer.setCharStream(stream);\r
- parser.setTokenStream(new CommonTokenStream(lexer));\r
- }\r
- \r
- public static Location location(ISource source, Tree tree) {\r
- return new Location(source, \r
- getStartLine(tree), \r
- getStartIndex(tree), \r
- getStopIndex(tree)+1\r
- );\r
- }\r
- \r
- public static int getStartLine(Tree tree) {\r
- Token payload = ((CommonTree)tree).token;\r
- int startLine = payload != null && payload.getTokenIndex() >= 0 ?\r
- ((CommonToken)payload).getLine()\r
- : Integer.MAX_VALUE;\r
- int count = tree.getChildCount();\r
- for(int i=0;i<count;++i) {\r
- Tree child = tree.getChild(i);\r
- int temp = getStartLine(child);\r
- if(temp < startLine)\r
- startLine = temp;\r
- }\r
- return startLine;\r
- }\r
- \r
- public static int getStartIndex(Tree tree) {\r
- Token payload = ((CommonTree)tree).token;\r
- int startIndex = payload != null && payload.getTokenIndex() >= 0 ?\r
- ((CommonToken)payload).getStartIndex()\r
- : Integer.MAX_VALUE;\r
- int count = tree.getChildCount();\r
- for(int i=0;i<count;++i) {\r
- Tree child = tree.getChild(i);\r
- int temp = getStartIndex(child);\r
- if(temp < startIndex)\r
- startIndex = temp;\r
- }\r
- return startIndex;\r
- }\r
- \r
- public static int getStopIndex(Tree tree) {\r
- Token payload = ((CommonTree)tree).token;\r
- int stopIndex = payload != null && payload.getTokenIndex() >= 0 ?\r
- ((CommonToken)payload).getStopIndex()\r
- : 0;\r
- int count = tree.getChildCount();\r
- for(int i=0;i<count;++i) {\r
- Tree child = tree.getChild(i);\r
- int temp = getStopIndex(child);\r
- if(temp > stopIndex)\r
- stopIndex = temp;\r
- }\r
- return stopIndex;\r
- }\r
- \r
- public static Location location(ISource source, Token token) {\r
- return new Location(source, \r
- token.getLine(),\r
- ((CommonToken) token).getStartIndex(), \r
- ((CommonToken) token).getStopIndex()+1\r
- );\r
- }\r
-\r
- public static Problem error(ISource source, RecognitionException e, Parser parser) {\r
- return new Problem(location(source, e.token),\r
- parser.getErrorMessage(e, parser.getTokenNames()));\r
- }\r
-\r
- public static Problem error(ISource source, RecognitionException e, Lexer lexer) {\r
- return new Problem(new Location(source, e.line, e.index, e.index+1),\r
- lexer.getErrorMessage(e, lexer.getTokenNames()));\r
- } \r
-}\r
+package org.simantics.ltk.antlr;
+
+import java.io.IOException;
+
+import org.antlr.runtime.ANTLRInputStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonToken;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.Lexer;
+import org.antlr.runtime.Parser;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.Token;
+import org.antlr.runtime.tree.CommonTree;
+import org.antlr.runtime.tree.Tree;
+import org.simantics.ltk.ISource;
+import org.simantics.ltk.Location;
+import org.simantics.ltk.Problem;
+
+public class ANTLRUtils {
+
+ public static void setUpParser(ISource source, Lexer lexer, Parser parser) throws IOException {
+ CharStream stream = new ANTLRInputStream(source.open());
+ lexer.setCharStream(stream);
+ parser.setTokenStream(new CommonTokenStream(lexer));
+ }
+
+ public static Location location(ISource source, Tree tree) {
+ return new Location(source,
+ getStartLine(tree),
+ getStartIndex(tree),
+ getStopIndex(tree)+1
+ );
+ }
+
+ public static int getStartLine(Tree tree) {
+ Token payload = ((CommonTree)tree).token;
+ int startLine = payload != null && payload.getTokenIndex() >= 0 ?
+ ((CommonToken)payload).getLine()
+ : Integer.MAX_VALUE;
+ int count = tree.getChildCount();
+ for(int i=0;i<count;++i) {
+ Tree child = tree.getChild(i);
+ int temp = getStartLine(child);
+ if(temp < startLine)
+ startLine = temp;
+ }
+ return startLine;
+ }
+
+ public static int getStartIndex(Tree tree) {
+ Token payload = ((CommonTree)tree).token;
+ int startIndex = payload != null && payload.getTokenIndex() >= 0 ?
+ ((CommonToken)payload).getStartIndex()
+ : Integer.MAX_VALUE;
+ int count = tree.getChildCount();
+ for(int i=0;i<count;++i) {
+ Tree child = tree.getChild(i);
+ int temp = getStartIndex(child);
+ if(temp < startIndex)
+ startIndex = temp;
+ }
+ return startIndex;
+ }
+
+ public static int getStopIndex(Tree tree) {
+ Token payload = ((CommonTree)tree).token;
+ int stopIndex = payload != null && payload.getTokenIndex() >= 0 ?
+ ((CommonToken)payload).getStopIndex()
+ : 0;
+ int count = tree.getChildCount();
+ for(int i=0;i<count;++i) {
+ Tree child = tree.getChild(i);
+ int temp = getStopIndex(child);
+ if(temp > stopIndex)
+ stopIndex = temp;
+ }
+ return stopIndex;
+ }
+
+ public static Location location(ISource source, Token token) {
+ return new Location(source,
+ token.getLine(),
+ ((CommonToken) token).getStartIndex(),
+ ((CommonToken) token).getStopIndex()+1
+ );
+ }
+
+ public static Problem error(ISource source, RecognitionException e, Parser parser) {
+ return new Problem(location(source, e.token),
+ parser.getErrorMessage(e, parser.getTokenNames()));
+ }
+
+ public static Problem error(ISource source, RecognitionException e, Lexer lexer) {
+ return new Problem(new Location(source, e.line, e.index, e.index+1),
+ lexer.getErrorMessage(e, lexer.getTokenNames()));
+ }
+}