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

72 lines
2.3 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 = Micheline_parser.point =
{ point : int ;
byte : int ;
line : int ;
column : int }
let point_zero = Micheline_parser.point_zero
2016-09-08 21:13:10 +04:00
type location = Micheline_parser.location =
{ start : point ;
stop : point }
2016-09-08 21:13:10 +04:00
2017-07-22 02:37:33 +04:00
let location_encoding =
let open Data_encoding in
let point_encoding =
conv
(fun { line ; column ; point ; byte } -> (line, column, point, byte))
(fun (line, column, point, byte) -> { line ; column ; point ; byte })
(obj4
2017-07-22 02:37:33 +04:00
(req "line" uint16)
(req "column" uint16)
(req "point" uint16)
(req "byte" uint16)) in
conv
(fun { start ; stop } -> (start, stop))
(fun (start, stop) -> { start ; stop })
(obj2
(req "start" point_encoding)
(req "stop" point_encoding))
2017-07-22 02:37:33 +04:00
type node = (location, string) Micheline.node
open Micheline
2016-09-08 21:13:10 +04:00
let node_location = function
| Int (loc, _)
| String (loc, _)
| Prim (loc, _, _, _)
| Seq (loc, _, _) -> loc
2016-09-08 21:13:10 +04:00
let strip_locations root =
let id = let id = ref (-1) 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 ;
Int (id, v)
2017-07-22 02:37:33 +04:00
| String (loc, v) ->
loc_table := (id, loc) :: !loc_table ;
String (id, v)
| Seq (loc, seq, annot) ->
2017-07-22 02:37:33 +04:00
loc_table := (id, loc) :: !loc_table ;
Seq (id, List.map strip_locations seq, annot)
| Prim (loc, name, seq, annot) ->
2017-07-22 02:37:33 +04:00
loc_table := (id, loc) :: !loc_table ;
Prim (id, name, List.map strip_locations seq, annot) in
2017-07-22 02:37:33 +04:00
let stripped = strip_locations root in
stripped, List.rev !loc_table
exception Missing_program_field of string