Ejemplo n.º 1
0
 //------------------------------------------------------------
 // コンストラクタ。
 public MemberFunctionDecl(
     Identifier aIdent
     , FunctionReturnValueDecl aReturnValueDecl
     , FunctionArgumentDeclList aArgDeclList
     , BlockStatement aStatement
     , bool aIsAbstract
     , bool aIsConst
     , bool aIsOverride
     , bool aIsStatic
     )
 {
     Ident           = aIdent;
     ReturnValueDecl = aReturnValueDecl;
     ArgDeclList     = aArgDeclList;
     mStatement      = aStatement;
     mIsAbstract     = aIsAbstract;
     mIsConst        = aIsConst;
     mIsOverride     = aIsOverride;
     mIsStatic       = aIsStatic;
 }
Ejemplo n.º 2
0
        //------------------------------------------------------------
        // 関数の引数リスト。
        // currentToken()が'('の位置にセットしておくこと。
        FunctionArgumentDeclList parseFunctionArgumentDeclList()
        {
            // '('
            if (currentToken().Value != Token.Kind.OpLParen)
            {
                setErrorKind(ErrorKind.FUNCTION_ARGUMENT_DECL_LIST_LPAREN_EXPECTED);
                return(null);
            }
            nextToken();

            // ArgDeclList
            FunctionArgumentDeclList argDeclList = new FunctionArgumentDeclList();

            while (currentToken().Value != Token.Kind.OpRParen)
            {
                // 2つめ以降は',' があるはず
                if (argDeclList.Count() != 0)
                {
                    if (currentToken().Value != Token.Kind.OpComma)
                    {
                        setErrorKind(ErrorKind.FUNCTION_ARGUMENT_DECL_LIST_COMMA_EXPECTED);
                        return(null);
                    }
                }
                nextToken();

                // FunctionArgumentDecl
                FunctionArgumentDecl argDecl = parseFunctionArgumentDecl();
                if (argDecl == null)
                {
                    return(null);
                }
                argDeclList.Add(argDecl);
            }
            nextToken();

            return(argDeclList);
        }
