Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language improvements #36

Merged
merged 6 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions src/Visp.Compiler/Lexer.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ rule token (args: LexArgs) (skip: bool) = parse

// punct
| "||" { BAR_BAR }
| "-||" { BAR_BAR }
| "-||" { BAR_BAR }
| '(' { LPAREN }
| ')' { RPAREN }
| '{' { LBRACE }
Expand All @@ -271,28 +271,26 @@ rule token (args: LexArgs) (skip: bool) = parse
| '.' propShort { DOT_METHOD (lexeme lexbuf) }
| '-' propShort { APPLY_METHOD (lexeme lexbuf) }
| '+' propShort { PROP_PLUS (lexeme lexbuf) }
| "#(" { HASH_PAREN }
| "#[" { HASH_BRACKET }
| "#{" { HASH_BRACE }
| "{|" { BRACE_BAR }
| "|}" { BAR_BRACE }
| "[|" { BRACKET_BAR }
| "|]" { BAR_BRACKET }
| "#(" { HASH_PAREN }
| "#[" { HASH_BRACKET }
| "#{" { HASH_BRACE }
| "{|" { BRACE_BAR }
| "|}" { BAR_BRACE }
| "[|" { BRACKET_BAR }
| "|]" { BAR_BRACKET }
| "(||)" { TOKENLIST [PAREN_BAR; BAR_PAREN] }
| "(||" { TOKENLIST [LPAREN; BAR_BAR] }
| "(|" { PAREN_BAR }
| "|)" { BAR_PAREN }
| ".." { DOTDOT }
| "'" { QUOTE_SYM }
| ".[" { DOT_BRACKET }
| ".+" { DOT_PLUS }
| '@' { AT }
| "(||" { TOKENLIST [LPAREN; BAR_BAR] }
| "(|" { PAREN_BAR }
| "|)" { BAR_PAREN }
| ".." { DOTDOT }
| "'" { QUOTE_SYM }
| ".[" { DOT_BRACKET }
| ".+" { DOT_PLUS }
| '@' { AT }
| "#\\" charWords { CHAR (lexeme lexbuf) }
| "#\\u" unicodeChars { CHAR (lexeme lexbuf) }
| "#\\U" unicodeChars { CHAR (lexeme lexbuf) }
| "#\\" nonWhitespace { CHAR (lexeme lexbuf) }
| whitespace { token args skip lexbuf }
| newline { newline lexbuf; token args skip lexbuf }

// --- START OF NUMBERS ---

Expand Down Expand Up @@ -428,25 +426,25 @@ rule token (args: LexArgs) (skip: bool) = parse

// --- END OF NUMBERS ---

| '+' { OP_PLUS }
| '-' { OP_MINUS }
| '.' { DOT }
| '+' { OP_PLUS }
| '-' { OP_MINUS }
| '.' { DOT }
| ">=" { GREATER_EQUALS }
| "<=" { LESS_EQUALS }
| "&&" { AMP_AMP }
| "!=" { BANG_EQUALS }
| '=' { EQUALS }
| '=' { EQUALS }

| ident_arrow { SYMBOL (lexeme lexbuf) }

| "->>" { THREAD_LAST }
| "->" { THREAD_FIRST }
| "<" { OP_LESS }
| ">" { OP_GREATER }
| ":>" { COLON_GREATER }
| ":?" { COLON_QMARK }
| "->" { THREAD_FIRST }
| "<" { OP_LESS }
| ">" { OP_GREATER }
| ":>" { COLON_GREATER }
| ":?" { COLON_QMARK }

| "#()" { UNIT }
| "#()" { UNIT }
| "unit" { UNIT }
| "true" { TRUE }
| "false" { FALSE }
Expand Down Expand Up @@ -528,8 +526,8 @@ and tokenStream (args: LexArgs) (skip: bool) = parse
| "[|" { BRACKET_BAR }
| "|]" { BAR_BRACKET }

| "(||)" { TOKENLIST [PAREN_BAR; BAR_PAREN] }
| "(||" { TOKENLIST [LPAREN; SYMBOL (lexemeTrimLeft lexbuf 1)] }
| "(||)" { TOKENLIST [PAREN_BAR; BAR_PAREN] }
| "(||" { TOKENLIST [LPAREN; SYMBOL (lexemeTrimLeft lexbuf 1)] }
| "(|" { PAREN_BAR }
| "|)" { BAR_PAREN }

