parser and test

This commit is contained in:
Pierre-Emmanuel Wulfman 2020-04-15 17:15:55 +02:00
parent 7872a1d4bc
commit fa7cc825eb
33 changed files with 291 additions and 100 deletions

View File

@ -56,6 +56,7 @@ type c_Some = Region.t
type arrow = Region.t (* "->" *) type arrow = Region.t (* "->" *)
type cons = Region.t (* "::" *) type cons = Region.t (* "::" *)
type percent = Region.t (* "%" *)
type cat = Region.t (* "^" *) type cat = Region.t (* "^" *)
type append = Region.t (* "@" *) type append = Region.t (* "@" *)
type dot = Region.t (* "." *) type dot = Region.t (* "." *)
@ -246,6 +247,7 @@ and expr =
| ELetIn of let_in reg | ELetIn of let_in reg
| EFun of fun_expr reg | EFun of fun_expr reg
| ESeq of expr injection reg | ESeq of expr injection reg
| ECodeInsert of code_insert reg
and annot_expr = expr * colon * type_expr and annot_expr = expr * colon * type_expr
@ -398,6 +400,13 @@ and cond_expr = {
ifnot : expr ifnot : expr
} }
and code_insert = {
language : string reg;
code : string reg;
colon : colon;
type_anno : type_expr;
rbracket : rbracket;
}
(* Projecting regions from some nodes of the AST *) (* Projecting regions from some nodes of the AST *)
let rec last to_region = function let rec last to_region = function
@ -481,7 +490,8 @@ let expr_to_region = function
| ECond {region;_} | ETuple {region;_} | ECase {region;_} | ECond {region;_} | ETuple {region;_} | ECase {region;_}
| ECall {region;_} | EVar {region; _} | EProj {region; _} | ECall {region;_} | EVar {region; _} | EProj {region; _}
| EUnit {region;_} | EPar {region;_} | EBytes {region; _} | EUnit {region;_} | EPar {region;_} | EBytes {region; _}
| ESeq {region; _} | ERecord {region; _} | EUpdate {region; _} -> region | ESeq {region; _} | ERecord {region; _} | EUpdate {region; _}
| ECodeInsert {region; _} -> region
let selection_to_region = function let selection_to_region = function
FieldName f -> f.region FieldName f -> f.region

View File

@ -87,6 +87,7 @@ type t =
| Verbatim of string Region.reg | Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg | Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg | Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *) (* Keywords *)
@ -154,6 +155,7 @@ val mk_verbatim : lexeme -> Region.t -> token
val mk_bytes : lexeme -> Region.t -> token val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token val eof : Region.t -> token
(* Predicates *) (* Predicates *)

View File

@ -71,6 +71,7 @@ type t =
| Verbatim of string Region.reg | Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg | Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg | Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *) (* Keywords *)
@ -130,6 +131,8 @@ let proj_token = function
region, sprintf "Constr %s" value region, sprintf "Constr %s" value
| Attr Region.{region; value} -> | Attr Region.{region; value} ->
region, sprintf "Attr \"%s\"" value region, sprintf "Attr \"%s\"" value
| Insert Region.{region; value} ->
region, sprintf "Insert \"%s\"" value
(* Symbols *) (* Symbols *)
@ -204,6 +207,7 @@ let to_lexeme = function
| Ident id -> id.Region.value | Ident id -> id.Region.value
| Constr id -> id.Region.value | Constr id -> id.Region.value
| Attr a -> a.Region.value | Attr a -> a.Region.value
| Insert i -> i.Region.value
(* Symbols *) (* Symbols *)
@ -508,6 +512,9 @@ let mk_attr header lexeme region =
if header = "[@" then Error Invalid_attribute if header = "[@" then Error Invalid_attribute
else Ok (Attr Region.{value=lexeme; region}) else Ok (Attr Region.{value=lexeme; region})
let mk_insert lexeme region =
Insert Region.{value=lexeme;region}
(* Predicates *) (* Predicates *)
let is_string = function String _ -> true | _ -> false let is_string = function String _ -> true | _ -> false

View File

