def _get_type_info_spec_qual_list(self, item: var_info, spec_qual_list: pp.ParseResults): # check if 'type_spec' not in spec_qual_list.keys(): # 構文構築上、type_specが存在しないのはありえない raise Exception("grammar is not preserve rule.") # 型情報取得 # type-qualifier # 未実装 # type-specifier if 'struct_spec' in spec_qual_list.keys(): # struct_specが存在するときは内部構造体定義 # 上位でチェックする raise Exception("grammar is not preserve rule.") else: # 基本型のとき item.type = " ".join(spec_qual_list.type_spec)
def _make_struct_info(self, tokens: pp.ParseResults, prefix: str = "", unnamed_cnt: int = 0) -> type_info: # grammarチェック if 'struct_spec' not in tokens.keys(): # 構文構築上、struct_specが存在しないのはありえない raise Exception("grammar is not preserve rule.") if 'struct_decl_list' not in tokens.struct_spec.keys(): # 不完全型のときにこのパスにくる # struct_decl_list が存在しないときは変数宣言 inf = self._make_struct_info_incomplete(tokens, prefix) return inf # struct/union情報取得 new_inf = type_info() # tag new_inf.set_struct_or_union(tokens.struct_spec.struct_or_union) # id if 'struct_id' in tokens.struct_spec.keys(): new_inf.id = prefix + tokens.struct_spec.struct_id[0] else: new_inf.id = prefix + "unnamed_" + str(unnamed_cnt) unnamed_cnt += 1 # comment取得 comment = None if 'comment' in tokens.struct_spec.keys(): comment = self._get_comment(tokens.struct_spec.comment) elif self._comment is not None: comment = self._comment self._comment = None new_inf.comment = comment # メンバ取得 for decl in tokens.struct_spec.struct_decl_list: mem_inf = var_info() # identifier mem_inf.id = decl.id[0] # comment if 'comment' in decl.keys(): comment = self._get_comment(decl.comment) elif 'comment_pre' in decl.keys(): comment = self._get_comment(decl.comment_pre) # type-spec if 'struct_spec' in decl.specifier_qualifier_list.keys(): # struct_specが存在するときは内部構造体定義 inner = self._make_struct_info(decl.specifier_qualifier_list, new_inf.id + "@", unnamed_cnt) new_inf.inner_type[inner.id] = inner # 型情報取得 mem_inf.type = inner else: # 通常変数 self._get_type_info_spec_qual_list( mem_inf, decl.specifier_qualifier_list) # member登録 new_inf.member[mem_inf.id] = mem_inf # 終了 return new_inf
def _get_type_info(self, tokens: pp.ParseResults) -> type_info: """ external_declを渡すこと。 external_decl情報から型情報を取得して返す。 (必要であれば専用のクラスを用意する) """ result = None if 'struct_spec' in tokens.keys(): # struct_specが存在するとき、struct/union id = tokens.struct_spec.struct_id[0] # 型情報有無チェック tmp_inf = self._get_type_info_by_id(id) if tmp_inf is None: # 型情報作成 tmp_inf = self._make_struct_info(tokens) # result = self._add_type_info(tmp_inf) else: # 存在するなら何もしない? result = tmp_inf elif 'enum_spec' in tokens.keys(): # とりあえずenumは無視 pass elif 'decl_spec' in tokens.keys(): # struct_specが存在せずdecl_specが存在するとき基本型 id = " ".join(tokens.decl_spec) # 型情報有無チェック tmp_inf = self._get_type_info_by_id(id) if tmp_inf is None: # 型情報作成 tmp_inf = self._make_base_type_info(tokens) # result = self._add_type_info(tmp_inf) else: # 存在するなら何もしない? result = tmp_inf else: # その他ケース? pass # 結果を返す return result
def _hdl_external_declaration(self, loc: int, tokens: pp.ParseResults): # グローバル定義の開始 # begin/endはempty()で実装してるので、次のtokenと同じlocになる # よって、locチェックしない print("external_declaration" + ":" + str(loc) + str(tokens)) if 'external_decl' not in tokens.keys(): # 構文構築上、external_declが存在しないのはありえない raise Exception("grammar is not preserve rule.") if 'typedef' in tokens.external_decl.keys(): self._analyze_external_decl_typedef(loc, tokens.external_decl) elif 'struct_spec' in tokens.external_decl.keys(): self._analyze_external_decl_struct(loc, tokens.external_decl) else: self._analyze_external_decl_var(loc, tokens.external_decl) # 事前コメントクリア self._comment = None
def _make_var_info(self, tokens: pp.ParseResults, type_inf: type_info): # comment comment = None if 'comment' in tokens.keys(): comment = self._get_comment(tokens.comment) # init-declarator-list for declarator in tokens.declarator_list: new_var = var_info() new_var.type = type_inf # pointerチェック if 'pointer' in declarator.declarator.keys(): new_var.pointer = " ".join(declarator.pointer) # idチェック if 'id' not in declarator.declarator.keys(): # 構文構築上、external_declが存在しないのはありえない raise Exception("grammar is not preserve rule.") new_var.id = declarator.declarator.id[0] # comment new_var.comment = comment # 追加 self._add_var_info(new_var)
def _analyze_external_decl_struct(self, loc: int, tokens: pp.ParseResults): # tokensを解析して構造体情報を取得 type_inf = self._get_type_info(tokens) # declaratorが存在するときは変数情報も登録 if 'declarator_list' in tokens.keys(): self._make_var_info(tokens, type_inf)