-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.peg
146 lines (126 loc) · 4.32 KB
/
lang.peg
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Scopes
Module <- _ Statement+ _EndOfFile
Code <- _CBOP Statement* _CBCL / Statement
# Expressions
Expression <- Logical
Logical <- BitLogical ((AND / OR) BitLogical)*
BitLogical <- Comparison ((BAND / BXOR / BOR) Comparison)*
Comparison <- BitShifting ((EQ / NEQ / LTE / GTE / LT / GT) BitShifting)*
BitShifting <- Term ((RSHIFT / LSHIFT) Term)*
Term <- Factor ((PLUS / MINUS) Factor)*
Factor <- Power ((STAR / SLASH) Power)*
Power <- Unary ((POWER / MOD) Unary)*
Unary <- (PLUS / MINUS / NOT)? Attribute
Attribute <- Attr (_DOT Attr)*
Attr <- Call / Access / Primary
Call <- Primary CallParams
Access <- Primary _SBOP Slice _SBCL
Slice <- Primary _COLON Primary / Primary
Primary <- _PROP Expression _PRCL
/ Value
/ Lambda
/ Identifier
# Function Declaration
Function <- _FN Identifier Params Code
Lambda <- _FN Params Code
Param <- Identifier (_ASSIGN Expression)?
Params <- _PROP (ParamsMult / ParamsOne / ParamsNone) _PRCL
ParamsMult <- Param (_COMA Param)+
ParamsOne <- Param
ParamsNone <-
# Function Call
CallParams <- _PROP (ListMult / ListOne / ListNone) _PRCL
# Statements
Statement <- BREAK / CONTINUE / ReturnStm / ThrowStm / WhileStm / ForStm / IfStm / TryStm / Function / Assignment / LexAssignment / ExprStm
ExprStm <- Expression
Assignment <- Identifier _ASSIGN Expression
LexAssignment <- _LET Identifier _ASSIGN Expression
# Control Flow Statements
ForStm <- _FOR Store _IN Expression Code
WhileStm <- _WHILE Expression Code
IfStm <- _IF Expression Code ElseStm?
ElseStm <- _ELSE Code
ReturnStm <- _RETURN Expression
ThrowStm <- _THROW Expression
TryStm <- _TRY Code CatchStm+
CatchStm <- (_CATCH Load _AS Store Code)
# Values
Load <- Identifier
Store <- Identifier
# Values
Value <- Number / BOOL / NULL / String / List
Number <- BIN / HEX / FLOAT / DEC
String <- _DQUOTE (!_DQUOTE .)* _DQUOTE _
List <- _SBOP (ListMult / ListOne / ListNone) _SBCL
ListMult <- Expression (_COMA Expression)+
ListOne <- Expression
ListNone <-
# Lexcical Hierarchy
# Values
Identifier <- [a-zA-Z_][a-zA-Z0-9_]* _
DEC <- [0-9]+ _
FLOAT <- [0-9]* '.' [0-9]+ _
BIN <- '0b' [0-1]+ _
HEX <- '0x' [0-9a-fA-F]+ _
BOOL <- ('true' / 'false') _
NULL <- 'null' _
# Keywords
BREAK <- 'break' _
CONTINUE <- 'continue' _
_IN <- 'in' _
_ELSE <- 'else' _
_FOR <- 'for' _
_FN <- 'fn' _
_WHILE <- 'while' _
_IF <- 'if' _
_RETURN <- 'return' _
_THROW <- 'throw' _
_LET <- 'let' _
_TRY <- 'try' _
_CATCH <- 'catch' _
_AS <- 'as' _
# Lexical Delimiters
_DQUOTE <- '"'
_COMA <- ',' _
_COLON <- ':' _
_PROP <- '(' _
_PRCL <- ')' _
_CBOP <- '{' _
_CBCL <- '}' _
_SBOP <- '[' _
_SBCL <- ']' _
_ASSIGN <- '=' _
## Arithmetic Operators
PLUS <- '+' _
MINUS <- '-' _
STAR <- '*' _
SLASH <- '/' _
MOD <- '%' _
POWER <- '**' _
## Comparison Operators
EQ <- '==' _
NEQ <- '!=' _
LT <- '<' _
GT <- '>' _
LTE <- '<=' _
GTE <- '>=' _
## Bit Shifting
RSHIFT <- '>>' _
LSHIFT <- '<<' _
## Binary Bit Operators
BAND <- '&' _
BOR <- '|' _
BXOR <- '^' _
## Logic Operators
AND <- 'and' _
OR <- 'or' _
NOT <- 'not' _
## Member Access Operators
_DOT <- '.' _
# Whitespace Handling
_ <- (Space / Comment)*
Comment <- '#' (!_EOL .)* _EOL
Space <- ' ' / '\t' / _EndOfLine
_EndOfLine <- ';' / _EOL
_EOL <- '\r\n' / '\n' / '\r'
_EndOfFile <- !.