def default_parameter_declaration(tokens, symbol_table): base_type = symbol_table['__ specifier_qualifier_list __'](tokens, symbol_table) c_decl = AbstractDeclarator(base_type, loc(base_type)) token = peek_or_terminal(tokens) if token in {TOKENS.STAR, TOKENS.LEFT_PARENTHESIS, TOKENS.LEFT_BRACKET} or isinstance(token, IDENTIFIER): c_decl = abstract_declarator(tokens, symbol_table) set_core_type(c_decl, base_type) return c_decl
def type_name(tokens, symbol_table): #: type_specifier abstract_declarator? # returns CType base_type = specifier_qualifier_list(tokens, symbol_table) abstract_declarator = symbol_table['__ abstract_declarator __'] if peek_or_terminal(tokens) in {TOKENS.LEFT_PARENTHESIS, TOKENS.LEFT_BRACKET, TOKENS.STAR} \ or isinstance(peek_or_terminal(tokens), IDENTIFIER): abs_decl = abstract_declarator(tokens, symbol_table) set_core_type(abs_decl, base_type) return c_type(abs_decl) return base_type
def type_name(tokens, symbol_table ): #: type_specifier abstract_declarator? # returns CType base_type = specifier_qualifier_list(tokens, symbol_table) abstract_declarator = symbol_table['__ abstract_declarator __'] if peek_or_terminal(tokens) in {TOKENS.LEFT_PARENTHESIS, TOKENS.LEFT_BRACKET, TOKENS.STAR} \ or isinstance(peek_or_terminal(tokens), IDENTIFIER): abs_decl = abstract_declarator(tokens, symbol_table) set_core_type(abs_decl, base_type) return c_type(abs_decl) return base_type
def default_parameter_declaration(tokens, symbol_table): base_type = symbol_table['__ specifier_qualifier_list __'](tokens, symbol_table) c_decl = AbstractDeclarator(base_type, loc(base_type)) token = peek_or_terminal(tokens) if token in {TOKENS.STAR, TOKENS.LEFT_PARENTHESIS, TOKENS.LEFT_BRACKET } or isinstance(token, IDENTIFIER): c_decl = abstract_declarator(tokens, symbol_table) set_core_type(c_decl, base_type) return c_decl
def init_declarator(tokens, symbol_table, base_type=CType(''), storage_class=None): # : declarator ('=' assignment_expression or initializer)? decl = set_core_type( symbol_table['__ declarator __'](tokens, symbol_table), base_type) if peek_or_terminal(tokens) == TOKENS.EQUAL and consume(tokens): decl = Definition(name(decl), c_type(decl), EmptyExpression(c_type(decl)), loc(decl), storage_class) symbol_table[name( decl )] = decl # we have to add it to the symbol table for things like `int a = a;` expr = initializer_or_assignment_expression(tokens, symbol_table) # if declaration is an array type and the expression is of string_type then convert to initializer for parsing if isinstance(c_type(decl), ArrayType) and isinstance( c_type(expr), StringType): expr = Initializer( enumerate(exp(expr)), ArrayType(c_type(c_type(expr)), len(c_type(expr)), loc(expr)), loc(expr)) decl.initialization = parse_initializer(expr, decl) if isinstance( expr, Initializer) else expr else: symbol_table[name(decl)] = decl = Declaration(name(decl), c_type(decl), loc(decl)) return decl
def parse_struct_members(tokens, symbol_table): declarator = symbol_table['__ declarator __'] location, members = loc(consume(tokens)), OrderedDict() while peek(tokens, TOKENS.RIGHT_BRACE) != TOKENS.RIGHT_BRACE: type_spec = specifier_qualifier_list(tokens, symbol_table) while peek(tokens, TOKENS.SEMICOLON) != TOKENS.SEMICOLON: decl = declarator(tokens, symbol_table) set_core_type(decl, type_spec) if name(decl) in members: raise ValueError('{l} Duplicate struct member {name} previous at {at}'.format( l=loc(decl), name=name(decl), at=loc(members[name(decl)]) )) members[name(decl)] = decl _ = peek_or_terminal(tokens) != TOKENS.SEMICOLON and error_if_not_value(tokens, TOKENS.COMMA) _ = error_if_not_value(tokens, TOKENS.SEMICOLON) _ = error_if_not_value(tokens, TOKENS.RIGHT_BRACE) return members
def pointer_type_abstract_declarator(tokens, symbol_table): decl = AbstractDeclarator(pointer(tokens, symbol_table), loc(peek_or_terminal(tokens))) if get_rule(direct_abstract_declarator, peek_or_terminal(tokens), None, hash_funcs=(type, identity)) is not None: decl = set_core_type(direct_abstract_declarator(tokens, symbol_table), c_type(decl)) return decl
def parse_struct_members(tokens, symbol_table): declarator = symbol_table['__ declarator __'] location, members = loc(consume(tokens)), OrderedDict() while peek(tokens, TOKENS.RIGHT_BRACE) != TOKENS.RIGHT_BRACE: type_spec = specifier_qualifier_list(tokens, symbol_table) while peek(tokens, TOKENS.SEMICOLON) != TOKENS.SEMICOLON: decl = declarator(tokens, symbol_table) set_core_type(decl, type_spec) if name(decl) in members: raise ValueError( '{l} Duplicate struct member {name} previous at {at}'. format(l=loc(decl), name=name(decl), at=loc(members[name(decl)]))) members[name(decl)] = decl _ = peek_or_terminal( tokens) != TOKENS.SEMICOLON and error_if_not_value( tokens, TOKENS.COMMA) _ = error_if_not_value(tokens, TOKENS.SEMICOLON) _ = error_if_not_value(tokens, TOKENS.RIGHT_BRACE) return members
def direct_declarator(tokens, symbol_table): """ : (IDENTIFIER | '(' declarator ')') declarator_suffix* declarator_suffix : '[' constant_expression ']' | '[' ']' | '(' parameter_type_list ')' | '(' ')' """ dec = get_rule(direct_declarator, peek_or_terminal(tokens), hash_funcs=(type, identity))(tokens, symbol_table) _ = peek_or_terminal(tokens) in rules(declarator_suffix) and set_core_type( dec, declarator_suffix(tokens, symbol_table)) return dec
def direct_declarator(tokens, symbol_table): """ : (IDENTIFIER | '(' declarator ')') declarator_suffix* declarator_suffix : '[' constant_expression ']' | '[' ']' | '(' parameter_type_list ')' | '(' ')' """ dec = get_rule(direct_declarator, peek_or_terminal(tokens), hash_funcs=(type, identity))(tokens, symbol_table) _ = peek_or_terminal(tokens) in rules(declarator_suffix) and set_core_type( dec, declarator_suffix(tokens, symbol_table)) return dec
def init_declarator(tokens, symbol_table, base_type=CType(''), storage_class=None): # : declarator ('=' assignment_expression or initializer)? decl = set_core_type(symbol_table['__ declarator __'](tokens, symbol_table), base_type) if peek_or_terminal(tokens) == TOKENS.EQUAL and consume(tokens): decl = Definition(name(decl), c_type(decl), EmptyExpression(c_type(decl)), loc(decl), storage_class) symbol_table[name(decl)] = decl # we have to add it to the symbol table for things like `int a = a;` expr = initializer_or_assignment_expression(tokens, symbol_table) # if declaration is an array type and the expression is of string_type then convert to initializer for parsing if isinstance(c_type(decl), ArrayType) and isinstance(c_type(expr), StringType): expr = Initializer( enumerate(exp(expr)), ArrayType(c_type(c_type(expr)), len(c_type(expr)), loc(expr)), loc(expr) ) decl.initialization = parse_initializer(expr, decl) if isinstance(expr, Initializer) else expr else: symbol_table[name(decl)] = decl = Declaration(name(decl), c_type(decl), loc(decl)) return decl
def pointer_type_declarator(tokens, symbol_table): pointer_type = pointer(tokens, symbol_table) return set_core_type(direct_declarator(tokens, symbol_table), pointer_type)
def pointer_type_abstract_declarator(tokens, symbol_table): decl = AbstractDeclarator(pointer(tokens, symbol_table), loc(peek_or_terminal(tokens))) if get_rule(direct_abstract_declarator, peek_or_terminal(tokens), None, hash_funcs=(type, identity)) is not None: decl = set_core_type(direct_abstract_declarator(tokens, symbol_table), c_type(decl)) return decl
def pointer_type_declarator(tokens, symbol_table): pointer_type = pointer(tokens, symbol_table) return set_core_type(direct_declarator(tokens, symbol_table), pointer_type)