Expand All @@ -544,16 +542,16 @@ and tokenStream (args: LexArgs) (skip: bool) = parse
| ident_arrow { SYMBOL (lexeme lexbuf) }

| "->>" { SYMBOL (lexeme lexbuf) }
| "->" { SYMBOL (lexeme lexbuf) }
| ">=" { SYMBOL (lexeme lexbuf) }
| "<=" { SYMBOL (lexeme lexbuf) }
| ">>" { SYMBOL (lexeme lexbuf) }
| "!=" { SYMBOL (lexeme lexbuf) }
| '<' { SYMBOL (lexeme lexbuf) }
| '>' { SYMBOL (lexeme lexbuf) }
| '=' { SYMBOL (lexeme lexbuf) }
| "&&" { SYMBOL (lexeme lexbuf) }
| "||" { SYMBOL (lexeme lexbuf) }
| "->" { SYMBOL (lexeme lexbuf) }
| ">=" { SYMBOL (lexeme lexbuf) }
| "<=" { SYMBOL (lexeme lexbuf) }
| ">>" { SYMBOL (lexeme lexbuf) }
| "!=" { SYMBOL (lexeme lexbuf) }
| '<' { SYMBOL (lexeme lexbuf) }
| '>' { SYMBOL (lexeme lexbuf) }
| '=' { SYMBOL (lexeme lexbuf) }
| "&&" { SYMBOL (lexeme lexbuf) }
| "||" { SYMBOL (lexeme lexbuf) }

// Constants
| "#()" { UNIT }
Expand Down
193 changes: 110 additions & 83 deletions src/Visp.Compiler/Parser.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ let mkRecoveryPat s = Syntax.parserRecoveryPat (lhs s)
%token DOTDOT
%token AT
%token OPEN MODULE
%token FN FNSTAR LET USE LET_BANG USE_BANG DO_BANG LETSTAR MUT SET DOT NEW DOT_BRACKET DOT_PLUS IF_KW BEGIN_KW DO_KW QUOTE_KW UNQUOTE_KW SPLICE_UNQUOTE_KW QUASIQUOTE_KW
%token ATOM_KW DEREF_KW WHILE TYPE TYPEALIAS INTERFACE RECORD MEMBER MEMBERS MEMBERFN MEMBER_GET MEMBER_SET OVERRIDE MACRO MATCH WHEN CONS CONCAT REQUIRE
%token FN FNSTAR LET USE VAL STATIC LET_BANG USE_BANG DO_BANG LETSTAR MUT SET DOT NEW DOT_BRACKET DOT_PLUS IF_KW BEGIN_KW DO_KW QUOTE_KW UNQUOTE_KW SPLICE_UNQUOTE_KW QUASIQUOTE_KW
%token ATOM_KW DEREF_KW WHILE TYPE TYPEALIAS INTERFACE RECORD MEMBER MEMBERS MEMBERFN MEMBER_GET MEMBER_SET OVERRIDE MACRO MATCH WHEN REQUIRE
%token THREAD_FIRST THREAD_LAST SYNTAX_MACRO SEQ UNION
%token INLINE REC RINIT
%token <bool> YIELD
Expand Down Expand Up @@ -339,9 +339,8 @@ parens_expr:
{ CollExpr.mkFsList ($1 :: $3) (lhs parseState)}
| function_def %prec prec_fn { $1 }
| function_call %prec prec_call { $1 }
| let { $1 }
| let_or_use { $1 }
| letstar { $1 }
| mut { $1 }
| set { $1 }
| if_expr { $1 }
| type_expr { $1 }
Expand All @@ -354,8 +353,6 @@ parens_expr:
| list_expr { $1 }
| match_expr { $1 }
| new_expr { $1 }
| cons_expr { $1 }
| concat_expr { $1 }
| operators_not_in_parens { SynExpr.Op($1) }
| syntax_macro_expr { $1 }
| macro_call_expr { $1 }
Expand Down Expand Up @@ -526,22 +523,6 @@ macro_body:
else
SynMacroBody.Symbol (text) }

cons_expr:
| CONS expr expr
{ SynExpr.Cons($2, $3, lhs parseState)}
| CONS expr
{ SynExpr.Cons($2, Syntax.parserRecoveryExpr (lhs parseState), lhs parseState)}
| CONS
{ SynExpr.Cons(Syntax.parserRecoveryExpr (lhs parseState), Syntax.parserRecoveryExpr (lhs parseState), lhs parseState)}

