move the work from lexer to parser: this makes the rules symetrics but impose the language to have the same syntax as constructor. This may change in the future

This commit is contained in:
Pierre-Emmanuel Wulfman 2020-05-20 11:09:21 +02:00
parent f25456a7a6
commit 0b8effbf2b
20 changed files with 73 additions and 80 deletions

View File

@ -401,6 +401,8 @@ and cond_expr = {
}
and code_insert = {
lbracket : lbracket;
percent : percent;
language : string reg;
code : expr;
rbracket : rbracket;

View File

@ -42,6 +42,7 @@ type t =
| PLUS of Region.t (* "+" *)
| SLASH of Region.t (* "/" *)
| TIMES of Region.t (* "*" *)
| PERCENT of Region.t (* "%" *)
(* Compounds *)
@ -87,7 +88,6 @@ type t =
| Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *)
@ -155,7 +155,6 @@ val mk_verbatim : lexeme -> Region.t -> token
val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token
(* Predicates *)

View File

@ -26,6 +26,7 @@ type t =
| PLUS of Region.t (* "+" *)
| SLASH of Region.t (* "/" *)
| TIMES of Region.t (* "*" *)
| PERCENT of Region.t (* "%" *)
(* Compounds *)
@ -71,7 +72,6 @@ type t =
| Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *)
@ -131,8 +131,6 @@ let proj_token = function
region, sprintf "Constr %s" value
| Attr Region.{region; value} ->
region, sprintf "Attr \"%s\"" value
| Insert Region.{region; value} ->
region, sprintf "Insert \"%s\"" value
(* Symbols *)
@ -143,6 +141,7 @@ let proj_token = function
| PLUS region -> region, "PLUS"
| SLASH region -> region, "SLASH"
| TIMES region -> region, "TIMES"
| PERCENT region -> region, "PERCENT"
| LPAR region -> region, "LPAR"
| RPAR region -> region, "RPAR"
| LBRACKET region -> region, "LBRACKET"
@ -207,7 +206,6 @@ let to_lexeme = function
| Ident id -> id.Region.value
| Constr id -> id.Region.value
| Attr a -> a.Region.value
| Insert i -> i.Region.value
(* Symbols *)
@ -218,6 +216,7 @@ let to_lexeme = function
| PLUS _ -> "+"
| SLASH _ -> "/"
| TIMES _ -> "*"
| PERCENT _ -> "%"
| LPAR _ -> "("
| RPAR _ -> ")"
| LBRACKET _ -> "["
@ -479,6 +478,7 @@ let mk_sym lexeme region =
| "-" -> Ok (MINUS region)
| "*" -> Ok (TIMES region)
| "/" -> Ok (SLASH region)
| "%" -> Ok (PERCENT region)
| "<" -> Ok (LT region)
| "<=" -> Ok (LE region)
| ">" -> Ok (GT region)
@ -512,9 +512,6 @@ let mk_attr header lexeme region =
if header = "[@" then Error Invalid_attribute
else Ok (Attr Region.{value=lexeme; region})
let mk_insert lexeme region =
Insert Region.{value=lexeme;region}
(* Predicates *)
let is_string = function String _ -> true | _ -> false

View File

@ -14,14 +14,14 @@
%token <string Region.reg> Ident "<ident>"
%token <string Region.reg> Constr "<constr>"
%token <string Region.reg> Attr "<attr>"
%token <string Region.reg> Insert "<insert>"
(* Symbols *)
%token <Region.t> MINUS "-"
%token <Region.t> PLUS "+"
%token <Region.t> SLASH "/"
%token <Region.t> TIMES "*"
%token <Region.t> MINUS "-"
%token <Region.t> PLUS "+"
%token <Region.t> SLASH "/"
%token <Region.t> TIMES "*"
%token <Region.t> PERCENT "%"
%token <Region.t> LPAR "("
%token <Region.t> RPAR ")"

View File

@ -709,10 +709,12 @@ seq_expr:
disj_expr_level | if_then_else (seq_expr) { $1 }
code_insert:
Insert expr "]" {
let region = cover $1.region $3 in
"[" "%" Constr expr "]" {
let region = cover $1 $5 in
let value = {
language =$1;
code =$2;
rbracket =$3}
lbracket =$1;
percent =$2;
language =$3;
code =$4;
rbracket =$5}
in {region; value} }

View File

@ -520,7 +520,9 @@ and print_record_expr state e =
print_ne_injection state print_field_assign e
and print_code_insert state {value; _} =
let {language;code;rbracket} : code_insert = value in
let {lbracket;percent;language;code;rbracket} : code_insert = value in
print_token state lbracket "[";
print_token state percent "%";
print_string state language;
print_expr state code;
print_token state rbracket "]"

