1 | package com.google.caja.parser.js; |
2 | |
3 | import java.io.IOException; |
4 | import java.io.PrintWriter; |
5 | import java.io.Reader; |
6 | import java.io.StringReader; |
7 | import java.net.URI; |
8 | |
9 | import com.google.caja.lexer.CharProducer; |
10 | import com.google.caja.lexer.InputSource; |
11 | import com.google.caja.lexer.JsLexer; |
12 | import com.google.caja.lexer.JsTokenQueue; |
13 | import com.google.caja.lexer.ParseException; |
14 | import com.google.caja.reporting.EchoingMessageQueue; |
15 | import com.google.caja.reporting.MessageContext; |
16 | import com.google.caja.reporting.MessageQueue; |
17 | import com.google.javascript.jscomp.jsonml.JsonML; |
18 | |
19 | public class JsonMLParser { |
20 | |
21 | public static JsonML parse(String source) throws IOException, ParseException { |
22 | return parse(new StringReader(source)); |
23 | } |
24 | |
25 | public static JsonML parse(Reader in) throws IOException, ParseException { |
26 | return parse(in, InputSource.UNKNOWN.getUri()); |
27 | } |
28 | |
29 | public static JsonML parse(Reader in, URI source) |
30 | throws IOException, ParseException { |
31 | return parse(CharProducer.Factory.create(in, new InputSource(source))); |
32 | } |
33 | |
34 | public static JsonML parse(CharProducer cp) throws ParseException { |
35 | JsLexer lexer = new JsLexer(cp, false); |
36 | JsTokenQueue tq = new JsTokenQueue( |
37 | lexer, cp.getCurrentPosition().source()); |
38 | tq.setInputRange(cp.filePositionForOffsets(cp.getOffset(), cp.getLimit())); |
39 | MessageQueue mq = new EchoingMessageQueue( |
40 | new PrintWriter(System.err), new MessageContext()); |
41 | Parser p = new Parser(tq, mq); |
42 | Block program = p.parse(); |
43 | tq.expectEmpty(); |
44 | return program.toJsonMLAsProgram(); |
45 | } |
46 | } |