From ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 26 Mar 2026 11:16:37 +0800 Subject: feat: add m_end position to AST nodes - Add end line and column (m_end.m_line, m_end.m_col) to AST output - New AST format: [name, begin_line, begin_col, end_line, end_col, ...children] - Update format_spec.yue to normalize end positions for comparison - Add ast_spec.yue tests for AST end position feature Closes #251 --- src/yuescript/yuescript.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 0dbfe2f..7645ef4 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -380,18 +380,22 @@ static int yuetoast(lua_State* L) { int count = current.children ? static_cast(current.children->size()) : 0; switch (count) { case 0: { - lua_createtable(L, 4, 0); + lua_createtable(L, 6, 0); getName(node); lua_rawseti(L, -2, 1); lua_pushinteger(L, node->m_begin.m_line); lua_rawseti(L, -2, 2); lua_pushinteger(L, node->m_begin.m_col); lua_rawseti(L, -2, 3); + lua_pushinteger(L, node->m_end.m_line); + lua_rawseti(L, -2, 4); + lua_pushinteger(L, node->m_end.m_col); + lua_rawseti(L, -2, 5); formatter.indent = 0; auto str = node->to_string(&formatter); yue::Utils::trim(str); lua_pushlstring(L, str.c_str(), str.length()); - lua_rawseti(L, -2, 4); + lua_rawseti(L, -2, 6); lua_rawseti(L, tableIndex, static_cast(lua_objlen(L, tableIndex)) + 1); break; } @@ -404,6 +408,10 @@ static int yuetoast(lua_State* L) { lua_rawseti(L, -2, 2); lua_pushinteger(L, node->m_begin.m_col); lua_rawseti(L, -2, 3); + lua_pushinteger(L, node->m_end.m_line); + lua_rawseti(L, -2, 4); + lua_pushinteger(L, node->m_end.m_col); + lua_rawseti(L, -2, 5); lua_pop(L, 1); break; } @@ -411,14 +419,18 @@ static int yuetoast(lua_State* L) { } default: { auto len = static_cast(lua_objlen(L, tableIndex)); - lua_createtable(L, count + 3, 0); + lua_createtable(L, count + 5, 0); getName(node); lua_rawseti(L, -2, 1); lua_pushinteger(L, node->m_begin.m_line); lua_rawseti(L, -2, 2); lua_pushinteger(L, node->m_begin.m_col); lua_rawseti(L, -2, 3); - for (int i = count, j = 4; i >= 1; i--, j++) { + lua_pushinteger(L, node->m_end.m_line); + lua_rawseti(L, -2, 4); + lua_pushinteger(L, node->m_end.m_col); + lua_rawseti(L, -2, 5); + for (int i = count, j = 6; i >= 1; i--, j++) { lua_rawgeti(L, tableIndex, len - i + 1); lua_rawseti(L, -2, j); } -- cgit v1.2.3-55-g6feb