View File

@ -438,6 +438,8 @@ and for_collect = {
}
and code_insert = {
lbracket : lbracket;
percent : percent;
language : string reg;
code : expr;
rbracket : rbracket;

View File

@ -44,7 +44,6 @@ type t =
| Mutez of (lexeme * Z.t) Region.reg
| Ident of lexeme Region.reg
| Constr of lexeme Region.reg
| Insert of lexeme Region.reg
(* Symbols *)
@ -74,6 +73,7 @@ type t =
| DOT of Region.t (* "." *)
| WILD of Region.t (* "_" *)
| CAT of Region.t (* "^" *)
| PERCENT of Region.t (* "%" *)
(* Keywords *)
@ -162,7 +162,6 @@ val mk_verbatim : lexeme -> Region.t -> token
val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token
(* Predicates *)

View File

@ -32,7 +32,6 @@ type t =
| Mutez of (lexeme * Z.t) Region.reg
| Ident of lexeme Region.reg
| Constr of lexeme Region.reg
| Insert of lexeme Region.reg
(* Symbols *)
@ -62,6 +61,7 @@ type t =
| DOT of Region.t
| WILD of Region.t
| CAT of Region.t
| PERCENT of Region.t (* "%" *)
(* Keywords *)
@ -142,9 +142,6 @@ let proj_token = function
| Constr Region.{region; 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
@ -178,6 +175,7 @@ let proj_token = function
| DOT region -> region, "DOT"
| WILD region -> region, "WILD"
| CAT region -> region, "CAT"
| PERCENT region -> region, "PERCENT"
(* Keywords *)
@ -242,7 +240,6 @@ let to_lexeme = function
| Mutez i -> fst i.Region.value
| Ident id
| Constr id -> id.Region.value
| Insert i -> i.Region.value
(* Symbols *)
@ -272,6 +269,7 @@ let to_lexeme = function
| DOT _ -> "."
| WILD _ -> "_"
| CAT _ -> "^"
| PERCENT _ -> "%"
(* Keywords *)
@ -523,6 +521,7 @@ let mk_sym lexeme region =
| "-" -> Ok (MINUS region)
| "*" -> Ok (TIMES region)
| "/" -> Ok (SLASH region)
| "%" -> Ok (PERCENT region)
| "<" -> Ok (LT region)
| "<=" -> Ok (LE region)
| ">" -> Ok (GT region)
@ -553,11 +552,6 @@ type attr_err = Invalid_attribute
let mk_attr _ _ _ = Error Invalid_attribute
(* Raw Code Insertion *)
let mk_insert lexeme region =
Insert Region.{value=lexeme;region}
(* Predicates *)
let is_string = function String _ -> true | _ -> false

View File

@ -13,7 +13,6 @@
%token <(LexToken.lexeme * Z.t) Region.reg> Mutez "<mutez>"
%token <LexToken.lexeme Region.reg> Ident "<ident>"
%token <LexToken.lexeme Region.reg> Constr "<constr>"
%token <LexToken.lexeme Region.reg> Insert "<insert>"
(* Symbols *)
@ -43,6 +42,7 @@
%token <Region.t> DOT "."
%token <Region.t> WILD "_"
%token <Region.t> CAT "^"
%token <Region.t> PERCENT "%"
(* Keywords *)

View File

@ -975,12 +975,14 @@ update_record:
in {region; value} }
code_insert_expr:
Insert expr "]" {
let region = cover $1.region $3 in
"[" "%" Constr expr "]" {
let region = cover $1 $5 in
let value = {
language =$1;
code =$2;
rbracket =$3}
lbracket =$1;
percent =$2;
language =$3;
code =$4;
rbracket =$5}
in {region; value} }
field_assignment:

View File

@ -231,7 +231,9 @@ and print_fun_expr state {value; _} =
print_expr state return
and print_code_insert state {value; _} =
let {language;code;rbracket} : code_insert = value in
let {lbracket;percent;language;code;rbracket} : code_insert = value in
print_token state lbracket "[";
print_token state percent "%";
print_string state language;
print_expr state code;
print_token state rbracket "]"

View File

@ -32,6 +32,7 @@ interactive_expr: BigMap LBRACKET With
##
## Ends in an error in state: 140.
##
## code_insert_expr -> LBRACKET . PERCENT Constr expr RBRACKET [ TIMES SLASH PLUS Or NE Mod MINUS LT LE GT GE EQ Contains CONS CAT And ARROW ]
## injection(BigMap,binding) -> BigMap LBRACKET . sep_or_term_list(binding,SEMI) RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
## injection(BigMap,binding) -> BigMap LBRACKET . RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
##
@ -876,7 +877,7 @@ interactive_expr: Function LPAR With
interactive_expr: Function With
##
## Ends in an error in state: 111.
## Ends in an error in state: 120.
##
## fun_expr -> Function . parameters COLON type_expr Is expr [ VBAR Type To Then Step SEMI Recursive RPAR RBRACKET RBRACE Of Function From End Else EOF Const COMMA Block Begin Attributes ARROW ]
##
@ -1228,7 +1229,7 @@ interactive_expr: Ident With Record Ident While
interactive_expr: Ident With Record Ident With
##
## Ends in an error in state: 166.
## Ends in an error in state: 169.
##
## field_path_assignment -> path . EQ expr [ SEMI RBRACKET End ]
##
@ -1576,6 +1577,7 @@ interactive_expr: List LBRACKET With
##
## Ends in an error in state: 359.
##
## code_insert_expr -> LBRACKET . PERCENT Constr expr RBRACKET [ TIMES SLASH SEMI PLUS Or NE Mod MINUS LT LE GT GE End EQ Contains CONS CAT And ]
## injection(List,expr) -> List LBRACKET . sep_or_term_list(expr,SEMI) RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
## injection(List,expr) -> List LBRACKET . RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
##
@ -1675,6 +1677,7 @@ interactive_expr: Map LBRACKET With
##
## Ends in an error in state: 376.
##
## code_insert_expr -> LBRACKET . PERCENT Constr expr RBRACKET [ TIMES SLASH PLUS Or NE Mod MINUS LT LE GT GE EQ Contains CONS CAT And ARROW ]
## injection(Map,binding) -> Map LBRACKET . sep_or_term_list(binding,SEMI) RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
## injection(Map,binding) -> Map LBRACKET . RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
##
@ -2109,6 +2112,7 @@ interactive_expr: Set LBRACKET With
##
## Ends in an error in state: 398.
##
## code_insert_expr -> LBRACKET . PERCENT Constr expr RBRACKET [ TIMES SLASH SEMI PLUS Or NE Mod MINUS LT LE GT GE End EQ Contains CONS CAT And ]
## injection(Set,expr) -> Set LBRACKET . sep_or_term_list(expr,SEMI) RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
## injection(Set,expr) -> Set LBRACKET . RBRACKET [ VBAR Type To Then TIMES Step SLASH SEMI Recursive RPAR RBRACKET RBRACE PLUS Or Of NE Mod MINUS LT LE GT GE Function From End Else EQ EOF Contains Const CONS COMMA COLON CAT Block Begin Attributes And ARROW ]
##
@ -3487,6 +3491,7 @@ contract: Function Ident LPAR Const Ident COLON Ident RPAR COLON String Is Begin
##
## Ends in an error in state: 441.
##
## code_insert_expr -> LBRACKET . PERCENT Constr expr RBRACKET [ TIMES SLASH PLUS Or NE Mod MINUS LT LE GT GE EQ Contains CONS CAT And ARROW ]
## ne_injection(Map,binding) -> Map LBRACKET . sep_or_term_list(binding,SEMI) RBRACKET [ VBAR SEMI RBRACKET RBRACE End Else ]
##
## The known suffix of the stack is as follows:
@ -3656,6 +3661,7 @@ contract: Function Ident LPAR Const Ident COLON Ident RPAR COLON String Is Begin
##
## Ends in an error in state: 429.
##
## code_insert_expr -> LBRACKET . PERCENT Constr expr RBRACKET [ TIMES SLASH SEMI PLUS Or NE Mod MINUS LT LE GT GE End EQ Contains CONS CAT And ]
## ne_injection(Set,expr) -> Set LBRACKET . sep_or_term_list(expr,SEMI) RBRACKET [ VBAR SEMI RBRACKET RBRACE End Else ]
##
## The known suffix of the stack is as follows:
@ -4767,4 +4773,3 @@ contract: With
##
<YOUR SYNTAX ERROR MESSAGE HERE>

View File

@ -35,10 +35,11 @@ type t =
(* Arithmetics *)
| MINUS of Region.t (* "-" *)
| PLUS of Region.t (* "+" *)
| SLASH of Region.t (* "/" *)
| TIMES of Region.t (* "*" *)
| MINUS of Region.t (* "-" *)
| PLUS of Region.t (* "+" *)
| SLASH of Region.t (* "/" *)
| TIMES of Region.t (* "*" *)
| PERCENT of Region.t (* "%" *)
(* Compounds *)
@ -90,7 +91,6 @@ type t =
| Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *)
@ -154,7 +154,6 @@ val mk_string : lexeme -> Region.t -> token
val mk_verbatim : lexeme -> Region.t -> token
val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token
(* Predicates *)

