ligo/src/client/embedded/alpha/script_located_ir.ml

87 lines
3.0 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
type point =
2017-07-22 02:37:33 +04:00
{ line : int ;
column : int ;
point : int }
2016-09-08 21:13:10 +04:00
type location =
point * point
2017-07-22 02:37:33 +04:00
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)
2016-09-08 21:13:10 +04:00
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
2017-07-22 02:37:33 +04:00
let loc_table = ref [] in
2016-09-08 21:13:10 +04:00
let rec strip_locations l =
let id = id () in
match l with
2017-07-22 02:37:33 +04:00
| Int (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
2016-09-08 21:13:10 +04:00
Script.Int (id, v)
2017-07-22 02:37:33 +04:00
| String (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
2016-09-08 21:13:10 +04:00
Script.String (id, v)
2017-07-22 02:37:33 +04:00
| Seq (loc, seq) ->
loc_table := (id, loc) :: !loc_table ;
2016-09-08 21:13:10 +04:00
Script.Seq (id, List.map strip_locations seq)
2017-07-22 02:37:33 +04:00
| Prim (loc, name, seq) ->
loc_table := (id, loc) :: !loc_table ;
2016-09-08 21:13:10 +04:00
Script.Prim (id, name, List.map strip_locations seq) in
2017-07-22 02:37:33 +04:00
let stripped = strip_locations root in
stripped, List.rev !loc_table