lezer-ludus/ludus.grammar

297 lines
4.4 KiB
Plaintext
Raw Normal View History

2023-11-26 18:32:25 +00:00
@top Script { terminator* line+ }
2023-11-26 18:00:23 +00:00
2023-11-26 18:32:25 +00:00
@skip { space | comment }
2023-11-26 22:28:15 +00:00
line { (expression | toplevel) terminator+ }
2023-11-26 18:32:25 +00:00
2023-11-27 06:11:01 +00:00
toplevel { Import | Use | Ns }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Import { silent<"import"> String silent<"as"> Word }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Use { silent<"use"> Word }
2023-11-26 22:28:15 +00:00
Ns {
2023-11-27 06:18:30 +00:00
silent<"ns"> Word "{"
2023-11-26 22:28:15 +00:00
separator*
assoc_term (separator+ assoc_term)*
separator*
"}"
}
expression { non_binding | binding }
binding { Let | Ref | Fn_Named | Fn_Compound }
2023-11-27 06:18:30 +00:00
Ref { silent<"ref"> Word "=" expression }
2023-11-26 22:28:15 +00:00
non_binding { simple | complex }
synth_root { Word | Keyword }
synth_term { Args | Keyword }
2023-11-27 06:11:01 +00:00
arg_term { Placeholder | simple }
2023-11-26 22:28:15 +00:00
Args {
("(" separator* ")")
| ("("
separator*
arg_term (separator+ arg_term)*
separator*
")")
}
Synthetic { synth_root synth_term+ }
complex {
Block
| If
| If_Let
| Match
| When
//| Do
//| Bind
| Loop
| Repeat
| Each
}
2023-11-27 06:18:30 +00:00
Repeat { silent<"repeat"> (Word | Number) Block }
2023-11-26 22:28:15 +00:00
2023-12-15 22:20:48 +00:00
// repeat 4 { thing () }
2023-11-27 06:18:30 +00:00
Each { silent<"each"> simple "do" (Fn_Clause | Fn_Clauses) }
2023-11-26 22:28:15 +00:00
2023-12-15 22:20:48 +00:00
// each [1, 2, 3] do (n) -> thing
2023-11-27 06:18:30 +00:00
Recur { silent<"recur"> Args }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Loop { silent<"loop"> simple "with" (Fn_Clause | Fn_Clauses) }
2023-11-26 22:28:15 +00:00
simple { atom | collection | Synthetic | Fn_Lambda | Recur }
Fn_Clause { Tuple_Pattern "->" expression }
2023-11-26 22:28:15 +00:00
Fn_Clauses {
"{"
terminator*
Fn_Clause (terminator+ Fn_Clause)*
terminator*
"}"
}
2023-11-27 06:18:30 +00:00
Fn_Compound { silent<"fn"> Word Fn_Clauses }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Fn_Named { silent<"fn"> Word Fn_Clause }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Fn_Lambda { silent<"fn"> Fn_Clause }
2023-11-26 22:28:15 +00:00
// TODO: figure out precedence with do/bind exprs
// do_expr { Fn_Lambda | Synthetic | Word | Keyword }
// Do {
2023-11-27 06:18:30 +00:00
// silent<"do"> simple (~ambig newline* ">" do_expr)+
2023-11-26 22:28:15 +00:00
// }
// Bind {
2023-11-27 06:18:30 +00:00
// silent<"bind">
2023-11-26 22:28:15 +00:00
// simple
// (!pipeline pipeline)+
// }
Pattern {
Tuple_Pattern
| List_Pattern
| Dict_Pattern
| Struct_Pattern
| atom
| Placeholder
}
Placeholder { "_" }
ellipsis { "..." }
Splattern { ellipsis (Word | Placeholder) }
Tuple_Pattern {
("(" separator* ")")
| ("("
separator*
(Pattern separator+)* (Pattern | Splattern)
separator*
")")
}
List_Pattern {
("[" separator* "]")
| ("["
separator*
(Pattern separator+)* (Pattern | Splattern)
separator*
"]")
}
2023-11-27 06:11:01 +00:00
Assoc_Pattern { Word | (Keyword Pattern) }
2023-11-26 22:28:15 +00:00
Dict_Pattern {
("#{" separator* "}")
| ("#{"
separator*
(Assoc_Pattern separator+)* (Assoc_Pattern | Splattern)
separator*
"}")
}
Struct_Pattern {
("@{" separator* "}")
| ("@{"
separator*
(Assoc_Pattern separator+)* (Assoc_Pattern | Splattern)
separator*
"}")
}
2023-11-27 06:18:30 +00:00
Let { silent<"let"> Pattern "=" non_binding }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Else { silent<"else"> }
2023-11-26 22:28:15 +00:00
Match_Clause {(Pattern | Else) "->" newline* expression}
match_body {
Match_Clause
| (
"{"
terminator*
Match_Clause (terminator+ Match_Clause)*
terminator*
"}"
)
}
Match {
2023-11-27 06:18:30 +00:00
silent<"match">
2023-11-26 22:28:15 +00:00
simple
2023-11-27 06:18:30 +00:00
silent<"with">
2023-11-26 22:28:15 +00:00
match_body
}
When_Clause {
(simple | Placeholder | Else) "->" newline* expression
}
When {
2023-11-27 06:18:30 +00:00
silent<"when"> "{"
2023-11-26 22:28:15 +00:00
terminator*
When_Clause (terminator+ When_Clause)*
terminator*
"}"
}
If {
2023-11-27 06:18:30 +00:00
silent<"if"> simple newline*
silent<"then"> expression newline*
silent<"else"> expression
2023-11-26 22:28:15 +00:00
}
If_Let {
2023-11-27 06:18:30 +00:00
silent<"if"> silent<"let">
2023-11-27 06:17:07 +00:00
Pattern "=" simple newline*
2023-11-27 06:18:30 +00:00
silent<"then"> expression newline*
silent<"else"> expression
2023-11-26 22:28:15 +00:00
}
Block {
"{"
terminator*
expression (terminator+ expression)*
terminator*
"}"
}
collection {
Tuple
| List
| Set
| Dict
| Struct
}
Tuple {
( "(" // non-empty
separator*
non_binding (separator+ non_binding)*
separator*
")" )
| "(" separator* ")" // empty
}
Splat { ellipsis Word }
linear_term { Splat | non_binding }
List {
("["
separator*
linear_term (separator+ linear_term)*
separator*
"]" )
| "[" separator* "]"
}
Set {
("${"
separator*
linear_term (separator+ linear_term)*
separator*
"}")
| "${" separator* "}"
}
2023-11-27 06:11:01 +00:00
assoc_term { Word | (Keyword non_binding) }
2023-11-26 22:28:15 +00:00
2023-11-27 06:11:01 +00:00
dict_term { assoc_term | Splat }
2023-11-26 22:28:15 +00:00
Dict {
("#{"
separator*
dict_term (separator+ dict_term)*
separator*
"}" )
| "#{" separator* "}"
}
Struct {
("@{"
separator*
assoc_term (separator+ assoc_term)*
separator*
"}")
| "@{" separator* "}"
}
atom { Boolean | Nil | String | Number | Keyword | Word }
2023-11-27 06:20:43 +00:00
reserved<term> { @specialize[@name={term}]<Word, term> }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
silent<term> { @specialize<Word, term> }
2023-11-26 22:28:15 +00:00
Keyword { ":" Word }
2023-11-27 06:20:43 +00:00
Boolean { reserved<"true"> | reserved<"false"> }
2023-11-26 22:28:15 +00:00
2023-11-27 06:18:30 +00:00
Nil { silent<"nil"> }
2023-11-26 18:00:23 +00:00
@tokens {
2023-11-26 22:28:15 +00:00
Word { $[a-z] $[a-zA-Z_\-?/!]* }
2023-11-26 18:32:25 +00:00
space { $[ \t\r]+ }
comment { "&" ![\n]* }
String { '"' (!["\\] | "\\" _)* '"' }
2023-11-26 22:28:15 +00:00
int { $[1-9]$[0-9]* | "0" }
float { ("0" | int ) "." $[0-9]+}
Number { "-"? (int | float) }
2023-11-26 18:32:25 +00:00
separator { $[,\n] }
2023-12-15 22:20:48 +00:00
terminator { $[;\n] }
2023-11-26 22:28:15 +00:00
newline { "\n" }
2023-11-26 18:00:23 +00:00
}