1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
50 NameComponent::~NameComponent() {}
52 QualifiedName
* Parser::typeExpression()
54 if (match(T_Multiply
))
56 QualifiedName
* n
= nameExpression(false);
57 if ((n
->qualifier
!= NULL
&& n
->qualifier
->tag() != TAG_simpleName
) ||
58 n
->name
->tag() != TAG_simpleName
)
59 compiler
->syntaxError(n
->pos
, SYNTAXERR_ILLEGAL_TYPENAME
);
60 if (hd() == T_LeftDotAngle
)
61 compiler
->internalError(position(), "Unimplemented: Cannot parse vector types");
65 QualifiedName
* Parser::nameExpression(bool is_attr
)
67 uint32_t pos
= position();
69 NameComponent
* n
= NULL
;
70 if (match(T_Multiply
))
71 n
= ALLOC(WildcardName
, ());
72 else if (match(T_Public
)) {
73 n
= ALLOC(BuiltinNamespace
, (T_Public
));
75 else if (match(T_Protected
)) {
76 n
= ALLOC(BuiltinNamespace
, (T_Protected
));
78 else if (match(T_Private
)) {
79 n
= ALLOC(BuiltinNamespace
, (T_Private
));
81 else if (match(T_Internal
)) {
82 n
= ALLOC(BuiltinNamespace
, (T_Internal
));
86 n
= ALLOC(SimpleName
, (name
));
88 if (match(T_DoubleColon
)) {
89 if (match(T_Multiply
))
90 return ALLOC(QualifiedName
, (n
, ALLOC(WildcardName
, ()), is_attr
, pos
));
91 if (match(T_LeftBracket
)) {
92 Expr
* e
= commaExpression(0);
94 return ALLOC(QualifiedName
, (n
, ALLOC(ComputedName
, (e
)), is_attr
, pos
));
96 return ALLOC(QualifiedName
, (n
, ALLOC(SimpleName
, (identifier())), is_attr
, pos
));
99 if (name
== compiler
->SYM_arguments
)
101 return ALLOC(QualifiedName
, (NULL
, n
, is_attr
, pos
));
105 Expr
* Parser::exprListToCommaExpr(Seq
<Expr
*>* es
) {
107 for ( es
=es
->tl
; es
!= NULL
; es
= es
->tl
)
108 expr
= ALLOC(BinaryExpr
, (OPR_comma
, expr
, es
->hd
));
112 Expr
* Parser::objectInitializer ()
114 uint32_t pos
= position();
116 Seq
<LiteralField
*>* fields
= fieldList();
119 return ALLOC(LiteralObject
, (fields
, pos
));
122 Seq
<LiteralField
*>* Parser::fieldList ()
124 SeqBuilder
<LiteralField
*> fields(allocator
);
125 if (hd () != T_RightBrace
) {
127 fields
.addAtEnd(literalField());
128 } while (match(T_Comma
));
133 LiteralField
* Parser::literalField()
137 case T_StringLiteral
:
138 name
= stringValue();
142 name
= doubleToStr(intValue());
146 name
= doubleToStr(uintValue());
149 case T_DoubleLiteral
:
150 name
= doubleToStr(doubleValue());
158 compiler
->syntaxError(position(), SYNTAXERR_ILLEGAL_FIELDNAME
);
163 Expr
* expr
= assignmentExpression(0);
164 return ALLOC(LiteralField
, (name
, expr
));
167 Expr
* Parser::arrayInitializer ()
169 uint32_t pos
= position();
171 Seq
<Expr
*>* elts
= elementList();
172 eat (T_RightBracket
);
173 return ALLOC(LiteralArray
, (elts
, pos
));
176 Seq
<Expr
*>* Parser::elementList()
178 SeqBuilder
<Expr
*> elts(allocator
);
198 elt
= assignmentExpression(0);
209 Expr
* Parser::functionExpression()
212 return ALLOC(LiteralFunction
, (functionGuts(&qual
, false, false, true)));
215 Expr
* Parser::primaryExpression()
217 if (hd() == T_BreakSlash
)
220 uint32_t pos
= position(); // Record the source location before consuming the token
224 return ALLOC(LiteralNull
, (pos
));
228 bool flag
= hd() == T_True
;
230 return ALLOC(LiteralBoolean
, (flag
, pos
));
234 int32_t i
= intValue();
236 return ALLOC(LiteralInt
, (i
, pos
));
239 case T_UIntLiteral
: {
240 uint32_t u
= uintValue();
242 return ALLOC(LiteralUInt
, (u
, pos
));
245 case T_DoubleLiteral
: {
246 double d
= doubleValue();
248 return ALLOC(LiteralDouble
, (d
, pos
));
251 case T_StringLiteral
: {
252 Str
* s
= stringValue();
254 return ALLOC(LiteralString
, (s
, pos
));
257 case T_RegexpLiteral
: {
258 Str
* r
= regexValue();
260 return ALLOC(LiteralRegExp
, (r
, pos
));
265 return ALLOC(ThisExpr
, ());
268 return superExpression();
271 return parenExpression();
274 return arrayInitializer ();
277 return objectInitializer ();
280 return functionExpression ();
284 return xmlInitializer();
287 return attributeIdentifier();
293 return nameExpression();
296 QualifiedName
* Parser::attributeIdentifier()
299 if (hd() == T_LeftBracket
) {
301 Expr
* e
= commaExpression(0);
303 return ALLOC(QualifiedName
, (NULL
, ALLOC(ComputedName
, (e
)), true, 0));
306 return nameExpression(true);
309 QualifiedName
* Parser::propertyIdentifier()
315 return (QualifiedName
*)primaryExpression();
317 compiler
->syntaxError(position(), SYNTAXERR_ILLEGAL_PROPNAME
);
323 Expr
* Parser::propertyOperator(Expr
* obj
)
325 uint32_t pos
= position();
329 if (hd() == T_LeftParen
)
330 return ALLOC(FilterExpr
, (obj
, parenExpression(), pos
));
331 if (hd() == T_AtSign
)
332 return ALLOC(ObjectRef
, (obj
, attributeIdentifier(), pos
));
333 return ALLOC(ObjectRef
, (obj
, nameExpression(), pos
));
337 return ALLOC(DescendantsExpr
, (obj
, propertyIdentifier(), pos
));
339 case T_LeftBracket
: {
341 Expr
* expr
= commaExpression(0);
342 eat (T_RightBracket
);
343 return ALLOC(ObjectRef
, (obj
, ALLOC(QualifiedName
, (NULL
, ALLOC(ComputedName
, (expr
)), false, pos
)), pos
));
346 compiler
->internalError(position(), "Unimplemented: Cannot parse vector syntax");
348 compiler
->internalError(position(), "Unexpected token in propertyOperator: %d", (int)hd());
354 Seq
<Expr
*>* Parser::argumentList ()
356 SeqBuilder
<Expr
*> exprs(allocator
);
359 if (hd() != T_RightParen
) {
361 exprs
.addAtEnd(assignmentExpression(0));
362 } while (match(T_Comma
));
368 Expr
* Parser::memberExpression ()
373 if (hd() == T_LessThan
) {
374 // vector initializer
375 compiler
->internalError(position(), "Unimplemented: Cannot parse vector initializer");
377 Expr
* object_expr
= memberExpression ();
378 Seq
<Expr
*>* argument_exprs
= argumentList();
379 return memberExpressionPrime (ALLOC(NewExpr
, (object_expr
, argument_exprs
)));
383 Expr
* expr
= primaryExpression ();
384 return memberExpressionPrime (expr
);
389 Expr
* Parser::memberExpressionPrime (Expr
* expr
)
396 return memberExpressionPrime (propertyOperator (expr
));
402 Expr
* Parser::superExpression ()
405 uint32_t pos
= position();
406 Seq
<Expr
*>* arguments
= NULL
;
407 bool argsPresent
= false;
408 if (hd() == T_LeftParen
) {
410 arguments
= argumentList();
412 if (argsPresent
&& (arguments
== NULL
|| arguments
->tl
!= NULL
))
413 compiler
->syntaxError(pos
, SYNTAXERR_ONE_ARGUMENT_REQUIRED
);
414 if (hd() != T_Dot
&& hd() != T_LeftBracket
)
415 compiler
->syntaxError(pos
, SYNTAXERR_PROPERTY_OPERATOR_REQUIRED
);
416 Expr
* obj
= argsPresent
? arguments
->hd
: ALLOC(ThisExpr
, ());
417 return propertyOperator(ALLOC(SuperExpr
, (obj
)));
420 Expr
* Parser::callExpression ()
422 uint32_t pos
= position();
423 Expr
* object_expr
= memberExpression ();
424 Seq
<Expr
*>* argument_exprs
= argumentList();
426 return callExpressionPrime (ALLOC(CallExpr
, (object_expr
, argument_exprs
, pos
)));
430 Expr
* Parser::callExpressionPrime (Expr
* call_expr
)
435 uint32_t pos
= position();
436 Seq
<Expr
*>* argument_exprs
= argumentList();
437 return callExpressionPrime (ALLOC(CallExpr
, (call_expr
, argument_exprs
, pos
)));
442 return callExpressionPrime (propertyOperator (call_expr
));
448 Expr
* Parser::newExpression (int new_count
)
450 Expr
* call_expression
;
452 bool is_new
= match(T_New
);
454 if (hd() == T_LessThan
) {
455 // vector initializer
456 compiler
->internalError(position(), "Unimplemented: Cannot parse vector initializer");
458 call_expression
= newExpression (new_count
+1);
461 call_expression
= memberExpression();
463 if (hd() == T_LeftParen
) { // No more new exprs so this paren must start a call expr
464 uint32_t pos
= position();
465 Seq
<Expr
*>* argument_exprs
= argumentList();
467 return ALLOC(NewExpr
, (call_expression
, argument_exprs
));
468 return callExpressionPrime (ALLOC(CallExpr
, (call_expression
, argument_exprs
, pos
)));
472 return ALLOC(NewExpr
, (call_expression
, NULL
));
475 return memberExpressionPrime (call_expression
);
477 return call_expression
;
480 Expr
* Parser::leftHandSideExpression ()
482 Expr
* oper
= (hd() == T_New
) ? newExpression (0) : memberExpression ();
483 if (hd () == T_LeftParen
) {
484 uint32_t pos
= position();
485 Seq
<Expr
*>* args
= argumentList();
486 return callExpressionPrime(ALLOC(CallExpr
, (oper
, args
, pos
)));
491 Expr
* Parser::postfixExpression ()
493 Expr
* expr
= leftHandSideExpression ();
495 if (match(T_PlusPlus
))
496 return ALLOC(UnaryExpr
, (OPR_postIncr
, expr
));
497 if (match(T_MinusMinus
))
498 return ALLOC(UnaryExpr
, (OPR_postDecr
, expr
));
503 Expr
* Parser::unaryExpression()
510 return ALLOC(UnaryExpr
, (OPR_delete
, postfixExpression()));
515 return ALLOC(UnaryExpr
, (tokenToUnaryOperator(t
), postfixExpression()));
524 return ALLOC(UnaryExpr
, (tokenToUnaryOperator(t
), unaryExpression()));
527 return postfixExpression();
531 Expr
* Parser::multiplicativeExpression()
533 Expr
* expr
= unaryExpression();
536 while (isMultiplicative(t
= hd()) || t
== T_BreakSlash
) {
537 if (t
== T_BreakSlash
) {
539 if (!isMultiplicative(t
= hd()))
543 expr
= ALLOC(BinaryExpr
, (tokenToBinaryOperator(t
), expr
, unaryExpression()));
548 Expr
* Parser::additiveExpression()
550 Expr
* expr
= multiplicativeExpression();
553 while (isAdditive(t
= hd())) {
555 expr
= ALLOC(BinaryExpr
, (tokenToBinaryOperator(t
), expr
, multiplicativeExpression()));
560 Expr
* Parser::shiftExpression()
562 Expr
* expr
= additiveExpression();
565 while (isShift(t
= hd()) || t
== T_BreakRightAngle
) {
566 if (t
== T_BreakRightAngle
) {
567 rightShiftOrRelationalOperator();
568 if (!isShift(t
= hd()))
572 expr
= ALLOC(BinaryExpr
, (tokenToBinaryOperator(t
), expr
, additiveExpression()));
578 Expr
* Parser::relationalExpression(int flags
)
580 Expr
* expr
= shiftExpression();
582 bool allowIn
= !(flags
& EFLAG_NoIn
);
585 if (hd() == T_Identifier
&& identValue() == compiler
->SYM_to
&& !compiler
->es3_keywords
)
587 if (!(isRelational(t
= hd(), allowIn
) || t
== T_BreakRightAngle
))
589 if (t
== T_BreakRightAngle
) {
590 rightShiftOrRelationalOperator();
591 if (!isRelational(t
= hd(), allowIn
))
595 expr
= ALLOC(BinaryExpr
, (tokenToBinaryOperator(t
), expr
, shiftExpression()));
601 Expr
* Parser::equalityExpression(int flags
)
603 Expr
* expr
= relationalExpression(flags
);
606 while (isEquality(t
= hd())) {
608 expr
= ALLOC(BinaryExpr
, (tokenToBinaryOperator(t
), expr
, relationalExpression(flags
)));
613 Expr
* Parser::bitwiseAndExpression(int flags
)
615 Expr
* expr
= equalityExpression(flags
);
616 while (match(T_BitwiseAnd
))
617 expr
= ALLOC(BinaryExpr
, (OPR_bitwiseAnd
, expr
, equalityExpression(flags
)));
621 Expr
* Parser::bitwiseXorExpression(int flags
)
623 Expr
* expr
= bitwiseAndExpression(flags
);
624 while (match(T_BitwiseXor
))
625 expr
= ALLOC(BinaryExpr
, (OPR_bitwiseXor
, expr
, bitwiseAndExpression(flags
)));
629 Expr
* Parser::bitwiseOrExpression(int flags
)
631 Expr
* expr
= bitwiseXorExpression(flags
);
632 while (match(T_BitwiseOr
))
633 expr
= ALLOC(BinaryExpr
, (OPR_bitwiseOr
, expr
, bitwiseXorExpression(flags
)));
637 Expr
* Parser::logicalAndExpression(int flags
)
639 Expr
* expr
= bitwiseOrExpression(flags
);
640 while (match(T_LogicalAnd
))
641 expr
= ALLOC(BinaryExpr
, (OPR_logicalAnd
, expr
, bitwiseOrExpression(flags
)));
645 Expr
* Parser::logicalOrExpression(int flags
)
647 Expr
* expr
= logicalAndExpression(flags
);
648 while (match(T_LogicalOr
))
649 expr
= ALLOC(BinaryExpr
, (OPR_logicalOr
, expr
, logicalAndExpression(flags
)));
653 Expr
* Parser::nonAssignmentExpression(int flags
)
655 Expr
* expr
= logicalOrExpression(flags
);
656 if (!match(T_Question
))
659 Expr
* consequent
= nonAssignmentExpression(flags
);
661 Expr
* alternate
= nonAssignmentExpression(flags
);
662 return ALLOC(ConditionalExpr
, (expr
, consequent
, alternate
));
665 Expr
* Parser::conditionalExpression(int flags
)
667 Expr
* expr
= logicalOrExpression(flags
);
668 if (!match(T_Question
))
671 Expr
* consequent
= assignmentExpression(flags
);
673 Expr
* alternate
= assignmentExpression(flags
);
674 return ALLOC(ConditionalExpr
, (expr
, consequent
, alternate
));
677 Expr
* Parser::assignmentExpression(int flags
)
679 Expr
* lhs
= conditionalExpression(flags
);
682 if (!((t
= hd()) == T_Assign
|| isOpAssign(t
) || t
== T_BreakRightAngle
))
685 if (t
== T_BreakRightAngle
) {
686 rightShiftOrRelationalOperator();
687 if (!isOpAssign(t
= hd()))
692 Expr
* rhs
= assignmentExpression (flags
);
693 return ALLOC(AssignExpr
, (tokenToBinaryOperator(t
), lhs
, rhs
));
696 Expr
* Parser::commaExpression(int flags
)
698 Expr
* expr
= assignmentExpression(flags
);
699 while (match(T_Comma
))
700 expr
= ALLOC(BinaryExpr
, (OPR_comma
, expr
, assignmentExpression(flags
)));
704 Expr
* Parser::parenExpression()
707 Expr
* expr
= commaExpression(0);