Package st.redline.compiler

Source Code of st.redline.compiler.Parser

/* Redline Smalltalk, Copyright (c) James C. Ladd. All rights reserved. See LICENSE in the root of this distribution. */
package st.redline.compiler;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import st.redline.compiler.ast.Program;

public class Parser {

    private final boolean ignoreParseErrors;

    public Parser(boolean ignoreParseErrors) {
        this.ignoreParseErrors = ignoreParseErrors;
    }

    public Program parse(String preprocessedSource, String className) {
        SmalltalkLexer smalltalkLexer = lexorOn(preprocessedSource);
        SmalltalkParser smalltalkParser = parserUsing(smalltalkLexer);
        try {
            Program program = smalltalkParser.program();
            if (!ignoreParseErrors && smalltalkParser.getNumberOfSyntaxErrors() > 0)
                throw new RuntimeException("Smalltalk Syntax error(s) detected in '" + className + "'.");
            return program;
        } catch (RecognitionException e) {
            throw new RuntimeException(e);
        }
    }

    private SmalltalkParser parserUsing(SmalltalkLexer smalltalkLexer) {
        return new SmalltalkParser(new CommonTokenStream(smalltalkLexer));
    }

    private SmalltalkLexer lexorOn(String sourceCode) {
        return new SmalltalkLexer(new ANTLRStringStream(sourceCode));
    }
}
TOP

Related Classes of st.redline.compiler.Parser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.