ajout pattern pour le parser, attention aux conflits never reduced de Or et And
This commit is contained in:
parent
be467f3503
commit
d957aa29a9
2 changed files with 47 additions and 3 deletions
|
@ -82,9 +82,13 @@ rule token = parse
|
|||
| ']' { RBRACK }
|
||||
| '_' { WILDCARD }
|
||||
| ':' { COLON }
|
||||
| '{' { LBRACE }
|
||||
| '}' { RBRACE }
|
||||
| "->" { ARROW }
|
||||
| "<" { INFERIOR }
|
||||
| ">" { SUPERIOR }
|
||||
| '|' { PIPE }
|
||||
| '&' { AND }
|
||||
|
||||
(** Values *)
|
||||
| integers as i { INT (Mint.of_string i) }
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
%}
|
||||
|
||||
%token EOF LET TYPE WILDCARD STAR INFERIOR SUPERIOR ARROW COLON EXTERN FUN MATCH COMMA IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK
|
||||
%token EOF LET TYPE WILDCARD LBRACE RBRACE STAR PIPE INFERIOR SUPERIOR ARROW COLON EXTERN FUN MATCH COMMA IF THEN ELSE REF AND WHILE DO UNTIL FOR FROM TO BINOP EQUAL LPAREN RPAREN LBRACK RBRACK
|
||||
|
||||
%token<Mint.t> INT
|
||||
%token<string> ID TID CID STRING
|
||||
|
@ -13,6 +13,8 @@
|
|||
|
||||
%start<HopixAST.t> program
|
||||
|
||||
%right PIPE
|
||||
|
||||
%%
|
||||
(************** PROGRAM ****************)
|
||||
|
||||
|
@ -47,7 +49,7 @@ fundef:
|
|||
| i=located(identifier) p=located(pattern) EQUAL e=located(expression){
|
||||
i,None,FunctionDefinition(p,e)
|
||||
}
|
||||
| DPOINT ts=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression){
|
||||
| COLON ts=option(located(type_scheme)) i=located(identifier) p=located(pattern) EQUAL e=located(expression){
|
||||
i,ts,FunctionDefinition(p,e)
|
||||
}
|
||||
|
||||
|
@ -56,6 +58,9 @@ fundef:
|
|||
(************* PATTERN **************)
|
||||
|
||||
|
||||
(* à revoir éventuellement : PTaggedValue (et PRecord) est réécrite 4 fois, mais peut être qu'en utilisant des option, on pourrait diminuer le nombre de répétition. *)
|
||||
(* + TODO : y'a environ 50 warnings ici, surtout au niveau du POr et PAnd *)
|
||||
|
||||
pattern:
|
||||
| i=located(identifier){
|
||||
PVariable i
|
||||
|
@ -63,8 +68,43 @@ pattern:
|
|||
| WILDCARD {
|
||||
PWildcard
|
||||
}
|
||||
| p=located(pattern) COLON ty=located(ty){
|
||||
PTypeAnnotation(p,ty)
|
||||
}
|
||||
| l=located(literal){
|
||||
PLiteral l
|
||||
}
|
||||
| const = located(constructor) {
|
||||
PTaggedValue(const,None,[])
|
||||
}
|
||||
| const = located(constructor) INFERIOR liste_ty=option(separated_nonempty_list(COMMA,located(ty))) SUPERIOR{
|
||||
PTaggedValue(const,liste_ty,[])
|
||||
}
|
||||
| const = located(constructor) LPAREN liste_pattern = separated_nonempty_list(COMMA,located(pattern)) RPAREN {
|
||||
PTaggedValue(const,None,liste_pattern)
|
||||
}
|
||||
| const = located(constructor) INFERIOR liste_ty=option(separated_nonempty_list(COMMA,located(ty))) SUPERIOR LPAREN liste_pattern = separated_nonempty_list(COMMA,located(pattern)) RPAREN {
|
||||
PTaggedValue(const,liste_ty,liste_pattern)
|
||||
}
|
||||
(* à refaire *)
|
||||
| LBRACE l=separated_nonempty_list(COMMA,separated_pair(located(label),EQUAL,located(pattern))) RBRACE {
|
||||
PRecord(l,None)
|
||||
}
|
||||
| LBRACE l=separated_nonempty_list(COMMA,separated_pair(located(label),EQUAL,located(pattern))) RBRACE INFERIOR SUPERIOR {
|
||||
PRecord(l,None)
|
||||
}
|
||||
| LBRACE l=separated_nonempty_list(COMMA,separated_pair(located(label),EQUAL,located(pattern))) RBRACE INFERIOR liste_ty = option(separated_nonempty_list(COMMA,located(ty))) SUPERIOR {
|
||||
PRecord(l,liste_ty)
|
||||
}
|
||||
|
||||
|
||||
|
||||
| p1=located(pattern) PIPE p_list = separated_nonempty_list(PIPE,located(pattern)){
|
||||
POr(p1::p_list)
|
||||
}
|
||||
| p1 = located(pattern) AND p_list = separated_nonempty_list(AND,located(pattern)){
|
||||
PAnd(p1::p_list)
|
||||
}
|
||||
(************** DATA TYPE **************)
|
||||
|
||||
ty:
|
||||
|
@ -125,7 +165,7 @@ constructor:
|
|||
|
||||
label:
|
||||
| label = TID {
|
||||
TId = label
|
||||
LId label
|
||||
}
|
||||
|
||||
|
||||
|
|
Reference in a new issue