JS parser: auto-reformat JsParser.java

This commit is contained in:
Alexey Tsvetkov
2014-11-10 22:23:29 +03:00
parent 54dbdce908
commit df41400fe5
@@ -84,13 +84,17 @@ import java.util.Stack;
*/ */
public class JsParser { public class JsParser {
public static List<JsStatement> parse(SourceInfo rootSourceInfo, public static List<JsStatement> parse(
JsScope scope, Reader r) throws IOException, JsParserException { SourceInfo rootSourceInfo,
JsScope scope, Reader r
) throws IOException, JsParserException {
return new JsParser().parseImpl(rootSourceInfo, scope, r); return new JsParser().parseImpl(rootSourceInfo, scope, r);
} }
public static void parseInto(SourceInfo rootSourceInfo, JsScope scope, public static void parseInto(
JsBlock block, Reader r) throws IOException, JsParserException { SourceInfo rootSourceInfo, JsScope scope,
JsBlock block, Reader r
) throws IOException, JsParserException {
List<JsStatement> childStmts = parse(rootSourceInfo, scope, r); List<JsStatement> childStmts = parse(rootSourceInfo, scope, r);
List<JsStatement> parentStmts = block.getStatements(); List<JsStatement> parentStmts = block.getStatements();
parentStmts.addAll(childStmts); parentStmts.addAll(childStmts);
@@ -102,8 +106,10 @@ public class JsParser {
private JsParser() { private JsParser() {
} }
List<JsStatement> parseImpl(final SourceInfo rootSourceInfo, JsScope scope, List<JsStatement> parseImpl(
Reader r) throws JsParserException, IOException { final SourceInfo rootSourceInfo, JsScope scope,
Reader r
) throws JsParserException, IOException {
// Create a custom error handler so that we can throw our own exceptions. // Create a custom error handler so that we can throw our own exceptions.
Context.enter().setErrorReporter(new ErrorReporter() { Context.enter().setErrorReporter(new ErrorReporter() {
@Override @Override
@@ -113,8 +119,10 @@ public class JsParser {
} }
@Override @Override
public EvaluatorException runtimeError(String msg, String loc, int ln, public EvaluatorException runtimeError(
String src, int col) { String msg, String loc, int ln,
String src, int col
) {
// Never called, but just in case. // Never called, but just in case.
throw new UncheckedJsParserException(new JsParserException(msg, ln, throw new UncheckedJsParserException(new JsParserException(msg, ln,
src, col, rootSourceInfo.getFileName())); src, col, rootSourceInfo.getFileName()));
@@ -138,9 +146,11 @@ public class JsParser {
List<JsStatement> stmts = mapStatements(topNode); List<JsStatement> stmts = mapStatements(topNode);
popScope(); popScope();
return stmts; return stmts;
} catch (UncheckedJsParserException e) { }
catch (UncheckedJsParserException e) {
throw e.getParserException(); throw e.getParserException();
} finally { }
finally {
Context.exit(); Context.exit();
} }
} }
@@ -377,7 +387,8 @@ public class JsParser {
JsStringLiteral lit = (JsStringLiteral) unknown; JsStringLiteral lit = (JsStringLiteral) unknown;
String litName = lit.getValue(); String litName = lit.getValue();
return new JsNameRef(makeSourceInfo(nameRefNode), litName); return new JsNameRef(makeSourceInfo(nameRefNode), litName);
} else { }
else {
throw createParserException("Expecting a name reference", nameRefNode); throw createParserException("Expecting a name reference", nameRefNode);
} }
} }
@@ -451,7 +462,8 @@ public class JsParser {
Node fromLabel = breakNode.getFirstChild(); Node fromLabel = breakNode.getFirstChild();
if (fromLabel != null) { if (fromLabel != null) {
return new JsBreak(makeSourceInfo(breakNode), mapName(fromLabel)); return new JsBreak(makeSourceInfo(breakNode), mapName(fromLabel));
} else { }
else {
return new JsBreak(makeSourceInfo(breakNode)); return new JsBreak(makeSourceInfo(breakNode));
} }
} }
@@ -497,7 +509,8 @@ public class JsParser {
Node fromLabel = contNode.getFirstChild(); Node fromLabel = contNode.getFirstChild();
if (fromLabel != null) { if (fromLabel != null) {
return new JsContinue(makeSourceInfo(contNode), mapName(fromLabel)); return new JsContinue(makeSourceInfo(contNode), mapName(fromLabel));
} else { }
else {
return new JsContinue(makeSourceInfo(contNode)); return new JsContinue(makeSourceInfo(contNode));
} }
} }
@@ -514,10 +527,12 @@ public class JsParser {
if (to instanceof JsNameRef) { if (to instanceof JsNameRef) {
return new JsPrefixOperation(makeSourceInfo(node), return new JsPrefixOperation(makeSourceInfo(node),
JsUnaryOperator.DELETE, to); JsUnaryOperator.DELETE, to);
} else if (to instanceof JsArrayAccess) { }
else if (to instanceof JsArrayAccess) {
return new JsPrefixOperation(makeSourceInfo(node), return new JsPrefixOperation(makeSourceInfo(node),
JsUnaryOperator.DELETE, to); JsUnaryOperator.DELETE, to);
} else { }
else {
throw createParserException( throw createParserException(
"'delete' can only operate on property names and array elements", "'delete' can only operate on property names and array elements",
from); from);
@@ -534,7 +549,8 @@ public class JsParser {
if (isWhile) { if (isWhile) {
fromTestExpr = ifNode.getFirstChild(); fromTestExpr = ifNode.getFirstChild();
fromBody = ifNode.getFirstChild().getNext(); fromBody = ifNode.getFirstChild().getNext();
} else { }
else {
fromBody = ifNode.getFirstChild(); fromBody = ifNode.getFirstChild();
fromTestExpr = ifNode.getFirstChild().getNext(); fromTestExpr = ifNode.getFirstChild().getNext();
} }
@@ -556,7 +572,8 @@ public class JsParser {
// //
if (isWhile) { if (isWhile) {
return new JsWhile(info, toTestExpr, toBody); return new JsWhile(info, toTestExpr, toBody);
} else { }
else {
return new JsDoWhile(info, toTestExpr, toBody); return new JsDoWhile(info, toTestExpr, toBody);
} }
} }
@@ -597,7 +614,8 @@ public class JsParser {
JsNode unknown = map(exprNode); JsNode unknown = map(exprNode);
if (unknown instanceof JsExpression) { if (unknown instanceof JsExpression) {
return (JsExpression) unknown; return (JsExpression) unknown;
} else { }
else {
throw createParserException("Expecting an expression", exprNode); throw createParserException("Expecting an expression", exprNode);
} }
} }
@@ -638,7 +656,8 @@ public class JsParser {
// //
toForIn.setIterExpr(mapOptionalExpression(fromIterInit)); toForIn.setIterExpr(mapOptionalExpression(fromIterInit));
} }
} else { }
else {
// An unnamed iterator var. // An unnamed iterator var.
// //
toForIn = new JsForIn(info); toForIn = new JsForIn(info);
@@ -651,12 +670,14 @@ public class JsParser {
JsStatement bodyStmt = mapStatement(fromBody); JsStatement bodyStmt = mapStatement(fromBody);
if (bodyStmt != null) { if (bodyStmt != null) {
toForIn.setBody(bodyStmt); toForIn.setBody(bodyStmt);
} else { }
else {
toForIn.setBody(new JsEmpty(info)); toForIn.setBody(new JsEmpty(info));
} }
return toForIn; return toForIn;
} else { }
else {
// Regular ol' for loop. // Regular ol' for loop.
// //
JsFor toFor = new JsFor(info); JsFor toFor = new JsFor(info);
@@ -666,7 +687,8 @@ public class JsParser {
if (initThingy != null) { if (initThingy != null) {
if (initThingy instanceof JsVars) { if (initThingy instanceof JsVars) {
toFor.setInitVars((JsVars) initThingy); toFor.setInitVars((JsVars) initThingy);
} else { }
else {
assert (initThingy instanceof JsExpression); assert (initThingy instanceof JsExpression);
toFor.setInitExpr((JsExpression) initThingy); toFor.setInitExpr((JsExpression) initThingy);
} }
@@ -677,7 +699,8 @@ public class JsParser {
JsStatement bodyStmt = mapStatement(fromBody); JsStatement bodyStmt = mapStatement(fromBody);
if (bodyStmt != null) { if (bodyStmt != null) {
toFor.setBody(bodyStmt); toFor.setBody(bodyStmt);
} else { }
else {
toFor.setBody(new JsEmpty(info)); toFor.setBody(new JsEmpty(info));
} }
return toFor; return toFor;
@@ -746,7 +769,8 @@ public class JsParser {
JsNameRef toNameRef; JsNameRef toNameRef;
if (from2 != null) { if (from2 != null) {
toNameRef = mapAsPropertyNameRef(from2); toNameRef = mapAsPropertyNameRef(from2);
} else { }
else {
// Special properties don't have a second expression. // Special properties don't have a second expression.
// //
Object obj = getPropNode.getProp(Node.SPECIAL_PROP_PROP); Object obj = getPropNode.getProp(Node.SPECIAL_PROP_PROP);
@@ -880,7 +904,8 @@ public class JsParser {
if (unknown != null) { if (unknown != null) {
if (unknown instanceof JsExpression) { if (unknown instanceof JsExpression) {
return (JsExpression) unknown; return (JsExpression) unknown;
} else { }
else {
throw createParserException("Expecting an expression or null", exprNode); throw createParserException("Expecting an expression or null", exprNode);
} }
} }
@@ -1030,12 +1055,15 @@ public class JsParser {
if (unknown != null) { if (unknown != null) {
if (unknown instanceof JsStatement) { if (unknown instanceof JsStatement) {
return (JsStatement) unknown; return (JsStatement) unknown;
} else if (unknown instanceof JsExpression) { }
else if (unknown instanceof JsExpression) {
return ((JsExpression) unknown).makeStmt(); return ((JsExpression) unknown).makeStmt();
} else { }
else {
throw createParserException("Expecting a statement", nodeStmt); throw createParserException("Expecting a statement", nodeStmt);
} }
} else { }
else {
// When map() returns null, we return an empty statement. // When map() returns null, we return an empty statement.
// //
return new JsEmpty(makeSourceInfo(nodeStmt)); return new JsEmpty(makeSourceInfo(nodeStmt));
@@ -1049,7 +1077,8 @@ public class JsParser {
JsStatement stmt = mapStatement(curr); JsStatement stmt = mapStatement(curr);
if (stmt != null) { if (stmt != null) {
stmts.add(stmt); stmts.add(stmt);
} else { }
else {
// When mapStatement() returns null, we just ignore it. // When mapStatement() returns null, we just ignore it.
// //
} }
@@ -1094,7 +1123,8 @@ public class JsParser {
// Attach the case to the switch. // Attach the case to the switch.
// //
toSwitch.getCases().add(toCase); toSwitch.getCases().add(toCase);
} else { }
else {
// This should be the only default statement. // This should be the only default statement.
// If more than one is present, we keep the last one. // If more than one is present, we keep the last one.
// //
@@ -1209,7 +1239,8 @@ public class JsParser {
case TokenStream.ADD: case TokenStream.ADD:
if (unOp.getFirstChild().getType() != TokenStream.NUMBER) { if (unOp.getFirstChild().getType() != TokenStream.NUMBER) {
return mapPrefixOperation(JsUnaryOperator.POS, unOp); return mapPrefixOperation(JsUnaryOperator.POS, unOp);
} else { }
else {
// Pretend we didn't see it. // Pretend we didn't see it.
return mapExpression(unOp.getFirstChild()); return mapExpression(unOp.getFirstChild());
} }