//------------------------------------------------------------ // コンストラクタ。 public TypeSymbolNode(ISymbolNode aParent, BCModule aBCModule, StaticTypeDef aStaticTypeDef) { mParent = aParent; mBCModule = aBCModule; mStaticTypeDef = aStaticTypeDef; mNodeList = new SymbolNodeList(); }
//------------------------------------------------------------ /// <summary> /// ModuleDef: /// StaticTypeDef: /// </summary> bool parseModuleDef(ModuleContext aModuleContext) { StaticTypeDef staticTypeDef = parseStaticTypeDef(Protection.Public, aModuleContext.ModuleDecl.IsProtoType); if (staticTypeDef == null) { return(false); } if (staticTypeDef.TypeProtection != Protection.Public) { setErrorKind(ErrorKind.MODULE_DEF_TYPE_PROTECTION_MUST_BE_PUBLIC); return(false); } // 設定 aModuleContext.ModuleDef = new ModuleDef(staticTypeDef); return(true); }
//------------------------------------------------------------ // コンストラクタ。 public ModuleDef(StaticTypeDef aStaticTypeDef) { StaticTypeDef = aStaticTypeDef; }
//------------------------------------------------------------ // 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); }
//------------------------------------------------------------ // StaticTypeDef bool parseStaticTypeStandardDef(StaticTypeDef aTD, bool aIsProtoType) { // 最初はIdentifierのはず if (currentToken().Value != Token.Kind.Identifier) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_IDENTIFIER_EXPECTED); return(false); } aTD.Ident = new Identifier(currentToken()); nextToken(); if (currentToken().Value == Token.Kind.OpColon) {// 継承 if (aTD.TypeKind != StaticTypeDef.Kind.Class || aTD.TypeKind != StaticTypeDef.Kind.Interface ) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_ONLY_CLASS_OR_INTERFACE_CAN_IMPLEMENT_INTERFACE); return(false); } nextToken(); // IdentifierPath , のループ while (true) { // 継承するインターフェースのパス IdentPath typeIdentPath = parseIdentPath(); if (typeIdentPath == null) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_IDENTIFIER_EXPECTED); return(false); } // 登録 aTD.InheritTypeList.Add(new InheritType(typeIdentPath)); if (currentToken().Value == Token.Kind.OpColon) {// 次のインターフェースへ nextToken(); continue; } break; } } // 開始括弧 if (currentToken().Value != Token.Kind.OpLCurly) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_LCURLY_EXPECTED); return(false); } nextToken(); // 内部実装ループ { Protection defaultProtection = Protection.Public; while (currentToken().Value != Token.Kind.OpRCurly && currentToken().Value != Token.Kind.EOF ) { // Protection if (parseTypeProtection(currentToken()) != Protection.Unknown && currentToken().Next.Value == Token.Kind.OpColon ) {// DefaultProtectionChange defaultProtection = parseTypeProtection(currentToken()); if (!checkProtection(defaultProtection, aIsProtoType)) { return(false); } nextToken(); nextToken(); continue; } // Symbol SymbolDef symbolDef = parseSymbolDef(aTD, defaultProtection, aIsProtoType); if (symbolDef == null) { return(false); } aTD.SymbolDefList.Add(symbolDef); } } // 終端括弧 if (currentToken().Value != Token.Kind.OpRCurly) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_RCURLY_EXPECTED); return(false); } nextToken(); // セミコロン if (currentToken().Value != Token.Kind.OpSemicolon) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_SEMICOLON_EXPECTED); return(false); } nextToken(); return(true); }
//------------------------------------------------------------ // TypedefDef bool parseTypedefDef(StaticTypeDef aTD) { // todo: impl return(false); }
//------------------------------------------------------------ /// <summary> /// StaticTypeDef: /// ClassDef /// EnumDef /// InterfaceDef /// PodDef /// TypedefDef /// UtilityDef /// </summary> StaticTypeDef parseStaticTypeDef(Protection aDefaultTypeProtection, bool aIsProtoType) { StaticTypeDef.Kind kind = StaticTypeDef.Kind.Unknown; Protection typeProtection = Protection.DEFAULT; // 前修飾解析 while (true) { Token t = currentToken(); {// typeProtection Protection tokenTypeProtection = parseTypeProtection(t); if (tokenTypeProtection != Protection.Unknown) { if (typeProtection != Protection.DEFAULT) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_ALREADY_ASIGNED_TYPE_PROTECTION); return(null); } typeProtection = tokenTypeProtection; nextToken(); continue; } } {// kind StaticTypeDef.Kind tokenStaticTypeKind = parseStaticTypeKind(t); if (tokenStaticTypeKind != StaticTypeDef.Kind.Unknown) { if (kind != StaticTypeDef.Kind.Unknown) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_ALREADY_ASIGNED_TYPE_KIND); return(null); } kind = tokenStaticTypeKind; nextToken(); continue; } } // exit break; } // デフォルト値を設定 typeProtection = getProtectionWithDefaultValue(typeProtection, aDefaultTypeProtection); // 種類の指定確認 if (kind == StaticTypeDef.Kind.Unknown) { setErrorKind(ErrorKind.STATIC_TYPE_DEF_TYPE_KEYWORD_EXPECTED); return(null); } // 型の解析 StaticTypeDef staticTypeDef = new StaticTypeDef(kind, typeProtection); if (kind == StaticTypeDef.Kind.Enum) {// enum if (!parseEnumDef(staticTypeDef)) { return(null); } } else if (kind == StaticTypeDef.Kind.Typedef) {// typedef if (!parseTypedefDef(staticTypeDef)) { return(null); } } else {// other if (!parseStaticTypeStandardDef(staticTypeDef, aIsProtoType)) { return(null); } } return(staticTypeDef); }
//------------------------------------------------------------ // 型の定義としてコンストラクトする。 public SymbolDef(StaticTypeDef aST) { SymbolKind = Kind.StaticTypeDef; StaticTypeDef = aST; }