Search in sources :

Example 96 with Location

use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.

the class ParserMarkerSupport method createOnTheFlySyntacticMarker.

public static void createOnTheFlySyntacticMarker(final IFile file, final SyntacticErrorStorage errorStorage, final int severity) {
    if (!file.isAccessible()) {
        return;
    }
    int lineNumber = errorStorage.lineNumber;
    int charStart = errorStorage.charStart;
    int charEnd = errorStorage.charEnd;
    String message = errorStorage.message;
    boolean justDoIt = errorStorage.exceptionType == SyntacticErrorStorage.ExceptionType.LEXER_NOVIABLEALT_EXCEPTION;
    try {
        if (justDoIt && lineNumber >= 0) {
            IDocument document = null;
            ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
            IPath fullPath = file.getFullPath();
            if (manager != null) {
                manager.connect(fullPath, LocationKind.IFILE, null);
                ITextFileBuffer buffer = manager.getTextFileBuffer(fullPath, LocationKind.IFILE);
                document = buffer.getDocument();
            }
            try {
                if (document != null && lineNumber > 0 && lineNumber <= document.getNumberOfLines()) {
                    charStart = document.getLineOffset(lineNumber - 1);
                    charEnd = document.getLineOffset(lineNumber - 1);
                    charStart += errorStorage.charStart;
                    charEnd += errorStorage.charEnd;
                }
            } catch (BadLocationException e) {
                ErrorReporter.logExceptionStackTrace(e);
            }
        }
        Location location = new Location(file, lineNumber, charStart, charEnd);
        location.reportSingularExternalProblem(message, severity, GeneralConstants.ONTHEFLY_SYNTACTIC_MARKER);
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace(e);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Location(org.eclipse.titan.designer.AST.Location)

Example 97 with Location

use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.

the class ParserUtilities method getCommentsBefore.

/**
 * Get comments before a token (TTCN-3)
 * @param aToken the token, this will NOT be printed
 * @param aParser parser to get the token list tokenized by the lexer
 * @param aFile parsed file
 * @return location, which contains all of the comments before the given token
 */
public static Location getCommentsBefore(final Token aToken, final Parser aParser, final IFile aFile) {
    if (aToken == null) {
        return null;
    }
    final TokenStream tokenStream = aParser.getTokenStream();
    if (!(tokenStream instanceof CommonTokenStream)) {
        ErrorReporter.INTERNAL_ERROR("tokenStream is not CommonTokenStream");
        return null;
    }
    final CommonTokenStream commonTokenStream = (CommonTokenStream) tokenStream;
    final List<Token> tokens = commonTokenStream.getTokens();
    List<Token> comments = new ArrayList<Token>();
    final int start = aToken.getTokenIndex();
    // a token is hidden if Token.getChannel() > 0
    for (int i = start - 1; i >= 0 && tokens.get(i).getChannel() > 0; i--) {
        final Token t = tokens.get(i);
        final int tokenType = t.getType();
        if (Ttcn3Lexer.LINE_COMMENT == tokenType || Ttcn3Lexer.BLOCK_COMMENT == tokenType) {
            // add new elements to the beginning of the list, because its index is smaller
            comments.add(0, t);
        }
    }
    if (comments.isEmpty()) {
        return null;
    }
    final Location loc = new Location(aFile, comments.get(0), comments.get(comments.size() - 1));
    return loc;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenStream(org.antlr.v4.runtime.TokenStream) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) Location(org.eclipse.titan.designer.AST.Location)

Example 98 with Location

use of org.eclipse.titan.designer.AST.Location 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();
                }
            }
        });
    }
}
Also used : CfgParseResult(org.eclipse.titan.common.parsers.cfg.CfgParseResult) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) Position(org.eclipse.jface.text.Position) ConfigTextEditor(org.eclipse.titan.designer.editors.configeditor.ConfigTextEditor) ConfigEditor(org.eclipse.titan.designer.editors.configeditor.ConfigEditor) ConfigFoldingSupport(org.eclipse.titan.designer.editors.configeditor.ConfigFoldingSupport) TITANMarker(org.eclipse.titan.common.parsers.TITANMarker) SyntacticErrorStorage(org.eclipse.titan.common.parsers.SyntacticErrorStorage) CfgAnalyzer(org.eclipse.titan.common.parsers.cfg.CfgAnalyzer) ISemanticTITANEditor(org.eclipse.titan.designer.editors.ISemanticTITANEditor) IDocument(org.eclipse.jface.text.IDocument) CfgLocation(org.eclipse.titan.common.parsers.cfg.CfgLocation) Location(org.eclipse.titan.designer.AST.Location)

Example 99 with Location

use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.

the class ProjectSourceParser method checkConfigurationRequirements.

/**
 * Checks that all directly referenced projects are using the
 * configuration required by the actual one, if set.
 *
 * @param project
 *                the actual project.
 * @param markerType
 *                the type of the marker to report the error with.
 *
 * @return true if there were not error, false otherwise.
 */
