com.google.caja.parser.js.Parser

Here are the examples of the java api class com.google.caja.parser.js.Parser taken from open source projects.

1. MatchExperiments#parse()

Project: caja
File: MatchExperiments.java
public static ParseTreeNode parse(String src) throws Exception {
    MessageContext mc = new MessageContext();
    MessageQueue mq = TestUtil.createTestMessageQueue(mc);
    InputSource is = new InputSource(new URI("file:///no/input/source"));
    CharProducer cp = CharProducer.Factory.create(new StringReader(src), is);
    JsLexer lexer = new JsLexer(cp);
    JsTokenQueue tq = new JsTokenQueue(lexer, is, JsTokenQueue.NO_COMMENT);
    Parser p = new Parser(tq, mq);
    Statement stmt = p.parse();
    p.getTokenQueue().expectEmpty();
    return stmt;
}

2. QuasiBuilder#parse()

Project: caja
File: QuasiBuilder.java
private static ParseTreeNode parse(InputSource inputSource, String sourceText) throws ParseException {
    Parser parser = new Parser(new JsTokenQueue(new JsLexer(CharProducer.Factory.fromString(sourceText, inputSource), true), inputSource), DevNullMessageQueue.singleton(), true);
    Statement topLevelStatement = parser.parse();
    parser.getTokenQueue().expectEmpty();
    return topLevelStatement;
}

3. CajaTestCase#jsExpr()

Project: caja
File: CajaTestCase.java
protected Expression jsExpr(CharProducer cp, boolean quasi) throws ParseException {
    JsLexer lexer = new JsLexer(cp);
    JsTokenQueue tq = new JsTokenQueue(lexer, sourceOf(cp), JsTokenQueue.NO_COMMENT);
    Parser p = new Parser(tq, mq, quasi);
    Expression e = p.parseExpression(true);
    tq.expectEmpty();
    return e;
}

4. CajaTestCase#js()

Project: caja
File: CajaTestCase.java
protected Block js(CharProducer cp, Criterion<Token<JsTokenType>> filt, boolean quasi) throws ParseException {
    JsLexer lexer = new JsLexer(cp);
    JsTokenQueue tq = new JsTokenQueue(lexer, sourceOf(cp), filt);
    Parser p = new Parser(tq, mq, quasi);
    Block b = p.parse();
    tq.expectEmpty();
    return b;
}

5. ProxyServlet#checkIdentifier()

Project: caja
File: ProxyServlet.java
/**
   * Checks whether a string is a JavaScript Identifier.
   */
/* visible for testing */
static boolean checkIdentifier(String candidate) {
    // Using a simple regex is possible if we reject anything but 7-bit ASCII.
    // However, this implementation ensures Caja has a single point of truth
    // regarding what constitutes a JS identifier.
    // TODO(kpreid): Reevaluate whether this is worth the complexity and the
    // runtime dependency on the JS parser now that there is no cajoler. 
    MessageQueue mq = new SimpleMessageQueue();
    Parser parser = new Parser(new JsTokenQueue(new JsLexer(CharProducer.Factory.fromString("var " + candidate + ";", InputSource.UNKNOWN)), InputSource.UNKNOWN), mq);
    ParseTreeNode node;
    try {
        node = parser.parse();
    } catch (ParseException e) {
        return false;
    }
    if (node == null || !mq.getMessages().isEmpty()) {
        return false;
    }
    Map<String, ParseTreeNode> bindings = Maps.newHashMap();
    if (!QuasiBuilder.match("{ var @p; }", node, bindings)) {
        return false;
    }
    if (bindings.size() != 1) {
        return false;
    }
    if (bindings.get("p") == null) {
        return false;
    }
    if (!(bindings.get("p") instanceof Identifier)) {
        return false;
    }
    Identifier p = (Identifier) bindings.get("p");
    if (!candidate.equals(p.getName())) {
        return false;
    }
    return true;
}

6. JsOptimizer#jsExpr()

Project: caja
File: JsOptimizer.java
private static Expression jsExpr(CharProducer cp, MessageQueue mq) throws ParseException {
    Parser p = jsParser(cp, mq);
    Expression e = p.parseExpression(true);
    p.getTokenQueue().expectEmpty();
    return e;
}