com.google.caja.lexer.CharProducer

Here are the examples of the java api class com.google.caja.lexer.CharProducer taken from open source projects.

1. SnippetProducerTest#testGetSnippetTwoPos()

Project: caja
Source File: SnippetProducerTest.java
View license
public final void testGetSnippetTwoPos() {
    CharProducer cp = CharProducer.Factory.create(new StringReader(F1_TEXT), f1);
    cp.consume(cp.length());
    Message msg = new Message(TestMessageType.TWO, FilePosition.instance(f2, 1, 1, 1, 2), // Starts on a newline to test that we use the line with text.
    cp.getSourceBreaks(0).toFilePosition(1 + F1_TEXT.indexOf("\r\nf1 line 3"), 1 + F1_TEXT.indexOf(" line 3")));
    assertEquals(("f2:1: f2      line 1\n" + "      ^^\n" + "f1:3: f1 line 3\n" + "      ^^"), s.getSnippet(msg));
}

2. DomParser#makeTokenQueue()

Project: caja
Source File: DomParser.java
View license
/**
   * Creates a TokenQueue suitable for this class's parse methods.
   * @param pos the position of the first character on in.
   * @param in closed by this method.
   * @param asXml true to parse as XML, false as HTML.
   * @throws IOException when in raises an exception during read.
   */
public static TokenQueue<HtmlTokenType> makeTokenQueue(FilePosition pos, Reader in, boolean asXml, boolean wantsComments) throws IOException {
    CharProducer cp = CharProducer.Factory.create(in, pos);
    HtmlLexer lexer = new HtmlLexer(cp);
    lexer.setTreatedAsXml(asXml);
    return new TokenQueue<HtmlTokenType>(lexer, pos.source(), wantsComments ? Criterion.Factory.<Token<HtmlTokenType>>optimist() : SKIP_COMMENTS);
}

3. DomParserTest#benchmark()

Project: caja
Source File: DomParserTest.java
View license
private int benchmark(int nRuns) throws IOException, ParseException {
    CharProducer testInput = fromResource("amazon.com.html");
    InputSource is = testInput.getSourceBreaks(0).source();
    MessageQueue mq = DevNullMessageQueue.singleton();
    long t0 = System.nanoTime();
    for (int i = nRuns; --i >= 0; ) {
        HtmlLexer lexer = new HtmlLexer(testInput.clone());
        lexer.setTreatedAsXml(false);
        TokenQueue<HtmlTokenType> tq = new TokenQueue<HtmlTokenType>(lexer, is, DomParser.SKIP_COMMENTS);
        DomParser p = new DomParser(tq, false, mq);
        p.setNeedsDebugData(false);
        p.parseDocument();
    }
    return (int) ((((double) (System.nanoTime() - t0)) / nRuns) / 1e3);
}

4. MatchExperiments#parse()

Project: caja
Source File: MatchExperiments.java
View license
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;
}

5. BuildServiceImplementation#loadEnvJsonFile()

View license
private static void loadEnvJsonFile(File f, JsOptimizer op, MessageQueue mq) {
    CharProducer cp;
    try {
        cp = read(f);
    } catch (IOException ex) {
        mq.addMessage(MessageType.IO_ERROR, MessagePart.Factory.valueOf(ex.toString()));
        return;
    }
    ObjectConstructor envJson;
    try {
        Parser p = parser(cp, mq);
        // TODO(mikesamuel): limit to JSON
        Expression e = p.parseExpression(true);
        p.getTokenQueue().expectEmpty();
        if (!(e instanceof ObjectConstructor)) {
            mq.addMessage(MessageType.IO_ERROR, MessagePart.Factory.valueOf("Invalid JSON in " + f));
            return;
        }
        envJson = (ObjectConstructor) e;
    } catch (ParseException ex) {
        ex.toMessageQueue(mq);
        return;
    }
    op.setEnvJson(envJson);
}