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
+14 -7
View File
@@ -4,16 +4,22 @@ let usage =
사용법:
cool check <file.cool>... 타입/effect/capability 검사 (fast path)
cool run <file.cool> typed IR 인터프리터로 실행
cool tokens <file.cool> 토큰 덤프 (렉서 디버깅)
cool version 버전 출력
|}
let report = function
| Ok () -> 0
| Error errors ->
List.iter
(fun e -> prerr_endline (Coollang.Driver.string_of_error e))
errors;
1
let report_errors errors =
List.iter (fun e -> prerr_endline (Coollang.Driver.string_of_error e)) errors;
1
let report = function Ok () -> 0 | Error errors -> report_errors errors
let dump_tokens file =
match Coollang.Driver.tokens file with
| Error errors -> report_errors errors
| Ok tokens ->
List.iter (fun t -> print_endline (Coollang.Token.show t)) tokens;
0
let () =
let argv = Array.to_list Sys.argv in
@@ -21,6 +27,7 @@ let () =
match List.tl argv with
| "check" :: files -> report (Coollang.Driver.check files)
| [ "run"; file ] -> report (Coollang.Driver.run file)
| [ "tokens"; file ] -> dump_tokens file
| [ "version" ] ->
print_endline Coollang.Version.string;
0