2017-11-02 21:57:17 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2017-11-14 03:36:14 +04:00
|
|
|
(* Copyright (c) 2014 - 2017. *)
|
2017-11-02 21:57:17 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
open Tezos_micheline
|
2017-11-02 21:57:17 +04:00
|
|
|
open Micheline_parser
|
|
|
|
open Micheline
|
|
|
|
|
|
|
|
type parsed =
|
|
|
|
{ source : string ;
|
|
|
|
unexpanded : string canonical ;
|
|
|
|
expanded : Michelson_v1_primitives.prim canonical ;
|
|
|
|
expansion_table : (int * (Micheline_parser.location * int list)) list ;
|
|
|
|
unexpansion_table : (int * int) list }
|
|
|
|
|
2017-12-01 20:00:32 +04:00
|
|
|
(* Unexpanded toplevel expression should be a sequence *)
|
2017-11-04 03:16:05 +04:00
|
|
|
let expand_all source ast errors =
|
2017-11-02 21:57:17 +04:00
|
|
|
let unexpanded, loc_table =
|
|
|
|
extract_locations ast in
|
2017-12-01 20:00:32 +04:00
|
|
|
let rec error_map (expanded, errors) f = function
|
|
|
|
| [] -> (List.rev expanded, List.rev errors)
|
|
|
|
| hd :: tl ->
|
|
|
|
let (new_expanded, new_errors) = f hd in
|
|
|
|
error_map
|
|
|
|
(new_expanded :: expanded, List.rev_append new_errors errors)
|
|
|
|
f tl in
|
|
|
|
let error_map = error_map ([], []) in
|
2017-11-02 21:57:17 +04:00
|
|
|
let rec expand expr =
|
|
|
|
match Michelson_macros.expand expr with
|
2017-12-01 20:00:32 +04:00
|
|
|
| Ok expanded ->
|
|
|
|
begin
|
|
|
|
match expanded with
|
|
|
|
| Seq (loc, items, annot) ->
|
|
|
|
let items, errors = error_map expand items in
|
|
|
|
(Seq (loc, items, annot), errors)
|
|
|
|
| Prim (loc, name, args, annot) ->
|
|
|
|
let args, errors = error_map expand args in
|
|
|
|
(Prim (loc, name, args, annot), errors)
|
|
|
|
| Int _ | String _ as atom -> (atom, []) end
|
|
|
|
| Error errors -> (expr, errors) in
|
|
|
|
let expanded, expansion_errors = expand (root unexpanded) in
|
2017-11-02 21:57:17 +04:00
|
|
|
let expanded, unexpansion_table =
|
2017-12-01 20:00:32 +04:00
|
|
|
extract_locations expanded in
|
2017-11-02 21:57:17 +04:00
|
|
|
let expansion_table =
|
|
|
|
let sorted =
|
|
|
|
List.sort (fun (_, a) (_, b) -> compare a b) unexpansion_table in
|
|
|
|
let grouped =
|
|
|
|
let rec group = function
|
|
|
|
| acc, [] -> acc
|
|
|
|
| [], (u, e) :: r ->
|
|
|
|
group ([ (e, [ u ]) ], r)
|
|
|
|
| ((pe, us) :: racc as acc), (u, e) :: r ->
|
|
|
|
if e = pe then
|
|
|
|
group (((e, u :: us) :: racc), r)
|
|
|
|
else
|
|
|
|
group (((e, [ u ]) :: acc), r) in
|
|
|
|
group ([], sorted) in
|
|
|
|
List.map2
|
|
|
|
(fun (l, ploc) (l', elocs) ->
|
|
|
|
assert (l = l') ;
|
|
|
|
(l, (ploc, elocs)))
|
|
|
|
(List.sort compare loc_table)
|
|
|
|
(List.sort compare grouped) in
|
2017-11-04 03:16:05 +04:00
|
|
|
match Environment.wrap_error (Michelson_v1_primitives.prims_of_strings expanded) with
|
|
|
|
| Ok expanded ->
|
|
|
|
{ source ; unexpanded ; expanded ;
|
|
|
|
expansion_table ; unexpansion_table },
|
2017-12-01 20:00:32 +04:00
|
|
|
errors @ expansion_errors
|
2017-11-04 03:16:05 +04:00
|
|
|
| Error errs ->
|
|
|
|
{ source ; unexpanded ;
|
|
|
|
expanded = Micheline.strip_locations (Seq ((), [], None)) ;
|
|
|
|
expansion_table ; unexpansion_table },
|
2017-12-01 20:00:32 +04:00
|
|
|
errs @ errors @ expansion_errors
|
2017-11-02 21:57:17 +04:00
|
|
|
|
|
|
|
let parse_toplevel ?check source =
|
2017-11-04 03:16:05 +04:00
|
|
|
let tokens, lexing_errors = Micheline_parser.tokenize source in
|
|
|
|
let asts, parsing_errors = Micheline_parser.parse_toplevel ?check tokens in
|
2017-11-02 21:57:17 +04:00
|
|
|
let ast = match asts with
|
|
|
|
| [ ast ] -> ast
|
|
|
|
| asts ->
|
|
|
|
let start = min_point asts and stop = max_point asts in
|
2017-11-13 17:29:28 +04:00
|
|
|
Seq ({ start ; stop }, asts, None) in
|
2017-11-04 03:16:05 +04:00
|
|
|
expand_all source ast (lexing_errors @ parsing_errors)
|
2017-11-02 21:57:17 +04:00
|
|
|
|
|
|
|
let parse_expression ?check source =
|
2017-11-04 03:16:05 +04:00
|
|
|
let tokens, lexing_errors = Micheline_parser.tokenize source in
|
|
|
|
let ast, parsing_errors = Micheline_parser.parse_expression ?check tokens in
|
|
|
|
expand_all source ast (lexing_errors @ parsing_errors)
|