refactor(lex_parser): 移除旧的词法解析器实现并更新依赖
移除了 libs/lex_parser 目录下的所有头文件和源文件,包括: - lex_parser.h 和 lex_parser.c 核心解析功能 - 所有测试文件(test_char.c, test_identifier.c, test_number.c, test_skip_block_comment.c, test_skip_line.c, test_string.c) 更新了 lexer 模块的依赖配置,将 lex_parser 替换为 sstream, 同时更新了 lexer.h 中的相关包含头文件和数据结构定义, 简化了 scc_lexer_t 结构体的字段。
This commit is contained in:
@@ -157,40 +157,128 @@ class VectorPrinter(gdb.ValuePrinter):
|
||||
|
||||
|
||||
class HashTablePrinter(gdb.ValuePrinter):
|
||||
def __init__(self, val: gdb.Value):
|
||||
self.val: gdb.Value = val
|
||||
"""打印 scc_hashtable_t 结构"""
|
||||
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
|
||||
@staticmethod
|
||||
def check_type(val: gdb.Value) -> bool:
|
||||
if val.type.name in ["scc_hashtable_t", "scc_hashtable"]:
|
||||
def check_type(val):
|
||||
# 通过类型名或关键字段检查
|
||||
type_name = val.type.name
|
||||
if type_name and type_name in ("scc_hashtable_t", "scc_hashtable"):
|
||||
return True
|
||||
try:
|
||||
fields = {f.name for f in val.type.fields()}
|
||||
required = {"entries", "count", "tombstone_count", "hash_func", "key_cmp"}
|
||||
if required.issubset(fields):
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def to_string(self):
|
||||
count = self.val["count"]
|
||||
tombstone = self.val["tombstone_count"]
|
||||
cap = self.val["entries"]["size"] # 总槽位数
|
||||
return f"hashtable(count={count}, tombstone={tombstone}, capacity={cap})"
|
||||
|
||||
def append_printer():
|
||||
"注册方式一:传统append方法(您之前有效的方式)self"
|
||||
gdb.pretty_printers.append(
|
||||
lambda val: VectorPrinter(val) if VectorPrinter.check_type(val) else None
|
||||
)
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
def num_children(self):
|
||||
return int(self.val["count"])
|
||||
|
||||
def children(self):
|
||||
entries = self.val["entries"]
|
||||
size = int(entries["size"])
|
||||
data = entries["data"]
|
||||
if size == 0 or data == 0:
|
||||
return
|
||||
# ENTRY_ACTIVE = 1(根据枚举定义)
|
||||
for i in range(size):
|
||||
entry = data[i]
|
||||
state = int(entry["state"])
|
||||
if state == 1: # 只输出有效条目
|
||||
yield (f"[{i}]", entry)
|
||||
|
||||
|
||||
def register_new_printer():
|
||||
"注册方式二:新版注册方法(备用方案)"
|
||||
class StrPoolPrinter(gdb.ValuePrinter):
|
||||
"""打印 scc_strpool_t,将键值作为字符串展示"""
|
||||
|
||||
def str_lookup_function(val):
|
||||
if VectorPrinter.check_type(val) is False:
|
||||
return None
|
||||
ret = VectorPrinter(val)
|
||||
# print(
|
||||
# f"ret {ret}, type {val.type.name}, {[(i.name, i.type) for i in val.type.fields()]}"
|
||||
# )
|
||||
return ret
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
self.ht = val["ht"] # 内部哈希表
|
||||
|
||||
gdb.printing.register_pretty_printer(gdb.current_objfile(), str_lookup_function)
|
||||
# if gdb.current_progspace() is not None:
|
||||
# pts = gdb.current_progspace().pretty_printers
|
||||
# print(pts, len(pts))
|
||||
# pts.append(str_lookup_function)
|
||||
@staticmethod
|
||||
def check_type(val):
|
||||
type_name = val.type.name
|
||||
if type_name and type_name == "scc_strpool_t":
|
||||
return True
|
||||
try:
|
||||
fields = {f.name for f in val.type.fields()}
|
||||
if "ht" in fields:
|
||||
# 可进一步检查 ht 的类型,但非必须
|
||||
return True
|
||||
except:
|
||||
pass
|
||||
return False
|
||||
|
||||
def to_string(self):
|
||||
count = self.ht["count"]
|
||||
cap = self.ht["entries"]["size"]
|
||||
return f"strpool(count={count}, capacity={cap})"
|
||||
|
||||
def display_hint(self):
|
||||
return "map"
|
||||
|
||||
def num_children(self):
|
||||
return int(self.ht["count"])
|
||||
|
||||
def children(self):
|
||||
entries = self.ht["entries"]
|
||||
size = int(entries["size"])
|
||||
data = entries["data"]
|
||||
if size == 0 or data == 0:
|
||||
return
|
||||
const_char_ptr = gdb.lookup_type("const char").pointer()
|
||||
char_ptr = gdb.lookup_type("char").pointer()
|
||||
|
||||
for i in range(size):
|
||||
entry = data[i]
|
||||
state = int(entry["state"])
|
||||
if state == 1: # ACTIVE
|
||||
key_val = entry["key"]
|
||||
value_val = entry["value"]
|
||||
|
||||
# 尝试将 void* 转为字符串
|
||||
try:
|
||||
key_str = key_val.cast(const_char_ptr).string()
|
||||
except:
|
||||
key_str = str(key_val) # 失败则回退到地址
|
||||
|
||||
try:
|
||||
value_str = value_val.cast(char_ptr).string()
|
||||
except:
|
||||
value_str = str(value_val)
|
||||
|
||||
# 使用带引号的字符串作为名称,值直接是字符串
|
||||
yield (repr(key_str), value_str)
|
||||
|
||||
|
||||
def register_pretty_printers():
|
||||
"""统一的查找函数,注册所有打印机"""
|
||||
|
||||
def lookup_function(val):
|
||||
if VectorPrinter.check_type(val):
|
||||
return VectorPrinter(val)
|
||||
if HashTablePrinter.check_type(val):
|
||||
return HashTablePrinter(val)
|
||||
if StrPoolPrinter.check_type(val):
|
||||
return StrPoolPrinter(val)
|
||||
return None
|
||||
|
||||
gdb.printing.register_pretty_printer(gdb.current_objfile(), lookup_function)
|
||||
|
||||
|
||||
class VectorInfoCommand(gdb.Command):
|
||||
@@ -216,7 +304,5 @@ class VectorInfoCommand(gdb.Command):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 双重注册保证兼容性
|
||||
# append_printer() # 保留您原来有效的方式
|
||||
register_new_printer() # 添加新版注册
|
||||
register_pretty_printers()
|
||||
VectorInfoCommand()
|
||||
|
||||
Reference in New Issue
Block a user