Search in sources :

Example 1 with ProgramTree

use of com.google.javascript.jscomp.parsing.parser.trees.ProgramTree in project closure-compiler by google.

the class ParserRunner method parse.

public static ParseResult parse(StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) {
    // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
    String sourceName = sourceFile.getName();
    try {
        SourceFile file = new SourceFile(sourceName, sourceString);
        boolean keepGoing = config.runMode() == Config.RunMode.KEEP_GOING;
        Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, keepGoing);
        com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = newParserConfig(config);
        Parser p = new Parser(es6config, es6ErrorReporter, file);
        ProgramTree tree = p.parseProgram();
        Node root = null;
        List<Comment> comments = ImmutableList.of();
        FeatureSet features = p.getFeatures();
        if (tree != null && (!es6ErrorReporter.hadError() || keepGoing)) {
            IRFactory factory = IRFactory.transformTree(tree, sourceFile, sourceString, config, errorReporter);
            root = factory.getResultNode();
            features = features.union(factory.getFeatures());
            root.putProp(Node.FEATURE_SET, features);
            if (config.jsDocParsingMode().shouldParseDescriptions()) {
                comments = p.getComments();
            }
        }
        return new ParseResult(root, comments, features, p.getSourceMapURL());
    } catch (Throwable t) {
        throw new RuntimeException("Exception parsing \"" + sourceName + "\"", t);
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) Node(com.google.javascript.rhino.Node) Parser(com.google.javascript.jscomp.parsing.parser.Parser) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile)

Example 2 with ProgramTree

use of com.google.javascript.jscomp.parsing.parser.trees.ProgramTree in project closure-compiler by google.

the class Parser method parseProgram.

// 14 Program
public ProgramTree parseProgram() {
    Timer t = new Timer("Parse Program");
    try {
        SourcePosition start = getTreeStartLocation();
        ImmutableList<ParseTree> sourceElements = parseGlobalSourceElements();
        eat(TokenType.END_OF_FILE);
        t.end();
        return new ProgramTree(getTreeLocation(start), sourceElements, commentRecorder.getComments());
    } catch (StackOverflowError e) {
        reportError("Too deep recursion while parsing");
        return null;
    }
}
Also used : Timer(com.google.javascript.jscomp.parsing.parser.util.Timer) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 3 with ProgramTree

use of com.google.javascript.jscomp.parsing.parser.trees.ProgramTree in project closure-compiler by google.

the class ParserRunner method detectFeatures.

// TODO(sdh): this is less useful if we end up needing the node for library version detection
public static FeatureSet detectFeatures(String sourcePath, String sourceString) {
    SourceFile file = new SourceFile(sourcePath, sourceString);
    ErrorReporter reporter = IRFactory.NULL_REPORTER;
    com.google.javascript.jscomp.parsing.parser.Parser.Config config = newParserConfig(IRFactory.NULL_CONFIG);
    Parser p = new Parser(config, new Es6ErrorReporter(reporter, false), file);
    ProgramTree tree = p.parseProgram();
    StaticSourceFile simpleSourceFile = new SimpleSourceFile(sourcePath, false);
    return IRFactory.detectFeatures(tree, simpleSourceFile, sourceString).union(p.getFeatures());
}
Also used : ErrorReporter(com.google.javascript.rhino.ErrorReporter) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) SimpleSourceFile(com.google.javascript.rhino.SimpleSourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) Parser(com.google.javascript.jscomp.parsing.parser.Parser)

Aggregations

ProgramTree (com.google.javascript.jscomp.parsing.parser.trees.ProgramTree)3 Parser (com.google.javascript.jscomp.parsing.parser.Parser)2 SourceFile (com.google.javascript.jscomp.parsing.parser.SourceFile)2 SimpleSourceFile (com.google.javascript.rhino.SimpleSourceFile)2 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)2 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)1 Timer (com.google.javascript.jscomp.parsing.parser.util.Timer)1 ErrorReporter (com.google.javascript.rhino.ErrorReporter)1 Node (com.google.javascript.rhino.Node)1