-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mangrove.bnf
154 lines (132 loc) · 5.71 KB
/
mangrove.bnf
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
147
148
149
150
151
152
153
154
alpha = "a"..."z" | "A"..."Z" | U+00C0...U+2000 | U+2070...U+D7FF | U+E000...U+FE4F |
U+FE70...U+FEFF | U+10000...U+2FA1F;
allNormalAlpha = " " | "!" | "#"..."&" | "("..."[" | "]"..."~" | U+0080...U+D7FF |
U+E000...U+0010FFFF;
digit = "0"..."9";
alphaNum = alpha | digit;
hex = digit | "a"..."f" | "A"..."F";
oct = "0"..."7";
bin = "0" | "1";
newLine = (U+000A | U+000D)+;
# For technical reasons in the parser, we technically have to treat comments as "whitespace" except when they're not.
whiteSpace = (" " | U+0009 | newLine | comment)+;
unicodeSeq = "\" ("u" | "U") hex+;
specialSeq = "\" ["\" | "b" | "r" | "n" | "t" | "v" | "f" | "a" ];
unicode = allNormalAlpha | unicodeSeq | specialSeq;
lineComment = ("#" | "//") (U+0000...U+0009 | U+000B...U+000C | U+000E...U+0010FFFF)* newLine;
partComment = "/*" (U+0000...U+0010FFFF)* "*/";
comment = lineComment | partComment;
int = ("0" ("x" | "X") hex+) | ("0" ("b" | "B") bin+) | ("0" ("c" | "C") oct+) | digit+;
float = digit+ "." digit* ["f" | "F"];
string = ('"' (unicode | "'" | '\"')* '"')+;
char = "'" (unicode | '"' | "\'") "'";
bool = "true" | "false";
null = "nullptr";
list = "[" [logicExpr ("," logicExpr)*] "]";
keyValue = string ":" logicExpr;
dict = "{" [keyValue ("," keyValue)*] "}";
# ident here means something that is not a reserved word such as "function"
ident = (alpha | "_") (alphaNum | "_")*;
dottedIdent = ident ('.' ident)*;
const = int | float | string | char | bool | null | list | dict;
bracketExpr = ("(" logicExpr ")");
slice = [logicExpr] ":" [logicExpr];
index = "[" (slice | logicExpr) "]";
pack = ident "...";
value = (dottedIdent [index]) | pack | const | functionCall | lambdaCall | bracketExpr;
templateArgs = "<" value, (",", value)* ">";
callArgs = [templateArgs] "(" [value ("," value)*] ")";
functionCall = dottedIdent callArgs;
lambdaExpr = "function" params returnType block;
lambdaCall = "(" lambdaExpr ")" callArgs;
logicalAnd = "&&" | "and";
logicalOr = "||" | "or";
logicalNot = "!" | "not";
invert = logicalNot | "~" | "-";
incOp = "++" | "--";
mulOp = "*" | "/" | "%";
addOp = "+" | "-";
shiftOp = "<<" | ">>";
bitOp = "&" | "|" | "^";
relOp = ("<" | ">") ["="];
equOp = relOp | (("!" | "=") "=");
logicOp = logicAnd | logicOr;
assignOp = (addOp | mulOp | shiftOp | bitOp) "=";
operator = invert | inc | mulOp | addOp | shiftOp | bitOp | equOp | logicOp | assignOp;
incExpr = (incOp value) | (value incOp);
derefExpr = ["*"] incExpr;
invertExpr = [invert] derefExpr;
mulExpr = invertExpr (mulOp invertExpr)*;
addExpr = mulExpr (andOp mulExpr)*;
shiftExpr = addExpr [shiftOp int];
bitExpr = shiftExpr (bitOp shiftExpr);
relExpr = bitExpr (equOp bitExpr);
betweenExpr = bitExpr relOp ident relOp bitExpr;
boolIsExpr = bitExpr "is" ["not"] (const | type);
boolInExpr = bitExpr ["not"] in value;
memberExpr = boolIsExpr | boolInExpr;
relation = relExpr | betweenExpr | memberExpr;
logicExpr = relation (logicOp relation)*;
locationSpec = "eeprom" | "flash" | "rom";
# NB: Unlike decorator below which must not have any space between the `@` and dottedIdent
# This requires whitespace between the `@` and integer
addressSpec = "@" int;
constSpec = "const";
cvSpec = [constSpec] ["volatile"];
staticSpec = "static";
storageSpec = [staticSpec] [cvSpec];
typeDecl = storageSpec [locationSpec] type;
identDef = typeDecl ident [addressSpec];
targetIdent = (identDef | dottedIdent);
assignOpExpr = [dottedIdent assignOp] logicExpr;
deleteExpr = "delete" dottedIdent;
newExpr = "new" functionCall;
valueExpr = newExpr | logicExpr | type;
assignExpr = (targetIdent ["=" valueExpr]) | assignOpExpr;
returnExpr = "return" valueExpr;
expression = (deleteExpr | assignExpr | returnExpr | logicExpr) [";"];
# The '.' preceeding a dotted identifier turn the import into a relative import.
importTarget = ["."+] dottedIdent;
importIdent = ident ["as" ident];
importStmt = "from" importTarget "import" importIdent ("," importIdent)*;
ifExpr = "if" logicExpr block;
elifExpr = "elif" logicExpr block;
elseExpr = "else" block;
ifStmt = ifExpr elifExpr* [elseExpr];
forStmt = "for" "(" cvType ident [refOrPtr] ":" value ")" block;
whileStmt = "while" logicExpr block;
doStmt = "do" block "while" logicExpr;
unsafeStmt = "unsafe" block;
intType = "Int8" | "UInt8" | "Int16" | "UInt16" | "Int32" | "UInt32" | "Int64" | "UInt64";
floatType = "Float32" | "Float64";
builtinType = "String" | "Char" | "Bool" | "Byte" | intType | floatType | "Array" | "Dict" | "Set";
valueType = builtinType | ident;
refOrPtr = "&" | "*";
type = (valueType | "auto" | "type") [refOrPtr];
cvType = [cvSpec] type;
# NB: the type part of param here is typeDecl minus locationSpec and should be parsed as such.
param = storageSpec type [ident ["..."]]
params = "(" [param ("," param)*] ")";
returnTypeDecl = cvType | "none";
returnType = [constSpec] [staticSpec] "->" returnTypeDecl;
enumMember = ident ["=" int]
enumBlock = "{" enumMember ("," enumMember)* "}";
tmplTypeParam = "type" ["..."] ident ["=" typeOrPattern];
tmplValueParam = valueType [refOrPtr] ["..."] ident ["=" logicExpr];
tmplParam = tmplTypeParam | tmplValueParam;
tmplDef = "<" [tmplParam ("," tmplParam)*] ">";
classDef = "class" ident [tmplDef] extBlock;
enumDef = "enum" ident [":" (intType | ident)] enumBlock;
functionDef = "function" ident [tmplDef] params returnType block;
operatorDef = "operator" (operator | valueType) params returnType block;
define = classDef | enumDef | functionDef | operatorDef;
decorator = "@" dottedIdent [callArgs];
decoratedDef = decorator* define;
statement = (importStmt | ifStmt | forStmt | whileStmt | doStmt | selectStmt |
expression | decoratedDef | unsafeStmt);
braceBlock = "{" statement* "}";
block = braceBlock | statement;
extStatement = visibility | statement;
extBraceBlock = "{" extStatement* "}";
extBlock = extBraceBlock;
sourceFile = extStatement+;