@ -14,6 +14,7 @@
%token <string Region.reg> Ident "<ident>" %token <string Region.reg> Ident "<ident>"
%token <string Region.reg> Constr "<constr>" %token <string Region.reg> Constr "<constr>"
%token <string Region.reg> Attr "<attr>" %token <string Region.reg> Attr "<attr>"
%token <string Region.reg> Insert "<insert>"
(* Symbols *) (* Symbols *)

View File

@ -583,6 +583,7 @@ core_expr:
| sequence { ESeq $1 } | sequence { ESeq $1 }
| record_expr { ERecord $1 } | record_expr { ERecord $1 }
| update_record { EUpdate $1 } | update_record { EUpdate $1 }
| code_insert { ECodeInsert $1 }
| par(expr) { EPar $1 } | par(expr) { EPar $1 }
| par(annot_expr) { EAnnot $1 } | par(annot_expr) { EAnnot $1 }
@ -706,3 +707,14 @@ last_expr:
seq_expr: seq_expr:
disj_expr_level | if_then_else (seq_expr) { $1 } disj_expr_level | if_then_else (seq_expr) { $1 }
code_insert:
Insert "<verbatim>" ":" type_expr "]" {
let region = cover $1.region $5 in
let value = {
language =$1;
code =$2;
colon =$3;
type_anno=$4;
rbracket =$5}
in {region; value} }

View File