concat_expr:
| CONCAT expr expr
{ SynExpr.Concat($2, $3, lhs parseState)}
| CONCAT expr
{ SynExpr.Concat($2, Syntax.parserRecoveryExpr (lhs parseState), lhs parseState)}
| CONCAT
{ SynExpr.Concat(Syntax.parserRecoveryExpr (lhs parseState), Syntax.parserRecoveryExpr (lhs parseState), lhs parseState)}

new_expr:
| NEW syn_type_name
{ SynExpr.New($2, [], lhs parseState) }
Expand Down Expand Up @@ -753,53 +734,30 @@ set:
{ let r = lhs parseState
SynExpr.Set(Syntax.parserRecoveryExpr r, Syntax.parserRecoveryExpr r, r) }

let:
| USE syn_pat expr
{ SynExpr.LetOrUse($2, $3, LetFlags.Use, lhs parseState) }
| LET syn_pat expr
{ SynExpr.LetOrUse($2, $3, LetFlags.None, lhs parseState) }
| LET_BANG syn_pat expr
{ SynExpr.LetOrUse($2, $3, LetFlags.Bang, lhs parseState) }
| USE_BANG syn_pat expr
{ SynExpr.LetOrUse($2, $3, LetFlags.Bang ||| LetFlags.Use, lhs parseState) }
// TODO: Report error
| LET syn_pat
{ let r = lhs parseState
SynExpr.LetOrUse($2, Syntax.parserRecoveryExpr r, LetFlags.None, r) }
| USE syn_pat
let_use_mut_kw:
| LET { LetFlags.None }
| LET_BANG { LetFlags.Bang }
| USE { LetFlags.Use }
| USE_BANG { LetFlags.Bang ||| LetFlags.Use }
| MUT { LetFlags.Mutable }

let_or_use:
| let_use_mut_kw syn_pat expr
{ SynExpr.LetOrUse($2, $3, $1, [], lhs parseState) }
| let_use_mut_kw syn_pat
{ let r = lhs parseState
SynExpr.LetOrUse($2, Syntax.parserRecoveryExpr r, LetFlags.Use, r) }
| LET_BANG syn_pat
{ let r = lhs parseState
SynExpr.LetOrUse($2, Syntax.parserRecoveryExpr r, LetFlags.Bang, r) }
| USE_BANG syn_pat
{ let r = lhs parseState
SynExpr.LetOrUse($2, Syntax.parserRecoveryExpr r, LetFlags.Bang ||| LetFlags.Use, r) }
// TODO: Report error
| LET
SynExpr.LetOrUse($2, Syntax.parserRecoveryExpr r, $1, [], r) }
| let_use_mut_kw
{ let r = lhs parseState
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, LetFlags.None, r) }
| LET_BANG
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, $1, [], r) }
| attribute_list let_use_mut_kw syn_pat expr
{ SynExpr.LetOrUse($3, $4, $2, $1, lhs parseState) }
| attribute_list let_use_mut_kw syn_pat
{ let r = lhs parseState
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, LetFlags.Bang, r) }
| USE
SynExpr.LetOrUse($3, Syntax.parserRecoveryExpr r, $2, $1, r) }
| attribute_list let_use_mut_kw
{ let r = lhs parseState
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, LetFlags.Use, r) }
| USE_BANG
{ let r = lhs parseState
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, LetFlags.Use ||| LetFlags.Bang, r) }

mut:
| MUT syn_pat expr
{ SynExpr.LetOrUse($2, $3, LetFlags.Mutable, lhs parseState) }
// TODO: Report error
| MUT syn_pat
{ let r = lhs parseState
SynExpr.LetOrUse($2, Syntax.parserRecoveryExpr r, LetFlags.Mutable, r) }
// TODO: Report error
| MUT
{ let r = lhs parseState
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, LetFlags.Mutable, r) }
SynExpr.LetOrUse(Syntax.parserRecoveryPat r, Syntax.parserRecoveryExpr r, $2, $1, r) }

