Example #1
0
        /*
         * function_definition : decl_specs declarator decl_list compound_stat
         | declarator decl_list compound_stat
         | decl_specs declarator	compound_stat
         | declarator compound_stat
         |  ;
         |
         | external_decl : function_definition
         | decl
         |  ;
         */

        private IParseResult ParseExternalDecl()
        {
            var declSpecs = ParseDeclSpecs();

            if (!declSpecs.IsSuccess)
            {
                return(declSpecs);
            }

            if (!declSpecs.IsNullStat() && Accept(OperatorType.SEMICOLON))
            {
                return(new SuccessParseResult(new Decl(declSpecs.ResultNode as DeclSpecs, new InitDeclaratorList())));
            }

            var initDeclarator = ParseInitDeclarator();

            if (!initDeclarator.IsSuccess)
            {
                return(initDeclarator);
            }
            var initDeclaratorList = new InitDeclaratorList();

            initDeclaratorList.Add(initDeclarator.ResultNode);
            if (!declSpecs.IsNullStat() && Accept(OperatorType.SEMICOLON))
            {
                return(new SuccessParseResult(new Decl(declSpecs.ResultNode as DeclSpecs, initDeclaratorList)));
            }

            var declList = ParseDeclList();

            if (!declList.IsSuccess)
            {
                return(declList);
            }

            var compoundStat = ParseCompoundStat();

            if (!compoundStat.IsSuccess)
            {
                return(compoundStat);
            }

            if (initDeclarator.ResultNode is InitDeclarator declarator && !compoundStat.IsNullStat())
            {
                return(new SuccessParseResult(new FuncDef(declSpecs.ResultNode, declarator,
                                                          declList.ResultNode, compoundStat.ResultNode as CompoundStat)));
            }

            if (initDeclarator.IsNullStat() && declSpecs.IsNullStat() && declList.IsNullStat())
            {
                return(new SuccessParseResult(new NullStat()));
            }

            return(new FailedParseResult("the external declaration is incorrectly", _currentToken));
        }
Example #2
0
 public Decl(DeclSpecs declSpec, InitDeclaratorList initDeclaratorList)
 {
     DeclSpec           = declSpec;
     InitDeclaratorList = initDeclaratorList;
 }