Beispiel #1
0
		private Expression ParseLeftHandSideExpression ()
		{
			Token start = current;
			Expression expr;
			switch (current.Kind) {
				//primary expression
				case Token.Type.@this:
					expr = new Expression (Expression.Operation.@this, new TextSpan (current, current));
					break;
				case Token.Type.Identifier:
					expr = new IdentifierExpression ((current as IdentifierToken).Spelling, new TextSpan (current, current));
					break;
				case Token.Type.@null:
					expr = new NullExpression (new TextSpan (current, current));
					break;
				case Token.Type.@true:
					expr = new Expression (Expression.Operation.@true, new TextSpan (current, current));
					break;
				case Token.Type.@false:
					expr = new Expression (Expression.Operation.@false, new TextSpan (current, current));
					break;
				case Token.Type.NumericLiteral:
					expr = new NumericLiteralExpression (((NumericLiteralToken)current).Spelling, new TextSpan (current, current));
					break;
				case Token.Type.OctalIntegerLiteral:
					expr = new OctalLiteralExpression (((OctalIntegerLiteralToken)current).Value, new TextSpan (current, current));
					break;
				case Token.Type.HexIntegerLiteral:
					expr = new HexLiteralExpression (((HexIntegerLiteralToken)current).Value, new TextSpan (current, current));
					break;
				case Token.Type.StringLiteral:
					expr = new StringLiteralExpression ((current as StringLiteralToken).Value, (current as StringLiteralToken).Spelling, new TextSpan (current, current));
					break;
				case Token.Type.LeftBracket:
					expr = ParseArrayLiteral ();
					break;
				case Token.Type.LeftBrace:
					expr = ParseObjectLiteral ();
					break;
				case Token.Type.LeftParenthesis:
					Next ();
					expr = new UnaryOperatorExpression(ParseExpression (), Expression.Operation.Parenthesized, new TextSpan(current,current));
					CheckSyntaxExpected (Token.Type.RightParenthesis);
					break;
				//end primary
				case Token.Type.function:
					expr = new FunctionExpression (ParseFunctionDefinition ());
					break;
				case Token.Type.@new:
					Next ();
					Expression target = ParseExpression ();
					ArgumentList arguments;
					if (current.Kind == Token.Type.LeftParenthesis) {
						arguments = this.ParseArgumentList ();
					} else {
						arguments = new ArgumentList (new List<ExpressionListElement> (), new TextSpan (start, current));
					}
					expr = new InvocationExpression (target, arguments,Expression.Operation.@new, new TextSpan(start,current));
					break;
				default:
					SyntaxError.Add ("Expression start with a strange token :" + current.Kind.ToString ());
					return new Expression (Expression.Operation.SyntaxError, new TextSpan (start, current));
			}
			Next (); // to go ahead for other part
			return ParseRightExpression (expr);
		}
Beispiel #2
0
		private Expression ParseObjectLiteral ()
		{
			Token start = current;
			Next ();
			List<ObjectLiteralElement> elements = new List<ObjectLiteralElement> ();
			if (current.Kind == Token.Type.RightBrace) {
				return new ObjectLiteralExpression (elements, new TextSpan (start, current));
			}
			
			ObjectLiteralElement element;
			TextPoint comma = new TextPoint ();
			TextPoint colon = new TextPoint ();
			Expression name;
			do {
				switch (current.Kind) {
					case Token.Type.Identifier:
						name = new IdentifierExpression ((current as IdentifierToken).Spelling, new TextSpan (current, current));
						break;
					case Token.Type.NumericLiteral:
						name = new NumericLiteralExpression (((NumericLiteralToken)current).Spelling, new TextSpan (current, current));
						break;
					case Token.Type.StringLiteral:
						name = new StringLiteralExpression ((current as StringLiteralToken).Value, (current as StringLiteralToken).Spelling, new TextSpan (current, current));
						break;
					default:
						Error (DiagnosticCode.SyntaxError, new TextSpan (current, current));
						return new Expression (Expression.Operation.SyntaxError, new TextSpan (current, current));					
				}
				Next ();
				CheckSyntaxExpected (Token.Type.Colon);
				colon = new TextPoint (current.StartPosition);
				Next ();
				Expression val = ParseAssignmentExpression (false);
				element = new ObjectLiteralElement (name, val, colon, comma);
				elements.Add (element);
				comma = new TextPoint (current.StartPosition);
			} while (current.Kind == Token.Type.Comma);
			CheckSyntaxExpected (Token.Type.RightBrace);
			elements.Add (element);
			return new ObjectLiteralExpression (elements, new TextSpan (start, current));
		}