-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlattice.grammar
90 lines (79 loc) · 3.18 KB
/
lattice.grammar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
program -> declaration* EOF
// Declarations
declaration -> modDecl
| modImport
| funDecl
| varDecl
| statement
modDecl -> "mod" primary
funDecl -> "def" function
varDecl -> "let" IDENTIFIER ( "=" expression )?
modImport -> modImportSingle | modImportMulti
// Statements
statement -> block
| closure
| whileStmt
| forStmt
| ifStmt
| returnStmt
| breakStmt
| continueStmt
| exprStmt
block -> "{" declaration* "}"
closure -> "{|" IDENTIFIER+ ( "," IDENTIFIER ) "|" declaration* "}"
whileStmt -> "while" "(" expression ")" statement
forStmt -> "for" primary "in" array|range closure
ifStmt -> "if" "(" expression ")" statement
("else" statement)?
returnStmt -> "return" expression?
breakStmt -> "break"
continueStmt -> "continue"
exprStmt -> expression
// Expressions
expression -> assignment
assignment -> pipe ( "=" pipe | block | closure )?
pipe -> logic_or ( "|" logic_or )*
logic_or -> logic_and ( ( "or" | "||" ) logic_and)*
logic_and -> equality ( ( "and" | "&&" ) equality)*
equality -> comparison (( "==" | "!=" ) comparison)*
comparison -> term (( ">" | ">=" | "<" | "<=") term)*
term -> factor (( "-" | "+" ) factor)*
factor -> unary (( "/" | "*" ) unary)*
unary -> ( "!" | "-" ) unary | call
call -> primary ( (" " arguments? " ") )*
primary -> "true" | "false" | "$nothing"
| INTEGER
| FLOAT
| STRING
| RAW_STRING
| FLAG
| VALUE
| IDENTIFIER
| RANGE
| RECORD
| LIST
| TABLE
| "(" expression ")"
// Utility rules
modImportSingle -> "use" IDENTIFIER "::" IDENTIFIER
modImportMulti -> "use" IDENTIFIER "::" "{" IDENTIFIER ("," IDENTIFIER)+ "}"
function -> IDENTIFIER "(" parameters? ")" block
parameters -> IDENTIFIER ( "," IDENTIFIER )*
arguments -> primary ( " " primary )*
// Tokens from Lexing
INTEGER -> DIGIT+
FLOAT -> DIGIT+ "." DIGIT+
STRING -> "\"" <any char except "\"">* "\""
RAW_STRING -> "\"\"\"" .* "\"\"\""
FLAG -> "-" "-"? ALPHA+
VALUE -> "$" IDENTIFIER
IDENTIFIER -> ALPHA ( ALPHA | DIGIT )*
RANGE -> DIGIT* ".." DIGIT*
ALPHA -> "a" ... "z" | "A" ... "Z" | "_"
DIGIT -> "0" ... "9"
// Collections
PRIMITIVE -> INTEGER | FLOAT | STRING | RAW_STRING | VALUE | IDENTIFIER
RECORD -> "{" ( PRIMITIVE ":" "(" expression ")" ) ( ","? PRIMITIVE ":" "(" expression ")" )* "}"
LIST -> "[" ( PRIMITIVE | "(" expression ")" ) ( ","? ( PRIMITIVE | "(" expression ")" )* "]"
TABLE -> "[" "[" PRIMITIVE ( ","? PRIMITIVE )* "]:" ARRAY* "]"
| "[" RECORD ( ","? RECORD )* "]"