letstar:
| LETSTAR binding_list_start expr_list
Expand Down Expand Up @@ -841,8 +799,12 @@ type_expr:
{ SynExpr.Type($2, $3, [], [], lhs parseState) }
| TYPE type_name tuple_arg_pats member_list_start
{ SynExpr.Type($2, $3, $4, [], lhs parseState) }
| TYPE type_name member_list_start
{ SynExpr.Type($2, (SynPat.Ignore (rhs2 parseState 2 3)), $3, [], lhs parseState) }
| attribute_list TYPE type_name tuple_arg_pats member_list_start
{ SynExpr.Type($3, $4, $5, $1, lhs parseState) }
| attribute_list TYPE type_name member_list_start
{ SynExpr.Type($3, (SynPat.Ignore (rhs2 parseState 2 3)), $4, $1, lhs parseState) }

tuple_arg_pats:
| UNIT { SynPat.Args(SynArgPats.Tuple([]), lhs parseState) }
Expand Down Expand Up @@ -1015,7 +977,10 @@ member:
| LPAREN member_in_parens RPAREN { $2 }

member_name:
| MEMBER symbol { $2 }
| MEMBER symbol { (MemberFlags.Default, $2) }
| OVERRIDE symbol { (MemberFlags.Override, $2) }
| STATIC MEMBER symbol { (MemberFlags.Static, $3) }
| STATIC OVERRIDE symbol { (MemberFlags.Static ||| MemberFlags.Override, $3) }

member_get:
| LPAREN MEMBER_GET tuple_arg_pats expr_list_or_empty RPAREN
Expand All @@ -1025,27 +990,81 @@ member_set:
| LPAREN MEMBER_SET tuple_arg_pats syn_pat expr_list_or_empty RPAREN
{ SynMemberSet($3, $4, $5, lhs parseState) }

| LPAREN MEMBER_SET syn_pat_named expr_list_or_empty RPAREN
{ SynMemberSet(SynPat.Ignore(rhs parseState 2), $3, $4, lhs parseState) }

member_member_in_parens:
| member_name member_get { SynTypeMember.GetSet($1, Some($2), None, lhs parseState) }
| member_name member_set { SynTypeMember.GetSet($1, None, Some($2), lhs parseState) }
| member_name member_get member_set { SynTypeMember.GetSet($1, Some($2), Some($3), lhs parseState) }
| member_name member_set member_get { SynTypeMember.GetSet($1, Some($3), Some($2), lhs parseState) }
| member_name expr { SynTypeMember.Member($1, $2, lhs parseState) }
| member_name member_get { SynTypeMember.GetSet(snd $1, Some($2), None, fst $1, [], lhs parseState) }
| member_name member_set { SynTypeMember.GetSet(snd $1, None, Some($2), fst $1, [], lhs parseState) }
| member_name member_get member_set { SynTypeMember.GetSet(snd $1, Some($2), Some($3), fst $1, [], lhs parseState) }
| member_name member_set member_get { SynTypeMember.GetSet(snd $1, Some($3), Some($2), fst $1, [], lhs parseState) }
| member_name expr_list { SynTypeMember.Member(snd $1, $2, fst $1, [], lhs parseState) }
| member_name { SynTypeMember.Member(snd $1, [], fst $1, [], lhs parseState) }

| attribute_list member_name member_get { SynTypeMember.GetSet(snd $2, Some($3), None, fst $2, $1, lhs parseState) }
| attribute_list member_name member_set { SynTypeMember.GetSet(snd $2, None, Some($3), fst $2, $1, lhs parseState) }
| attribute_list member_name member_get member_set { SynTypeMember.GetSet(snd $2, Some($3), Some($4), fst $2, $1, lhs parseState) }
| attribute_list member_name member_set member_get { SynTypeMember.GetSet(snd $2, Some($4), Some($3), fst $2, $1, lhs parseState) }
| attribute_list member_name expr_list { SynTypeMember.Member(snd $2, $3, fst $2, $1, lhs parseState) }
| attribute_list member_name { SynTypeMember.Member(snd $2, [], fst $2, $1, lhs parseState) }

member_let_mut_kw:
| LET { LetFlags.None }
| MUT %prec prec_member { LetFlags.Mutable }
| STATIC LET { LetFlags.Static ||| LetFlags.None }
| STATIC MUT { LetFlags.Static ||| LetFlags.Mutable }

member_val_kw:
| VAL { LetFlags.None }
| VAL MUT { LetFlags.Mutable }