public static boolean checkConfigurationRequirements(final IProject project, final String markerType) {
    IProject[] referencedProjects;
    try {
        referencedProjects = project.getReferencedProjects();
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace(e);
        return false;
    }
    List<ProjectConfigurationsPropertyData.ConfigurationRequirement> requirements = ProjectConfigurationsPropertyData.getConfigurationRequirements(project);
    for (int i = 0, size = requirements.size(); i < size; i++) {
        ProjectConfigurationsPropertyData.ConfigurationRequirement temp = requirements.get(i);
        if (temp.getConfiguration() == null || "".equals(temp.getConfiguration())) {
            continue;
        }
        for (IProject referencedProject : referencedProjects) {
            String name = referencedProject.getName();
            if (name.equals(temp.getProjectName())) {
                String tempActiveConfiguration = ProjectFileHandler.getActiveConfigurationName(referencedProject);
                if (!temp.getConfiguration().equals(tempActiveConfiguration)) {
                    Location location = new Location(project);
                    location.reportExternalProblem("In order to build project `" + project.getName() + "' project `" + name + "' must be using the `" + temp.getConfiguration() + "' configuration, but right now it is using `" + tempActiveConfiguration + "'", IMarker.SEVERITY_ERROR, markerType);
                    return false;
                }
                break;
            }
        }
    }
    return true;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ProjectConfigurationsPropertyData(org.eclipse.titan.designer.properties.data.ProjectConfigurationsPropertyData) IProject(org.eclipse.core.resources.IProject) Location(org.eclipse.titan.designer.AST.Location)

Example 100 with Location

use of org.eclipse.titan.designer.AST.Location in project titan.EclipsePlug-ins by eclipse.

the class ProjectSourceParser method internalDoAnalyzeWithReferences.

/**
 * Internal function.
 * <p>
 * Builds the walking order of the projects from their referencing
 * graph, and analyzes all found to be related to the actual.
 *
 * @param monitor
 *                the progress monitor to provide feedback to the user
 *                about the progress.
 *
 * @return status information on exit.
 */
private IStatus internalDoAnalyzeWithReferences(final SubMonitor monitor) {
    MarkerHandler.markMarkersForRemoval(GeneralConstants.ONTHEFLY_SEMANTIC_MARKER, project);
    if (!checkConfigurationRequirements(project, GeneralConstants.ONTHEFLY_SEMANTIC_MARKER)) {
        MarkerHandler.removeMarkedMarkers(GeneralConstants.ONTHEFLY_SEMANTIC_MARKER, project);
        return Status.OK_STATUS;
    }
    MarkerHandler.removeMarkedMarkers(GeneralConstants.ONTHEFLY_SEMANTIC_MARKER, project);
    if (OutOfMemoryCheck.isOutOfMemoryAlreadyReported()) {
        return Status.CANCEL_STATUS;
    }
    List<IProject> tobeAnalyzed = ProjectBasedBuilder.getProjectBasedBuilder(project).getAllReachableProjects();
    // collect the projects referencing the just now analyzed
    // projects in a bottom up order into the list "tobeAnalyzed"
    Deque<IProject> temporalList = new LinkedList<IProject>();
    temporalList.addLast(project);
    tobeAnalyzed.remove(project);
    while (!temporalList.isEmpty()) {
        IProject tempProject = temporalList.getFirst();
        temporalList.removeFirst();
        if (!tobeAnalyzed.contains(tempProject)) {
            tobeAnalyzed.add(tempProject);
            IProject[] tempProjects = ProjectBasedBuilder.getProjectBasedBuilder(tempProject).getReferencingProjects();
            for (IProject tempProject2 : tempProjects) {
                if (!GlobalParser.getProjectSourceParser(tempProject2).analyzesRunning) {
                    if (tempProject2.isAccessible()) {
                        temporalList.addLast(tempProject2);
                    } else {
                        Location location = new Location(project);
                        location.reportExternalProblem(MessageFormat.format("The project {0} is not accessible but requires to analyze the project {1}", tempProject2.getName(), tempProject.getName()), IMarker.SEVERITY_ERROR, GeneralConstants.ONTHEFLY_SEMANTIC_MARKER);
                    }
                }
            }
        }
    }
    // Collect those projects that might be needed to do the correct
    // analysis.
    List<IProject> additionalRequired = new ArrayList<IProject>();
    for (IProject project : tobeAnalyzed) {
        List<IProject> temp = ProjectBasedBuilder.getProjectBasedBuilder(project).getAllReachableProjects();
        for (IProject temp2 : temp) {
            if (!tobeAnalyzed.contains(temp2) && !additionalRequired.contains(temp2) && GlobalParser.getProjectSourceParser(temp2).getLastTimeChecked() == null) {
                if (temp2.isAccessible()) {
                    additionalRequired.add(temp2);
                } else {
                    Location location = new Location(project);
                    location.reportExternalProblem(MessageFormat.format(REQUIREDPROJECTNOTACCESSIBLE, temp2.getName(), project.getName()), IMarker.SEVERITY_ERROR, GeneralConstants.ONTHEFLY_SEMANTIC_MARKER);
                }
            }
        }
    }
    tobeAnalyzed.addAll(additionalRequired);
    // Do the analyzes in the determined order.
    CompilationTimeStamp compilationCounter = CompilationTimeStamp.getNewCompilationCounter();
    SubMonitor progress = SubMonitor.convert(monitor, tobeAnalyzed.size() * 2);
    progress.setTaskName("Analysis of projects");
    IPreferencesService preferenceService = Platform.getPreferencesService();
    boolean reportDebugInformation = preferenceService.getBoolean(ProductConstants.PRODUCT_ID_DESIGNER, PreferenceConstants.DISPLAYDEBUGINFORMATION, true, null);
    if (reportDebugInformation) {
        TITANDebugConsole.println("On-the-fly analyzation of project " + project.getName() + " started");
    }
    List<IProject> tobeSemanticallyAnalyzed = new ArrayList<IProject>();
    try {
        for (int i = 0; i < tobeAnalyzed.size(); i++) {
            progress.subTask("Analyzing project " + tobeAnalyzed.get(i).getName());
            GlobalParser.getProjectSourceParser(tobeAnalyzed.get(i)).analyzesRunning = true;
            if (tobeAnalyzed.get(i).isAccessible()) {
                if (TITANNature.hasTITANNature(tobeAnalyzed.get(i))) {
                    GlobalParser.getProjectSourceParser(tobeAnalyzed.get(i)).syntacticAnalyzer.internalDoAnalyzeSyntactically(progress.newChild(1));
                    tobeSemanticallyAnalyzed.add(tobeAnalyzed.get(i));
                } else {
                    Location location = new Location(project, 0, 0, 0);
                    location.reportExternalProblem(MessageFormat.format(REQUIREDPROJECTNOTTITANPROJECT, tobeAnalyzed.get(i).getName(), project.getName()), IMarker.SEVERITY_ERROR, GeneralConstants.ONTHEFLY_SEMANTIC_MARKER);
                    progress.worked(1);
                }
            } else {
                Location location = new Location(project);
                location.reportExternalProblem(MessageFormat.format(REQUIREDPROJECTNOTACCESSIBLE, tobeAnalyzed.get(i).getName(), project.getName()), IMarker.SEVERITY_ERROR, GeneralConstants.ONTHEFLY_SEMANTIC_MARKER);
                progress.worked(1);
            }
        }
        ProjectSourceSemanticAnalyzer.analyzeMultipleProjectsSemantically(tobeSemanticallyAnalyzed, progress.newChild(tobeAnalyzed.size()), compilationCounter);
    // semantic check for config file
    // GlobalParser.getConfigSourceParser(project).doSemanticCheck();
    } finally {
        for (int i = 0; i < tobeAnalyzed.size(); i++) {
            GlobalParser.getProjectSourceParser(tobeAnalyzed.get(i)).analyzesRunning = false;
        }
    }
    progress.done();
    lastTimeChecked = compilationCounter;
    return Status.OK_STATUS;
}
Also used : ArrayList(java.util.ArrayList) SubMonitor(org.eclipse.core.runtime.SubMonitor) IProject(org.eclipse.core.resources.IProject) LinkedList(java.util.LinkedList) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

Location (org.eclipse.titan.designer.AST.Location)109 Identifier (org.eclipse.titan.designer.AST.Identifier)24 ReParseException (org.eclipse.titan.designer.parsers.ttcn3parser.ReParseException)24 NULL_Location (org.eclipse.titan.designer.AST.NULL_Location)21 IIdentifierReparser (org.eclipse.titan.designer.parsers.ttcn3parser.IIdentifierReparser)18 IdentifierReparser (org.eclipse.titan.designer.parsers.ttcn3parser.IdentifierReparser)18 Module (org.eclipse.titan.designer.AST.Module)16 ArrayList (java.util.ArrayList)15 IFile (org.eclipse.core.resources.IFile)13 HashMap (java.util.HashMap)11 Reference (org.eclipse.titan.designer.AST.Reference)10 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)10 Assignment (org.eclipse.titan.designer.AST.Assignment)9 IDocument (org.eclipse.jface.text.IDocument)8 InsertEdit (org.eclipse.text.edits.InsertEdit)8 ILocateableNode (org.eclipse.titan.designer.AST.ILocateableNode)8 CoreException (org.eclipse.core.runtime.CoreException)7 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)7 TITANMarker (org.eclipse.titan.common.parsers.TITANMarker)7 Definition (org.eclipse.titan.designer.AST.TTCN3.definitions.Definition)7