feat: Token 機能拡張 (Kind/IsInserted/派生型) の LightGlr 対応 + テスト - #26
Merged
Conversation
OnAccepted コールバックのドキュメント: - タイミング (LALR: reduce直後 / LightGlr: fork収束時) - OnReduce との違い (読み取り専用 vs 書き込み可) - コード例 (宣言の追加) - いつ使うべきかのガイド Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
ダミートークンの Span が default(SourceSpan) = (0,0) だったため、 エラー回復で挿入されたトークンを含むノードの Span 自動計算で (0,0) が混ざる問題を修正。 前後トークンの位置から補間: - Start = 前トークンの End (位置・行・列) - End = 次トークンの Start (位置・行・列) - EOF 時は前トークンの End - ゼロ幅スパン (Start == End) も許可 全 352 テスト合格。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
ja/en の grammar-reference.md: 旧: 「挿入トークンは値 null」(NullReferenceException 警告) 新: 「挿入トークンは空文字 + 推測 Span」(前後トークンから補間) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Token 基底クラスに IsInserted プロパティ (internal set) を追加。 ErrorRepair が挿入するダミートークンに IsInserted = true を設定。 ユーザーは OnReduce/OnAccepted で挿入トークンを判定可能: if (Num.IsInserted) return; // 挿入トークン: スキップ 全 352 テスト合格。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
[Token]/[Pattern] に Kind プロパティを追加。指定した文字列が Token.Kind に設定される。 使い方: [Token(@"[0-9]+", Kind = "number")] Token num [Token(@"if", Kind = "keyword", Priority = 1)] Token kw 生成コード: - __tokenKinds 配列 (TokenId → Kind) - ToToken で __tok.Kind = __tokenKinds[t.TokenId] Token.IsInserted に加え、Token.Kind でトークンの属性判定が可能。 全 352 テスト合格。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Token 基底クラスに Kind (public get/set) と IsInserted (public get/set) を追加。
Token 派生型 (NumberToken 等) を reduce 時に再構築する際、
Text だけでなく Kind と IsInserted も引き継ぐよう生成コードを修正。
生成ヘルパー (型ごとに __ct_TypeName メソッドを生成):
private static NumberToken __ct_NumberToken(Token src)
{
var t = new NumberToken(src.Text);
t.Kind = src.Kind;
t.IsInserted = src.IsInserted;
return t;
}
全 352 テスト合格。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
LightGlr モード (GlrParserEmitter) が Token 機能拡張に未対応だったのを LALR と対称化: - __tokenKinds 配列生成 + __ToToken で Kind 設定 - __ct_<型> ヘルパー生成 (派生型の Kind/IsInserted 引き継ぎ) - reduce 時の派生型生成を __ct_ 呼出しに変更 ParserEmitter の hasKinds スコープバグ (CS0103 で Generator がビルド不可) を修正。 9652b2c の時点で壊れており「352テスト合格」は古い状態だった。 テスト追加: - TokenFeature/ (派生型 NumberToken、LALR+LightGlr 両モードで Kind/IsInserted/派生型引き継ぎを検証) - GlrTests に IsInserted_OnOperator_LightGlr 追加 (入力 "1 2" → ER1 の + 挿入を観測) - GlrAmbiguousGrammar/GlrTests に Kind 検証 Generator.Tests の stubs を更新 (NotifyAccepted, Kind, IsInserted, LexToken 参照)。 Step 0 のビルド復旧で顕在化した潜在バグ。 全 359 テスト合格 (AstFirst.Tests 306 + Generator.Tests 53)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
Token 機能拡張 (
Kind/IsInserted/ 派生型引き継ぎ) の LightGlr モード対応と、テスト追加。背景
最近の3コミット (
43adef5IsInserted、382e41cKind、9652b2c派生型引き継ぎ) で Token 機能拡張を実装したが、2つの問題があった:GlrParserEmitter) が未対応 — LALR 用ParserEmitterのみ更新され、LightGlr ではKindが常にnull、派生型の引き継ぎも Text のみ。ドキュメントはモードを限定せず「Text + Kind + IsInserted を引き継ぐ」と書いており不整合。ParserEmitterのhasKindsスコープバグ —EmitHelpersがhasKindsを参照するがスコープ外 (CS0103) で Generator がビルド不可。9652b2cの「全352テスト合格」はコード確定前の古い実行結果だった。変更内容
実装 (
src/)GlrParserEmitter.cs: LALR (ParserEmitter) と対称化__tokenKinds配列生成 +__ToTokenで Kind 設定__ct_<型>ヘルパー生成 (派生型の Kind/IsInserted 引き継ぎ)new <型>(...Text)から__ct_<型>(src)呼出しに変更ParserEmitter.cs:hasKindsスコープバグ修正 (パラメータ化) +__ct_生成コードの見栄え改善テスト (
tests/)TokenFeature/(新規): 派生型NumberTokenで LALR+LightGlr 両モードの Kind/IsInserted/派生型引き継ぎを検証。LALR/LightGlr を別名前空間に分離 (ModelExtraction が [Grammar] と同名前空間の AstNode 派生を収集するため)GlrTests.cs:IsInserted_OnOperator_LightGlr追加 (入力"1 2"→ ER1 の+挿入をGlrAdd.Op.IsInsertedで観測)GlrAmbiguousGrammar.cs:Kind="number"で Kind 検証ParserEmitterTests.cs: stubs 更新 (NotifyAccepted,Kind,IsInserted,LexToken参照) — Step 0 のビルド復旧で顕在化した潜在バグドキュメント
grammar-reference.md(ja/en): Kind/IsInserted/派生型のプロパティ表と使用例を記載検証
全 359 テスト合格 (AstFirst.Tests 306 + Generator.Tests 53)。
🤖 Generated with Claude Code