codetable accessors now convert

This commit is contained in:
kevstone 2024-02-13 15:39:02 +00:00
parent b92106b350
commit fcdb83a1d1
4 changed files with 64 additions and 23 deletions

View File

@ -14,7 +14,6 @@ grib_accessor_class_bytes.cc
grib_accessor_class_double.cc
grib_accessor_class_long.cc
grib_accessor_class_proj_string.cc
# In progress...
grib_accessor_class_blob.cc
#grib_accessor_class_bufr_data_array.cc
#grib_accessor_class_bufr_data_element.cc
@ -24,9 +23,9 @@ grib_accessor_class_blob.cc
#grib_accessor_class_bufr_extract_subsets.cc
#grib_accessor_class_bufr_simple_thinning.cc
grib_accessor_class_change_alternative_row_scanning.cc
#grib_accessor_class_change_scanning_direction.cc
#grib_accessor_class_codetable_title.cc
#grib_accessor_class_codetable_units.cc
grib_accessor_class_change_scanning_direction.cc
grib_accessor_class_codetable_title.cc
grib_accessor_class_codetable_units.cc
#grib_accessor_class_concept.cc
#grib_accessor_class_data_apply_bitmap.cc
#grib_accessor_class_data_apply_boustrophedonic.cc

View File

@ -692,33 +692,19 @@ class AstParser:
return value_declaration_reference.ValueDeclarationReference(node.spelling)
# The top-level node contains the tokens showing the access, e.g. [['self', '->', 'grid_type']]
# The child node defines the first parameter ("self" in this case)
# However, as we're just storing strings, we'll use the tokens directly!
def parse_MEMBER_REF_EXPR(self, node):
debug.line("parse_MEMBER_REF_EXPR", f"Nodes...")
ast_utils.dump_node(node)
tokens = [token.spelling for token in node.get_tokens()]
debug.line("parse_MEMBER_REF_EXPR", f"[IN] node spelling=[{node.spelling}] type=[{node.type.spelling}] tokens=[{tokens}]")
assert len(tokens) >= 3, f"Expected at least 3 tokens for member ref, but got [{len(tokens)}]"
# find the index of the first -> or .
pointer_index = -1
for i in range(len(tokens)):
if tokens[i] in ["->", "."]:
pointer_index = i
break
debug.line("parse_MEMBER_REF_EXPR", f"Matched [{tokens[pointer_index]}] at pointer_index=[{pointer_index}]")
cstruct_member_access = struct_member_access.StructMemberAccess(None, ''.join(t for t in tokens[:pointer_index]), None)
tokens = tokens[pointer_index:]
next_cmember = cstruct_member_access
while len(tokens) > 0:
debug.line("parse_MEMBER_REF_EXPR", f" remaining tokens=[{tokens}]")
next_cmember.member = struct_member_access.StructMemberAccess(tokens[0], tokens[1], None)
tokens = tokens[2:]
next_cmember = next_cmember.member
cstruct_member_access = ast_utils.extract_struct_member_access(tokens)
debug.line("parse_MEMBER_REF_EXPR", f"[OUT] cstruct_member_access=[{debug.as_debug_string(cstruct_member_access)}]")
return cstruct_member_access

View File

@ -7,6 +7,7 @@ import code_object.arg as arg
import code_object.init_list as init_list
import code_object.declaration_specifier as declaration_specifier
import code_object.struct_arg as struct_arg
import code_object.struct_member_access as struct_member_access
# Utilities for working with C AST Nodes
@ -129,3 +130,57 @@ def create_cinit_list(cnode):
return cinit_list
# Create a StructMemberAccess from the tokens
def create_struct_member_access(tokens):
assert len(tokens) >= 1, f"Expected at least 1 token, got [{len(tokens)}]"
access = name = index = None
if tokens[0] in ["->", "."]:
access = tokens[0]
tokens = tokens[1:]
name = tokens[0]
tokens = tokens[1:]
if len(tokens) > 0:
# Check for index token '[' or function call token '('
if tokens[0] == "[":
index = " ".join([t for t in tokens])
elif tokens[0] == "(":
name += " ".join([t for t in tokens])
else:
assert False, f"Unexpected token = [{tokens[0]}]"
return struct_member_access.StructMemberAccess(access, name, index)
# Parse the tokens, extracting the members
# Returns StructMemberAccess or None
def extract_struct_member_access(tokens):
debug.line("extract_struct_member_access", f"[IN] tokens={tokens}")
pointer_indexes = []
for i in range(len(tokens)):
if tokens[i] in ["->", "."]:
pointer_indexes.append(i)
debug.line("extract_struct_member_access", f"pointer_indexes={pointer_indexes}")
if len(pointer_indexes) == 0:
assert False, f"No -> or . found: could not extract members!"
assert pointer_indexes[0] != 0, f"Member access should not start with -> or ."
cstruct_member_access = create_struct_member_access(tokens[0:pointer_indexes[0]])
# Add end of token string as final index (to ensure all tokens are processed!)
pointer_indexes.append(len(tokens))
next_cmember = cstruct_member_access
while len(pointer_indexes) >= 2:
next_cmember.member = create_struct_member_access(tokens[pointer_indexes[0]:pointer_indexes[1]])
pointer_indexes = pointer_indexes[1:]
next_cmember = next_cmember.member
return cstruct_member_access

View File

@ -31,6 +31,7 @@ class BaseConversionPackUpdates:
# Default data member mappings
for cmember, cppmember in {
DataMember("grib_accessor_class**", "cclass->super"): DataMember("AccessorData", conversion_pack.conversion_data.info.super_class_name),
DataMember("const char*", "cclass->name"): DataMember("std::string", "name_"),
}.items():
conversion_pack.conversion_data.add_data_member_mapping(cmember, cppmember)