essai parser

This commit is contained in:
Nicolas PENELOUX 2023-10-20 20:45:16 +02:00
parent aa664edcd4
commit 66af6713b5
2 changed files with 75 additions and 32 deletions

View file

@ -50,9 +50,8 @@ let char = '\'' atom '\''
let letter = (digit | ['A'-'Z'] | ['a'-'z']) let letter = (digit | ['A'-'Z'] | ['a'-'z'])
(* pas sûr pour str *) (* pas sûr pour str *)
(* let str = '\"' (([atom] | ['\''] | ["\\\""]) # '"')* '\"' *) let str = '\"' (atom | '\'' | "\\\"")* '\"'
(* let binop = '+' | '-' | '*' | '/' | "&&" | "||"| "=?"| "<=?" |">=?" |"<?" |">?" *)
rule token = parse rule token = parse
@ -81,8 +80,18 @@ rule token = parse
| "and" { AND_KW } | "and" { AND_KW }
| "for" { FOR } | "for" { FOR }
(** Binar operation : pas sûr pour celui là *) (** Opérateurs binaires *)
(* | binop as b { BINOP b } *) | "+" { PLUS }
| "-" { MINUS }
| "/" { SLASH }
| "&&" { D_AND }
| "||" { D_OR }
| "=?" { EQUAL_OP }
| "<=?" { INF_EQUAL_OP }
| ">=?" { SUP_EQUAL_OP }
| "<?" { INF_OP }
| ">?" { SUP_OP }
(** Ponctuation *) (** Ponctuation *)
| '=' { EQUAL } | '=' { EQUAL }

View file

@ -5,10 +5,11 @@
%} %}
%token EOF LET TYPE WILDCARD STAR ARROW COLON EXTERN FUN COMMA AND EQUAL LPAREN %token EOF LET TYPE WILDCARD ARROW COLON EXTERN FUN COMMA AND EQUAL LPAREN
%token RPAREN LBRACK RBRACK LBRACE RBRACE INFERIOR SUPERIOR BINOP DO ELSE FOR %token RPAREN LBRACK RBRACK LBRACE RBRACE INFERIOR SUPERIOR BINOP DO ELSE FOR
%token FROM IF MATCH PIPE REF THEN TO UNTIL WHILE AND_KW DOT SEMICOLON BACKSLASH %token FROM IF MATCH PIPE REF THEN TO UNTIL WHILE AND_KW DOT SEMICOLON BACKSLASH
%token ASSIGN EXCLA %token ASSIGN EXCLA
%token PLUS MINUS STAR SLASH D_AND D_OR EQUAL_OP INF_EQUAL_OP SUP_EQUAL_OP INF_OP SUP_OP
%token<Mint.t> INT %token<Mint.t> INT
%token<string> ID TID CID STRING %token<string> ID TID CID STRING
@ -51,14 +52,24 @@ definition:
tdefinition: tdefinition:
/* Type sommes */ /* Type sommes */
/* | option(PIPE) type_constructor option() separated_nonempty_list(COMMA, ty) { /* la définition étant assez compliqué, on va utilisé d'autre terme pour réduire la taille */
DefineSumType() | option(PIPE) l=separated_nonempty_list(PIPE,list_constructor_and_ty) {
} */ DefineSumType(l)
}
/* Type produit étiqueté */ /* Type produit étiqueté */
| LBRACE lt=separated_nonempty_list(COMMA, label_with_type) RBRACE { | LBRACE lt=separated_nonempty_list(COMMA, label_with_type) RBRACE {
DefineRecordType(lt) DefineRecordType(lt)
} }
list_constructor_and_ty: c=located(constructor) t=list_ty{
(c,t)
}
list_ty: LPAREN l=separated_nonempty_list(COMMA,located(ty)) RPAREN {
l
}
label_with_type: label_with_type:
| l=located(label) COLON t=located(ty) { l, t } | l=located(label) COLON t=located(ty) { l, t }
@ -341,8 +352,6 @@ simple_expression:
Apply(e1,e2) Apply(e1,e2)
} }
/* TODO operation binaire mais j'ai pas très bien compris encore */
/* Match (exp) {| ...| ... | ...} */ /* Match (exp) {| ...| ... | ...} */
| MATCH LPAREN e=located(expression) RPAREN | MATCH LPAREN e=located(expression) RPAREN
@ -351,10 +360,11 @@ simple_expression:
} }
/* TODO if ( exp ) then { expr } j'ai RIEN COMPRIS */ /* TODO if ( exp ) then { expr } j'ai RIEN COMPRIS */
/* /*
| IF LPAREN e=located(expression) RPAREN | IF LPAREN e=located(expression) RPAREN
THEN LBRACE e2=located(expression) RBRACE{ THEN LBRACE e2=located(expression) RBRACE{
IfThenElse(e,e2,None) IfThenElse(e,e2,Position.unknown_pos )
} }
*/ */
/* if ( expr ) then { expr } else { expr } */ /* if ( expr ) then { expr } else { expr } */
@ -399,6 +409,8 @@ simple_expression:
e e
} }
/* Annotation de type */ /* Annotation de type */
/* (e : ty) */ /* (e : ty) */
| LPAREN e=located(expression) COLON t=located(ty) RPAREN { | LPAREN e=located(expression) COLON t=located(ty) RPAREN {
@ -406,6 +418,14 @@ simple_expression:
} }
/* operateurs binaires */
/*
| e1=located(expression) b=binop e2=located(expression) {
Apply(Apply(b,e1),e2)
}*/
@ -455,6 +475,20 @@ identifier:
} }
%inline binop:
/*| loc=located(PLUS) { ("`+`",loc) }*/
| MINUS { "`-`" }
| STAR { "`*`" }
| SLASH { "`/`" }
| D_AND { "`&&`" }
| D_OR { "`||`" }
| EQUAL_OP { "`=?`" }
| INF_EQUAL_OP { "`<=?`" }
| SUP_EQUAL_OP { "`>=?`" }
| INF_OP { "`<?`" }
| SUP_OP { "`>?`" }
%inline located(X): x=X { %inline located(X): x=X {
Position.with_poss $startpos $endpos x Position.with_poss $startpos $endpos x
} }