Example usage for org.eclipse.jdt.internal.compiler.parser RecoveryScanner RecoveryScanner

List of usage examples for org.eclipse.jdt.internal.compiler.parser RecoveryScanner RecoveryScanner

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.parser RecoveryScanner RecoveryScanner.

Prototype

public RecoveryScanner(Scanner scanner, RecoveryScannerData data) 

Source Link

Usage

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public CompilationUnit convert(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration unit,
        char[] source) {
    try {/*from w ww.  jav a 2s. c o m*/
        if (unit.compilationResult.recoveryScannerData != null) {
            RecoveryScanner recoveryScanner = new RecoveryScanner(this.scanner,
                    unit.compilationResult.recoveryScannerData.removeUnused());
            this.scanner = recoveryScanner;
            this.docParser.scanner = this.scanner;
        }
        this.compilationUnitSource = source;
        this.compilationUnitSourceLength = source.length;
        this.scanner.setSource(source, unit.compilationResult);
        // GROOVY start
        /* old {
        CompilationUnit compilationUnit = new CompilationUnit(this.ast);
         } new */
        CompilationUnit compilationUnit = unit.getSpecialDomCompilationUnit(this.ast);
        if (compilationUnit == null) {
            compilationUnit = new CompilationUnit(this.ast);
        }
        // GROOVY end
        compilationUnit.setStatementsRecoveryData(unit.compilationResult.recoveryScannerData);

        // Parse comments
        int[][] comments = unit.comments;
        if (comments != null) {
            buildCommentsTable(compilationUnit, comments);
        }

        // handle the package declaration immediately
        // There is no node corresponding to the package declaration
        if (this.resolveBindings) {
            recordNodes(compilationUnit, unit);
        }
        if (unit.currentPackage != null) {
            PackageDeclaration packageDeclaration = convertPackage(unit);
            compilationUnit.setPackage(packageDeclaration);
        }
        org.eclipse.jdt.internal.compiler.ast.ImportReference[] imports = unit.imports;
        if (imports != null) {
            int importLength = imports.length;
            for (int i = 0; i < importLength; i++) {
                compilationUnit.imports().add(convertImport(imports[i]));
            }
        }

        org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = unit.types;
        if (types != null) {
            int typesLength = types.length;
            for (int i = 0; i < typesLength; i++) {
                org.eclipse.jdt.internal.compiler.ast.TypeDeclaration declaration = types[i];
                if (CharOperation.equals(declaration.name, TypeConstants.PACKAGE_INFO_NAME)) {
                    continue;
                }
                ASTNode type = convert(declaration);
                if (type == null) {
                    compilationUnit.setFlags(compilationUnit.getFlags() | ASTNode.MALFORMED);
                } else {
                    compilationUnit.types().add(type);
                }
            }
        }
        compilationUnit.setSourceRange(unit.sourceStart, unit.sourceEnd - unit.sourceStart + 1);

        int problemLength = unit.compilationResult.problemCount;
        if (problemLength != 0) {
            CategorizedProblem[] resizedProblems = null;
            final CategorizedProblem[] problems = unit.compilationResult.getProblems();
            final int realProblemLength = problems.length;
            if (realProblemLength == problemLength) {
                resizedProblems = problems;
            } else {
                System.arraycopy(problems, 0, (resizedProblems = new CategorizedProblem[realProblemLength]), 0,
                        realProblemLength);
            }
            ASTSyntaxErrorPropagator syntaxErrorPropagator = new ASTSyntaxErrorPropagator(resizedProblems);
            compilationUnit.accept(syntaxErrorPropagator);
            ASTRecoveryPropagator recoveryPropagator = new ASTRecoveryPropagator(resizedProblems,
                    unit.compilationResult.recoveryScannerData);
            compilationUnit.accept(recoveryPropagator);
            compilationUnit.setProblems(resizedProblems);
        }
        if (this.resolveBindings) {
            lookupForScopes();
        }
        compilationUnit.initCommentMapper(this.scanner);
        return compilationUnit;
    } catch (IllegalArgumentException e) {
        StringBuffer message = new StringBuffer("Exception occurred during compilation unit conversion:"); //$NON-NLS-1$
        String lineDelimiter = Util.findLineSeparator(source);
        if (lineDelimiter == null)
            lineDelimiter = System.getProperty("line.separator");//$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(
                "----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
        message.append(lineDelimiter);
        message.append(source);
        message.append(lineDelimiter);
        message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$
        Util.log(e, message.toString());
        throw e;
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void parse() {
    if (DEBUG)//from ww  w .  j av a2s  . co m
        System.out.println("-- ENTER INSIDE PARSE METHOD --"); //$NON-NLS-1$

    if (DEBUG_AUTOMATON) {
        System.out.println("- Start --------------------------------"); //$NON-NLS-1$
    }

    boolean isDietParse = this.diet;
    int oldFirstToken = getFirstToken();
    this.hasError = false;

    this.hasReportedError = false;
    int act = START_STATE;
    this.stateStackTop = -1;
    this.currentToken = getFirstToken();
    ProcessTerminals: for (;;) {
        int stackLength = this.stack.length;
        if (++this.stateStackTop >= stackLength) {
            System.arraycopy(this.stack, 0, this.stack = new int[stackLength + StackIncrement], 0, stackLength);
        }
        this.stack[this.stateStackTop] = act;

        act = tAction(act, this.currentToken);
        if (act == ERROR_ACTION || this.restartRecovery) {

            if (DEBUG_AUTOMATON) {
                if (this.restartRecovery) {
                    System.out.println("Restart      - "); //$NON-NLS-1$
                } else {
                    System.out.println("Error        - "); //$NON-NLS-1$
                }
            }

            int errorPos = this.scanner.currentPosition - 1;
            if (!this.hasReportedError) {
                this.hasError = true;
            }
            int previousToken = this.currentToken;
            if (resumeOnSyntaxError()) {
                if (act == ERROR_ACTION && previousToken != 0)
                    this.lastErrorEndPosition = errorPos;
                act = START_STATE;
                this.stateStackTop = -1;
                this.currentToken = getFirstToken();
                continue ProcessTerminals;
            }
            act = ERROR_ACTION;
            break ProcessTerminals;
        }
        if (act <= NUM_RULES) {
            this.stateStackTop--;

            if (DEBUG_AUTOMATON) {
                System.out.print("Reduce       - "); //$NON-NLS-1$
            }

        } else if (act > ERROR_ACTION) { /* shift-reduce */
            consumeToken(this.currentToken);
            if (this.currentElement != null) {
                boolean oldValue = this.recordStringLiterals;
                this.recordStringLiterals = false;
                recoveryTokenCheck();
                this.recordStringLiterals = oldValue;
            }
            try {
                this.currentToken = this.scanner.getNextToken();
            } catch (InvalidInputException e) {
                if (!this.hasReportedError) {
                    problemReporter().scannerError(this, e.getMessage());
                    this.hasReportedError = true;
                }
                this.lastCheckPoint = this.scanner.currentPosition;
                this.currentToken = 0;
                this.restartRecovery = true;
            }
            if (this.statementRecoveryActivated) {
                jumpOverType();
            }
            act -= ERROR_ACTION;

            if (DEBUG_AUTOMATON) {
                System.out.print("Shift/Reduce - (" + name[terminal_index[this.currentToken]] + ") "); //$NON-NLS-1$  //$NON-NLS-2$
            }

        } else {
            if (act < ACCEPT_ACTION) { /* shift */
                consumeToken(this.currentToken);
                if (this.currentElement != null) {
                    boolean oldValue = this.recordStringLiterals;
                    this.recordStringLiterals = false;
                    recoveryTokenCheck();
                    this.recordStringLiterals = oldValue;
                }
                try {
                    this.currentToken = this.scanner.getNextToken();
                } catch (InvalidInputException e) {
                    if (!this.hasReportedError) {
                        problemReporter().scannerError(this, e.getMessage());
                        this.hasReportedError = true;
                    }
                    this.lastCheckPoint = this.scanner.currentPosition;
                    this.currentToken = 0;
                    this.restartRecovery = true;
                }
                if (this.statementRecoveryActivated) {
                    jumpOverType();
                }
                if (DEBUG_AUTOMATON) {
                    System.out.println("Shift        - (" + name[terminal_index[this.currentToken]] + ")"); //$NON-NLS-1$  //$NON-NLS-2$
                }
                continue ProcessTerminals;
            }
            break ProcessTerminals;
        }

        // ProcessNonTerminals :
        do { /* reduce */

            if (DEBUG_AUTOMATON) {
                System.out.println(name[non_terminal_index[lhs[act]]]);
            }

            consumeRule(act);
            this.stateStackTop -= (rhs[act] - 1);
            act = ntAction(this.stack[this.stateStackTop], lhs[act]);

            if (DEBUG_AUTOMATON) {
                if (act <= NUM_RULES) {
                    System.out.print("             - "); //$NON-NLS-1$
                }
            }

        } while (act <= NUM_RULES);

        if (DEBUG_AUTOMATON) {
            System.out.println("----------------------------------------"); //$NON-NLS-1$
        }
    }

    if (DEBUG_AUTOMATON) {
        System.out.println("- End ----------------------------------"); //$NON-NLS-1$
    }

    endParse(act);
    // record all nls tags in the corresponding compilation unit
    final NLSTag[] tags = this.scanner.getNLSTags();
    if (tags != null) {
        this.compilationUnit.nlsTags = tags;
    }

    this.scanner.checkNonExternalizedStringLiterals = false;
    if (this.reportSyntaxErrorIsRequired && this.hasError && !this.statementRecoveryActivated) {
        if (!this.options.performStatementsRecovery) {
            reportSyntaxErrors(isDietParse, oldFirstToken);
        } else {
            RecoveryScannerData data = this.referenceContext.compilationResult().recoveryScannerData;

            if (this.recoveryScanner == null) {
                this.recoveryScanner = new RecoveryScanner(this.scanner, data);
            } else {
                this.recoveryScanner.setData(data);
            }

            this.recoveryScanner.setSource(this.scanner.source);
            this.recoveryScanner.lineEnds = this.scanner.lineEnds;
            this.recoveryScanner.linePtr = this.scanner.linePtr;

            reportSyntaxErrors(isDietParse, oldFirstToken);

            if (data == null) {
                this.referenceContext.compilationResult().recoveryScannerData = this.recoveryScanner.getData();
            }

            if (this.methodRecoveryActivated && this.options.performStatementsRecovery) {
                this.methodRecoveryActivated = false;
                recoverStatements();
                this.methodRecoveryActivated = true;

                this.lastAct = ERROR_ACTION;
            }
        }
    }

    if (DEBUG)
        System.out.println("-- EXIT FROM PARSE METHOD --"); //$NON-NLS-1$
}