use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.
the class Definition method parseErrAttrSpecString.
private static ErroneousAttributeSpecification parseErrAttrSpecString(final AttributeSpecification aAttrSpec) {
String code = aAttrSpec.getSpecification();
if (code == null) {
return null;
}
final Location location = aAttrSpec.getLocation();
// code must be transformed, according to
// compiler2/ttcn3/charstring_la.l
// TODO
code = Ttcn3CharstringLexer.parseCharstringValue(code, location);
final Reader reader = new StringReader(code);
final CharStream charStream = new UnbufferedCharStream(reader);
final Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
// needs to be shifted by one because of the \" of the string
lexer.setCharPositionInLine(0);
// lexer and parser listener
final 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);
final Ttcn3Reparser parser = new Ttcn3Reparser(tokenStream);
ParserUtilities.setBuildParseTree(parser);
final IFile file = (IFile) location.getFile();
parser.setActualFile(file);
parser.setOffset(location.getOffset());
parser.setLine(location.getLine());
// remove ConsoleErrorListener
parser.removeErrorListeners();
parser.addErrorListener(parserListener);
MarkerHandler.markMarkersForRemoval(GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER, location.getFile(), location.getOffset(), location.getEndOffset());
final Pr_ErroneousAttributeSpecContext root = parser.pr_ErroneousAttributeSpec();
ParserUtilities.logParseTree(root, parser);
final ErroneousAttributeSpecification returnValue = root.errAttrSpec;
final List<SyntacticErrorStorage> errors = parser.getErrors();
final List<TITANMarker> warnings = parser.getWarnings();
final List<TITANMarker> unsupportedConstructs = parser.getUnsupportedConstructs();
// add markers
if (errors != null) {
for (int i = 0; i < errors.size(); i++) {
final Location temp = new Location(location);
temp.setOffset(temp.getOffset());
ParserMarkerSupport.createOnTheFlySyntacticMarker(file, errors.get(i), IMarker.SEVERITY_ERROR, temp);
}
}
if (warnings != null) {
for (final TITANMarker marker : warnings) {
if (file.isAccessible()) {
final Location loc = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
loc.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
if (unsupportedConstructs != null) {
for (final TITANMarker marker : unsupportedConstructs) {
if (file.isAccessible()) {
final Location loc = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
loc.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
return returnValue;
}
use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.
the class ProjectConfigurationParser method fileBasedAnalysis.
/**
* Parses the provided file.
*
* @param file (in) the file to be parsed
* @param aMacros (in/out) collected macro references
* @param aFilesChecked files, which are already processed (there are no duplicates)
* @param aFilesToCheck files, which will be processed (there are no duplicates)
*/
private void fileBasedAnalysis(final IFile file, final List<Macro> aMacros, final List<IFile> aFilesToCheck, final List<IFile> aFilesChecked) {
List<TITANMarker> warnings = null;
List<SyntacticErrorStorage> errorsStored = null;
IDocument document = null;
ISemanticTITANEditor tempEditor = null;
List<ISemanticTITANEditor> editors = null;
if (EditorTracker.containsKey(file)) {
editors = EditorTracker.getEditor(file);
tempEditor = editors.get(0);
document = tempEditor.getDocument();
}
ConfigTextEditor editor = null;
if (tempEditor instanceof ConfigTextEditor) {
editor = (ConfigTextEditor) tempEditor;
}
String oldConfigFilePath = fileMap.get(file);
if (oldConfigFilePath != null) {
fileMap.remove(file);
}
CfgAnalyzer cfgAnalyzer = new CfgAnalyzer();
cfgAnalyzer.parse(file, document == null ? null : document.get());
errorsStored = cfgAnalyzer.getErrorStorage();
final CfgParseResult cfgParseResult = cfgAnalyzer.getCfgParseResult();
if (cfgParseResult != null) {
warnings = cfgParseResult.getWarnings();
aMacros.addAll(cfgParseResult.getMacros());
definitions.putAll(cfgParseResult.getDefinitions());
// add included files to the aFilesToCheck list
final List<String> includeFilenames = cfgParseResult.getIncludeFiles();
for (final String includeFilename : includeFilenames) {
// example value: includeFilename == MyExample2.cfg
// example value: file == L/hw/src/MyExample.cfg
final IPath includeFilePath = PathConverter.getProjectRelativePath(file, includeFilename);
// example value: includeFilePath == src/MyExample2.cfg
if (includeFilePath != null) {
final IFile includeFile = project.getFile(includeFilePath);
// includeFile is null if the file does not exist in the project
if (includeFile != null && !uptodateFiles.containsKey(includeFile) && !aFilesChecked.contains(includeFile) && !aFilesToCheck.contains(includeFile)) {
removeMarkersAndDefinitions(includeFile);
aFilesToCheck.add(includeFile);
}
}
}
if (editor != null && editor.getDocument() != null) {
ConfigEditor parentEditor = editor.getParentEditor();
if (errorsStored == null || errorsStored.isEmpty()) {
parentEditor.setParseTreeRoot(cfgParseResult.getParseTreeRoot());
parentEditor.setTokens(cfgParseResult.getTokens());
parentEditor.refresh(cfgAnalyzer);
parentEditor.setErrorMessage(null);
} else {
if (errorsStored.size() > 1) {
parentEditor.setErrorMessage("There were " + errorsStored.size() + " problems found while parsing");
} else {
parentEditor.setErrorMessage("There was 1 problem found while parsing");
}
}
}
}
fileMap.put(file, file.getFullPath().toOSString());
uptodateFiles.put(file, file.getFullPath().toOSString());
if (document != null) {
GlobalIntervalHandler.putInterval(document, cfgAnalyzer.getRootInterval());
}
if (warnings != null) {
for (TITANMarker marker : warnings) {
if (file.isAccessible()) {
Location location = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
location.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
if (errorsStored != null && !errorsStored.isEmpty()) {
String reportLevel = Platform.getPreferencesService().getString(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.REPORTERRORSINEXTENSIONSYNTAX, GeneralConstants.WARNING, null);
int errorLevel;
if (GeneralConstants.ERROR.equals(reportLevel)) {
errorLevel = IMarker.SEVERITY_ERROR;
} else if (GeneralConstants.WARNING.equals(reportLevel)) {
errorLevel = IMarker.SEVERITY_WARNING;
} else {
return;
}
for (int i = 0; i < errorsStored.size(); i++) {
ParserMarkerSupport.createOnTheFlySyntacticMarker(file, errorsStored.get(i), errorLevel);
}
}
if (document != null && editors != null) {
ConfigFoldingSupport foldingSupport = new ConfigFoldingSupport();
final IDocument tempDocument = document;
final List<ISemanticTITANEditor> editors2 = editors;
final List<Position> positions = foldingSupport.calculatePositions(tempDocument);
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
for (ISemanticTITANEditor editor : editors2) {
editor.updateFoldingStructure(positions);
editor.invalidateTextPresentation();
}
}
});
}
}
use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.
the class ProjectSourceSyntacticAnalyzer method fileBasedGeneralAnalysis.
/**
* Parses the provided file.
*
* @param file
* the file to be parsed
* @param analyzer
* the source code analyzer that should be used to
* analyze the code
*
* @return the temporal data structure needed to insert the parsed
* module in the list of modules, in the post-analyzes step.
*/
private TemporalParseData fileBasedGeneralAnalysis(final IFile file, final ISourceAnalyzer analyzer) {
if (analyzer == null) {
return null;
}
IDocument document = DocumentTracker.get(file);
unsupportedConstructMap.remove(file);
try {
analyzer.parse(file, document == null ? null : document.get());
} catch (FileNotFoundException e) {
ErrorReporter.logExceptionStackTrace(e);
}
final boolean hadParseErrors = processParserErrors(file, analyzer);
List<TITANMarker> warnings = analyzer.getWarnings();
List<TITANMarker> unsupportedConstructs = analyzer.getUnsupportedConstructs();
Module module = analyzer.getModule();
if (warnings != null) {
for (TITANMarker marker : warnings) {
if (file.isAccessible()) {
Location location = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
location.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
if (document != null) {
GlobalIntervalHandler.putInterval(document, analyzer.getRootInterval());
}
return new TemporalParseData(module, file, unsupportedConstructs, hadParseErrors, document);
}
use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.
the class TTCN3ReparseUpdater method reportSyntaxErrors.
public final void reportSyntaxErrors() {
reportSpecificSyntaxErrors();
if (warnings != null) {
for (TITANMarker marker : warnings) {
if (file.isAccessible()) {
Location location = new Location(file, marker.getLine(), marker.getOffset(), marker.getEndOffset());
location.reportExternalProblem(marker.getMessage(), marker.getSeverity(), GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
}
}
}
if (unsupportedConstructs != null && !unsupportedConstructs.isEmpty()) {
Iterator<TITANMarker> iterator = unsupportedConstructs.iterator();
while (iterator.hasNext()) {
TITANMarker marker = iterator.next();
if (marker.getOffset() >= modificationEndOffset) {
marker.setOffset(marker.getOffset() + shift);
marker.setEndOffset(marker.getEndOffset() + shift);
}
}
unsupportedConstructMap.put(file, unsupportedConstructs);
}
}
use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.
the class PreprocessedTokenStream method fetch.
@Override
public int fetch(int n) {
if (fetchedEOF) {
return 0;
}
int i = 0;
do {
Token t;
if (tokenStreamStack.isEmpty()) {
t = getTokenSource().nextToken();
} else {
t = tokenStreamStack.peek().getTokenSource().nextToken();
}
if (t == null) {
return 0;
}
int tokenType = t.getType();
if (tokenType == Ttcn3Lexer.PREPROCESSOR_DIRECTIVE) {
lastPPDirectiveLocation = new Location(actualFile, t.getLine(), t.getStartIndex(), t.getStopIndex() + 1);
// 1. the first # shall be discarded
// 2. "\\\n" strings are removed, so multiline tokens, which are split by backslash are extracted to one line
final String text = t.getText().substring(1).replace("\\\n", "");
Reader reader = new StringReader(text);
CharStream charStream = new UnbufferedCharStream(reader);
PreprocessorDirectiveLexer lexer = new PreprocessorDirectiveLexer(charStream);
lexer.setTokenFactory(new PPDirectiveTokenFactory(true, t));
lexerListener = new PPListener();
lexer.removeErrorListeners();
lexer.addErrorListener(lexerListener);
lexer.setLine(t.getLine());
lexer.setCharPositionInLine(t.getCharPositionInLine());
// 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);
PreprocessorDirectiveParser localParser = new PreprocessorDirectiveParser(tokenStream);
localParser.setBuildParseTree(false);
parserListener = new PPListener(localParser);
localParser.removeErrorListeners();
localParser.addErrorListener(parserListener);
localParser.setIsActiveCode(condStateStack.isPassing());
localParser.setMacros(macros);
localParser.setLine(t.getLine());
PreprocessorDirective ppDirective = null;
ppDirective = localParser.pr_Directive().ppDirective;
errorsStored.addAll(localParser.getErrorStorage());
warnings.addAll(localParser.getWarnings());
unsupportedConstructs.addAll(localParser.getUnsupportedConstructs());
if (ppDirective != null) {
ppDirective.line = t.getLine();
if (ppDirective.isConditional()) {
boolean preIsPassing = condStateStack.isPassing();
condStateStack.processDirective(ppDirective);
boolean postIsPassing = condStateStack.isPassing();
if (preIsPassing != postIsPassing && tokenStreamStack.isEmpty() && getTokenSource() instanceof Ttcn3Lexer) {
// included files are ignored because of ambiguity
Location ppLocation = lastPPDirectiveLocation;
if (ppLocation != null) {
if (preIsPassing) {
// switched to inactive: begin a new inactive location
Location loc = new Location(actualFile, ppLocation.getLine(), ppLocation.getEndOffset(), ppLocation.getEndOffset());
inactiveCodeLocations.add(loc);
} else {
// switched to active: end the current inactive location
int iclSize = inactiveCodeLocations.size();
if (iclSize > 0) {
Location lastLocation = inactiveCodeLocations.get(iclSize - 1);
lastLocation.setEndOffset(ppLocation.getOffset());
}
}
}
}
} else {
// other directive types
if (condStateStack.isPassing()) {
// directive
switch(ppDirective.type) {
case INCLUDE:
{
if (tokenStreamStack.size() > RECURSION_LIMIT) {
// dumb but safe defense against infinite recursion, default value from gcc
TITANMarker marker = new TITANMarker("Maximum #include recursion depth reached", ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
unsupportedConstructs.add(marker);
} else {
// TODO: Makes the Eclipse slow down
processIncludeDirective(ppDirective);
}
}
break;
case ERROR:
{
String errorMessage = ppDirective.str == null ? "" : ppDirective.str;
TITANMarker marker = new TITANMarker(errorMessage, ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
unsupportedConstructs.add(marker);
}
break;
case WARNING:
{
String warningMessage = ppDirective.str == null ? "" : ppDirective.str;
TITANMarker marker = new TITANMarker(warningMessage, ppDirective.line, -1, -1, IMarker.SEVERITY_WARNING, IMarker.PRIORITY_NORMAL);
warnings.add(marker);
}
break;
case LINECONTROL:
case LINEMARKER:
case PRAGMA:
case NULL:
{
String reportPreference = Platform.getPreferencesService().getString(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.REPORT_IGNORED_PREPROCESSOR_DIRECTIVES, GeneralConstants.WARNING, null);
if (!GeneralConstants.IGNORE.equals(reportPreference)) {
boolean isError = GeneralConstants.ERROR.equals(reportPreference);
TITANMarker marker = new TITANMarker(MessageFormat.format("Preprocessor directive {0} is ignored", ppDirective.type.getName()), ppDirective.line, -1, -1, isError ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING, IMarker.PRIORITY_NORMAL);
if (isError) {
unsupportedConstructs.add(marker);
} else {
warnings.add(marker);
}
}
}
break;
default:
}
}
}
}
} else if (tokenType == Token.EOF) {
if (!tokenStreamStack.isEmpty()) {
// the included file ended, drop lexer
// from the stack and ignore EOF token
TokenStreamData tsd = tokenStreamStack.pop();
if (parser != null) {
if (tokenStreamStack.isEmpty()) {
parser.setActualFile(actualFile);
} else {
parser.setActualFile(tokenStreamStack.peek().file);
}
}
if (tsd.reader != null) {
try {
tsd.reader.close();
} catch (IOException e) {
}
}
} else {
fetchedEOF = true;
condStateStack.eofCheck();
tokens.add(t);
((CommonToken) t).setTokenIndex(tokens.size() - 1);
--n;
++i;
if (n == 0) {
return i;
}
}
} else {
if (condStateStack.isPassing()) {
tokens.add(t);
((CommonToken) t).setTokenIndex(tokens.size() - 1);
--n;
++i;
if (n == 0) {
return i;
}
}
}
} while (true);
}
Aggregations