@@ -3,53 +3,38 @@ module I = Grammar.MenhirInterpreter
33module PE = Lrgrep_runtime. Interpreter (Parse_errors. Table_error_message ) (I )
44module Error = Error
55
6- type 'a located =
7- { span : Span .t ;
8- start : Lexing .position ;
9- finish : Lexing .position ;
10- token : 'a
11- }
6+ type located_token = Token .lexer_token * Lexing .position * Lexing .position
127
13- let lex_one lines (lexbuf : Lexing.lexbuf ) =
8+ let lex_one lines (lexbuf : Lexing.lexbuf ) : located_token =
149 let start = lexbuf.lex_curr_p in
1510 let token = Lexer. token lines lexbuf in
1611 let finish = lexbuf.lex_curr_p in
17- { token; span = Span. of_pos2 lines start lexbuf.lex_curr_p; start; finish }
12+ ( token, start, finish)
1813
19- let lex_leading lines lexbuf =
20- let rec go xs =
21- match lex_one lines lexbuf with
22- | { token = Trivial value ; span; _ } -> go ({ Span. value; span } :: xs)
23- | { token = Token token ; _ } as rest -> ( List. rev xs, { rest with token })
24- in
25- go []
14+ let rec lex_leading_worker lines ( lexbuf : Lexing.lexbuf ) xs =
15+ let start = lexbuf.lex_curr_p in
16+ match Lexer. token lines lexbuf with
17+ | TRIVIA value ->
18+ lex_leading_worker lines lexbuf
19+ ({ Span. value; span = Span. of_pos2 lines start lexbuf.lex_curr_p } :: xs)
20+ | token -> ( List. rev xs, (token, start, lexbuf.lex_curr_p))
2621
27- let lex_trailing file lexbuf prev_line =
22+ let lex_leading lines lexbuf (token : located_token ) =
23+ match token with
24+ | TRIVIA value , start , finish ->
25+ lex_leading_worker lines lexbuf [ { Span. value; span = Span. of_pos2 lines start finish } ]
26+ | tok -> ([] , tok)
27+
28+ let lex_trailing lines (lexbuf : Lexing.lexbuf ) prev_line =
2829 let rec go xs =
29- match lex_one file lexbuf with
30- | { token = Trivial value ; span; start; _ } when start.pos_lnum = prev_line ->
31- go ({ Span. value; span } :: xs)
32- | t -> (List. rev xs, t)
30+ let start = lexbuf.lex_curr_p in
31+ match Lexer. token lines lexbuf with
32+ | TRIVIA value when start.pos_lnum = prev_line ->
33+ go ({ Span. value; span = Span. of_pos2 lines start lexbuf.lex_curr_p } :: xs)
34+ | t -> (List. rev xs, (t, start, lexbuf.lex_curr_p))
3335 in
3436 go []
3537
36- let lex_token file lexbuf (next : Token.lexer_token located ) =
37- let leading, { token; span = tok_span; start; finish } =
38- match next with
39- | { token = Trivial value ; span; _ } ->
40- let leading, t = lex_leading file lexbuf in
41- ({ Span. value; span } :: leading, t)
42- | { token = Token token ; _ } as rest -> ([] , { rest with token })
43- in
44- match token with
45- | EoF ->
46- (* Just return the current "next" token (we won't inspect it after all, and an EOF token with
47- no trailing data. *)
48- (Token. make_token leading [] tok_span token, start, finish, next)
49- | _ ->
50- let trailing, next = lex_trailing file lexbuf start.pos_lnum in
51- (Token. make_token leading trailing tok_span token, start, finish, next)
52-
5338let get_error_message token ~pre_env ~post_env : Error. message =
5439 match
5540 PE. run pre_env
@@ -68,18 +53,29 @@ let get_error_message token ~pre_env ~post_env : Error.message =
6853let parse start (file : Illuaminate.File_id.t ) (lexbuf : Lexing.lexbuf ) =
6954 Span.Lines. using file lexbuf @@ fun lines ->
7055 let position_map = Span.Lines. position_map lines in
71- let rec go env token token_start token_end next = function
56+ let rec go env token next = function
7257 | I. InputNeeded env as checkpoint -> go_input env checkpoint next
73- | (I. Shifting _ | I. AboutToReduce _ ) as checkpoint ->
74- I. resume checkpoint |> go env token token_start token_end next
58+ | (I. Shifting _ | I. AboutToReduce _ ) as checkpoint -> I. resume checkpoint |> go env token next
7559 | I. HandlingError post_env ->
76- let message = get_error_message ( token, token_start, token_end) ~pre_env: env ~post_env in
60+ let message = get_error_message token ~pre_env: env ~post_env in
7761 Error { Error. file; position_map; message }
7862 | I. Accepted x -> Ok x
7963 | I. Rejected -> assert false
8064 and go_input env checkpoint token =
81- let token, start, finish, next = lex_token lines lexbuf token in
82- I. offer checkpoint (token, start, finish) |> go env token start finish next
65+ let leading_trivia, ((token, start, finish) as lex_token) = lex_leading lines lexbuf token in
66+ let span = Span. of_pos2 lines start lexbuf.lex_curr_p in
67+ let token, next =
68+ match token with
69+ | EOF ->
70+ (* Just return the current "next" token (we won't inspect it after all, and an EOF token
71+ with no trailing data. *)
72+ ( (Token. make_token ~leading_trivia ~trailing_trivia: [] ~span token, start, finish),
73+ lex_token )
74+ | _ ->
75+ let trailing_trivia, next = lex_trailing lines lexbuf start.pos_lnum in
76+ ((Token. make_token ~leading_trivia ~trailing_trivia ~span token, start, finish), next)
77+ in
78+ I. offer checkpoint token |> go env token next
8379 in
8480 try
8581 match start Lexing. dummy_pos with
@@ -91,18 +87,26 @@ let program = parse Grammar.Incremental.program
9187let repl_exprs = parse Grammar.Incremental. repl_exprs
9288
9389module Lexer = struct
94- type token = Token .lexer_token =
95- | Token of IlluaminateCore.Token .t
90+ type token =
91+ | Token of string
9692 | Trivial of IlluaminateCore.Node .trivial
9793
9894 let lex (file : Illuaminate.File_id.t ) (lexbuf : Lexing.lexbuf ) =
9995 Span.Lines. using file lexbuf @@ fun lines ->
10096 try
10197 let rec go xs =
102- let { token; span; _ } = lex_one lines lexbuf in
103- let xs = { Span. value = token; span } :: xs in
98+ let token, start, finish = lex_one lines lexbuf in
99+ let span = Span. of_pos2 lines start finish in
100+ let value =
101+ match token with
102+ | TRIVIA t -> Trivial t
103+ | t ->
104+ Token
105+ (Token. make_token ~leading_trivia: [] ~trailing_trivia: [] ~span t |> Token. to_string)
106+ in
107+ let xs = { Span. value; span } :: xs in
104108 match token with
105- | Token EoF -> xs
109+ | EOF -> xs
106110 | _ -> go xs
107111 in
108112 go [] |> List. rev |> Array. of_list |> Result. ok
0 commit comments