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
+162
View File
@@ -0,0 +1,162 @@
(* 토큰 정의. 위치는 진단 품질이 헌법급이므로 모든 토큰이 들고 다닌다. *)
type pos = { line : int; col : int }
type kind =
(* 리터럴과 이름 *)
| Ident of string
| Int of string
| Str of string
(* 키워드 *)
| Kw_pub
| Kw_fn
| Kw_struct
| Kw_enum
| Kw_capability
| Kw_const
| Kw_import
| Kw_as
| Kw_reexport
| Kw_let
| Kw_mut
| Kw_own
| Kw_affine
| Kw_copyable
| Kw_effects
| Kw_return
| Kw_if
| Kw_else
| Kw_match
| Kw_scope
| Kw_true
| Kw_false
(* 구두점 *)
| LParen
| RParen
| LBrace
| RBrace
| LBracket
| RBracket
| Comma
| Colon
| Dot
| Arrow
| FatArrow
| Question
| Underscore
| Eq
| EqEq
| Bang
| BangEq
| Lt
| Le
| Gt
| Ge
| Plus
| Minus
| Star
| Slash
| Percent
| AmpAmp
| PipePipe
| Pipe
(* 렉서가 삽입하는 문 구분자 *)
| Newline
| Eof
type t = { kind : kind; pos : pos }
let keyword = function
| "pub" -> Some Kw_pub
| "fn" -> Some Kw_fn
| "struct" -> Some Kw_struct
| "enum" -> Some Kw_enum
| "capability" -> Some Kw_capability
| "const" -> Some Kw_const
| "import" -> Some Kw_import
| "as" -> Some Kw_as
| "reexport" -> Some Kw_reexport
| "let" -> Some Kw_let
| "mut" -> Some Kw_mut
| "own" -> Some Kw_own
| "affine" -> Some Kw_affine
| "copyable" -> Some Kw_copyable
| "effects" -> Some Kw_effects
| "return" -> Some Kw_return
| "if" -> Some Kw_if
| "else" -> Some Kw_else
| "match" -> Some Kw_match
| "scope" -> Some Kw_scope
| "true" -> Some Kw_true
| "false" -> Some Kw_false
| _ -> None
let show_kind = function
| Ident s -> Printf.sprintf "이름 %s" s
| Int s -> Printf.sprintf "정수 %s" s
| Str _ -> "문자열"
| Kw_pub -> "pub"
| Kw_fn -> "fn"
| Kw_struct -> "struct"
| Kw_enum -> "enum"
| Kw_capability -> "capability"
| Kw_const -> "const"
| Kw_import -> "import"
| Kw_as -> "as"
| Kw_reexport -> "reexport"
| Kw_let -> "let"
| Kw_mut -> "mut"
| Kw_own -> "own"
| Kw_affine -> "affine"
| Kw_copyable -> "copyable"
| Kw_effects -> "effects"
| Kw_return -> "return"
| Kw_if -> "if"
| Kw_else -> "else"
| Kw_match -> "match"
| Kw_scope -> "scope"
| Kw_true -> "true"
| Kw_false -> "false"
| LParen -> "("
| RParen -> ")"
| LBrace -> "{"
| RBrace -> "}"
| LBracket -> "["
| RBracket -> "]"
| Comma -> ","
| Colon -> ":"
| Dot -> "."
| Arrow -> "->"
| FatArrow -> "=>"
| Question -> "?"
| Underscore -> "_"
| Eq -> "="
| EqEq -> "=="
| Bang -> "!"
| BangEq -> "!="
| Lt -> "<"
| Le -> "<="
| Gt -> ">"
| Ge -> ">="
| Plus -> "+"
| Minus -> "-"
| Star -> "*"
| Slash -> "/"
| Percent -> "%"
| AmpAmp -> "&&"
| PipePipe -> "||"
| Pipe -> "|"
| Newline -> "줄바꿈"
| Eof -> "파일 끝"
let show { kind; pos } =
Printf.sprintf "%d:%d %s" pos.line pos.col (show_kind kind)
(* 줄 끝에서 문 구분자를 삽입할지 결정한다 (grammar.ebnf 어휘 절).
값으로 끝날 수 있는 토큰 뒤에서만 삽입하므로, 연산자나 여는 괄호로
끝나는 줄은 다음 줄로 이어진다. *)
let can_end_statement = function
| Ident _ | Int _ | Str _ | RParen | RBracket | RBrace | Question | Kw_return
| Kw_true | Kw_false ->
true
| _ -> false