Ejemplo n.º 3
0
        //------------------------------------------------------------
        // SymbolDef。
        SymbolDef parseSymbolDef(StaticTypeDef aTD, Protection aDefaultProtection, bool aIsProtoType)
        {
            Protection protect    = Protection.DEFAULT;
            Token      isAbstract = null;
            Token      isConst    = null;
            Token      isOverride = null;
            Token      isReadonly = null;
            Token      isRef      = null;
            Token      isStatic   = null;

            while (true)
            {         // pre
                Token t = currentToken();
                {     // attribute
                    { // ChangeProtection
                        Protection tmpProtection = parseTypeProtection(t);
                        if (tmpProtection != Protection.Unknown)
                        {// set protect
                            if (protect != Protection.DEFAULT)
                            {
                                setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASIGNED_TYPE_PROTECTION);
                                return(null);
                            }
                            protect = tmpProtection;
                            if (!checkProtection(protect, aIsProtoType))
                            {
                                return(null);
                            }
                            nextToken();
                            continue;
                        }
                    }
                    if (t.Value == Token.Kind.KeyAbstract)
                    {// SetAbstractAttribute
                        if (aTD.TypeKind != StaticTypeDef.Kind.Interface)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ONLY_INTERFACE_TYPE_CAN_ABSTRACT_ATTRIBUTE_MEMBER);
                            return(null);
                        }
                        if (isAbstract != null)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_ABSTRACT);
                            return(null);
                        }
                        isAbstract = currentToken();
                        nextToken();
                        continue;
                    }
                    if (t.Value == Token.Kind.KeyConst)
                    {// SetConstAttribute
                        if (isConst != null)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_CONST);
                            return(null);
                        }
                        isConst = currentToken();
                        nextToken();
                        continue;
                    }
                    if (t.Value == Token.Kind.KeyOverride)
                    {// SetOverrideAttribute
                        if (aTD.TypeKind != StaticTypeDef.Kind.Class)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ONLY_CLASS_TYPE_CAN_OVERRIDE_ATTRIBUTE_MEMBER);
                            return(null);
                        }
                        if (isConst != null)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_OVERRIDE);
                            return(null);
                        }
                        isOverride = currentToken();
                        nextToken();
                        continue;
                    }
                    if (t.Value == Token.Kind.KeyRef)
                    {// SetRefAttribute
                        if (isRef != null)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_REF);
                            return(null);
                        }
                        isRef = currentToken();
                        nextToken();
                        continue;
                    }
                    if (t.Value == Token.Kind.KeyReadonly)
                    {// SetReadonlyAttribute
                        if (isReadonly != null)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_READONLY);
                            return(null);
                        }
                        isReadonly = currentToken();
                        nextToken();
                        continue;
                    }
                    if (t.Value == Token.Kind.KeyStatic)
                    {// SetStaticAttribute
                        if (isStatic != null)
                        {
                            setErrorKind(ErrorKind.SYMBOL_DEF_ALREADY_ASSIGNED_ATTRIBUTE_STATIC);
                            return(null);
                        }
                        isStatic = currentToken();
                        nextToken();
                        continue;
                    }
                }

                // シンボルの実装の前準備
                protect = getProtectionWithDefaultValue(protect, aDefaultProtection);

                {// StaticTypeDef
                    StaticTypeDef.Kind typeKind = parseStaticTypeKind(t);
                    if (typeKind != StaticTypeDef.Kind.Unknown)
                    {
                        // 無効な属性のチェック
                        if (checkErrorSymbolDefIllegalAttr(isAbstract, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) ||
                            checkErrorSymbolDefIllegalAttr(isConst, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) ||
                            checkErrorSymbolDefIllegalAttr(isOverride, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) ||
                            checkErrorSymbolDefIllegalAttr(isReadonly, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) ||
                            checkErrorSymbolDefIllegalAttr(isRef, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF) ||
                            checkErrorSymbolDefIllegalAttr(isStatic, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_STATIC_TYPE_DEF)
                            )
                        {
                            return(null);
                        }
                        // 解析
                        StaticTypeDef staticTypeDef = parseStaticTypeDef(protect, aIsProtoType);
                        if (staticTypeDef == null)
                        {
                            return(null);
                        }
                        return(new SymbolDef(staticTypeDef));
                    }
                }
                {// StaticCtorDef,CtorDef
                 // todo: impl
                }
                {// StaticDtorDef,DtorDef
                 // todo: impl
                }
                {// MemberDef
                    // 戻り値の型もしくは変数の型
                    TypePath firstTypePath = parseTypePath();
                    if (firstTypePath != null)
                    {
                        // メンバ名
                        if (currentToken().Value != Token.Kind.Identifier)
                        {
                            setErrorKind(ErrorKind.MEMBER_DEF_IDENTIFIER_EXPECTED);
                            return(null);
                        }
                        Identifier nameIdent = new Identifier(currentToken());
                        nextToken();

                        if (currentToken().Value == Token.Kind.OpAssign ||
                            currentToken().Value == Token.Kind.OpSemicolon
                            )
                        {// '=' or ';' ならMemberVariableDecl
                            // エラーチェック
                            if (checkErrorSymbolDefIllegalAttr(isAbstract, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_MEMBER_VARIABLE_DECL) ||
                                checkErrorSymbolDefIllegalAttr(isOverride, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_MEMBER_VARIABLE_DECL) ||
                                checkErrorSymbolDefIllegalAttr(isRef, ErrorKind.SYMBOL_DEF_ILLEGAL_ATTRIBUTE_FOR_MEMBER_VARIABLE_DECL)
                                )
                            {
                                return(null);
                            }
                            if (isStatic == null)
                            {
                                if (aTD.TypeKind == StaticTypeDef.Kind.Interface)
                                {// interface型は非staticメンバ変数をもてない
                                    setErrorKind(ErrorKind.MEMBER_VARIABLE_DECL_INTERFACE_CANT_HAVE_NONSTATIC_MEMBER_VARIABLE);
                                    return(null);
                                }
                                if (aTD.TypeKind == StaticTypeDef.Kind.Utility)
                                {// utility型は非staticメンバ変数をもてない
                                    setErrorKind(ErrorKind.MEMBER_VARIABLE_DECL_UTILITY_CANT_HAVE_NONSTATIC_MEMBER_VARIABLE);
                                    return(null);
                                }
                            }

                            // 右辺
                            IExpression expr = null;
                            if (currentToken().Value == Token.Kind.OpAssign)
                            {
                                nextToken();

                                // 右辺解析
                                expr = parseConditionalExpression();
                            }

                            // ';'
                            if (currentToken().Value != Token.Kind.OpSemicolon)
                            {
                                setErrorKind(ErrorKind.SYMBOL_DEF_SEMICOLON_EXPECTED);
                                return(null);
                            }
                            nextToken();

                            return(new SymbolDef(
                                       new MemberVariableDecl(
                                           new VariableDecl(firstTypePath, nameIdent, expr)
                                           , isStatic != null
                                           , isConst != null
                                           , isReadonly != null
                                           )
                                       ));
                        }
                        else if (currentToken().Value == Token.Kind.OpLParen)
                        {// '(' ならMemberFunctionDecl
                            // 戻り値
                            FunctionReturnValueDecl retValueDecl = new FunctionReturnValueDecl(
                                firstTypePath
                                , isConst != null
                                , isRef != null
                                );

                            // 引数リスト
                            FunctionArgumentDeclList argDeclList = parseFunctionArgumentDeclList();
                            if (argDeclList == null)
                            {
                                return(null);
                            }

                            // ) の後のconst
                            Token isFunctionConst = null;
                            if (currentToken().Value == Token.Kind.KeyConst)
                            {
                                isFunctionConst = currentToken();
                                nextToken();
                            }

                            // '{' or ';'
                            BlockStatement blockStatement = null;
                            if (isAbstract != null || aIsProtoType)
                            {// 宣言のみ。セミコロンがくるはず。
                                if (currentToken().Value != Token.Kind.OpSemicolon)
                                {
                                    setErrorKind(ErrorKind.MEMBER_FUNCTION_DECL_SEMICOLON_EXPECTED);
                                    return(null);
                                }
                                nextToken();
                            }
                            else
                            {// non abstract function
                                if (currentToken().Value != Token.Kind.OpLCurly)
                                {
                                    setErrorKind(ErrorKind.MEMBER_FUNCTION_DECL_LCURLY_EXPECTED);
                                    return(null);
                                }
                                // BlockStatement
                                blockStatement = parseBlockStatement();
                                if (blockStatement == null)
                                {
                                    return(null);
                                }
                            }

                            // 作成
                            return(new SymbolDef(new MemberFunctionDecl(
                                                     nameIdent
                                                     , retValueDecl
                                                     , argDeclList
                                                     , blockStatement
                                                     , isAbstract != null
                                                     , isFunctionConst != null
                                                     , isOverride != null
                                                     , isStatic != null
                                                     )));
                        }
                        else
                        {// エラー
                            setErrorKind(ErrorKind.SYMBOL_DEF_ILLEGAL_MEMBER_SYNTAX);
                            return(null);
                        }
                    }
                }
                break;
            }
            return(null);
        }