diff options
| author | Li Jin <dragon-fly@qq.com> | 2026-03-26 11:16:37 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2026-03-26 11:34:38 +0800 |
| commit | ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e (patch) | |
| tree | 211f3f6e58c32d379aaf5fe0c4483f374705e4ba /src | |
| parent | ffdbbbd3e286d7440af411b475c5a13d49897898 (diff) | |
| download | yuescript-ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e.tar.gz yuescript-ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e.tar.bz2 yuescript-ad0cd3a39d5d77ec32d2f203c7258f727a06ba6e.zip | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/yuescript/yuescript.cpp | 20 |
1 files changed, 16 insertions, 4 deletions
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) { | |||
| 380 | int count = current.children ? static_cast<int>(current.children->size()) : 0; | 380 | int count = current.children ? static_cast<int>(current.children->size()) : 0; |
| 381 | switch (count) { | 381 | switch (count) { |
| 382 | case 0: { | 382 | case 0: { |
| 383 | lua_createtable(L, 4, 0); | 383 | lua_createtable(L, 6, 0); |
| 384 | getName(node); | 384 | getName(node); |
| 385 | lua_rawseti(L, -2, 1); | 385 | lua_rawseti(L, -2, 1); |
| 386 | lua_pushinteger(L, node->m_begin.m_line); | 386 | lua_pushinteger(L, node->m_begin.m_line); |
| 387 | lua_rawseti(L, -2, 2); | 387 | lua_rawseti(L, -2, 2); |
| 388 | lua_pushinteger(L, node->m_begin.m_col); | 388 | lua_pushinteger(L, node->m_begin.m_col); |
| 389 | lua_rawseti(L, -2, 3); | 389 | lua_rawseti(L, -2, 3); |
| 390 | lua_pushinteger(L, node->m_end.m_line); | ||
| 391 | lua_rawseti(L, -2, 4); | ||
| 392 | lua_pushinteger(L, node->m_end.m_col); | ||
| 393 | lua_rawseti(L, -2, 5); | ||
| 390 | formatter.indent = 0; | 394 | formatter.indent = 0; |
| 391 | auto str = node->to_string(&formatter); | 395 | auto str = node->to_string(&formatter); |
| 392 | yue::Utils::trim(str); | 396 | yue::Utils::trim(str); |
| 393 | lua_pushlstring(L, str.c_str(), str.length()); | 397 | lua_pushlstring(L, str.c_str(), str.length()); |
| 394 | lua_rawseti(L, -2, 4); | 398 | lua_rawseti(L, -2, 6); |
| 395 | lua_rawseti(L, tableIndex, static_cast<int>(lua_objlen(L, tableIndex)) + 1); | 399 | lua_rawseti(L, tableIndex, static_cast<int>(lua_objlen(L, tableIndex)) + 1); |
| 396 | break; | 400 | break; |
| 397 | } | 401 | } |
| @@ -404,6 +408,10 @@ static int yuetoast(lua_State* L) { | |||
| 404 | lua_rawseti(L, -2, 2); | 408 | lua_rawseti(L, -2, 2); |
| 405 | lua_pushinteger(L, node->m_begin.m_col); | 409 | lua_pushinteger(L, node->m_begin.m_col); |
| 406 | lua_rawseti(L, -2, 3); | 410 | lua_rawseti(L, -2, 3); |
| 411 | lua_pushinteger(L, node->m_end.m_line); | ||
| 412 | lua_rawseti(L, -2, 4); | ||
| 413 | lua_pushinteger(L, node->m_end.m_col); | ||
| 414 | lua_rawseti(L, -2, 5); | ||
| 407 | lua_pop(L, 1); | 415 | lua_pop(L, 1); |
| 408 | break; | 416 | break; |
| 409 | } | 417 | } |
| @@ -411,14 +419,18 @@ static int yuetoast(lua_State* L) { | |||
| 411 | } | 419 | } |
| 412 | default: { | 420 | default: { |
| 413 | auto len = static_cast<int>(lua_objlen(L, tableIndex)); | 421 | auto len = static_cast<int>(lua_objlen(L, tableIndex)); |
| 414 | lua_createtable(L, count + 3, 0); | 422 | lua_createtable(L, count + 5, 0); |
| 415 | getName(node); | 423 | getName(node); |
| 416 | lua_rawseti(L, -2, 1); | 424 | lua_rawseti(L, -2, 1); |
| 417 | lua_pushinteger(L, node->m_begin.m_line); | 425 | lua_pushinteger(L, node->m_begin.m_line); |
| 418 | lua_rawseti(L, -2, 2); | 426 | lua_rawseti(L, -2, 2); |
| 419 | lua_pushinteger(L, node->m_begin.m_col); | 427 | lua_pushinteger(L, node->m_begin.m_col); |
| 420 | lua_rawseti(L, -2, 3); | 428 | lua_rawseti(L, -2, 3); |
| 421 | for (int i = count, j = 4; i >= 1; i--, j++) { | 429 | lua_pushinteger(L, node->m_end.m_line); |
| 430 | lua_rawseti(L, -2, 4); | ||
| 431 | lua_pushinteger(L, node->m_end.m_col); | ||
| 432 | lua_rawseti(L, -2, 5); | ||
| 433 | for (int i = count, j = 6; i >= 1; i--, j++) { | ||
| 422 | lua_rawgeti(L, tableIndex, len - i + 1); | 434 | lua_rawgeti(L, tableIndex, len - i + 1); |
| 423 | lua_rawseti(L, -2, j); | 435 | lua_rawseti(L, -2, j); |
| 424 | } | 436 | } |