member_fn_kw:
| MEMBERFN { MemberFlags.Default }
| MEMBER FN { MemberFlags.Default }
| OVERRIDE FN { MemberFlags.Override }
| STATIC MEMBERFN { MemberFlags.Static }
| STATIC MEMBER FN { MemberFlags.Static }
| STATIC OVERRIDE FN { MemberFlags.Static ||| MemberFlags.Override }

member_fn:
| member_fn_kw syn_symbol list_arg_pats expr_list
{ SynTypeMember.MemberFn($2, $3, $4, $1, [], lhs parseState) }
| member_fn_kw syn_symbol list_arg_pats
{ SynTypeMember.MemberFn($2, $3, [], $1, [], lhs parseState) }
| member_fn_kw syn_symbol
{ SynTypeMember.MemberFn($2, SynPat.Ignore(lhs parseState), [], $1, [], lhs parseState) }

| attribute_list member_fn_kw syn_symbol list_arg_pats expr_list
{ SynTypeMember.MemberFn($3, $4, $5, $2, $1, lhs parseState) }

| attribute_list member_fn_kw syn_symbol list_arg_pats
{ SynTypeMember.MemberFn($3, $4, [], $2, $1, lhs parseState) }

| attribute_list member_fn_kw syn_symbol
{ SynTypeMember.MemberFn($3, SynPat.Ignore(lhs parseState), [], $2, $1, lhs parseState) }

member_in_parens:
| LET syn_pat expr
{ SynTypeMember.Let($2, $3, lhs parseState) }
| MUT syn_pat expr
{ SynTypeMember.Mut($2, $3, lhs parseState) }
| member_let_mut_kw syn_pat expr { SynTypeMember.Let($2, $3, $1, [], lhs parseState) }
| member_let_mut_kw syn_pat
{ let r = lhs parseState
SynTypeMember.Let($2, Syntax.parserRecoveryExpr r, $1, [], r) }
| attribute_list member_let_mut_kw syn_pat expr { SynTypeMember.Let($3, $4, $2, $1, lhs parseState) }
| attribute_list member_let_mut_kw syn_pat
{ let r = lhs parseState
SynTypeMember.Let($3, Syntax.parserRecoveryExpr r, $2, $1, r) }
| member_val_kw syn_pat syn_type { SynTypeMember.Val($2, $3, $1, [], lhs parseState) }
| member_val_kw syn_pat
{ let r = lhs parseState
SynTypeMember.Val($2, Syntax.parserRecoveryType r, $1, [], r) }
| attribute_list member_val_kw syn_pat syn_type { SynTypeMember.Val($3, $4, $2, $1, lhs parseState) }
| attribute_list member_val_kw syn_pat
{ let r = lhs parseState
SynTypeMember.Val($3, Syntax.parserRecoveryType r, $2, $1, r) }

| member_member_in_parens { $1 }
| MEMBERFN syn_symbol list_arg_pats expr_list
{ SynTypeMember.MemberFn($2, $3, $4, (lhs parseState)) }
| MEMBER FN syn_symbol list_arg_pats expr_list
{ SynTypeMember.MemberFn($3, $4, $5, (lhs parseState)) }
| OVERRIDE syn_symbol expr
{ SynTypeMember.OverrideMember($2, $3, lhs parseState) }
| OVERRIDE FN syn_symbol list_arg_pats expr_list
{ SynTypeMember.OverrideFn($3, $4, $5, (lhs parseState)) }
| member_fn { $1 }
| NEW tuple_arg_pats expr_list
{ SynTypeMember.Constructor($2, $3, (lhs parseState)) }
| INTERFACE symbol_with_ops
{ SynTypeMember.Interface($2, [], lhs parseState)}
| INTERFACE symbol_with_ops member_list
Expand Down Expand Up @@ -1296,6 +1315,14 @@ pat_open_kind:
| BRACKET_BAR %prec prec_syn_pat_open { CollectionKind.FsArray }
| PAREN_BAR %prec prec_syn_pat_open { CollectionKind.FsList }

syn_pat_named:
| SYMBOL %prec prec_syn_pat {
let text = $1
if (text = "_") then
SynPat.Discard (lhs parseState)
else
SynPat.Named (Syntax.mkSynSymbol text (rhs parseState 1), (rhs parseState 1)) }

syn_pat:
| constant %prec prec_match_pattern_const { SynPat.Const(fst $1, snd $1) }
| pat_open_kind syn_pat COLON syn_type collection_close_kind %prec prec_syn_type
Expand Down
Loading