コード例 #1
0
        public ASTNodeDeclFunct ParseDeclFunct(bool withBody)
        {
            var functNode = new ASTNodeDeclFunct();

            functNode.SetSpan(this.Current().span);

            this.Match(TokenKind.KeywordFn, "expected 'fn'");
            functNode.SetNameNode(this.ParseName(false, false));

            // Parse parameter list.
            this.Match(TokenKind.ParenOpen, "expected '('");
            while (this.CurrentIsNot(TokenKind.ParenClose))
            {
                var paramNode = new ASTNodeDeclFunctParam();
                paramNode.SetNameNode(this.ParseName(false, true));
                this.Match(TokenKind.Colon, "expected ':'");
                paramNode.SetTypeNode(this.ParseType());
                functNode.AddParameterNode(paramNode);
                if (this.Current().kind == TokenKind.Comma)
                {
                    this.Advance();
                }
                else if (this.Current().kind != TokenKind.ParenClose)
                {
                    throw this.FatalAfterPrevious(MessageCode.Expected, "expected ',' or ')'");
                }
            }
            this.Match(TokenKind.ParenClose, "expected ')'");

            // Parse return type.
            if (this.CurrentIs(TokenKind.Arrow))
            {
                this.Advance();
                functNode.SetReturnTypeNode(this.ParseType());
            }

            // Parse body.
            if (withBody)
            {
                functNode.SetBodyNode(this.ParseExprBlock());
            }

            return(functNode);
        }
コード例 #2
0
ファイル: decl_group.cs プロジェクト: hlorenzi/trapl
 public void AddFunctDeclNode(ASTNodeDeclFunct decl)
 {
     decl.SetParent(this);
     this.AddSpan(decl.GetSpan());
     this.functDecls.Add(decl);
 }