lexer: 어휘 분석 구현과 ASI 함정 하나 정정

grammar.ebnf의 어휘 절을 구현한다. 렉서는 선읽기를 요구하지 않고,
문법과 얽히는 유일한 부분인 NEWLINE 삽입은 can_end_statement 하나로
판정한다. 모든 토큰이 위치를 들고 다닌다 — 진단 품질이 헌법급이므로
나중에 붙이지 않는다.

샘플을 실제로 렉싱해 함정을 하나 잡았다. effects 절이 줄 끝에 오면 "}"가
값 종료 토큰이라 NEWLINE이 삽입되어 다음 줄의 "->"와 끊긴다(Go ASI와 같은
형태). 렉서에 문맥을 주는 대신 — 렉서 피드백은 철학 2가 배제한다 —
시그니처 머리의 흡수 위치를 프로덕션에 명시적으로 적어 닫았다. 파서가
임의로 건너뛰는 것이 아니라 문법에 적힌 자리에서만 흡수한다.
다중 줄 목록의 후행 콤마 필수도 함께 명시(콤마로 끝난 줄은 NEWLINE을
만들지 않으므로 목록이 자연히 이어진다).

cool check는 어휘 분석을 돌리되 통과했다고 말하지 않는다. 파이프라인의
나머지가 없는 이상 그 파일은 검사된 것이 아니다. 디버깅용 cool tokens 추가.

테스트 30건: 키워드, 두 글자 연산자 우선, NEWLINE 삽입 6가지 경우,
리터럴과 이스케이프, 오류 7종과 오류 위치, 그리고 samples/*.cool 전체가
어휘 분석을 통과하는지.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZVDeU6KLuUVL3gs18Hm3E
This commit is contained in:
2026-08-30 02:14:37 +09:00
co-authored by Claude Opus 5
parent 630d12104a
commit 66a2cc6959
7 changed files with 657 additions and 25 deletions
+20 -5
View File
@@ -24,6 +24,15 @@
* NEWLINE . .
* .
* NEWLINE .
*
* (, , , variant, )
* . NEWLINE .
* formatter .
*
* NEWLINE . effects
* "}" NEWLINE ,
* { NEWLINE } .
* .
*)
ident = letter , { letter | digit | "_" } ;
@@ -37,7 +46,9 @@ literal = int_lit | string_lit | bool_lit ;
(* ------------------------------------------------------------------ *)
module = { NEWLINE } , { item } ;
item = ( import | reexport | decl ) , NEWLINE ;
item = ( import | reexport | decl ) , { NEWLINE } ;
(* decl NEWLINE 0 .
* stmt NEWLINE *)
import = "import" , string_lit , "as" , ident ;
reexport = "reexport" , ident ;
@@ -50,7 +61,10 @@ decl = [ "pub" ] , ( fn_decl | struct_decl | enum_decl
(* ------------------------------------------------------------------ *)
fn_decl = "fn" , ident , [ gen_params ] , "(" , [ params ] , ")" ,
[ eff_result ] , [ "->" , type ] , [ block ] ;
{ NEWLINE } ,
[ eff_result , { NEWLINE } ] ,
[ "->" , type , { NEWLINE } ] ,
[ block ] ;
(* block . interface capability *)
struct_decl = [ "copyable" ] , "struct" , ident , [ gen_params ] ,
@@ -61,8 +75,8 @@ enum_decl = "enum" , ident , [ gen_params ] , "{" , { variant } , "}" ;
variant = ident , [ "(" , type_list , ")" ] , "," , { NEWLINE } ;
capability_decl = "capability" , ident , "{" , { cap_method } , "}" ;
cap_method = "fn" , ident , "(" , [ params ] , ")" ,
[ eff_result ] , [ "->" , type ] , NEWLINE ;
cap_method = "fn" , ident , "(" , [ params ] , ")" , { NEWLINE } ,
[ eff_result , { NEWLINE } ] , [ "->" , type ] , NEWLINE ;
const_decl = "const" , ident , ":" , type , "=" , expr ;
@@ -110,7 +124,8 @@ type_list = type , { "," , type } , [ "," ] ;
(* ------------------------------------------------------------------ *)
block = "{" , { NEWLINE } , { stmt } , "}" ;
stmt = ( let_stmt | return_stmt | assign_stmt | expr ) , NEWLINE ;
stmt = ( let_stmt | return_stmt | assign_stmt | expr ) ,
NEWLINE , { NEWLINE } ;
let_stmt = "let" , [ "mut" ] , pattern , [ ":" , type ] , "=" , expr ;
return_stmt = "return" , [ expr ] ;