-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_utils.ml
More file actions
24 lines (18 loc) · 807 Bytes
/
parse_utils.ml
File metadata and controls
24 lines (18 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
type position = {line_number: int; column_number: int; index: int}
let zero_position = {line_number = 0; column_number = 0; index = 0}
let pos_to_string pos = "line " ^ string_of_int (pos.line_number + 1) ^
", column " ^ string_of_int (pos.column_number + 1)
let tab_width = 8
let add_tab idx = idx + tab_width - (idx mod tab_width)
let increment_position pos char =
let new_pos = {
index = pos.index + 1;
column_number = pos.column_number + 1;
line_number = pos.line_number
} in
match char with
| '\n' -> {new_pos with line_number = pos.line_number + 1; column_number = 0}
| '\t' -> {new_pos with column_number = add_tab pos.column_number}
| _ -> new_pos
let syntax_error error pos =
failwith ("Syntax Error: " ^ error ^ " on " ^ pos_to_string pos)