Skip to content

fix(lowering): handle line_continuation nodes in CstToAstMapper - #1

Merged
SuperIceCN merged 1 commit into
masterfrom
fix/line-continuation-cst-mapping
Sep 20, 2026
Merged

SuperIceCN merged 1 commit into
masterfrom
fix/line-continuation-cst-mapping

Conversation

@SuperIceCN

Copy link
Copy Markdown
Owner

问题

遇到以 \ 折行的源码时,CstToAstMapper 会生成错误的 AST 结构。上游 tree-sitter 语法(PrestonKnopp/tree-sitter-gdscript)将行尾 \ 折行建模为命名 extra 节点 line_continuation(grammar.js 中 line_continuation: ($) => token(seq("\\", /\r?\n/)),且 node-types.json 中 named: true),它会出现在任何跨行构造的 named children 中,而 mapper 此前对它完全不感知,导致两类故障:

  1. 操作符文本被污染:operatorBetween / prefixOperator 从源码字节区间直接提取操作符,折行时被提取为 + \、> \、= \、is \ 等脏文本。其中 is / as / is not 因无法匹配 switch 分支而静默降级为 BinaryExpression(本应是 TypeTestExpression / CastExpression),其余二元/比较/赋值/复合赋值表达式则带上错误操作符。
  2. 伪表达式节点:line_continuation 作为命名子节点混入 arguments、array 等构造的子节点枚举,被当作表达式映射,产生 UnknownExpression 节点并伴随 Unsupported expression node: line_continuation 警告,调用参数列表 / 数组元素数量与内容均出错。

修复

  • 新增 significantNamedChildren(node) 辅助方法,统一在 lowering 的所有子节点枚举点(语句序列、注解、枚举体、if/match 子句、块体、attribute 链、数组、字典、参数列表、实参列表、firstNamedChild 系列、findNamedChildByType 等)跳过 line_continuation 节点;CST 层保持对语法树的忠实呈现,不做改动。
  • 新增 normalizeOperatorText,在恢复操作符源码文本时先剥离 \<换行> 续行序列,再折叠空白(保证 is not 这类多词操作符仍可比较)。

测试

新增 CstToAstMapperLineContinuationTest(6 个用例,全部先复现失败、修复后通过):

  • 二元运算折行(1 + \)
  • 比较运算折行(a > \)
  • 赋值 / 复合赋值折行(=、+=,含 var value = \ 推导形式)
  • is / is not / as 折行(断言映射为 TypeTestExpression / CastExpression)
  • 调用参数内折行(参数数量与内容正确、无警告)
  • 数组元素内折行(元素数量与内容正确、无警告)

本地完整测试套件:116 个测试全部通过(含既有 110 个回归测试)。

The bundled tree-sitter grammar emits `\` line continuations as named
extra nodes, which previously leaked into lowering:

- operatorBetween/prefixOperator recovered operator text containing the
  continuation (e.g. `+ \`, `is \`), corrupting binary, comparison,
  assignment, augmented assignment and is/as/is not expressions
- line_continuation surfaced as a named child of arguments, arrays and
  other constructs, producing bogus UnknownExpression nodes and
  "Unsupported expression node" warnings for call arguments and array
  elements split across lines

Skip line_continuation nodes in every child enumeration during lowering
via a shared significantNamedChildren helper, and strip continuation
sequences (plus whitespace collapsing for `is not`) when recovering
operator source text.

Add CstToAstMapperLineContinuationTest covering binary/comparison/
assignment/augmented assignment continuations, is/as continuations, and
call argument/array element continuations.
Copilot AI lite review requested due to automatic review settings September 20, 2026 09:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

No blocking issues remain; the outstanding nit is non-blocking.

Review effort: Lite
Findings: None

What changed in this PR

Fixes CST-to-AST lowering for GDScript line continuations.

Changes:

  • Filters line_continuation nodes during AST lowering.
  • Normalizes operators across continued lines.
  • Adds regression tests for operators, calls, and arrays.
File Summary
src/​test/​java/​dev/​superice/​gdparser/​frontend/​lowering/​CstToAstMapperLineContinuationTest.java Adds six regression tests for continued-line syntax.
src/​main/​java/​dev/​superice/​gdparser/​frontend/​lowering/​CstToAstMapper.java Filters continuation nodes and normalizes operator text; one non-blocking nit requests unary-operator coverage.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@SuperIceCN
SuperIceCN merged commit 651bf2e into master Sep 20, 2026
5 checks passed
@SuperIceCN
SuperIceCN deleted the fix/line-continuation-cst-mapping branch September 20, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants