Source file automaton_state.ml
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
open! Import
include Automaton_state_intf
type ('u, 's) t = ('u, 's) State.t
open State
let initial_user_state : type u s. (u, s) Kind.t -> Positions.pos -> u =
fun kind initial_pos ->
match kind with
| Positions -> Positions.Builder.create ~initial_pos ()
| Sexp -> ()
| Sexp_with_positions -> Positions.Builder.create ~initial_pos ()
| Cst ->
{ token_buffer = Buffer.create 128; token_start_pos = Positions.beginning_of_file }
;;
let initial_state = 0
let error_state = 1
let create ?(initial_pos = Positions.beginning_of_file) mode kind =
{ kind
; depth = 0
; automaton_state = initial_state
; block_comment_depth = 0
; ignoring_stack = []
; escaped_value = 0
; atom_buffer = Buffer.create 128
; user_state = initial_user_state kind initial_pos
; mode
; full_sexps = 0
; offset = initial_pos.offset
; line_number = initial_pos.line
; bol_offset = initial_pos.offset - initial_pos.col
}
;;
let mode t = t.mode
let positions t = Positions.Builder.contents t.user_state
let atom_buffer t = t.atom_buffer
let offset state = state.offset
let line state = state.line_number
let column state = state.offset - state.bol_offset
let position t = { Positions.col = column t; line = line t; offset = offset t }
let reset_user_state : type u s. (u, s) t -> unit =
fun t ->
match t.kind with
| Positions -> Positions.Builder.reset t.user_state (position t)
| Sexp -> ()
| Sexp_with_positions -> Positions.Builder.reset t.user_state (position t)
| Cst -> Buffer.clear t.user_state.token_buffer
;;
let reset ?(pos = Positions.beginning_of_file) t =
t.depth <- 0;
t.automaton_state <- initial_state;
t.block_comment_depth <- 0;
t.ignoring_stack <- [];
t.escaped_value <- 0;
t.full_sexps <- 0;
t.offset <- pos.offset;
t.line_number <- pos.line;
t.bol_offset <- pos.offset - pos.col;
reset_user_state t;
Buffer.clear t.atom_buffer
;;
let context state : Context.t =
match state.ignoring_stack with
| _ :: _ -> Sexp_comment
| [] -> Sexp
;;
let has_unclosed_paren state = state.depth > 0
let set_error_state state = state.automaton_state <- error_state
let automaton_state state = state.automaton_state