View File

@ -21,10 +21,11 @@ type t =
(* Arithmetics *)
| MINUS of Region.t (* "-" *)
| PLUS of Region.t (* "+" *)
| SLASH of Region.t (* "/" *)
| TIMES of Region.t (* "*" *)
| MINUS of Region.t (* "-" *)
| PLUS of Region.t (* "+" *)
| SLASH of Region.t (* "/" *)
| TIMES of Region.t (* "*" *)
| PERCENT of Region.t (* "%" *)
(* Compounds *)
@ -76,7 +77,6 @@ type t =
| Verbatim of string Region.reg
| Bytes of (string * Hex.t) Region.reg
| Attr of string Region.reg
| Insert of string Region.reg
(* Keywords *)
@ -133,6 +133,7 @@ let proj_token = function
| PLUS region -> region, "PLUS"
| SLASH region -> region, "SLASH"
| TIMES region -> region, "TIMES"
| PERCENT region -> region, "PERCENT"
| LPAR region -> region, "LPAR"
| RPAR region -> region, "RPAR"
| LBRACKET region -> region, "LBRACKET"
@ -170,7 +171,6 @@ let proj_token = function
| C_None region -> region, "C_None"
| C_Some region -> region, "C_Some"
| Attr Region.{region; value} -> region, sprintf "Attr %s" value
| Insert Region.{region; value} -> region, sprintf "Insert %s" value
| EOF region -> region, "EOF"
let to_lexeme = function
@ -185,7 +185,6 @@ let to_lexeme = function
| Ident id -> id.Region.value
| Constr id -> id.Region.value
| Attr a -> a.Region.value
| Insert i -> i.Region.value
(* Symbols *)
@ -194,6 +193,7 @@ let to_lexeme = function
| PLUS _ -> "+"
| SLASH _ -> "/"
| TIMES _ -> "*"
| PERCENT _ -> "%"
| LPAR _ -> "("
| RPAR _ -> ")"
| LBRACKET _ -> "["
@ -432,6 +432,7 @@ let mk_sym lexeme region =
| "+" -> Ok (PLUS region)
| "/" -> Ok (SLASH region)
| "*" -> Ok (TIMES region)
| "%" -> Ok (PERCENT region)
| "[" -> Ok (LBRACKET region)
| "]" -> Ok (RBRACKET region)
| "{" -> Ok (LBRACE region)
@ -487,11 +488,6 @@ let mk_attr header lexeme region =
Ok (Attr Region.{value=lexeme; region})
else Error Invalid_attribute
(* Raw Code Insertion *)
let mk_insert lexeme region =
Insert Region.{value=lexeme;region}
(* Predicates *)
let is_string = function String _ -> true | _ -> false

