Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(*
Un module au dessus de lexer et parser
pour éviter les dépendances bouclées
*)
open Lexing
(* pour calculer line/col *)
let line_num = ref 1
let line_start_pos = ref 0
let new_line ( lexbuf ) = (
line_start_pos := Lexing.lexeme_end lexbuf;
incr line_num;
()
)
(* le type ``lexeme'', string + info source *)
type t = {
_str : string ;
_line : int ;
_cstart : int ;
_cend : int
}
let str x = (x._str)
let line x = (x._line)
let cstart x = (x._cstart)
let cend x = (x._cend)
(* affichage standard: *)
let details lxm = (
Printf.sprintf "'%s' (line:%d, col:%d to %d)"
lxm._str lxm._line lxm._cstart lxm._cend
)
let position lxm = (
Printf.sprintf "line:%d, col:%d to %d"
lxm._line lxm._cstart lxm._cend
)
(* constructeur de type flaggé avec un lexeme *)
type 'a srcflaged = {
src : t ;
it : 'a
}
(* flagage d'une valeur quelconque *)
let (flagit : 'a -> t -> 'a srcflaged) =
function x -> function lxm ->
{ it = x; src = lxm }
let dummy = { _str = "dummy" ; _line = 0 ; _cstart = 0 ; _cend = 0 }
let last_lexeme = ref dummy
let make ( lexbuf ) = (
let s = (Lexing.lexeme lexbuf) in
let l = !line_num in
let c1 = (Lexing.lexeme_start lexbuf - !line_start_pos + 1) in
let c2 = (Lexing.lexeme_end lexbuf - !line_start_pos) in
last_lexeme := { _str = s ; _line = l; _cstart = c1 ; _cend = c2 };
!last_lexeme
)
let last_made () = !last_lexeme