JS parser: literals creation

This commit is contained in:
Alexey Tsvetkov
2014-04-16 03:12:25 +04:00
parent 5256a99c54
commit f69398b915
@@ -31,11 +31,13 @@ import java.util.Stack;
*/ */
public class JsParser { public class JsParser {
private JsProgram program;
public static List<JsStatement> parse( public static List<JsStatement> parse(
SourceInfo rootSourceInfo, SourceInfo rootSourceInfo,
JsScope scope, Reader r, boolean insideFunction JsScope scope, Reader r, boolean insideFunction
) throws IOException, JsParserException { ) throws IOException, JsParserException {
return new JsParser().parseImpl(rootSourceInfo, scope, r, insideFunction); return new JsParser(scope.getProgram()).parseImpl(rootSourceInfo, scope, r, insideFunction);
} }
private final Stack<JsScope> scopeStack = new Stack<JsScope>(); private final Stack<JsScope> scopeStack = new Stack<JsScope>();
@@ -45,7 +47,8 @@ public class JsParser {
*/ */
private static final String sourceNameStub = "jsCode"; private static final String sourceNameStub = "jsCode";
private JsParser() { private JsParser(JsProgram program) {
this.program = program;
} }
List<JsStatement> parseImpl( List<JsStatement> parseImpl(
@@ -180,7 +183,7 @@ public class JsParser {
return mapConditional(node); return mapConditional(node);
case TokenStream.STRING: case TokenStream.STRING:
return new JsStringLiteral(node.getString()); return program.getStringLiteral(node.getString());
case TokenStream.NUMBER_INT: case TokenStream.NUMBER_INT:
return mapIntNumber(node); return mapIntNumber(node);
@@ -565,7 +568,7 @@ public class JsParser {
toForIn.setBody(bodyStmt); toForIn.setBody(bodyStmt);
} }
else { else {
toForIn.setBody(new JsEmpty()); toForIn.setBody(program.getEmptyStatement());
} }
return toForIn; return toForIn;
@@ -593,7 +596,7 @@ public class JsParser {
toFor.setBody(bodyStmt); toFor.setBody(bodyStmt);
} }
else { else {
toFor.setBody(new JsEmpty()); toFor.setBody(program.getEmptyStatement());
} }
return toFor; return toFor;
} }
@@ -755,11 +758,11 @@ public class JsParser {
} }
private JsExpression mapIntNumber(Node numberNode) { private JsExpression mapIntNumber(Node numberNode) {
return new JsNumberLiteral.JsIntLiteral((int) numberNode.getDouble()); return program.getNumberLiteral((int) numberNode.getDouble());
} }
private JsExpression mapDoubleNumber(Node numberNode) { private JsExpression mapDoubleNumber(Node numberNode) {
return new JsNumberLiteral.JsDoubleLiteral(numberNode.getDouble()); return program.getNumberLiteral(numberNode.getDouble());
} }
private JsExpression mapObjectLit(Node objLitNode) throws JsParserException { private JsExpression mapObjectLit(Node objLitNode) throws JsParserException {
@@ -823,7 +826,7 @@ public class JsParser {
private JsExpression mapPrimary(Node node) throws JsParserException { private JsExpression mapPrimary(Node node) throws JsParserException {
switch (node.getIntDatum()) { switch (node.getIntDatum()) {
case TokenStream.THIS: case TokenStream.THIS:
return new JsLiteral.JsThisRef(); return JsLiteral.THIS;
case TokenStream.TRUE: case TokenStream.TRUE:
return JsBooleanLiteral.TRUE; return JsBooleanLiteral.TRUE;
@@ -956,7 +959,7 @@ public class JsParser {
else { else {
// When map() returns null, we return an empty statement. // When map() returns null, we return an empty statement.
// //
return new JsEmpty(); return program.getEmptyStatement();
} }
} }