View File

@ -14,14 +14,14 @@
%token <string Region.reg> Ident "<ident>"
%token <string Region.reg> Constr "<constr>"
%token <string Region.reg> Attr "<attr>"
%token <string Region.reg> Insert "<insert>"
(* Symbols *)
%token <Region.t> MINUS "-"
%token <Region.t> PLUS "+"
%token <Region.t> SLASH "/"
%token <Region.t> TIMES "*"
%token <Region.t> MINUS "-"
%token <Region.t> PLUS "+"
%token <Region.t> SLASH "/"
%token <Region.t> TIMES "*"
%token <Region.t> PERCENT "%"
%token <Region.t> LPAR "("
%token <Region.t> RPAR ")"

View File

@ -921,12 +921,14 @@ update_record:
in {region; value} }
code_insert:
Insert expr "]" {
let region = cover $1.region $3 in
"[" "%" Constr expr "]" {
let region = cover $1 $5 in
let value = {
language =$1;
code =$2;
rbracket =$3}
lbracket =$1;
percent =$2;
language =$3;
code =$4;
rbracket =$5}
in {region; value} }
expr_with_let_expr:

View File

@ -3794,4 +3794,3 @@ contract: WILD
##
<YOUR SYNTAX ERROR MESSAGE HERE>

View File

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

View File

@ -43,7 +43,6 @@ module type TOKEN =
val mk_bytes : lexeme -> Region.t -> token
val mk_constr : lexeme -> Region.t -> token
val mk_attr : string -> lexeme -> Region.t -> (token, attr_err) result
val mk_insert : lexeme -> Region.t -> token
val eof : Region.t -> token
(* Predicates *)
@ -269,11 +268,6 @@ module Make (Token : TOKEN) : (S with module Token = Token) =
| Error Token.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 region, lexeme, state = state#sync buffer in
let token = Token.mk_constr lexeme region
@ -359,7 +353,6 @@ let line_comments =
(* #include files *)
let string = [^'"' '\\' '\n']* (* For strings of #include *)
let insert = attr
(* RULES *)
@ -395,7 +388,6 @@ and scan state = parse
| eof { mk_eof 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