Get string literals working again.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
21 #include "../FixedArray.h"
23 #include "../Strings.h"
24 #include "../TypeManager.h"
26 #include "../Heap-inl.h"
30 Parser::Parser(Zone *zone, CompileContext &cc, const char *stream, size_t length)
34 scanner_(cc, stream, length),
37 unbound_ = new (pool_) PoolList<NameProxy *>();
38 imports_ = new (pool_) PoolList<ImportStatement *>();
42 Parser::peek(TokenKind kind)
44 return scanner_.peek() == kind;
48 Parser::match(TokenKind kind)
57 Parser::matchSemicolon()
59 return expect(TOK_SEMICOLON);
63 Parser::expect(TokenKind kind)
65 TokenKind got = scanner_.scan();
68 cc_.reportError(scanner_.begin(), Message_WrongToken, TokenNames[kind], TokenNames[got]);
73 Parser::matchName(String **name, TokenKind kind)
75 assert(kind == TOK_NAME || kind == TOK_LABEL);
79 *name = zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
86 if (!expect(TOK_NAME))
88 return zone_->makeSymbol(scanner_.literal(), scanner_.literal_length());
94 TokenKind kind = scanner_.peek();
96 case TOK_FLOAT_LITERAL:
99 double value = Scanner::ParseDouble(scanner_.literal());
100 return new (pool_) FloatLiteral(scanner_.begin(), value);
103 case TOK_HEX_LITERAL:
106 int value = strtol(scanner_.literal(), NULL, 16);
107 return new (pool_) IntegerLiteral(scanner_.begin(), value);
110 case TOK_INTEGER_LITERAL:
113 int value = atoi(scanner_.literal());
114 return new (pool_) IntegerLiteral(scanner_.begin(), value);
120 return new (pool_) BooleanLiteral(scanner_.begin(), kind);
122 case TOK_STRING_LITERAL:
125 Local<String> string(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
126 return new (pool_) StringLiteral(scanner_.begin(), string);
131 if (kind != TOK_ERROR)
132 cc_.reportError(scanner_.begin(), Message_ExpectedExpression, TokenNames[kind]);
139 Parser::fixedArrayLiteral()
141 SourcePosition pos = scanner_.begin();
143 ExpressionList *list = new (pool_) ExpressionList();
145 // { } is an illegal array literal.
146 if (list->length() > 0 && match(TOK_LBRACE))
149 Expression *expr = NULL;
150 if (peek(TOK_LBRACE))
151 expr = fixedArrayLiteral();
157 if (!list->append(expr))
160 if (!match(TOK_COMMA)) {
161 if (!expect(TOK_RBRACE))
167 return new (pool_) ArrayLiteral(pos, TOK_LBRACE, list);
173 switch (scanner_.peek()) {
177 Expression *expr = expression();
180 if (!expect(TOK_RPAREN))
187 Local<String> name(zone_, expectName());
190 return new (pool_) NameProxy(scanner_.begin(), name);
198 // Parses only old-style declarations.
200 Parser::typeAndName(Expression **type, String **namep)
202 if (match(TOK_LABEL)) {
203 // Old-style declaration:
205 Local<String> name(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
209 *type = new (pool_) NameProxy(scanner_.begin(), name);
210 *namep = expectName();
215 *namep = expectName();
220 // Parses both old and new style declarations.
222 Parser::fullTypeAndName(Expression **type, String **namep, DeclKind kind)
225 // NewDecl ::= Type Ident
226 // OldDecl ::= Label? (Ident | "...")
228 // Label ::= Ident ":"
230 if (match(TOK_LABEL)) {
231 // We something of the form:
234 Local<String> name(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
238 *type = new (pool_) NameProxy(scanner_.begin(), name);
239 if (kind == DeclArg && match(TOK_ELLIPSES)) {
242 if ((*namep = expectName()) == NULL)
248 Local<String> name(zone_, expectName());
252 if (peek(TOK_NAME) || (kind == DeclArg && peek(TOK_ELLIPSES))) {
253 // We got something of the form:
255 *type = new (pool_) NameProxy(scanner_.begin(), name);
256 if (peek(TOK_NAME)) {
257 if ((*namep = expectName()) == NULL)
265 // We got something of the form:
273 Parser::call(Expression *callee)
275 ExpressionList *arguments = new (pool_) ExpressionList();
277 SourcePosition pos = scanner_.begin();
280 if (!match(TOK_RPAREN)) {
282 Expression *expr = expression();
286 if (!arguments->append(expr))
289 if (!match(TOK_COMMA))
293 if (!expect(TOK_RPAREN))
297 return new (pool_) CallExpression(pos, callee, arguments);
301 Parser::index(Expression *left)
303 expect(TOK_LBRACKET);
305 SourcePosition pos = scanner_.begin();
306 Expression *expr = expression();
310 if (!expect(TOK_RBRACKET))
313 return new (pool_) IndexExpression(pos, left, expr);
317 Parser::field(Expression *left)
321 SourcePosition pos = scanner_.begin();
322 Local<String> name(zone_, expectName());
326 return new (pool_) FieldExpression(pos, left, name);
332 Expression *expr = prefix();
337 switch (scanner_.peek()) {
339 if ((expr = call(expr)) == NULL)
344 if ((expr = index(expr)) == NULL)
349 if ((expr = field(expr)) == NULL)
362 TokenKind token = scanner_.peek();
364 SourcePosition pos = scanner_.begin();
370 Expression *expr = unary();
373 return new (pool_) IncDecExpression(pos, token, expr, false);
381 Expression *expr = unary();
384 return new (pool_) UnaryExpression(pos, token, expr);
390 if (!expect(TOK_LPAREN))
392 Expression *expr = unary();
395 if (!expect(TOK_RPAREN))
397 return new (pool_) UnaryExpression(pos, token, expr);
403 Local<String> tag(zone_, zone_->makeSymbol(scanner_.literal(), scanner_.literal_length()));
407 NameProxy *proxy = new (pool_) NameProxy(scanner_.begin(), tag);
409 Expression *expr = unary();
413 return new (pool_) UnaryExpression(pos, TOK_LABEL, expr, proxy);
420 Expression *expr = primary();
422 token = scanner_.peek();
423 if (token == TOK_INCREMENT || token == TOK_DECREMENT) {
425 return new (pool_) IncDecExpression(pos, token, expr, true);
432 Parser::multiplication()
434 Expression *left = unary();
436 if (!match(TOK_SLASH) && !match(TOK_STAR) && !match(TOK_PERCENT))
438 SourcePosition pos = scanner_.begin();
439 TokenKind kind = scanner_.last();
440 Expression *right = unary();
443 left = new (pool_) BinaryExpression(pos, kind, left, right);
451 Expression *left = multiplication();
453 if (!match(TOK_PLUS) && !match(TOK_MINUS))
455 SourcePosition pos = scanner_.begin();
456 TokenKind kind = scanner_.last();
457 Expression *right = multiplication();
460 left = new (pool_) BinaryExpression(pos, kind, left, right);
468 Expression *left = addition();
470 if (!match(TOK_SHL) && !match(TOK_SHR) && !match(TOK_USHR))
472 SourcePosition pos = scanner_.begin();
473 TokenKind kind = scanner_.last();
474 Expression *right = addition();
477 left = new (pool_) BinaryExpression(pos, kind, left, right);
485 Expression *left = shift();
487 if (!match(TOK_BITAND))
489 SourcePosition pos = scanner_.begin();
490 Expression *right = shift();
493 left = new (pool_) BinaryExpression(pos, TOK_BITAND, left, right);
501 Expression *left = bitand_();
503 if (!match(TOK_BITXOR))
505 SourcePosition pos = scanner_.begin();
506 Expression *right = shift();
509 left = new (pool_) BinaryExpression(pos, TOK_BITXOR, left, right);
517 Expression *left = bitxor();
519 if (!match(TOK_BITOR))
521 SourcePosition pos = scanner_.begin();
522 Expression *right = bitxor();
525 left = new (pool_) BinaryExpression(pos, TOK_BITOR, left, right);
533 Expression *left = bitor_();
535 TokenKind kind = scanner_.peek();
536 if (kind < TOK_LT || kind > TOK_GE)
539 SourcePosition pos = scanner_.begin();
540 Expression *right = shift();
543 left = new (pool_) BinaryExpression(pos, kind, left, right);
551 Expression *left = relational();
553 if (!match(TOK_EQUALS) && !match(TOK_NOTEQUALS))
555 TokenKind kind = scanner_.last();
556 SourcePosition pos = scanner_.begin();
557 Expression *right = relational();
560 left = new (pool_) BinaryExpression(pos, kind, left, right);
568 Expression *left = equals();
572 SourcePosition pos = scanner_.begin();
573 Expression *right = equals();
576 left = new (pool_) BinaryExpression(pos, TOK_AND, left, right);
584 Expression *left = and_();
588 SourcePosition pos = scanner_.begin();
589 Expression *right = and_();
592 left = new (pool_) BinaryExpression(pos, TOK_OR, left, right);
600 Expression *cond = or_();
604 if (!match(TOK_QMARK))
608 SourcePosition pos = scanner_.begin();
609 AutoAllowTags<false> disableTags(scanner_);
611 if ((left = expression()) == NULL)
615 if (!expect(TOK_COLON))
618 Expression *right = expression();
622 return new (pool_) TernaryExpression(pos, cond, left, right);
628 Expression *left = ternary();
630 TokenKind token = scanner_.peek();
631 if (token != TOK_ASSIGN && (token < TOK_ASSIGN_ADD || token > TOK_ASSIGN_SHL))
634 SourcePosition pos = scanner_.begin();
635 Expression *expr = ternary();
639 left = new (pool_) Assignment(pos, token, left, expr);
653 SourcePosition pos = scanner_.begin();
656 if (!expect(TOK_LPAREN))
659 Expression *condition = expression();
663 if (!expect(TOK_RPAREN))
667 if (scanner_.peek() == TOK_LBRACE)
670 body = localStatement();
674 return new (pool_) WhileStatement(pos, TOK_WHILE, condition, body);
680 SourcePosition pos = scanner_.begin();
684 if (scanner_.peek() == TOK_LBRACE)
687 body = localStatement();
691 if (!expect(TOK_WHILE))
694 if (!expect(TOK_LPAREN))
696 Expression *condition = expression();
699 if (!expect(TOK_RPAREN))
702 return new (pool_) WhileStatement(pos, TOK_DO, condition, body);
710 SourcePosition pos = scanner_.begin();
712 if (!expect(TOK_LPAREN))
714 Expression *expr = expression();
717 if (!expect(TOK_RPAREN))
720 if (!expect(TOK_LBRACE))
723 SourcePosition defaultPos;
724 PoolList<Case *> *cases = new (pool_) PoolList<Case *>();
726 Statement *defaultCase = NULL;
728 while (!peek(TOK_RBRACE)) {
729 Expression *expr = NULL;
730 ExpressionList *others = NULL;
731 if (match(TOK_DEFAULT)) {
733 cc_.reportError(scanner_.begin(), Message_OneDefaultPerSwitch);
737 defaultPos = scanner_.begin();
740 cc_.reportError(defaultPos, Message_DefaultMustBeLastCase);
744 if (!expect(TOK_CASE))
747 // A limitation in the grammar is that |case <NAME>:| will be
748 // detected as a label.
749 AutoAllowTags<false> disableTags(scanner_);
751 if ((expr = expression()) == NULL)
754 if (peek(TOK_COMMA)) {
755 others = new (pool_) ExpressionList();
756 while (match(TOK_COMMA)) {
757 Expression *other = expression();
760 if (!others->append(other))
766 if (!expect(TOK_COLON))
769 Statement *statement = localStatement();
773 if (!peek(TOK_CASE) &&
774 !peek(TOK_DEFAULT) &&
777 cc_.reportError(scanner_.begin(), Message_SingleStatementPerCase);
782 Case *caze = new (pool_) Case(expr, others, statement);
783 if (!cases->append(caze))
786 defaultCase = statement;
790 if (!expect(TOK_RBRACE))
793 return new (pool_) SwitchStatement(pos, expr, cases, defaultCase);
797 Parser::forStatement()
799 SourcePosition pos = scanner_.begin();
801 if (!expect(TOK_LPAREN))
804 Statement *decl = NULL;
805 if (!match(TOK_SEMICOLON)) {
806 if (match(TOK_NEW)) {
807 if ((decl = vars(Qual_None)) == NULL)
810 if ((decl = expressionStatement()) == NULL)
813 if (!expect(TOK_SEMICOLON))
817 Expression *condition = NULL;
818 if (!match(TOK_SEMICOLON)) {
819 if ((condition = expression()) == NULL)
821 if (!expect(TOK_SEMICOLON))
825 Statement *update = NULL;
826 if (!match(TOK_RPAREN)) {
827 if ((update = expressionStatement()) == NULL)
829 if (!expect(TOK_RPAREN))
834 if (scanner_.peek() == TOK_LBRACE)
837 body = localStatement();
841 return new (pool_) ForStatement(pos, decl, condition, update, body);
847 ExpressionList *postDimensions = new (pool_) ExpressionList();
848 while (match(TOK_LBRACKET)) {
849 Expression *dim = NULL;
850 if (!match(TOK_RBRACKET)) {
851 if ((dim = expression()) == NULL)
853 if (!expect(TOK_RBRACKET))
857 if (!postDimensions->append(dim))
860 return postDimensions;
863 VariableDeclaration *
864 Parser::var(TypeQualifiers qual)
867 Local<String> name(zone_);
869 if (!typeAndName(&type, name.ref()))
872 ExpressionList *dims = NULL;
873 if (peek(TOK_LBRACKET)) {
874 if ((dims = dimensions()) == NULL)
878 SourcePosition pos = scanner_.begin();
880 Expression *expr = NULL;
881 if (match(TOK_ASSIGN)) {
882 switch (scanner_.peek()) {
883 // Note: we don't support [] literals yet.
886 if ((expr = fixedArrayLiteral()) == NULL)
891 if ((expr = expression()) == NULL)
897 return new (pool_) VariableDeclaration(pos, qual, name, type, dims, expr);
901 Parser::vars(TypeQualifiers qual)
903 VariableDeclaration *decl = var(qual);
907 if (!peek(TOK_COMMA))
910 StatementList *statements = new (pool_) StatementList();
911 if (!statements->append(decl))
914 while (match(TOK_COMMA)) {
915 Statement *decl = var(qual);
918 if (!statements->append(decl))
922 // Create a fake-o block with no scope to create a statement list.
923 return new (pool_) BlockStatement(decl->pos(), statements);
927 Parser::returnStatement()
929 SourcePosition pos = scanner_.begin();
930 if (!expect(TOK_RETURN))
933 Expression *expr = NULL;
934 if (scanner_.peek() != TOK_SEMICOLON) {
935 if ((expr = expression()) == NULL)
938 // We only care about non-void returns when determining whether a
939 // tagless function is non-void.
940 encounteredReturn_ = true;
943 return new (pool_) ReturnStatement(pos, expr);
947 Parser::expressionStatement()
949 Expression *left = assignment();
953 return new (pool_) ExpressionStatement(left);
959 if (!expect(TOK_LBRACE))
962 SourcePosition pos = scanner_.begin();
964 StatementList *statements = new (pool_) StatementList();
965 while (!match(TOK_RBRACE)) {
966 Statement *statement = localStatement();
967 if (statement == NULL)
969 if (!statements->append(statement))
973 BlockStatement *block = new (pool_) BlockStatement(pos, statements);
982 SourcePosition pos = scanner_.begin();
983 if (!expect(TOK_LPAREN))
986 Expression *cond = expression();
990 if (!expect(TOK_RPAREN))
993 Statement *ifTrue = localStatement();
997 IfStatement *outer = new (pool_) IfStatement(pos, cond, ifTrue);
999 IfStatement *last = outer;
1000 while (match(TOK_ELSE)) {
1001 if (!match(TOK_IF)) {
1002 Statement *ifFalse = localStatement();
1006 last->setIfFalse(ifFalse);
1010 SourcePosition pos = scanner_.begin();
1011 if (!expect(TOK_LPAREN))
1014 Expression *otherCond = expression();
1018 if (!expect(TOK_RPAREN))
1021 Statement *otherIfTrue = localStatement();
1025 IfStatement *inner = new (pool_) IfStatement(pos, otherCond, otherIfTrue);
1026 last->setIfFalse(inner);
1034 Parser::localStatement()
1036 Statement *statement = NULL;
1038 // Statements which must be followed by a semicolon will break out of the
1039 // switch. If they may end in BlockStatements, they'll immediately return.
1041 TokenKind kind = scanner_.peek();
1046 TypeQualifiers quals = (kind == TOK_CONST)
1050 if ((statement = vars(quals)) == NULL)
1056 return forStatement();
1063 statement = new (pool_) BreakStatement(scanner_.begin());
1068 statement = new (pool_) ContinueStatement(scanner_.begin());
1072 if ((statement = do_()) == NULL)
1077 if ((statement = returnStatement()) == NULL)
1082 if ((statement = enum_()) == NULL)
1087 if ((statement = struct_()) == NULL)
1105 if ((statement = expressionStatement()) == NULL)
1109 if (!expect(TOK_SEMICOLON))
1120 Local<String> name(zone_, expectName());
1124 SourcePosition pos = scanner_.begin();
1126 if (!expect(TOK_LBRACE))
1129 FieldList *fields = new (pool_) FieldList();
1131 while (!peek(TOK_RBRACE)) {
1133 Local<String> fieldName(zone_);
1135 if (!fullTypeAndName(&type, fieldName.ref(), DeclField))
1138 SourcePosition pos = scanner_.begin();
1140 if (!matchSemicolon())
1143 StructureStatement::Field *field =
1144 new (pool_) StructureStatement::Field(pos, type, fieldName);
1145 if (!field || !fields->append(field))
1149 if (!expect(TOK_RBRACE))
1152 if (fields->empty()) {
1153 cc_.reportError(pos, Message_StructCantBeEmpty);
1157 return new (pool_) StructureStatement(pos, name, fields);
1165 SourcePosition pos = scanner_.begin();
1167 Local<String> name(zone_);
1168 if (matchName(name.ref()) && !name)
1171 EnumStatement::EntryList *entries = new (pool_) EnumStatement::EntryList();
1173 if (!expect(TOK_LBRACE))
1177 if (scanner_.peek() == TOK_RBRACE)
1180 SourcePosition &pos = scanner_.begin();
1181 Local<String> name(zone_, expectName());
1185 Expression *expr = NULL;
1186 if (match(TOK_ASSIGN)) {
1187 if ((expr = expression()) == NULL)
1191 if (!entries->append(EnumStatement::Entry(name, expr, pos)))
1193 } while (match(TOK_COMMA));
1194 if (!expect(TOK_RBRACE))
1197 return new (pool_) EnumStatement(pos, name, entries);
1201 static inline ScopedRoot<T> *
1202 NewScopedRoot(PoolAllocator &pool, Handle<T> obj)
1204 ScopedRoot<T> *root = pool.alloc<ScopedRoot<T> >();
1205 new (root) ScopedRoot<T>(obj);
1212 NameList *names = new (pool_) NameList();
1214 SourcePosition pos = scanner_.begin();
1216 Local<String> name(zone_, expectName());
1217 if (!name || !names->append(NewScopedRoot<String>(pool_, name)))
1220 TokenKind suffix = TOK_NONE;
1221 while (match(TOK_DOT)) {
1222 if (match(TOK_STAR)) {
1227 if ((name = expectName()) == NULL)
1230 if (!names->append(NewScopedRoot<String>(pool_, name)))
1234 return new (pool_) ImportStatement(pos, names, suffix);
1238 Parser::function(TokenKind token)
1240 Expression *returnType;
1241 Local<String> name(zone_);
1243 if (!fullTypeAndName(&returnType, name.ref(), DeclFun))
1246 SourcePosition pos = scanner_.begin();
1247 ParameterList *parameters = new (pool_) ParameterList();
1249 if (!expect(TOK_LPAREN))
1252 bool variadic = false;
1254 if (!match(TOK_RPAREN)) {
1256 bool isConst = match(TOK_CONST);
1257 bool ref = match(TOK_AMPERSAND);
1259 SourcePosition begin = scanner_.begin();
1261 Local<String> name(zone_);
1262 if (!fullTypeAndName(&type, name.ref(), DeclArg))
1266 if (isConst || ref) {
1267 cc_.reportError(begin, Message_InvalidVarArgs);
1271 if (token != TOK_NATIVE) {
1272 cc_.reportError(begin, Message_VarArgsOutsideNative);
1280 ExpressionList *dims = NULL;
1281 if (peek(TOK_LBRACKET)) {
1282 if ((dims = dimensions()) == NULL)
1286 Expression *expr = NULL;
1287 if (match(TOK_ASSIGN)) {
1288 if ((expr = expression()) == NULL)
1292 Parameter *parameter = new (pool_) Parameter(name, type, ref, isConst, dims, expr, begin);
1293 if (!parameters->append(parameter))
1296 if (!match(TOK_COMMA))
1299 if (!expect(TOK_RPAREN))
1303 encounteredReturn_ = false;
1305 Statement *body = NULL;
1306 if (token != TOK_FORWARD && token != TOK_NATIVE && token != TOK_TYPEDEF) {
1312 FunctionSignature signature(returnType, parameters, variadic);
1313 if (token == TOK_TYPEDEF)
1314 return new (pool_) FunctionTypeStatement(pos, name, signature);
1316 return new (pool_) FunctionStatement(pos, name, token, body, signature, encounteredReturn_);
1320 DeclareIntrinsicType(Zone *zone, Scope *scope, const char *name, Handle<Type> type)
1322 Local<String> tag(zone, zone->makeSymbol(name));
1327 return false; //return !!scope->declareType(tag, type);
1331 DeclareIntrinsicType(Zone *zone, Scope *scope, const char *name, PrimitiveType type)
1333 Local<Type> primitive(zone, zone->types()->getPrimitive(type));
1335 return DeclareIntrinsicType(zone, scope, name, primitive);
1339 Parser::setupIntrinsics()
1342 if (!DeclareIntrinsicType(zone_, scope_, "Float", PrimitiveType_Float))
1344 if (!DeclareIntrinsicType(zone_, scope_, "_", PrimitiveType_Int32))
1346 if (!DeclareIntrinsicType(zone_, scope_, "bool", PrimitiveType_Bool))
1348 if (!DeclareIntrinsicType(zone_, scope_, "String", PrimitiveType_Char))
1351 Local<Type> unchecked(zone_, zone_->types()->getUnchecked());
1352 if (!DeclareIntrinsicType(zone_, scope_, "unchecked", unchecked))
1355 Local<Type> voidType(zone_, zone_->types()->getVoid());
1356 if (!DeclareIntrinsicType(zone_, scope_, "void", voidType))
1359 Local<Type> intType(zone_, zone_->types()->getPrimitive(PrimitiveType_Int32));
1360 if (!DeclareIntrinsicType(zone_, scope_, "int", intType))
1369 StatementList *statements = new (pool_) StatementList();
1371 if (!setupIntrinsics())
1375 Statement *statement = NULL;
1377 TokenKind kind = scanner_.peek();
1386 if ((statement = function(TOK_NAME)) == NULL)
1393 if (statements->length())
1394 cc_.reportError(scanner_.begin(), Message_ImportsMustBeFirst);
1395 ImportStatement *stmt = import();
1398 if (!imports_->append(stmt))
1400 if (!matchSemicolon())
1412 if (kind != TOK_LABEL)
1414 if (kind == TOK_PUBLIC && peek(TOK_CONST)) {
1415 TypeQualifiers quals = TypeQualifiers(Qual_Public | Qual_Const);
1416 if ((statement = var(quals)) == NULL)
1418 if (!matchSemicolon())
1421 if ((statement = function(kind)) == NULL)
1423 if ((kind == TOK_FORWARD || kind == TOK_NATIVE || kind == TOK_TYPEDEF) && !matchSemicolon())
1432 TypeQualifiers quals = (kind == TOK_NEW) ? Qual_Const : Qual_None;
1433 // Note: at global scope, we don't accept comma-delimited
1434 // declarations yet, to see if we can get away with forcing
1435 // a nicer top-level style.
1437 if ((statement = var(quals)) == NULL)
1439 if (!matchSemicolon())
1445 if ((statement = enum_()) == NULL)
1447 if (!matchSemicolon())
1452 if ((statement = struct_()) == NULL)
1454 if (!matchSemicolon())
1459 cc_.reportError(scanner_.begin(), Message_ExpectedGlobal);
1466 if (!statements->append(statement))
1470 return new (pool_) ParseTree(statements, unbound_, imports_);
1473 class AstPrinter : public AstVisitor
1480 for (size_t i = 0; i < level_; i++)
1491 AstPrinter(FILE *fp)
1497 void dump(const FunctionSignature &sig) {
1499 fprintf(fp_, "()\n");
1501 void visitNameProxy(NameProxy *name) {
1503 fprintf(fp_, "[ NameProxy (%s)\n", name->name()->chars());
1505 void visitCallExpression(CallExpression *node) {
1507 fprintf(fp_, "[ CallExpression\n");
1509 node->callee()->accept(this);
1510 for (size_t i = 0; i < node->arguments()->length(); i++)
1511 node->arguments()->at(i)->accept(this);
1514 void visitFunctionTypeStatement(FunctionTypeStatement *node) {
1516 fprintf(fp_, "[ FunctionTypeStatement (%s)\n", node->name()->chars());
1518 dump(node->signature());
1521 void visitFunctionStatement(FunctionStatement *node) {
1523 fprintf(fp_, "[ FunctionStatement (%s)\n", node->name()->chars());
1525 dump(node->signature());
1527 node->body()->accept(this);
1530 void visitExpressionStatement(ExpressionStatement *node) {
1532 fprintf(fp_, "[ ExpressionStatement\n");
1534 node->expression()->accept(this);
1537 void visitAssignment(Assignment *node) {
1539 fprintf(fp_, "[ Assignment\n");
1541 node->lvalue()->accept(this);
1542 node->expression()->accept(this);
1545 void visitTernaryExpression(TernaryExpression *node) {
1547 fprintf(fp_, "[ TernaryExpression\n");
1549 node->condition()->accept(this);
1550 node->left()->accept(this);
1551 node->right()->accept(this);
1554 void visitBinaryExpression(BinaryExpression *node) {
1556 fprintf(fp_, "[ BinaryExpression (%s)\n", TokenNames[node->token()]);
1558 node->left()->accept(this);
1559 node->right()->accept(this);
1562 void visitUnaryExpression(UnaryExpression *node) {
1564 fprintf(fp_, "[ UnaryExpression (%s)\n", TokenNames[node->token()]);
1566 node->expression()->accept(this);
1569 void visitReturnStatement(ReturnStatement *node) {
1571 fprintf(fp_, "[ ReturnStatement\n");
1573 if (node->expression())
1574 node->expression()->accept(this);
1577 void visitForStatement(ForStatement *node) {
1579 fprintf(fp_, "[ ForStatement\n");
1581 if (node->initialization())
1582 node->initialization()->accept(this);
1583 if (node->condition())
1584 node->condition()->accept(this);
1586 node->update()->accept(this);
1587 node->body()->accept(this);
1590 void visitBlockStatement(BlockStatement *node) {
1592 fprintf(fp_, "[ BlockStatement\n");
1594 for (size_t i = 0; i < node->statements()->length(); i++)
1595 node->statements()->at(i)->accept(this);
1598 void visitVariableDeclaration(VariableDeclaration *node) {
1600 fprintf(fp_, "[ VariableDeclaration (%s)\n", node->name()->chars());
1602 if (node->initialization())
1603 node->initialization()->accept(this);
1606 void visitIntegerLiteral(IntegerLiteral *node) {
1608 fprintf(fp_, "[ IntegerLiteral (%d)\n", node->value());
1610 void visitBooleanLiteral(BooleanLiteral *node) {
1612 fprintf(fp_, "[ BooleanLiteral (%s)\n",
1613 (node->token() == TOK_TRUE) ? "true" : "false");
1615 void visitFloatLiteral(FloatLiteral *node) {
1617 fprintf(fp_, "[ FloatLiteral (%f)\n", node->value());
1619 void visitIfStatement(IfStatement *node) {
1621 fprintf(fp_, "[ IfStatement\n");
1623 node->ifTrue()->accept(this);
1624 if (node->ifFalse())
1625 node->ifFalse()->accept(this);
1628 void visitIndexExpression(IndexExpression *node) {
1630 fprintf(fp_, "[ IndexExpression\n");
1632 node->left()->accept(this);
1633 node->right()->accept(this);
1636 void visitEnumStatement(EnumStatement *node) {
1638 fprintf(fp_, "[ EnumStatement (%s)\n", node->name() ? node->name()->chars() : "<anonymous>");
1640 for (size_t i = 0; i < node->entries()->length(); i++) {
1642 fprintf(fp_, "%s =\n", node->entries()->at(i).name->chars());
1643 if (node->entries()->at(i).expr) {
1645 node->entries()->at(i).expr->accept(this);
1651 void visitWhileStatement(WhileStatement *node) {
1653 fprintf(fp_, "[ WhileStatement (%s)\n",
1654 (node->token() == TOK_DO) ? "do" : "while");
1656 if (node->token() == TOK_DO) {
1657 node->condition()->accept(this);
1658 node->body()->accept(this);
1660 node->condition()->accept(this);
1661 node->body()->accept(this);
1665 void visitBreakStatement(BreakStatement *node) {
1667 fprintf(fp_, "[ BreakStatement\n");
1669 void visitContinueStatement(ContinueStatement *node) {
1671 fprintf(fp_, "[ ContinueStatement\n");
1673 void visitStringLiteral(StringLiteral *node) {
1675 fprintf(fp_, "[ StringLiteral\n");
1677 void visitIncDecExpression(IncDecExpression *node) {
1679 fprintf(fp_, "[ IncDecExpression (postfix=%d)\n", node->postfix());
1681 node->expression()->accept(this);
1684 void visitStructureStatement(StructureStatement *node) {
1686 fprintf(fp_, "[ StructureStatement %s\n", node->name()->chars());
1688 void visitFieldExpression(FieldExpression *node) {
1690 fprintf(fp_, "[ FieldExpression %s\n", node->field()->chars());
1692 node->left()->accept(this);
1695 void visitSwitchStatement(SwitchStatement *node) {
1697 fprintf(fp_, "[ SwitchStatement\n");
1699 node->expression()->accept(this);
1700 for (size_t i = 0; i < node->cases()->length(); i++) {
1701 Case *c = node->cases()->at(i);
1702 c->expression()->accept(this);
1704 for (size_t j = 0; j < c->others()->length(); j++)
1705 c->others()->at(j)->accept(this);
1708 c->statement()->accept(this);
1711 if (node->defaultCase())
1712 node->defaultCase()->accept(this);
1715 void visitArrayLiteral(ArrayLiteral *node) {
1717 fprintf(fp_, "[ ArrayLiteral\n");
1719 for (size_t i = 0; i < node->expressions()->length(); i++) {
1720 Expression *expr = node->expressions()->at(i);
1726 void visitImportStatement(ImportStatement *node) {
1728 fprintf(fp_, "[ ImportStatement\n");
1733 ParseTree::dump(FILE *fp)
1735 AstPrinter printer(fp);
1737 for (size_t i = 0; i < statements_->length(); i++)
1738 statements_->at(i)->accept(&printer);