Step 14: source locations — pos-to-loc, error-loc, sx-parse-loc — 15 tests
Pure SX layer: pos-to-loc (offset→line/col), error-loc (parse result→loc), format-parse-error (human-readable error with source context line). OCaml platform: cst_to_ast_loc (CST spans→loc dicts), sx-parse-loc primitive (parse with locations), source-loc accessor. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -426,6 +426,47 @@
|
||||
|
||||
;; ── Layer 8: SX tokenizer ─────────────────────────────────────────
|
||||
|
||||
(define
|
||||
pos-to-loc
|
||||
(fn
|
||||
(input pos)
|
||||
(let
|
||||
loop
|
||||
((i 0) (line 1) (col 0))
|
||||
(if
|
||||
(>= i pos)
|
||||
{:line line :col col}
|
||||
(if
|
||||
(= (char-at input i) "\n")
|
||||
(loop (+ i 1) (+ line 1) 0)
|
||||
(loop (+ i 1) line (+ col 1)))))))
|
||||
|
||||
(define
|
||||
error-loc
|
||||
(fn (result input) (pos-to-loc input (result-pos result))))
|
||||
|
||||
(define
|
||||
format-parse-error
|
||||
(fn
|
||||
(result input)
|
||||
(let
|
||||
((loc (error-loc result input))
|
||||
(line-num (get loc :line))
|
||||
(col-num (get loc :col))
|
||||
(expected (result-expected result))
|
||||
(lines (split input "\n"))
|
||||
(source-line
|
||||
(if (<= line-num (len lines)) (nth lines (- line-num 1)) "")))
|
||||
(str
|
||||
"line "
|
||||
line-num
|
||||
", col "
|
||||
col-num
|
||||
": expected "
|
||||
expected
|
||||
"\n"
|
||||
source-line))))
|
||||
|
||||
(define
|
||||
sx-comment
|
||||
(fn
|
||||
|
||||
Reference in New Issue
Block a user