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 /spec/outputs/test | |
| 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 'spec/outputs/test')
| -rw-r--r-- | spec/outputs/test/ast_spec.lua | 85 | ||||
| -rw-r--r-- | spec/outputs/test/format_spec.lua | 4 |
2 files changed, 88 insertions, 1 deletions
diff --git a/spec/outputs/test/ast_spec.lua b/spec/outputs/test/ast_spec.lua new file mode 100644 index 0000000..bab5b9e --- /dev/null +++ b/spec/outputs/test/ast_spec.lua | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | local yue = require("yue") | ||
| 2 | return describe("yue.to_ast", function() | ||
| 3 | it("should return AST with end position for simple expression", function() | ||
| 4 | local ast = yue.to_ast("x = 1") | ||
| 5 | assert.is_not_nil(ast) | ||
| 6 | assert.same(ast[1], "File") | ||
| 7 | assert.same(ast[2], 1) | ||
| 8 | assert.same(ast[3], 1) | ||
| 9 | assert.is_number(ast[4]) | ||
| 10 | return assert.is_number(ast[5]) | ||
| 11 | end) | ||
| 12 | it("should have correct end position for leaf nodes", function() | ||
| 13 | local ast = yue.to_ast("1") | ||
| 14 | assert.is_not_nil(ast) | ||
| 15 | assert.same(ast[1], "File") | ||
| 16 | assert.same(ast[2], 1) | ||
| 17 | assert.same(ast[3], 1) | ||
| 18 | assert.is_number(ast[4]) | ||
| 19 | return assert.is_number(ast[5]) | ||
| 20 | end) | ||
| 21 | it("should have end position for multi-line code", function() | ||
| 22 | local code = [[x = 1 | ||
| 23 | y = 2]] | ||
| 24 | local ast = yue.to_ast(code) | ||
| 25 | assert.is_not_nil(ast) | ||
| 26 | assert.same(ast[1], "File") | ||
| 27 | assert.same(ast[2], 1) | ||
| 28 | assert.same(ast[3], 1) | ||
| 29 | return assert.is_true(ast[4] >= 2) | ||
| 30 | end) | ||
| 31 | it("should have end position for function definition", function() | ||
| 32 | local code = [[add = (a, b) -> | ||
| 33 | a + b]] | ||
| 34 | local ast = yue.to_ast(code) | ||
| 35 | assert.is_not_nil(ast) | ||
| 36 | assert.same(ast[1], "File") | ||
| 37 | assert.same(ast[2], 1) | ||
| 38 | assert.same(ast[3], 1) | ||
| 39 | assert.is_number(ast[4]) | ||
| 40 | return assert.is_number(ast[5]) | ||
| 41 | end) | ||
| 42 | it("should have end position for table literal", function() | ||
| 43 | local ast = yue.to_ast("{a: 1, b: 2}") | ||
| 44 | assert.is_not_nil(ast) | ||
| 45 | assert.same(ast[1], "File") | ||
| 46 | assert.same(ast[2], 1) | ||
| 47 | assert.same(ast[3], 1) | ||
| 48 | assert.is_number(ast[4]) | ||
| 49 | return assert.is_number(ast[5]) | ||
| 50 | end) | ||
| 51 | it("should have end position for class definition", function() | ||
| 52 | local code = [[class Person | ||
| 53 | new: (@name) => | ||
| 54 | getName: => @name]] | ||
| 55 | local ast = yue.to_ast(code) | ||
| 56 | assert.is_not_nil(ast) | ||
| 57 | assert.same(ast[1], "File") | ||
| 58 | assert.same(ast[2], 1) | ||
| 59 | assert.same(ast[3], 1) | ||
| 60 | return assert.is_true(ast[4] >= 3) | ||
| 61 | end) | ||
| 62 | it("should return nil and error message for invalid syntax", function() | ||
| 63 | local ast, err = yue.to_ast("if then else") | ||
| 64 | assert.is_nil(ast) | ||
| 65 | return assert.is_string(err) | ||
| 66 | end) | ||
| 67 | it("should support flatten level parameter", function() | ||
| 68 | local ast = yue.to_ast("x = 1", 0) | ||
| 69 | assert.is_not_nil(ast) | ||
| 70 | assert.same(ast[1], "File") | ||
| 71 | assert.is_number(ast[4]) | ||
| 72 | return assert.is_number(ast[5]) | ||
| 73 | end) | ||
| 74 | return it("should have end position in nested structures", function() | ||
| 75 | local code = "x = [i for i = 1, 10]" | ||
| 76 | local ast = yue.to_ast(code) | ||
| 77 | assert.is_not_nil(ast) | ||
| 78 | assert.same(ast[1], "File") | ||
| 79 | assert.same(ast[2], 1) | ||
| 80 | assert.same(ast[3], 1) | ||
| 81 | assert.is_number(ast[4]) | ||
| 82 | assert.is_number(ast[5]) | ||
| 83 | return assert.is_true(ast[5] > 1) | ||
| 84 | end) | ||
| 85 | end) | ||
diff --git a/spec/outputs/test/format_spec.lua b/spec/outputs/test/format_spec.lua index c9ea3c2..d38a0ad 100644 --- a/spec/outputs/test/format_spec.lua +++ b/spec/outputs/test/format_spec.lua | |||
| @@ -164,7 +164,9 @@ local rewriteLineCol | |||
| 164 | rewriteLineCol = function(item) | 164 | rewriteLineCol = function(item) |
| 165 | item[2] = 0 | 165 | item[2] = 0 |
| 166 | item[3] = 0 | 166 | item[3] = 0 |
| 167 | for i = 4, #item do | 167 | item[4] = 0 |
| 168 | item[5] = 0 | ||
| 169 | for i = 6, #item do | ||
| 168 | local _exp_0 = type(item[i]) | 170 | local _exp_0 = type(item[i]) |
| 169 | if "table" == _exp_0 then | 171 | if "table" == _exp_0 then |
| 170 | if item[i][1] == "comment" then | 172 | if item[i][1] == "comment" then |