@ -366,6 +366,7 @@ and print_expr state = function
| ESeq seq -> print_sequence state seq | ESeq seq -> print_sequence state seq
| ERecord e -> print_record_expr state e | ERecord e -> print_record_expr state e
| EConstr e -> print_constr_expr state e | EConstr e -> print_constr_expr state e
| ECodeInsert e -> print_code_insert state e
and print_constr_expr state = function and print_constr_expr state = function
ENone e -> print_none_expr state e ENone e -> print_none_expr state e
@ -518,6 +519,14 @@ and print_comp_expr state = function
and print_record_expr state e = and print_record_expr state e =
print_ne_injection state print_field_assign e print_ne_injection state print_field_assign e
and print_code_insert state {value; _} =
let {language;code;colon;type_anno;rbracket} : code_insert = value in
print_string state language;
print_string state code;
print_token state colon ":";
print_type_expr state type_anno;
print_token state rbracket "]"
and print_field_assign state {value; _} = and print_field_assign state {value; _} =
let {field_name; assignment; field_expr} = value in let {field_name; assignment; field_expr} = value in
print_var state field_name; print_var state field_name;
@ -860,6 +869,9 @@ and pp_expr state = function
| ESeq {value; region} -> | ESeq {value; region} ->
pp_loc_node state "ESeq" region; pp_loc_node state "ESeq" region;
pp_injection pp_expr state value pp_injection pp_expr state value
| ECodeInsert {value; region} ->
pp_loc_node state "ECodeInsert" region;
pp_code_insert state value
and pp_fun_expr state node = and pp_fun_expr state node =
let {binders; lhs_type; body; _} = node in let {binders; lhs_type; body; _} = node in
@ -881,6 +893,21 @@ and pp_fun_expr state node =
pp_expr (state#pad 1 0) body pp_expr (state#pad 1 0) body
in () in ()
and pp_code_insert state (rc : code_insert) =
let () =
let state = state#pad 3 0 in
pp_node state "<language>";
pp_string (state#pad 1 0) rc.language in
let () =
let state = state#pad 3 1 in
pp_node state "<code>";
pp_string (state#pad 1 0) rc.code in
let () =
let state = state#pad 3 2 in
pp_node state "<type annotation>";
pp_type_expr (state#pad 1 0) rc.type_anno
in ()
and pp_let_in state node = and pp_let_in state node =
let {binding; body; attributes; kwd_rec; _} = node in let {binding; body; attributes; kwd_rec; _} = node in
let {binders; lhs_type; let_rhs; _} = binding in let {binders; lhs_type; let_rhs; _} = binding in

View File

@ -43,6 +43,7 @@ let concrete = function
| "PLUS" -> "+" | "PLUS" -> "+"
| "SLASH" -> "/" | "SLASH" -> "/"
| "TIMES" -> "*" | "TIMES" -> "*"
| "PERCENT" -> "%"
| "LPAR" -> "(" | "LPAR" -> "("
| "RPAR" -> ")" | "RPAR" -> ")"

View File

@ -82,6 +82,7 @@ type rbrace = Region.t (* "}" *)
type lbracket = Region.t (* "[" *) type lbracket = Region.t (* "[" *)
type rbracket = Region.t (* "]" *) type rbracket = Region.t (* "]" *)
type cons = Region.t (* "#" *) type cons = Region.t (* "#" *)
type percent = Region.t (* "%" *)
type vbar = Region.t (* "|" *) type vbar = Region.t (* "|" *)
type arrow = Region.t (* "->" *) type arrow = Region.t (* "->" *)
type assign = Region.t (* ":=" *) type assign = Region.t (* ":=" *)
@ -436,6 +437,14 @@ and for_collect = {
block : block reg block : block reg
} }
and code_insert = {
language : string reg;
code : string reg;
colon : colon;
type_anno : type_expr;
rbracket : rbracket;
}
and collection = and collection =
Map of kwd_map Map of kwd_map
| Set of kwd_set | Set of kwd_set
@ -464,6 +473,7 @@ and expr =
| ETuple of tuple_expr | ETuple of tuple_expr
| EPar of expr par reg | EPar of expr par reg
| EFun of fun_expr reg | EFun of fun_expr reg
| ECodeInsert of code_insert reg
and annot_expr = expr * colon * type_expr and annot_expr = expr * colon * type_expr
@ -687,7 +697,8 @@ let rec expr_to_region = function
| ECase {region;_} | ECase {region;_}
| ECond {region; _} | ECond {region; _}
| EPar {region; _} | EPar {region; _}
| EFun {region; _} -> region | EFun {region; _}
| ECodeInsert {region; _} -> region
and tuple_expr_to_region {region; _} = region and tuple_expr_to_region {region; _} = region

View File

@ -44,6 +44,7 @@ type t =
| Mutez of (lexeme * Z.t) Region.reg | Mutez of (lexeme * Z.t) Region.reg
| Ident of lexeme Region.reg | Ident of lexeme Region.reg
| Constr of lexeme Region.reg | Constr of lexeme Region.reg
| Insert of lexeme Region.reg
(* Symbols *) (* Symbols *)
@ -161,6 +162,7 @@ val mk_verbatim : lexeme -> Region.t -> token
val mk_bytes : lexeme -> Region.t -> token val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token val eof : Region.t -> token
(* Predicates *) (* Predicates *)

View File

@ -32,6 +32,7 @@ type t =
| Mutez of (lexeme * Z.t) Region.reg | Mutez of (lexeme * Z.t) Region.reg
| Ident of lexeme Region.reg | Ident of lexeme Region.reg
| Constr of lexeme Region.reg | Constr of lexeme Region.reg
| Insert of lexeme Region.reg
(* Symbols *) (* Symbols *)
@ -141,6 +142,14 @@ let proj_token = function
| Constr Region.{region; value} -> | Constr Region.{region; value} ->
region, sprintf "Constr \"%s\"" value region, sprintf "Constr \"%s\"" value
| Insert Region.{region; value} ->
region, sprintf "Insert \"%s\"" value
(*
| Attr {header; string={region; value}} ->
region, sprintf "Attr (\"%s\",\"%s\")" header value
*)
(* Symbols *) (* Symbols *)
| SEMI region -> region, "SEMI" | SEMI region -> region, "SEMI"
@ -233,6 +242,7 @@ let to_lexeme = function
| Mutez i -> fst i.Region.value | Mutez i -> fst i.Region.value
| Ident id | Ident id
| Constr id -> id.Region.value | Constr id -> id.Region.value
| Insert i -> i.Region.value
(* Symbols *) (* Symbols *)
@ -365,7 +375,7 @@ let keywords = [
(fun reg -> Unit reg); (fun reg -> Unit reg);
(fun reg -> Var reg); (fun reg -> Var reg);
(fun reg -> While reg); (fun reg -> While reg);
(fun reg -> With reg) (fun reg -> With reg);
] ]
let reserved = SSet.empty let reserved = SSet.empty
@ -543,6 +553,11 @@ type attr_err = Invalid_attribute
let mk_attr _ _ _ = Error Invalid_attribute let mk_attr _ _ _ = Error Invalid_attribute
(* Raw Code Insertion *)
let mk_insert lexeme region =
Insert Region.{value=lexeme;region}
(* Predicates *) (* Predicates *)
let is_string = function String _ -> true | _ -> false let is_string = function String _ -> true | _ -> false

View File

@ -13,6 +13,7 @@
%token <(LexToken.lexeme * Z.t) Region.reg> Mutez "<mutez>" %token <(LexToken.lexeme * Z.t) Region.reg> Mutez "<mutez>"
%token <LexToken.lexeme Region.reg> Ident "<ident>" %token <LexToken.lexeme Region.reg> Ident "<ident>"
%token <LexToken.lexeme Region.reg> Constr "<constr>" %token <LexToken.lexeme Region.reg> Constr "<constr>"
%token <LexToken.lexeme Region.reg> Insert "<insert>"
(* Symbols *) (* Symbols *)

View File

@ -855,6 +855,7 @@ core_expr:
| set_expr { ESet $1 } | set_expr { ESet $1 }
| record_expr { ERecord $1 } | record_expr { ERecord $1 }
| update_record { EUpdate $1 } | update_record { EUpdate $1 }
| code_insert_expr { ECodeInsert $1 }
| "<constr>" arguments { | "<constr>" arguments {
let region = cover $1.region $2.region in let region = cover $1.region $2.region in
EConstr (ConstrApp {region; value = $1, Some $2}) EConstr (ConstrApp {region; value = $1, Some $2})
@ -973,6 +974,17 @@ update_record:
let value = {record=$1; kwd_with=$2; updates} let value = {record=$1; kwd_with=$2; updates}
in {region; value} } in {region; value} }
code_insert_expr:
Insert "<verbatim>" ":" type_expr "]" {
let region = cover $1.region $5 in
let value = {
language =$1;
code =$2;
colon =$3;
type_anno=$4;
rbracket =$5}
in {region; value} }
field_assignment: field_assignment:
field_name "=" expr { field_name "=" expr {
let region = cover $1.region (expr_to_region $3) let region = cover $1.region (expr_to_region $3)

View File

@ -230,6 +230,14 @@ and print_fun_expr state {value; _} =
print_token state kwd_is "is"; print_token state kwd_is "is";
print_expr state return print_expr state return
and print_code_insert state {value; _} =
let {language;code;colon;type_anno;rbracket} : code_insert = value in
print_string state language;
print_string state code;
print_token state colon ":";
print_type_expr state type_anno;
print_token state rbracket "]"
and print_parameters state {value; _} = and print_parameters state {value; _} =
let {lpar; inside; rpar} = value in let {lpar; inside; rpar} = value in
print_token state lpar "("; print_token state lpar "(";
@ -459,6 +467,7 @@ and print_expr state = function
| ETuple e -> print_tuple_expr state e | ETuple e -> print_tuple_expr state e
| EPar e -> print_par_expr state e | EPar e -> print_par_expr state e
| EFun e -> print_fun_expr state e | EFun e -> print_fun_expr state e
| ECodeInsert e -> print_code_insert state e
and print_annot_expr state node = and print_annot_expr state node =
let {inside; _} : annot_expr par = node in let {inside; _} : annot_expr par = node in
@ -1010,6 +1019,21 @@ and pp_fun_expr state (expr: fun_expr) =
pp_expr (state#pad 1 0) expr.return pp_expr (state#pad 1 0) expr.return
in () in ()
and pp_code_insert state (rc : code_insert) =
let () =
let state = state#pad 3 0 in
pp_node state "<language>";
pp_string (state#pad 1 0) rc.language in
let () =
let state = state#pad 3 1 in
pp_node state "<code>";
pp_string (state#pad 1 0) rc.code in
let () =
let state = state#pad 3 2 in
pp_node state "<type annotation>";
pp_type_expr (state#pad 1 0) rc.type_anno
in ()
and pp_parameters state {value; _} = and pp_parameters state {value; _} =
let params = Utils.nsepseq_to_list value.inside in let params = Utils.nsepseq_to_list value.inside in
let arity = List.length params in let arity = List.length params in
@ -1491,6 +1515,9 @@ and pp_expr state = function
| EFun {value; region} -> | EFun {value; region} ->
pp_loc_node state "EFun" region; pp_loc_node state "EFun" region;
pp_fun_expr state value; pp_fun_expr state value;
| ECodeInsert {value; region} ->
pp_loc_node state "ECodeInsert" region;
pp_code_insert state value;
and pp_list_expr state = function and pp_list_expr state = function
ECons {value; region} -> ECons {value; region} ->

View File

@ -90,6 +90,7 @@ type t =
| Verbatim of string Region.reg | Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg | Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg | Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *) (* Keywords *)
@ -153,6 +154,7 @@ val mk_string : lexeme -> Region.t -> token
val mk_verbatim : lexeme -> Region.t -> token val mk_verbatim : lexeme -> Region.t -> token
val mk_bytes : lexeme -> Region.t -> token val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token val mk_constr : lexeme -> Region.t -> token
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token val eof : Region.t -> token
(* Predicates *) (* Predicates *)

View File

@ -76,6 +76,7 @@ type t =
| Verbatim of string Region.reg | Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg | Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg | Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *) (* Keywords *)
@ -169,6 +170,7 @@ let proj_token = function
| C_None region -> region, "C_None" | C_None region -> region, "C_None"
| C_Some region -> region, "C_Some" | C_Some region -> region, "C_Some"
| Attr Region.{region; value} -> region, sprintf "Attr %s" value | Attr Region.{region; value} -> region, sprintf "Attr %s" value
| Insert Region.{region; value} -> region, sprintf "Insert %s" value
| EOF region -> region, "EOF" | EOF region -> region, "EOF"
let to_lexeme = function let to_lexeme = function
@ -183,6 +185,7 @@ let to_lexeme = function
| Ident id -> id.Region.value | Ident id -> id.Region.value
| Constr id -> id.Region.value | Constr id -> id.Region.value
| Attr a -> a.Region.value | Attr a -> a.Region.value
| Insert i -> i.Region.value
(* Symbols *) (* Symbols *)
@ -484,6 +487,11 @@ let mk_attr header lexeme region =
Ok (Attr Region.{value=lexeme; region}) Ok (Attr Region.{value=lexeme; region})
else Error Invalid_attribute else Error Invalid_attribute
(* Raw Code Insertion *)
let mk_insert lexeme region =
Insert Region.{value=lexeme;region}
(* Predicates *) (* Predicates *)
let is_string = function String _ -> true | _ -> false let is_string = function String _ -> true | _ -> false

View File

@ -14,6 +14,7 @@
%token <string Region.reg> Ident "<ident>" %token <string Region.reg> Ident "<ident>"
%token <string Region.reg> Constr "<constr>" %token <string Region.reg> Constr "<constr>"
%token <string Region.reg> Attr "<attr>" %token <string Region.reg> Attr "<attr>"
%token <string Region.reg> Insert "<insert>"
(* Symbols *) (* Symbols *)

View File

@ -814,6 +814,7 @@ common_expr:
| unit { EUnit $1 } | unit { EUnit $1 }
| "false" { ELogic (BoolExpr (False $1)) } | "false" { ELogic (BoolExpr (False $1)) }
| "true" { ELogic (BoolExpr (True $1)) } | "true" { ELogic (BoolExpr (True $1)) }
| code_insert { ECodeInsert $1 }
core_expr_2: core_expr_2:
common_expr { $1 } common_expr { $1 }
@ -919,6 +920,17 @@ update_record:
rbrace = $6} rbrace = $6}
in {region; value} } in {region; value} }
code_insert:
Insert "<verbatim>" ":" type_expr "]" {
let region = cover $1.region $5 in
let value = {
language =$1;
code =$2;
colon =$3;
type_anno=$4;
rbracket =$5}
in {region; value} }
expr_with_let_expr: expr_with_let_expr:
expr expr
| let_expr(expr_with_let_expr) { $1 } | let_expr(expr_with_let_expr) { $1 }

View File

@ -79,6 +79,7 @@ module type TOKEN =
val mk_bytes : lexeme -> Region.t -> token val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token val eof : Region.t -> token
(* Predicates *) (* Predicates *)

View File

@ -43,6 +43,7 @@ module type TOKEN =
val mk_bytes : lexeme -> Region.t -> token val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token val eof : Region.t -> token
(* Predicates *) (* Predicates *)
@ -268,6 +269,11 @@ module Make (Token : TOKEN) : (S with module Token = Token) =
| Error Token.Invalid_attribute -> | Error Token.Invalid_attribute ->
fail region Invalid_attribute fail region Invalid_attribute
let mk_insert insert state buffer =
let region, _, state = state#sync buffer in
let token = Token.mk_insert insert region
in state#enqueue token
let mk_constr state buffer = let mk_constr state buffer =
let region, lexeme, state = state#sync buffer in let region, lexeme, state = state#sync buffer in
let token = Token.mk_constr lexeme region let token = Token.mk_constr lexeme region
@ -314,7 +320,7 @@ let esc = "\\n" | "\\\"" | "\\\\" | "\\b"
let common_sym = ';' | ',' | '(' | ')' | '[' | ']' | '{' | '}' let common_sym = ';' | ',' | '(' | ')' | '[' | ']' | '{' | '}'
| '=' | ':' | '|' | "->" | '.' | '_' | '^' | '=' | ':' | '|' | "->" | '.' | '_' | '^'
| '+' | '-' | '*' | '/' | '<' | "<=" | '>' | ">=" | '+' | '-' | '*' | '/' | '%' | '<' | "<=" | '>' | ">="
let pascaligo_sym = "=/=" | '#' | ":=" let pascaligo_sym = "=/=" | '#' | ":="
let cameligo_sym = "<>" | "::" | "||" | "&&" let cameligo_sym = "<>" | "::" | "||" | "&&"
let reasonligo_sym = '!' | "=>" | "!=" | "==" | "++" | "..." | "||" | "&&" let reasonligo_sym = '!' | "=>" | "!=" | "==" | "++" | "..." | "||" | "&&"
@ -353,6 +359,7 @@ let line_comments =
(* #include files *) (* #include files *)
let string = [^'"' '\\' '\n']* (* For strings of #include *) let string = [^'"' '\\' '\n']* (* For strings of #include *)
let insert = attr
(* RULES *) (* RULES *)
@ -388,6 +395,7 @@ and scan state = parse
| eof { mk_eof state lexbuf } | eof { mk_eof state lexbuf }
| "[@" (attr as a) "]" { mk_attr "[@" a state lexbuf } | "[@" (attr as a) "]" { mk_attr "[@" a state lexbuf }
| "[@@" (attr as a) "]" { mk_attr "[@@" a state lexbuf } | "[@@" (attr as a) "]" { mk_attr "[@@" a state lexbuf }
| "[%" (insert as i) "]" { mk_insert i state lexbuf }
(* Management of #include preprocessing directives (* Management of #include preprocessing directives

View File

@ -638,6 +638,13 @@ in trace (abstracting_expr t) @@
let%bind match_false = compile_expression c.ifnot in let%bind match_false = compile_expression c.ifnot in
return @@ e_cond ~loc expr match_true match_false return @@ e_cond ~loc expr match_true match_false
) )
| ECodeInsert ci -> (
let (ci, loc) = r_split ci in
let language = ci.language.value in
let code = ci.code.value in
let%bind type_anno = compile_type_expression ci.type_anno in
return @@ e_raw_code ~loc language code type_anno
)
and compile_fun lamb' : expr result = and compile_fun lamb' : expr result =
let return x = ok x in let return x = ok x in

View File

@ -459,6 +459,13 @@ let rec compile_expression (t:Raw.expr) : expr result =
let (f , loc) = r_split f in let (f , loc) = r_split f in
let%bind (_ty_opt, f') = compile_fun_expression ~loc f let%bind (_ty_opt, f') = compile_fun_expression ~loc f
in return @@ f' in return @@ f'
| ECodeInsert ci ->
let (ci, loc) = r_split ci in
let language = ci.language.value in
let code = ci.code.value in
let%bind type_anno = compile_type_expression ci.type_anno in
return @@ e_raw_code ~loc language code type_anno
and compile_update (u: Raw.update Region.reg) = and compile_update (u: Raw.update Region.reg) =
let u, loc = r_split u in let u, loc = r_split u in
let name, path = compile_path u.record in let name, path = compile_path u.record in

View File

@ -220,7 +220,7 @@ and compile_expression' : I.expression -> (O.expression option -> O.expression)
return @@ O.e_let_in ~loc (binder,ty_opt) false inline rhs let_result return @@ O.e_let_in ~loc (binder,ty_opt) false inline rhs let_result
| I.E_raw_code {language;code;type_anno} -> | I.E_raw_code {language;code;type_anno} ->
let%bind type_anno = compile_type_expression type_anno in let%bind type_anno = compile_type_expression type_anno in
return @@ O.E_raw_code {language;code;type_anno} return @@ O.e_raw_code ~loc language code type_anno
| I.E_constructor {constructor;element} -> | I.E_constructor {constructor;element} ->
let%bind element = compile_expression element in let%bind element = compile_expression element in
return @@ O.e_constructor ~loc constructor element return @@ O.e_constructor ~loc constructor element

View File

@ -72,7 +72,7 @@ let rec compile_expression : I.expression -> O.expression result =
let%bind let_result = compile_expression let_result in let%bind let_result = compile_expression let_result in
return @@ O.E_let_in {let_binder=(binder,ty_opt);inline;rhs;let_result} return @@ O.E_let_in {let_binder=(binder,ty_opt);inline;rhs;let_result}
| I.E_raw_code {language;code;type_anno} -> | I.E_raw_code {language;code;type_anno} ->
let%bind type_anno = idle_type_expression type_anno in let%bind type_anno = compile_type_expression type_anno in
return @@ O.E_raw_code {language;code;type_anno} return @@ O.E_raw_code {language;code;type_anno}
| I.E_constructor {constructor;element} -> | I.E_constructor {constructor;element} ->
let%bind element = compile_expression element in let%bind element = compile_expression element in

View File

@ -24,7 +24,7 @@ let rec fold_expression : 'a folder -> 'a -> expression -> 'a result = fun f ini
let self = fold_expression f in let self = fold_expression f in
let%bind init' = f init e in let%bind init' = f init e in
match e.expression_content with match e.expression_content with
| E_literal _ | E_variable _ -> ok init' | E_literal _ | E_variable _ | E_raw_code _ -> ok init'
| E_constant {arguments=lst} -> ( | E_constant {arguments=lst} -> (
let%bind res = bind_fold_list self init' lst in let%bind res = bind_fold_list self init' lst in
ok res ok res
@ -148,7 +148,7 @@ let rec map_expression : exp_mapper -> expression -> expression result = fun f e
let%bind args = bind_map_list self c.arguments in let%bind args = bind_map_list self c.arguments in
return @@ E_constant {c with arguments=args} return @@ E_constant {c with arguments=args}
) )
| E_literal _ | E_variable _ as e' -> return e' | E_literal _ | E_variable _ | E_raw_code _ as e' -> return e'
and map_type_expression : ty_exp_mapper -> type_expression -> type_expression result = fun f ({type_content ; location ; type_meta} as te) -> and map_type_expression : ty_exp_mapper -> type_expression -> type_expression result = fun f ({type_content ; location ; type_meta} as te) ->
let self = map_type_expression f in let self = map_type_expression f in
@ -262,7 +262,7 @@ let rec fold_map_expression : 'a fold_mapper -> 'a -> expression -> ('a * expres
let%bind (res,args) = bind_fold_map_list self init' c.arguments in let%bind (res,args) = bind_fold_map_list self init' c.arguments in
ok (res, return @@ E_constant {c with arguments=args}) ok (res, return @@ E_constant {c with arguments=args})
) )
| E_literal _ | E_variable _ as e' -> ok (init', return e') | E_literal _ | E_variable _ | E_raw_code _ as e' -> ok (init', return e')
and fold_map_cases : 'a fold_mapper -> 'a -> matching_expr -> ('a * matching_expr) result = fun f init m -> and fold_map_cases : 'a fold_mapper -> 'a -> matching_expr -> ('a * matching_expr) result = fun f init m ->
match m with match m with

View File

@ -311,8 +311,8 @@ let raw_code : T.type_expression -> (constraints * T.type_variable) =
fun type_anno -> fun type_anno ->
let type_anno = type_expression_to_type_value type_anno in let type_anno = type_expression_to_type_value type_anno in
let whole_expr = Core.fresh_type_variable () in let whole_expr = Core.fresh_type_variable () in
O.[ [
C_equation (type_anno, P_variable whole_expr) c_equation type_anno (P_variable whole_expr) "wrap: raw_code: type_anno (whole)";
], whole_expr ], whole_expr
let assign : T.type_expression -> T.type_expression -> (constraints * T.type_variable) = let assign : T.type_expression -> T.type_expression -> (constraints * T.type_variable) =

View File

@ -115,6 +115,7 @@ let e_application ?loc a b = make_e ?loc @@ E_application {lamb=a ; args=b}
let e_lambda ?loc binder input_type output_type result : expression = make_e ?loc @@ E_lambda {binder; input_type; output_type; result} let e_lambda ?loc binder input_type output_type result : expression = make_e ?loc @@ E_lambda {binder; input_type; output_type; result}
let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda} let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda}
let e_let_in ?loc (binder, ascr) inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline } let e_let_in ?loc (binder, ascr) inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline }
let e_raw_code ?loc language code type_anno = make_e ?loc @@ E_raw_code {language; code; type_anno}
let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = Constructor s; element = a} let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = Constructor s; element = a}
let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b} let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b}

