ajout pattern pour le parser, attention aux conflits never reduced de Or et And

This commit is contained in:
Nicolas PENELOUX 2023-10-17 17:02:54 +02:00
parent be467f3503
commit d957aa29a9
2 changed files with 47 additions and 3 deletions

View file

@ -82,9 +82,13 @@ rule token = parse
| ']' { RBRACK } | ']' { RBRACK }
| '_' { WILDCARD } | '_' { WILDCARD }
| ':' { COLON } | ':' { COLON }
| '{' { LBRACE }
| '}' { RBRACE }
| "->" { ARROW } | "->" { ARROW }
| "<" { INFERIOR } | "<" { INFERIOR }
| ">" { SUPERIOR } | ">" { SUPERIOR }
| '|' { PIPE }
| '&' { AND }
(** Values *) (** Values *)
| integers as i { INT (Mint.of_string i) } | integers as i { INT (Mint.of_string i) }

View file

@ -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<Mint.t> INT
%token<string> ID TID CID STRING %token<string> ID TID CID STRING
@ -13,6 +13,8 @@
%start<HopixAST.t> program %start<HopixAST.t> program
%right PIPE
%% %%
(************** PROGRAM ****************) (************** PROGRAM ****************)
@ -47,7 +49,7 @@ fundef:
| i=located(identifier) p=located(pattern) EQUAL e=located(expression){ | i=located(identifier) p=located(pattern) EQUAL e=located(expression){
i,None,FunctionDefinition(p,e) 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) i,ts,FunctionDefinition(p,e)
} }
@ -56,6 +58,9 @@ fundef:
(************* PATTERN **************) (************* 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: pattern:
| i=located(identifier){ | i=located(identifier){
PVariable i PVariable i
@ -63,8 +68,43 @@ pattern:
| WILDCARD { | WILDCARD {
PWildcard 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 **************) (************** DATA TYPE **************)
ty: ty:
@ -125,7 +165,7 @@ constructor:
label: label:
| label = TID { | label = TID {
TId = label LId label
} }