1067 while (currentType != TokenTypes::closeBrace && currentType != TokenTypes::eof)
1068 b->statements.
add (parseStatement());
1075 match (TokenTypes::openParen);
1077 while (currentType != TokenTypes::closeParen)
1079 auto paramName = currentValue.toString();
1080 match (TokenTypes::identifier);
1083 if (currentType != TokenTypes::closeParen)
1084 match (TokenTypes::comma);
1087 match (TokenTypes::closeParen);
1088 fo.body.reset (parseBlock());
1093 ExpPtr lhs (parseLogicOperator());
1095 if (matchIf (TokenTypes::question))
return parseTernaryOperator (lhs);
1096 if (matchIf (TokenTypes::assign)) { ExpPtr rhs (parseExpression());
return new Assignment (location, lhs, rhs); }
1105 return lhs.release();
1109 void throwError (
const String&
err)
const { location.throwError (
err); }
1111 template <
typename OpType>
1112 Expression* parseInPlaceOpExpression (ExpPtr& lhs)
1114 ExpPtr rhs (parseExpression());
1121 match (TokenTypes::openBrace);
1122 std::unique_ptr<BlockStatement>
b (parseStatementList());
1123 match (TokenTypes::closeBrace);
1129 if (currentType == TokenTypes::openBrace)
return parseBlock();
1130 if (matchIf (TokenTypes::var))
return parseVar();
1131 if (matchIf (TokenTypes::if_))
return parseIf();
1132 if (matchIf (TokenTypes::while_))
return parseDoOrWhileLoop (
false);
1133 if (matchIf (TokenTypes::do_))
return parseDoOrWhileLoop (
true);
1134 if (matchIf (TokenTypes::for_))
return parseForLoop();
1135 if (matchIf (TokenTypes::return_))
return parseReturn();
1136 if (matchIf (TokenTypes::break_))
return new BreakStatement (location);
1138 if (matchIf (TokenTypes::function))
return parseFunction();
1139 if (matchIf (TokenTypes::semicolon))
return new Statement (location);
1143 if (matchesAny (TokenTypes::openParen, TokenTypes::openBracket))
1144 return matchEndOfStatement (parseFactor());
1146 if (matchesAny (TokenTypes::identifier, TokenTypes::literal, TokenTypes::minus))
1147 return matchEndOfStatement (parseExpression());
1149 throwError (
"Found " + getTokenName (currentType) +
" when expecting a statement");
1153 Expression* matchEndOfStatement (
Expression*
ex) { ExpPtr
e (
ex);
if (currentType != TokenTypes::eof) match (TokenTypes::semicolon);
return e.release(); }
1158 std::unique_ptr<IfStatement>
s (
new IfStatement (location));
1159 match (TokenTypes::openParen);
1160 s->condition.reset (parseExpression());
1161 match (TokenTypes::closeParen);
1162 s->trueBranch.reset (parseStatement());
1163 s->falseBranch.reset (matchIf (TokenTypes::else_) ? parseStatement() :
new Statement (location));
1169 if (matchIf (TokenTypes::semicolon))
1173 matchIf (TokenTypes::semicolon);
1179 std::unique_ptr<VarStatement>
s (
new VarStatement (location));
1180 s->name = parseIdentifier();
1181 s->initialiser.reset (matchIf (TokenTypes::assign) ? parseExpression() :
new Expression (location));
1183 if (matchIf (TokenTypes::comma))
1187 block->statements.
add (parseVar());
1188 return block.release();
1191 match (TokenTypes::semicolon);
1198 auto fn = parseFunctionDefinition (name);
1201 throwError (
"Functions defined at statement-level must have a name");
1209 std::unique_ptr<LoopStatement>
s (
new LoopStatement (location,
false));
1210 match (TokenTypes::openParen);
1211 s->initialiser.reset (parseStatement());
1213 if (matchIf (TokenTypes::semicolon))
1217 s->condition.reset (parseExpression());
1218 match (TokenTypes::semicolon);
1221 if (matchIf (TokenTypes::closeParen))
1222 s->iterator.reset (
new Statement (location));
1225 s->iterator.reset (parseExpression());
1226 match (TokenTypes::closeParen);
1229 s->body.reset (parseStatement());
1233 Statement* parseDoOrWhileLoop (
bool isDoLoop)
1235 std::unique_ptr<LoopStatement>
s (
new LoopStatement (location, isDoLoop));
1236 s->initialiser.reset (
new Statement (location));
1237 s->iterator.reset (
new Statement (location));
1241 s->body.reset (parseBlock());
1242 match (TokenTypes::while_);
1245 match (TokenTypes::openParen);
1246 s->condition.reset (parseExpression());
1247 match (TokenTypes::closeParen);
1250 s->body.reset (parseStatement());
1258 if (currentType == TokenTypes::identifier)
1261 match (TokenTypes::identifier);
1269 if (currentType == TokenTypes::identifier)
1270 functionName = parseIdentifier();
1273 parseFunctionParamsAndBody (*
fo);
1275 return var (
fo.release());
1280 std::unique_ptr<FunctionCall>
s (call);
1281 s->object.reset (function.release());
1282 match (TokenTypes::openParen);
1284 while (currentType != TokenTypes::closeParen)
1286 s->arguments.
add (parseExpression());
1287 if (currentType != TokenTypes::closeParen)
1288 match (TokenTypes::comma);
1291 return matchCloseParen (
s.release());
1298 if (matchIf (TokenTypes::dot))
1299 return parseSuffixes (
new DotOperator (location, input, parseIdentifier()));
1301 if (currentType == TokenTypes::openParen)
1302 return parseSuffixes (parseFunctionCall (
new FunctionCall (location), input));
1304 if (matchIf (TokenTypes::openBracket))
1307 s->object.reset (input.release());
1308 s->index.reset (parseExpression());
1309 match (TokenTypes::closeBracket);
1310 return parseSuffixes (
s.release());
1316 return input.release();
1321 if (currentType == TokenTypes::identifier)
return parseSuffixes (
new UnqualifiedName (location, parseIdentifier()));
1322 if (matchIf (TokenTypes::openParen))
return parseSuffixes (matchCloseParen (parseExpression()));
1323 if (matchIf (TokenTypes::true_))
return parseSuffixes (
new LiteralValue (location, (
int) 1));
1324 if (matchIf (TokenTypes::false_))
return parseSuffixes (
new LiteralValue (location, (
int) 0));
1325 if (matchIf (TokenTypes::null_))
return parseSuffixes (
new LiteralValue (location,
var()));
1326 if (matchIf (TokenTypes::undefined))
return parseSuffixes (
new Expression (location));
1328 if (currentType == TokenTypes::literal)
1330 var v (currentValue); skip();
1334 if (matchIf (TokenTypes::openBrace))
1338 while (currentType != TokenTypes::closeBrace)
1341 match ((currentType == TokenTypes::literal && currentValue.isString())
1342 ? TokenTypes::literal : TokenTypes::identifier);
1343 match (TokenTypes::colon);
1346 e->initialisers.
add (parseExpression());
1348 if (currentType != TokenTypes::closeBrace)
1349 match (TokenTypes::comma);
1352 match (TokenTypes::closeBrace);
1353 return parseSuffixes (
e.release());
1356 if (matchIf (TokenTypes::openBracket))
1360 while (currentType != TokenTypes::closeBracket)
1362 e->values.
add (parseExpression());
1364 if (currentType != TokenTypes::closeBracket)
1365 match (TokenTypes::comma);
1368 match (TokenTypes::closeBracket);
1369 return parseSuffixes (
e.release());
1372 if (matchIf (TokenTypes::function))
1375 var fn = parseFunctionDefinition (name);
1378 throwError (
"Inline functions definitions cannot have a name");
1383 if (matchIf (TokenTypes::new_))
1387 while (matchIf (TokenTypes::dot))
1388 name.reset (
new DotOperator (location, name, parseIdentifier()));
1390 return parseFunctionCall (
new NewOperator (location), name);
1393 throwError (
"Found " + getTokenName (currentType) +
" when expecting an expression");
1397 template <
typename OpType>
1405 template <
typename OpType>
1415 std::unique_ptr<FunctionCall> f (
new FunctionCall (location));
1417 f->arguments.add (parseUnary());
1423 if (matchIf (TokenTypes::minus)) { ExpPtr
a (
new LiteralValue (location, (
int) 0)),
b (parseUnary());
return new SubtractionOp (location,
a,
b); }
1424 if (matchIf (TokenTypes::logicalNot)) { ExpPtr
a (
new LiteralValue (location, (
int) 0)),
b (parseUnary());
return new EqualsOp (location,
a,
b); }
1427 if (matchIf (TokenTypes::typeof_))
return parseTypeof();
1429 return parseFactor();
1434 ExpPtr
a (parseUnary());
1438 if (matchIf (TokenTypes::times)) { ExpPtr
b (parseUnary());
a.reset (
new MultiplyOp (location,
a,
b)); }
1439 else if (matchIf (TokenTypes::divide)) { ExpPtr
b (parseUnary());
a.reset (
new DivideOp (location,
a,
b)); }
1440 else if (matchIf (TokenTypes::modulo)) { ExpPtr
b (parseUnary());
a.reset (
new ModuloOp (location,
a,
b)); }
1449 ExpPtr
a (parseMultiplyDivide());
1453 if (matchIf (TokenTypes::plus)) { ExpPtr
b (parseMultiplyDivide());
a.reset (
new AdditionOp (location,
a,
b)); }
1454 else if (matchIf (TokenTypes::minus)) { ExpPtr
b (parseMultiplyDivide());
a.reset (
new SubtractionOp (location,
a,
b)); }
1463 ExpPtr
a (parseAdditionSubtraction());
1467 if (matchIf (TokenTypes::leftShift)) { ExpPtr
b (parseExpression());
a.reset (
new LeftShiftOp (location,
a,
b)); }
1468 else if (matchIf (TokenTypes::rightShift)) { ExpPtr
b (parseExpression());
a.reset (
new RightShiftOp (location,
a,
b)); }
1469 else if (matchIf (TokenTypes::rightShiftUnsigned)) { ExpPtr
b (parseExpression());
a.reset (
new RightShiftUnsignedOp (location,
a,
b)); }
1478 ExpPtr
a (parseShiftOperator());
1482 if (matchIf (TokenTypes::equals)) { ExpPtr
b (parseShiftOperator());
a.reset (
new EqualsOp (location,
a,
b)); }
1483 else if (matchIf (TokenTypes::notEquals)) { ExpPtr
b (parseShiftOperator());
a.reset (
new NotEqualsOp (location,
a,
b)); }
1484 else if (matchIf (TokenTypes::typeEquals)) { ExpPtr
b (parseShiftOperator());
a.reset (
new TypeEqualsOp (location,
a,
b)); }
1485 else if (matchIf (TokenTypes::typeNotEquals)) { ExpPtr
b (parseShiftOperator());
a.reset (
new TypeNotEqualsOp (location,
a,
b)); }
1486 else if (matchIf (TokenTypes::lessThan)) { ExpPtr
b (parseShiftOperator());
a.reset (
new LessThanOp (location,
a,
b)); }
1487 else if (matchIf (TokenTypes::lessThanOrEqual)) { ExpPtr
b (parseShiftOperator());
a.reset (
new LessThanOrEqualOp (location,
a,
b)); }
1488 else if (matchIf (TokenTypes::greaterThan)) { ExpPtr
b (parseShiftOperator());
a.reset (
new GreaterThanOp (location,
a,
b)); }
1489 else if (matchIf (TokenTypes::greaterThanOrEqual)) { ExpPtr
b (parseShiftOperator());
a.reset (
new GreaterThanOrEqualOp (location,
a,
b)); }
1498 ExpPtr
a (parseComparator());
1502 if (matchIf (TokenTypes::logicalAnd)) { ExpPtr
b (parseComparator());
a.reset (
new LogicalAndOp (location,
a,
b)); }
1503 else if (matchIf (TokenTypes::logicalOr)) { ExpPtr
b (parseComparator());
a.reset (
new LogicalOrOp (location,
a,
b)); }
1504 else if (matchIf (TokenTypes::bitwiseAnd)) { ExpPtr
b (parseComparator());
a.reset (
new BitwiseAndOp (location,
a,
b)); }
1505 else if (matchIf (TokenTypes::bitwiseOr)) { ExpPtr
b (parseComparator());
a.reset (
new BitwiseOrOp (location,
a,
b)); }
1506 else if (matchIf (TokenTypes::bitwiseXor)) { ExpPtr
b (parseComparator());
a.reset (
new BitwiseXorOp (location,
a,
b)); }
1513 Expression* parseTernaryOperator (ExpPtr& condition)
1516 e->condition.reset (condition.release());
1517 e->trueBranch.reset (parseExpression());
1518 match (TokenTypes::colon);
1519 e->falseBranch.reset (parseExpression());