-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_located_ir.ml
More file actions
88 lines (77 loc) · 2.99 KB
/
script_located_ir.ml
File metadata and controls
88 lines (77 loc) · 2.99 KB
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
83
84
85
86
87
88
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Tezos_context
type point =
{ line : int ;
column : int ;
point : int }
type location =
point * point
let location_encoding =
let open Data_encoding in
let point_encoding =
conv
(fun { line ; column ; point } -> (line, column, point))
(fun (line, column, point) -> { line ; column ; point })
(obj3
(req "line" uint16)
(req "column" uint16)
(req "point" uint16)) in
obj2
(req "start" point_encoding)
(req "stop" point_encoding)
type node =
| Int of location * string
| String of location * string
| Prim of location * string * node list
| Seq of location * node list
let node_location = function
| Int (loc, _)
| String (loc, _)
| Prim (loc, _, _)
| Seq (loc, _) -> loc
(*-- Located errors ---------------------------------------------------------*)
(* Lexer error *)
exception Illegal_character of location * char
exception Illegal_escape of location * string
exception Invalid_indentation of location
exception Invalid_indentation_after_opener of location * char
exception Invalid_indentation_in_block of location * char * location
exception Newline_in_string of location
exception Unaligned_closer of location * char * char * location
exception Unclosed of location * char * location
exception Unopened of location * char
exception Unterminated_comment of location * location
exception Unterminated_string of location
exception Unterminated_string_in_comment of location * location * location
(* Parser error *)
exception Invalid_application of location
exception Sequence_in_parens of location
exception Missing_program_field of string
(*-- Converters between IR and Located IR -----------------------------------*)
let strip_locations root =
let id = let id = ref 0 in fun () -> incr id ; !id in
let loc_table = ref [] in
let rec strip_locations l =
let id = id () in
match l with
| Int (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
Script.Int (id, v)
| String (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
Script.String (id, v)
| Seq (loc, seq) ->
loc_table := (id, loc) :: !loc_table ;
Script.Seq (id, List.map strip_locations seq)
| Prim (loc, name, seq) ->
loc_table := (id, loc) :: !loc_table ;
Script.Prim (id, name, List.map strip_locations seq) in
let stripped = strip_locations root in
stripped, List.rev !loc_table