View File

@ -95,6 +95,7 @@ val e_application : ?loc:Location.t -> expression -> expression -> expression
val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression
val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression
val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression
val e_raw_code : ?loc:Location.t -> string -> string -> type_expression -> expression
val e_constructor : ?loc:Location.t -> string -> expression -> expression val e_constructor : ?loc:Location.t -> string -> expression -> expression
val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression

View File

@ -104,6 +104,7 @@ let e_lambda ?loc binder input_type output_type result : expression = make_e ?lo
let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda} let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda}
let e_let_in ?loc (binder, ascr) mut inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut } let e_let_in ?loc (binder, ascr) mut inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut }
let e_raw_code ?loc language code type_anno: expression = make_e ?loc @@ E_raw_code { language; code; type_anno}
let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = s; element = a} let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = s; element = a}
let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b} let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b}

View File

@ -70,6 +70,7 @@ val e_some : ?loc:Location.t -> expression -> expression
val e_none : ?loc:Location.t -> unit -> expression val e_none : ?loc:Location.t -> unit -> expression
val e_variable : ?loc:Location.t -> expression_variable -> expression val e_variable : ?loc:Location.t -> expression_variable -> expression
val e_raw_code : ?loc:Location.t -> string -> string -> type_expression -> expression
val e_constructor : ?loc:Location.t -> constructor' -> expression -> expression val e_constructor : ?loc:Location.t -> constructor' -> expression -> expression
val e_constant : ?loc:Location.t -> constant' -> expression list -> expression val e_constant : ?loc:Location.t -> constant' -> expression list -> expression

View File

@ -45,6 +45,7 @@ let rec expression : environment -> expression -> expression = fun env expr ->
let (lamb , args) = self_2 c.lamb c.args in let (lamb , args) = self_2 c.lamb c.args in
return @@ E_application { lamb ; args } return @@ E_application { lamb ; args }
) )
| E_raw_code _ -> return_id
| E_constructor c -> ( | E_constructor c -> (
let element = self c.element in let element = self c.element in
return @@ E_constructor { c with element } return @@ E_constructor { c with element }

View File

@ -0,0 +1,4 @@
// Test michelson insertion in PascaLIGO
function michelson_add (var n : nat) : nat is
[%Michelson {| DUP;ADD |} : nat -> nat ]

View File

@ -0,0 +1,4 @@
// Test michelson insertion in CameLIGO
let michelson_add (n : nat) : nat =
[%Michelson {| DUP;ADD; PUSH "hello" |} : nat -> nat ]

View File

@ -0,0 +1,4 @@
// Test michelson insertion in ReasonLIGO
let michelson_add = (n : nat) : nat =>
[%Michelson {| DUP;ADD; PUSH "hello" |} : nat => nat ]