use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.
the class TTCN3ReferenceAnalyzer method parse.
/**
* @return the parsed reference or null if the text can not form a reference
*/
public Reference parse(final IFile file, final String code, final boolean reportErrors, final int aLine, final int aOffset) {
Reference reference = null;
Reader reader = new StringReader(code);
CharStream charStream = new UnbufferedCharStream(reader);
Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.initRootInterval(code.length());
lexer.removeErrorListeners();
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
Ttcn3Parser parser = new Ttcn3Parser(tokenStream);
ParserUtilities.setBuildParseTree(parser);
lexer.setActualFile(file);
parser.setActualFile(file);
parser.setProject(file.getProject());
parser.setLine(aLine);
parser.setOffset(aOffset);
parser.removeErrorListeners();
final Pr_UnifiedReferenceParserContext root = parser.pr_UnifiedReferenceParser();
ParserUtilities.logParseTree(root, parser);
reference = root.reference;
return reference;
}
use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.
the class TTCN3ReparseUpdater method startsWithFollow.
/**
* Checks if the first TTCN-3 lexical token in the substring, that covers the possibly changed interval of the document belongs to a given list of
* expected tokens or not.
*
* @param followSet the possible tokens that can follow the element before the suspected
*
* @return true if the first lexical token is part of the followset, false otherwise
*/
public boolean startsWithFollow(final List<Integer> followSet) {
if (followSet == null || followSet.isEmpty()) {
return false;
}
if (code == null) {
return false;
}
int line = getLineOfOffset(code, modificationStartOffset);
int column = getPositionInLine(code, modificationStartOffset);
String substring;
if (code.length() <= modificationEndOffset + shift) {
substring = code.substring(modificationStartOffset);
} else {
substring = code.substring(modificationStartOffset, modificationEndOffset + shift);
}
Reader reader = new StringReader(substring);
CharStream charStream = new UnbufferedCharStream(reader);
Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.setLine(line + 1);
lexer.setCharPositionInLine(column);
lexer.initRootInterval(modificationEndOffset - modificationStartOffset + 1);
Token token = lexer.nextToken();
if (token == null) {
return false;
}
return followSet.contains(token.getType());
}
use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.
the class TTCN3ReparseUpdater method parse.
public int parse(final ITTCN3ReparseBase userDefined) {
if (modificationStartOffset == modificationEndOffset + shift) {
return 0;
}
// double wideparsing = System.nanoTime();
mErrors = null;
warnings = null;
Iterator<TITANMarker> iterator = unsupportedConstructs.iterator();
while (iterator.hasNext()) {
TITANMarker marker = iterator.next();
if ((marker.getOffset() > modificationStartOffset && marker.getOffset() <= modificationEndOffset) || (marker.getEndOffset() > modificationStartOffset && marker.getEndOffset() <= modificationEndOffset)) {
iterator.remove();
}
}
MarkerHandler.markAllOnTheFlyMarkersForRemoval(file, modificationStartOffset, modificationEndOffset + shift);
if (code == null) {
return Integer.MAX_VALUE;
}
int line = getLineOfOffset(code, modificationStartOffset);
String substring;
if (code.length() <= modificationEndOffset + shift) {
substring = code.substring(modificationStartOffset);
} else {
substring = code.substring(modificationStartOffset, modificationEndOffset + shift);
}
Reader reader = new StringReader(substring);
CharStream charStream = new UnbufferedCharStream(reader);
Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.initRootInterval(modificationEndOffset - modificationStartOffset + 1);
// lexer and parser listener
TitanListener parserListener = new TitanListener();
// remove ConsoleErrorListener
lexer.removeErrorListeners();
lexer.addErrorListener(parserListener);
// 1. Previously it was UnbufferedTokenStream(lexer), but it was changed to BufferedTokenStream, because UnbufferedTokenStream seems to be unusable. It is an ANTLR 4 bug.
// Read this: https://groups.google.com/forum/#!topic/antlr-discussion/gsAu-6d3pKU
// pr_PatternChunk[StringBuilder builder, boolean[] uni]:
// $builder.append($v.text); <-- exception is thrown here: java.lang.UnsupportedOperationException: interval 85..85 not in token buffer window: 86..341
// 2. Changed from BufferedTokenStream to CommonTokenStream, otherwise tokens with "-> channel(HIDDEN)" are not filtered out in lexer.
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
Ttcn3Reparser parser = new Ttcn3Reparser(tokenStream);
ParserUtilities.setBuildParseTree(parser);
lexer.setActualFile(file);
parser.setActualFile(file);
parser.setProject(file.getProject());
parser.setOffset(modificationStartOffset);
parser.setLine(line + 1);
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
userDefined.reparse(parser);
mErrors = parserListener.getErrorsStored();
warnings = parser.getWarnings();
unsupportedConstructs.addAll(parser.getUnsupportedConstructs());
int result = measureIntervallDamage();
if (!parser.isErrorListEmpty()) {
++result;
}
return result;
}
use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.
the class TTCN3ReparseUpdater method endsWithToken.
/**
* Checks if the last TTCN-3 lexical token in the substring, that covers the possibly changed interval of the document belongs to a given list of
* expected tokens or not.
*
* @param followSet the possible tokens that can prepend the element before the suspected
*
* @return true if the first lexical token is part of the followset, false otherwise
*/
public boolean endsWithToken(final List<Integer> followSet) {
if (followSet == null || followSet.isEmpty()) {
return false;
}
if (code == null) {
return false;
}
int line = getLineOfOffset(code, modificationStartOffset);
int column = getPositionInLine(code, modificationStartOffset);
String substring;
if (code.length() <= modificationEndOffset + shift) {
substring = code.substring(modificationStartOffset);
} else {
substring = code.substring(modificationStartOffset, modificationEndOffset + shift);
}
Reader reader = new StringReader(substring);
CharStream charStream = new UnbufferedCharStream(reader);
Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
lexer.setLine(line + 1);
lexer.setCharPositionInLine(column);
lexer.initRootInterval(modificationEndOffset - modificationStartOffset + 1);
Token token = lexer.nextToken();
if (token == null) {
return false;
}
return followSet.contains(token.getType());
}
use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.
the class VariantAttributeAnalyzer method parse.
public void parse(final RawAST rawAST, final AttributeSpecification specification, final int lengthMultiplier, final AtomicBoolean raw_found) {
VariantAttributeLexer lexer;
Location location = specification.getLocation();
StringReader reader = new StringReader(specification.getSpecification());
CharStream charStream = new UnbufferedCharStream(reader);
lexer = new VariantAttributeLexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
TitanListener lexerListener = new TitanListener();
lexer.removeErrorListeners();
lexer.addErrorListener(lexerListener);
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
VariantAttributeParser parser = new VariantAttributeParser(tokenStream);
parser.setBuildParseTree(false);
TitanListener parserListener = new TitanListener();
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
parser.setActualFile((IFile) location.getFile());
parser.setLine(location.getLine());
parser.setOffset(location.getOffset() + 1);
MarkerHandler.markMarkersForRemoval(GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER, location.getFile(), location.getOffset(), location.getEndOffset());
parser.setRawAST(rawAST);
parser.setLengthMultiplier(lengthMultiplier);
parser.pr_AttribSpec();
if (!lexerListener.getErrorsStored().isEmpty()) {
for (int i = 0; i < lexerListener.getErrorsStored().size(); i++) {
Location temp = new Location(location);
temp.setOffset(temp.getOffset() + 1);
ParserMarkerSupport.createOnTheFlyMixedMarker((IFile) location.getFile(), lexerListener.getErrorsStored().get(i), IMarker.SEVERITY_ERROR, temp);
}
}
if (!parserListener.getErrorsStored().isEmpty()) {
for (int i = 0; i < parserListener.getErrorsStored().size(); i++) {
Location temp = new Location(location);
temp.setOffset(temp.getOffset() + 1);
ParserMarkerSupport.createOnTheFlyMixedMarker((IFile) location.getFile(), parserListener.getErrorsStored().get(i), IMarker.SEVERITY_ERROR, temp);
}
}
if (!raw_found.get()) {
raw_found.set(parser.getRawFound());
}
}
Aggregations