コード例 #1
0
        private object ToTypeName(TypeNameOptions options)
        {
            var allowShortName = (options & TypeNameOptions.ShortNames) != 0;
            var allowAliases   = (options & TypeNameOptions.Aliases) != 0;
            var allowArrays    = (options & TypeNameOptions.Arrays) != 0;

            if (this.Type == TokenType.Identifier && this.nodes.Count == 0 && allowShortName)
            {
                var typeName = default(string);
                if (allowAliases && TypeAliases.TryGetValue(this.Value, out typeName))
                {
                    return(typeName);
                }
                else
                {
                    return(this.Value);
                }
            }

            if (this.Type == TokenType.Call && this.nodes.Count == 2 && this.Value == "[" && this.nodes[1].Count == 0 && allowArrays)
            {
                var arrayNode     = new ParseTreeNode(TokenType.Identifier, this.Lexeme, typeof(Array).Name);
                var argumentsNode = new ParseTreeNode(TokenType.Arguments, this.Lexeme, "<");
                argumentsNode.Add(this.nodes[0]);
                arrayNode.Add(argumentsNode);
                return(arrayNode.ToTypeName(TypeNameOptions.None));
            }

            var node = new Dictionary <string, object>
            {
                { Constants.EXPRESSION_POSITION, this.Lexeme.Position },
                { Constants.EXPRESSION_TYPE_ATTRIBUTE, Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD },
            };

            if (this.Type == TokenType.Resolve)
            {
                Ensure(this, 2, TokenType.None, TokenType.Identifier);
                node[Constants.EXPRESSION_ATTRIBUTE]             = this.nodes[0].ToTypeName(TypeNameOptions.None);
                node[Constants.PROPERTY_OR_FIELD_NAME_ATTRIBUTE] = this.nodes[1].Value;
                node[Constants.ARGUMENTS_ATTRIBUTE]            = PrepareTypeArguments(this.nodes[1], 0);
                node[Constants.USE_NULL_PROPAGATION_ATTRIBUTE] = Constants.FalseObject;
            }
            else if (this.Type == TokenType.Identifier)
            {
                var typeName = this.Value;
                if (allowAliases && TypeAliases.TryGetValue(this.Value, out typeName) == false)
                {
                    typeName = this.Value;
                }

                node[Constants.EXPRESSION_ATTRIBUTE]             = null;
                node[Constants.PROPERTY_OR_FIELD_NAME_ATTRIBUTE] = typeName;
                node[Constants.USE_NULL_PROPAGATION_ATTRIBUTE]   = Constants.FalseObject;
                node[Constants.ARGUMENTS_ATTRIBUTE] = PrepareTypeArguments(this, 0);
            }
            else
            {
                throw new ExpressionParserException(Properties.Resources.EXCEPTION_PARSER_TYPENAMEEXPECTED, this);
            }

            return(new SyntaxTreeNode(node));
        }
コード例 #2
0
        private static object ToTypeName(ParseTreeNode parseNode, TypeNameOptions options)
        {
            var allowShortName = (options & TypeNameOptions.ShortNames) != 0;
            var allowAliases   = (options & TypeNameOptions.Aliases) != 0;
            var allowArrays    = (options & TypeNameOptions.Arrays) != 0;

            if (parseNode.Type == TokenType.Identifier && parseNode.Count == 0 && allowShortName)
            {
                var typeName = default(string);
                if (allowAliases && TypeAliases.TryGetValue(parseNode.Value, out typeName))
                {
                    return(typeName);
                }
                else
                {
                    return(parseNode.Value);
                }
            }

            if (parseNode.Type == TokenType.Call && parseNode.Count == 2 && parseNode.Value == "[" && parseNode[1].Count == 0 && allowArrays)
            {
                var arrayNode     = new ParseTreeNode(parseNode.Token, TokenType.Identifier, typeof(Array).Name);
                var argumentsNode = new ParseTreeNode(parseNode.Token, TokenType.Arguments, "<");
                argumentsNode.Add(parseNode[0]);
                arrayNode.Add(argumentsNode);
                return(ToTypeName(arrayNode, TypeNameOptions.None));
            }

            var syntaxNode = new Dictionary <string, object>
            {
                { Constants.EXPRESSION_POSITION, parseNode.Token.Position },
                { Constants.EXPRESSION_TYPE_ATTRIBUTE, Constants.EXPRESSION_TYPE_MEMBER_RESOLVE },
            };

            if (parseNode.Type == TokenType.Resolve)
            {
                CheckNode(parseNode, 2, TokenType.None, TokenType.Identifier);
                syntaxNode[Constants.EXPRESSION_ATTRIBUTE]           = ToTypeName(parseNode[0], TypeNameOptions.None);
                syntaxNode[Constants.NAME_ATTRIBUTE]                 = parseNode[1].Value;
                syntaxNode[Constants.ARGUMENTS_ATTRIBUTE]            = PrepareTypeArguments(parseNode[1], 0);
                syntaxNode[Constants.USE_NULL_PROPAGATION_ATTRIBUTE] = Constants.FalseObject;
            }
            else if (parseNode.Type == TokenType.Identifier)
            {
                var typeName = parseNode.Value;
                if (allowAliases && TypeAliases.TryGetValue(parseNode.Value, out typeName) == false)
                {
                    typeName = parseNode.Value;
                }

                syntaxNode[Constants.EXPRESSION_ATTRIBUTE]           = null;
                syntaxNode[Constants.NAME_ATTRIBUTE]                 = typeName;
                syntaxNode[Constants.USE_NULL_PROPAGATION_ATTRIBUTE] = Constants.FalseObject;
                syntaxNode[Constants.ARGUMENTS_ATTRIBUTE]            = PrepareTypeArguments(parseNode, 0);
            }
            else
            {
                throw new ExpressionParserException(Properties.Resources.EXCEPTION_PARSER_TYPENAMEEXPECTED, parseNode);
            }

            return(new SyntaxTreeNode(syntaxNode));
        }