Exemplo n.º 1
0
        public static SymbolDeclarationNode Parse(ContextNode context, IAbstractSyntaxTree lexerNode)
        {
            Contract.Requires(lexerNode.Type == Lexer.TokenType.DeclarationNode);
            var            info         = DeclarationInfo.Parse(context.Parser, lexerNode);
            var            name         = info.SymbolName.GetSingleSymbolOrThrow();
            var            declaredType = TypeNode.Parse(context, info.Type);
            var            point        = context.Parser.GetSequencePoint(lexerNode);
            ExpressionNode initializer  = info.Initializer == null ? null : ExpressionNode.Parse(context, info.Initializer, declaredType);

            return(Create(context, info.Modifiers, declaredType, name, initializer, point));
        }
        private void ParseDeclaration(IAbstractSyntaxTree lexerNode)
        {
            var declaration = DeclarationInfo.Parse(Parser, lexerNode);

            if (declaration.Initializer != null && declaration.Initializer.IsFunctionDeclaration() && !declaration.Modifiers.HasFlag(Modifiers.Mutable))
            {
                //method
                var method = FunctionDeclarationNode.ParseAsMethod(this, declaration);
                declaredMethods.Add(method);
            }
            else
            {
                //field
                var field = new FieldDeclarationNode(this, declaration, Parser.GetSequencePoint(lexerNode));
                fields.Add(field);
            }
        }
        public static DeclarationInfo Parse(Parser parser, IAbstractSyntaxTree lexerNode)
        {
            DeclarationInfo instance = new DeclarationInfo();

            instance.Point = parser.GetSequencePoint(lexerNode);
            foreach (var node in lexerNode.Children)
            {
                switch (node.Type)
                {
                case Lexer.TokenType.VariableModifier:
                    instance.Modifiers = instance.Modifiers.AddModifier(parser, node);
                    break;

                case Lexer.TokenType.Symbol:
                    instance.SymbolName = node;
                    break;

                case Lexer.TokenType.Type:
                    instance.Type = node;
                    break;

                case Lexer.TokenType.Value:
                    instance.Initializer = node;
                    break;

                case Lexer.TokenType.Assignment:
                case Lexer.TokenType.EndOfLine:
                    break;

                default:
                    ErrorCode.InvalidStructure.ReportAndThrow(parser.GetSequencePoint(node), String.Format("Unexpected node in declaration: {0}", node.Type));
                    break;
                }
            }

            if (instance.SymbolName == null || instance.Type == null)
            {
                ErrorCode.InvalidStructure.ReportAndThrow(parser.GetSequencePoint(lexerNode),
                                                          "Missing elements in declaration {0}, lexer messed up", lexerNode.Content);
            }

            return(instance);
        }
        public FieldDeclarationNode(ClassNode parent, DeclarationInfo declaration, SequencePoint point)
            : base(parent.Parser, parent, point)
        {
            this.IsStatic      = true;
            this.initializer   = declaration.Initializer;
            this.Name          = declaration.SymbolName.GetSingleSymbolOrThrow();
            this.TypeReference = TypeNode.Parse(this, declaration.Type);
            this.modifiers     = declaration.Modifiers;
            this.field         = new Lazy <FieldDefinition>(() => new FieldDefinition(Name, GetAttributes(), TypeReference));

            if (TypeReference.IsAuto() && declaration.Initializer != null && declaration.Initializer.IsFunctionDeclaration())
            {
                TypeReference = FunctionDeclarationNode.ParseFunctorType(parent, declaration.Initializer);
            }

            if (!TypeReference.IsAuto())
            {
                DeclareField();
            }
        }
        public static FunctionDeclarationNode ParseAsMethod(ClassNode parent, DeclarationInfo declaration)
        {
            var instance = new FunctionDeclarationNode(parent, declaration.Modifiers, declaration.SymbolName.GetSingleSymbolOrThrow(), declaration.Initializer.Children[0]);

            return(instance);
        }