Search in sources :

Example 11 with TITANMarker

use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.

the class PreprocessedTokenStream method processIncludeDirective.

/**
 * Adds a new lexer to the lexer stack to read tokens from the included
 * file
 *
 * @param fileName
 *                the file name paramtere of the #include directive
 */
private void processIncludeDirective(final PreprocessorDirective ppDirective) {
    if (ppDirective.str == null || "".equals(ppDirective.str)) {
        TITANMarker marker = new TITANMarker("File name was not provided", ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
        unsupportedConstructs.add(marker);
        return;
    }
    IFile includedFile = GlobalParser.getProjectSourceParser(actualFile.getProject()).getTTCN3IncludeFileByName(ppDirective.str);
    if (includedFile == null) {
        TITANMarker marker = new TITANMarker(MessageFormat.format("Included file `{0}'' could not be found", ppDirective.str), ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
        unsupportedConstructs.add(marker);
        return;
    }
    // check extension
    if (!GlobalParser.TTCNIN_EXTENSION.equals(includedFile.getFileExtension())) {
        TITANMarker marker = new TITANMarker(MessageFormat.format("File `{0}'' does not have the `{1}'' extension", ppDirective.str, GlobalParser.TTCNIN_EXTENSION), ppDirective.line, -1, -1, IMarker.SEVERITY_WARNING, IMarker.PRIORITY_NORMAL);
        warnings.add(marker);
    }
    // check if the file is already loaded into an editor
    String code = null;
    if (EditorTracker.containsKey(includedFile)) {
        List<ISemanticTITANEditor> editors = EditorTracker.getEditor(includedFile);
        ISemanticTITANEditor editor = editors.get(0);
        IDocument document = editor.getDocument();
        code = document.get();
    }
    // create lexer and set it up
    Reader reader = null;
    CharStream charStream = null;
    Ttcn3Lexer lexer = null;
    int rootInt;
    if (code != null) {
        reader = new StringReader(code);
        charStream = new UnbufferedCharStream(reader);
        lexer = new Ttcn3Lexer(charStream);
        lexer.setTokenFactory(new CommonTokenFactory(true));
        rootInt = code.length();
    } else {
        try {
            InputStreamReader temp = new InputStreamReader(includedFile.getContents());
            if (!includedFile.getCharset().equals(temp.getEncoding())) {
                try {
                    temp.close();
                } catch (IOException e) {
                    ErrorReporter.logWarningExceptionStackTrace(e);
                }
                temp = new InputStreamReader(includedFile.getContents(), includedFile.getCharset());
            }
            reader = new BufferedReader(temp);
        } catch (CoreException e) {
            ErrorReporter.logExceptionStackTrace(e);
            return;
        } catch (UnsupportedEncodingException e) {
            ErrorReporter.logExceptionStackTrace(e);
            return;
        }
        charStream = new UnbufferedCharStream(reader);
        lexer = new Ttcn3Lexer(charStream);
        lexer.setTokenFactory(new CommonTokenFactory(true));
        lexerListener = new TitanListener();
        // remove ConsoleErrorListener
        lexer.removeErrorListeners();
        lexer.addErrorListener(lexerListener);
        IFileStore store;
        try {
            store = EFS.getStore(includedFile.getLocationURI());
        } catch (CoreException e) {
            ErrorReporter.logExceptionStackTrace(e);
            return;
        }
        IFileInfo fileInfo = store.fetchInfo();
        rootInt = (int) fileInfo.getLength();
    }
    lexer.setTokenFactory(new CommonTokenFactory(true));
    lexer.setTTCNPP();
    lexer.initRootInterval(rootInt);
    lexer.setActualFile(includedFile);
    // add the lexer to the stack of lexers
    tokenStreamStack.push(new TokenStreamData(lexer, includedFile, reader));
    if (parser != null) {
        parser.setActualFile(includedFile);
    }
    includedFiles.add(includedFile);
}
Also used : IFile(org.eclipse.core.resources.IFile) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) TITANMarker(org.eclipse.titan.common.parsers.TITANMarker) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IFileStore(org.eclipse.core.filesystem.IFileStore) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) ISemanticTITANEditor(org.eclipse.titan.designer.editors.ISemanticTITANEditor) IDocument(org.eclipse.jface.text.IDocument)

Example 12 with TITANMarker

use of org.eclipse.titan.common.parsers.TITANMarker in project titan.EclipsePlug-ins by eclipse.

the class PreprocessedTokenStream method transition.

public void transition(final PreprocessorDirective ppDirective, final List<TITANMarker> errors) {
    ConditionalTransition transition;
    boolean newCond;
    switch(ppDirective.type) {
        case ELIF:
            transition = ConditionalTransition.ELIF;
            newCond = ppDirective.condition;
            break;
        case ELSE:
            transition = ConditionalTransition.ELSE;
            newCond = true;
            break;
        case ENDIF:
            transition = ConditionalTransition.ENDIF;
            newCond = true;
            break;
        default:
            ErrorReporter.INTERNAL_ERROR();
            return;
    }
    ConditionalState newState = state.transition(transition);
    if (newState == null) {
        // invalid transition was requested
        TITANMarker marker = new TITANMarker(MessageFormat.format("Directive {0} after {1} is not a valid preprocessor conditional", ppDirective.type.getName(), state.getName()), ppDirective.line, -1, -1, IMarker.SEVERITY_ERROR, IMarker.PRIORITY_NORMAL);
        errors.add(marker);
        return;
    }
    // execute transition
    state = newState;
    if (actCond) {
        prevCond = true;
    }
    actCond = newCond;
}
Also used : TITANMarker(org.eclipse.titan.common.parsers.TITANMarker)

Aggregations

TITANMarker (org.eclipse.titan.common.parsers.TITANMarker)12 Location (org.eclipse.titan.designer.AST.Location)7 IFile (org.eclipse.core.resources.IFile)6 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 CharStream (org.antlr.v4.runtime.CharStream)4 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)4 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 IDocument (org.eclipse.jface.text.IDocument)3 TitanListener (org.eclipse.titan.common.parsers.TitanListener)3 Module (org.eclipse.titan.designer.AST.Module)3 TTCN3Module (org.eclipse.titan.designer.AST.TTCN3.definitions.TTCN3Module)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 SyntacticErrorStorage (org.eclipse.titan.common.parsers.SyntacticErrorStorage)2 CfgLocation (org.eclipse.titan.common.parsers.cfg.CfgLocation)2 ISemanticTITANEditor (org.eclipse.titan.designer.editors.ISemanticTITANEditor)2 File (java.io.File)1