示例#1
0
        /// <summary>
        /// Reads a property name, used in object literals.
        /// </summary>
        /// <param name="nameType"> Identifies the particular way the property was specified. </param>
        /// <returns> An expression that evaluates to the property name. </returns>
        private Expression ReadPropertyNameExpression(out PropertyNameType nameType)
        {
            Expression result;
            if (this.nextToken is LiteralToken)
            {
                // The property name can be a string or a number or (in ES5) a keyword.
                if (((LiteralToken)this.nextToken).IsKeyword == true)
                {
                    // false, true or null.
                    result = new LiteralExpression(this.nextToken.Text);
                }
                else
                {
                    object literalValue = ((LiteralToken)this.nextToken).Value;
                    if (literalValue is string || literalValue is int)
                        result = new LiteralExpression(((LiteralToken)this.nextToken).Value.ToString());
                    else if (literalValue is double)
                        result = new LiteralExpression(((double)((LiteralToken)this.nextToken).Value).ToString(CultureInfo.InvariantCulture));
                    else
                        throw new JavaScriptException(this.engine, ErrorType.SyntaxError, string.Format("Expected property name but found {0}", Token.ToText(this.nextToken)), this.LineNumber, this.SourcePath);
                }
                nameType = PropertyNameType.Name;
            }
            else if (this.nextToken is IdentifierToken)
            {
                // An identifier is also okay.
                var name = ((IdentifierToken)this.nextToken).Name;
                if (name == "get")
                    nameType = PropertyNameType.Get;
                else if (name == "set")
                    nameType = PropertyNameType.Set;
                else
                    nameType = PropertyNameType.Name;
                result = new LiteralExpression(name);
            }
            else if (this.nextToken is KeywordToken)
            {
                // In ES5 a keyword is also okay.
                result = new LiteralExpression(((KeywordToken)this.nextToken).Name);
                nameType = PropertyNameType.Name;
            }
            else if (this.nextToken == PunctuatorToken.LeftBracket)
            {
                // ES6 computed property.
                this.Consume();
                result = ParseExpression(PunctuatorToken.RightBracket);
                nameType = PropertyNameType.Expression;
            }
            else
                throw new JavaScriptException(this.engine, ErrorType.SyntaxError, string.Format("Expected property name but found {0}", Token.ToText(this.nextToken)), this.LineNumber, this.SourcePath);

            // Consume the token.
            this.Consume();

            // Return the property name.
            return result;
        }
示例#2
0
 public SopJsonResolver(PropertyNameType type = PropertyNameType.Default)
 {
     this._type = type;
 }