X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.ltk.antlr%2Fsrc%2Forg%2Fsimantics%2Fltk%2Fantlr%2FANTLRUtils.java;h=ec26244c057e0587096e2da2408308c8d97f9d2d;hb=daab168e98bc21070ccb45beba497e3d712adfeb;hp=61c7dbd54f6f5d87eab4c5655e8dd7e7cfc13e8e;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.ltk.antlr/src/org/simantics/ltk/antlr/ANTLRUtils.java b/bundles/org.simantics.ltk.antlr/src/org/simantics/ltk/antlr/ANTLRUtils.java index 61c7dbd54..ec26244c0 100644 --- a/bundles/org.simantics.ltk.antlr/src/org/simantics/ltk/antlr/ANTLRUtils.java +++ b/bundles/org.simantics.ltk.antlr/src/org/simantics/ltk/antlr/ANTLRUtils.java @@ -1,97 +1,97 @@ -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= 0 ? - ((CommonToken)payload).getStartIndex() - : Integer.MAX_VALUE; - int count = tree.getChildCount(); - for(int i=0;i= 0 ? - ((CommonToken)payload).getStopIndex() - : 0; - int count = tree.getChildCount(); - for(int i=0;i 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())); - } -} +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= 0 ? + ((CommonToken)payload).getStartIndex() + : Integer.MAX_VALUE; + int count = tree.getChildCount(); + for(int i=0;i= 0 ? + ((CommonToken)payload).getStopIndex() + : 0; + int count = tree.getChildCount(); + for(int i=0;i 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())); + } +}