Beispiel #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);
        }
Beispiel #2
0
 public void AddParameterNode(ASTNodeDeclFunctParam param)
 {
     param.SetParent(this);
     this.AddSpan(param.GetSpan());
     this.parameters.Add(param);
 }