File: syntax.bnf

package info (click to toggle)
storm-lang 0.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 51,836 kB
  • sloc: ansic: 261,420; cpp: 138,870; sh: 14,877; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (438 lines) | stat: -rw-r--r-- 20,488 bytes parent folder | download | duplicates (4)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use core.lang;

optional delimiter = SDelimiter;
required delimiter = SReqDelimiter;

// Delimiter. May contain comments.
void SDelimiter();
SDelimiter : "[ \n\r\t]*" - (SCommentStart #comment - SDelimiter)?;

void SReqDelimiter();
SReqDelimiter : "[ \n\r\t]+";
SReqDelimiter : "[ \n\r\t]*" - SCommentStart #comment - SDelimiter;

// Start of comments
void SCommentStart();
SCommentStart : "//[^\n\r]*[\n\r]";
SCommentStart : "/" - "\*" - [SInComment]@;

// Inside multi-line comments.
void SInComment();
// End.
SInComment : "\*+/";
// Start of nested comment.
SInComment : "/\*+" - SInComment - SInComment;
// Everything except * and / are fine.
SInComment : "[^\*/]*" - SInComment;
// Match any number of * followed by anything other than a /
SInComment : "\*+[^\*/]" - SInComment;
// Match any number of /
SInComment : "/+" - SInComment;


// Root production.
Content SRoot();
SRoot => Content() : SDelimiter - (SDef(me),)*;

// Definitions inside the file.
void SDef(Content content);

// Preprocessor directives (we just ignore them at the moment).
SDef : "# *[a-zA-Z]+[^\n]*\n" #comment;

// Global variables (We do not support initialization at the moment).
SDef => addGlobal(content, pos, type, name, init) : SMaybeStatic - STypeName @type, SIdentifier name #varName, ("=", SExpr @init,)? ";";
SDef => addGlobalArray(content, pos, type, name, count, init) : SMaybeStatic - STypeName @type, SIdentifier name #varName, "\[", SExpr @count, "\]", ("=", SArrayInit @init)?, ";";
SDef => addGlobalArray(content, pos, type, name, init) : SMaybeStatic - STypeName @type, SIdentifier name #varName, "\[", "\]", "=", SArrayInit @init, ";";

void SMaybeStatic();
SMaybeStatic :;
SMaybeStatic : "static" #keyword ~;

// Class declarations

// Forward declaration:
SDef : "class" #keyword ~ SIdentifier name #typeName, ";";
SDef : "struct" #keyword ~ SIdentifier name #typeName, ";";

// Definition:
SDef => content : SClassDecl -> add, ";";

CppType SClassDecl();
SClassDecl => CppType(name, true, inherit, body) : "class" #keyword ~ SIdentifier name #typeName, (SClassInherit @inherit,)? "{", SClassBody @body, "}";
SClassDecl => CppType(name, false, inherit, body) : "struct" #keyword ~ SIdentifier name #typeName, (SClassInherit @inherit,)? "{", SClassBody @body, "}";

void SClassInherit(CppType me);
SClassInherit : ":", (SClassAccess access,)? STypeNamePlain(me.scope) type;

Visibility SClassAccess() #keyword;
SClassAccess => public() : "public";
SClassAccess => typePrivate() : "private";
SClassAccess => typeProtected() : "protected";

void SClassBody(TypeState state);
SClassBody : (SClassItem(state),)*;

void SClassItem(TypeState state);
// Access modifiers:
SClassItem => state : SClassAccess -> set, ":";
// Nested type declarations:
SClassItem => state : SClassDecl -> add;
// Function declarations:
SClassItem => state : SFnDecl -> add;
// Variable declarations:
SClassItem => state : SVarDecl(state.to) -> add, ";";
// Constructors:
SClassItem => state : SCtorDecl -> add;
// Destructors:
SClassItem => state : SDtorDecl -> add;

MemberVar SVarDecl(CppType type);
SVarDecl => CppMemberVar(name.pos, name.v, t, type) : STypeName(type.scope) t, SIdentifier name #varName;

// Function declarations
SDef => content : SMaybeStatic - SFnDecl -> add;

CppFnDecl SFnDecl();
SFnDecl => CppFnDecl(res, name, formals, body) : STypeName@ res, SIdentifier name #fnName, "(", SFormals formals, ")", SFnTail body;
SFnDecl => operatorDecl(res, name, formals, body) : STypeName@ res, "operator" #keyword, (SOpName)@ name, "(", SFormals formals, ")", SFnTail body;

void SOpName();
SOpName : "==?";
SOpName : "!=";
SOpName : "<=?";
SOpName : ">=?";
SOpName : "\+\+";
SOpName : "\-\-";
SOpName : "\+=?";
SOpName : "\-=?";
SOpName : "\*=?";
SOpName : "/=?";
SOpName : "\*";

Maybe<FnBody> SFnTail();
SFnTail => Maybe<FnBody>() : ";";
SFnTail => FnBody(block) : "{", SStmtList@ block - "}";
SFnTail => FnBody(block, false) : "NO_STEP" #keyword, "{", SStmtList@ block - "}";


// Formal parameters.
Array<Formal> SFormals();
SFormals => Array<Formal>() : ;
SFormals => Array<Formal>() : "void" #keyword; // Valid in C
SFormals => Array<Formal>() : SFormal -> push - (, ",", SFormal -> push)*;

Formal SFormal();
SFormal => Formal(type, name) : STypeName @type, SIdentifier name #varName;

// Constructors and destructors.
CppCtorDecl SCtorDecl();
SCtorDecl => CppCtorDecl(name, formals) : SIdentifier name #fnName, "(", SFormals formals, ")", SCtorTail(me);

CppDtorDecl SDtorDecl();
SDtorDecl => CppDtorDecl(name, body) : "~", SIdentifier name #fnName, "(", ")", SFnTail body;

void SCtorTail(CppCtorDecl me);
SCtorTail : ";";
SCtorTail : (":", SInitList(me.init) ,)? "{", SStmtList@ -> body, "}";

void SInitList(Array<Initializer> me);
SInitList : SInitItem -> push - (, ",", SInitItem -> push)*;

Initializer SInitItem();
SInitItem => Initializer(pos, name, expr) : SIdentifier name #varName, "(", SActuals@ expr, ")";
SInitItem => Initializer(pos, name, expr) : SIdentifier name #varName, "{", SActuals@ expr, "}";

// Identifiers
SStr SIdentifier();
SIdentifier => v : "[A-Za-z_][A-Za-z0-9_]*" @v;

// Type declarations.
// There are two kinds of them: STypeName and STypeNamePlain
// The first requires that the type name ends with a non-alnum character (&* or space). The second one does not have
// this requirement. The first is needed to not confuse "intx" as "int x". The second is needed to not require
// spaces in template lists (e.g., x<int,int>).

// Requires space or special character at the end.
CppValue STypeName(Scope scope);
STypeName => CppValue(original) : STypeNameRecWS(scope) original;
STypeName => CppValue(original, true) : STypeNameRecWS(scope) original - "const" #keyword ~;
STypeName => CppValue(original, true) : "const" #keyword ~ STypeNameEnd(scope) original ~;
STypeName => CppValue(original) : STypeNameCommon(scope) original;

// Does not require space or special character at the end.
CppValue STypeNamePlain(Scope scope);
STypeNamePlain => CppValue(original) : STypeNameRec(scope) original;
STypeNamePlain => CppValue(original, true) : STypeNameRecWS(scope) original - "const" #keyword;
STypeNamePlain => CppValue(original, true) : "const" #keyword ~ STypeNameEnd(scope) original;
STypeNamePlain => CppValue(original) : STypeNameCommon(scope) original;

// Common parts between STypeName and STypeNamePlain. All end in special characters.
Value STypeNameCommon(Scope scope);
STypeNameCommon => wrapRef(original) : STypeNameRec(scope) original, "&";
STypeNameCommon => wrapConstRef(original) : STypeNameRecWS(scope) original - "const" #keyword, "&";
STypeNameCommon => wrapConstRef(original) : "const" #keyword ~ STypeNameEnd(scope) original, "&";
STypeNameCommon => wrapRRef(original) : STypeNameRec(scope) original, "&&";
STypeNameCommon => wrapConstRRef(original) : STypeNameRecWS(scope) original - "const" #keyword, "&&";
STypeNameCommon => wrapConstRRef(original) : "const" #keyword ~ STypeNameEnd(scope) original, "&&";


// Recursive part of type declarations. WS version enforces a whitespace (or *) at the end.
Value STypeNameRec(Scope scope);
STypeNameRec => original : STypeNameEnd(scope) original;
STypeNameRec => wrapPtr(original) : STypeNameRec(scope) original, "\*";
STypeNameRec => wrapConstPtr(original) : STypeNameRecWS(scope) original - "const" #keyword, "\*";
STypeNameRec => wrapConstPtr(original) : "const" #keyword ~ STypeNameEnd(scope) original, "\*";

Value STypeNameRecWS(Scope scope);
STypeNameRecWS => original : STypeNameEnd(scope) original ~;
STypeNameRecWS => wrapPtr(original) : STypeNameRec(scope) original, "\*";
STypeNameRecWS => wrapConstPtr(original) : STypeNameRecWS(scope) original - "const" #keyword, "\*";
STypeNameRecWS => wrapConstPtr(original) : "const" #keyword ~ STypeNameEnd(scope) original, "\*";



// Actual type names. Always ends with an identifier. Never matches spaces after that identifier.
Value STypeNameEnd(Scope scope);
STypeNameEnd[-1] => findType(pos, scope, first, rest) : SNamePart(scope) first #typeName - ("::", SNamePart(scope) rest #typeName)*;
// "struct foo" is needed in C.
STypeNameEnd[-1] => findType(pos, scope, first, rest) : "struct" #keyword ~ SNamePart(scope) first #typeName - ("::", SNamePart(scope) rest #typeName)*;
STypeNameEnd => x : SPrimitiveType x;

// Built-in types.
Value SPrimitiveType();
SPrimitiveType => charType() : "char" #typeName;
SPrimitiveType => intType() : "int" #typeName;
SPrimitiveType => uIntType() : "unsigned" #typeName, "int" #typeName;
SPrimitiveType => longType() : "long" #typeName;
SPrimitiveType => uLongType() : "unsigned" #typeName, "long" #typeName;
SPrimitiveType => boolType() : "bool" #typeName;
SPrimitiveType => Value() : "void" #keyword;
// ...

// Parts of a name.
SimplePart SNamePart(Scope scope);
SNamePart => SimplePart(name) : SIdentifier name;
SNamePart => SimplePart(name.v, params) : SIdentifier name, "<", SNameParams(scope) params, ">";

Array<Value> SNameParams(Scope scope);
SNameParams => Array<Value>() :;
SNameParams => Array<Value>() : STypeNamePlain(scope) -> push, (",", STypeNamePlain(scope) -> push)*;

// Block or statement, directly after keyword. Ensures that we must have a whitespace before a
// statement, but does not require it before a block.
Stmt SKeywordStmt(Block block);
SKeywordStmt => StmtBlock(pos, block) : "{", (SStmt(me) -> add,)* "}";
SKeywordStmt => e : ~ SStmt(block) e;

// Statements and expressions:
Stmt SStmt(Block block);
SStmt => StmtBlock(pos, block) : "{", (SStmt(me) -> add,)* "}";
SStmt => e : SExpr(block) e, ";";
SStmt => NullStmt(pos) : ";";
SStmt[50] => Return(pos, block, expr) : "return" #keyword ~ SExpr(block) expr, ";";
SStmt[50] => Return(pos, block) : "return" #keyword, ";";
SStmt[10] => VarDecl(pos, block, type, name) : STypeName(block.scope) type, SIdentifier name #varName, ";";
SStmt[10] => VarDecl.assign(pos, block, type, name, init) : STypeName(block.scope) type, SIdentifier name #varName, "=", SExpr(block) init, ";";
SStmt[10] => VarDecl.parens(pos, block, type, name, actuals) : STypeName(block.scope) type, SIdentifier name #varName, "(", SActuals(block) actuals, ")", ";";
SStmt[10] => VarDecl.parens(pos, block, type, name, actuals) : STypeName(block.scope) type, SIdentifier name #varName, "{", SActuals(block) actuals, "}", ";";
SStmt[10] => arrayDecl(pos, block, type, name, size, init) : STypeName(block.scope) type, SIdentifier name #varName, "\[", SExpr(block) size, "\]", ("=", SArrayInit(block) init,)? ";";
SStmt[10] => arrayDecl(pos, block, type, name, init) : STypeName(block.scope) type, SIdentifier name #varName, "\[", "\]", "=", SArrayInit(block) init, ";";
// "delete" is technically an expression, but we don't do that. That's crazy!
SStmt[50] => DeleteStmt(pos, expr, false) : "delete" #keyword ~ SOp2(block) expr, ";";
SStmt[50] => DeleteStmt(pos, expr, true) : "delete" #keyword, "\[", "\]", SOp2(block) expr, ";";
SStmt[50] => WhileStmt(pos, block) : "while" #keyword, "(", SExpr(me) -> cond, ")", SStmt(me) -> body;
SStmt[50] => DoWhileStmt(pos, block) : "do" #keyword - SKeywordStmt(me) -> body, "while" #keyword, "(", SExpr(me) -> cond, ")", ";";
SStmt[50] => ForStmt(pos, block) : "for" #keyword, "(", SStmt(me) -> init, SForCond(me), ";", SForUpdate(me), ")", SStmt(me) -> body;
SStmt[40] => IfStmt(pos, block) : "if" #keyword, "(", SExpr(me) -> cond, ")", SStmt(me) -> ifTrue - (,"else" #keyword - SKeywordStmt(me) -> ifFalse)?;
SStmt[50] => Break(pos, block) : "break" #keyword, ";";
SStmt[50] => Continue(pos, block) : "continue" #keyword, ";";

// Condition in a for loop. May be empty.
void SForCond(ForStmt me);
SForCond : (SExpr(me) -> cond)?;

// Update part of a for loop. May be empty.
void SForUpdate(ForStmt me);
SForUpdate : (SExpr(me) -> update)?;

// Disable stepping in a block.
SStmt[50] => NoStep(pos, block) : "NO_STEP" #keyword, "{", (SStmt(me) -> add,)* "}";

// An array initializer. It is either a comma-separated list of expressions or a string.
Array<Expr> SArrayInit(Block block);
SArrayInit => Array<Expr>() : "{", SExpr(block) -> push - (, ",", SExpr(block) -> push)*, "}";
SArrayInit => Array<Expr>() : "{", (SExpr(block) -> push, )?;
SArrayInit => createExprs(str) : "\"" #string - (SStrContent) @str - "\"" #string;

// Add a list of statements to a block.
void SStmtList(StmtBlock block);
SStmtList => block : (SStmt(block) -> add,)*;

Expr SExpr(Block block);
SExpr => n : SExprNoComma(block) n;

// The comma-operator.
SExpr => CommaExpr(pos, lhs, rhs) : SExpr(block) lhs, ",", SExprNoComma(block) rhs;

// Operators, level 16 according to https://en.cppreference.com/w/cpp/language/operator_precedence
Expr SExprNoComma(Block block);
SExprNoComma => n : SOp15(block) n;
SExprNoComma => operatorNoConst(block, l, op, r) : SOp15(block) l, "[+\-*/%&^|]?=" @op, SExprNoComma(block) r;
SExprNoComma => operatorNoConst(block, l, op, r) : SOp15(block) l, "<<=" @op, SExprNoComma(block) r;
SExprNoComma => operatorNoConst(block, l, op, r) : SOp15(block) l, ">>=" @op, SExprNoComma(block) r;

Expr SOp15(Block block);
SOp15 => n : SOp14(block) n;
SOp15[10] => LogicOp(pos, block, l, r, false) : SOp15(block) l, "||", SOp14(block) r;

Expr SOp14(Block block);
SOp14 => n : SOp13(block) n;
SOp14[10] => LogicOp(pos, block, l, r, true) : SOp14(block) l, "&&", SOp13(block) r;

Expr SOp13(Block block);
SOp13 => n : SOp12(block) n;
SOp13 => operator(block, l, op, r) : SOp13(block) l, "|" @op, SOp12(block) r;

Expr SOp12(Block block);
SOp12 => n : SOp11(block) n;
SOp12 => operator(block, l, op, r) : SOp12(block) l, "^" @op, SOp11(block) r;

Expr SOp11(Block block);
SOp11 => n : SOp10(block) n;
SOp11 => operator(block, l, op, r) : SOp11(block) l, "&" @op, SOp10(block) r;

Expr SOp10(Block block);
SOp10 => n : SOp9(block) n;
SOp10 => operator(block, l, op, r) : SOp10(block) l, "[!=]=" @op, SOp9(block) r;

Expr SOp9(Block block);
SOp9 => n : SOp7(block) n;
SOp9 => operator(block, l, op, r) : SOp9(block) l, "[<>]=?" @op, SOp7(block) r;

// Level 8 is not implemented. It is the <=> operator from C++20.

Expr SOp7(Block block);
SOp7 => n : SOp6(block) n;
SOp7 => operator(block, l, op, r) : SOp7(block) l, "<<" @op, SOp6(block) r;
SOp7 => operator(block, l, op, r) : SOp7(block) l, ">>" @op, SOp6(block) r;

Expr SOp6(Block block);
SOp6 => n : SOp5(block) n;
SOp6 => operator(block, l, op, r) : SOp6(block) l, "[+\-]" @op, SOp5(block) r;

Expr SOp5(Block block);
SOp5 => n : SOp4(block) n;
SOp5 => operator(block, l, op, r) : SOp5(block) l, "[*/%]" @op, SOp4(block) r;

Expr SOp4(Block block);
SOp4 => n : SOp3(block) n;
SOp4 => operator(block, l, op, r) : SOp4(block) l, "\.\*" @op, SOp3(block) r;
SOp4 => operator(block, l, op, r) : SOp4(block) l, "->\*" @op, SOp3(block) r;

Expr SOp3(Block block);
SOp3 => n : SOp2(block) n;
SOp3 => Deref.ptr(pos, n) : "\*", SOp3(block) n;
SOp3 => addressOf(pos, n) : "&", SOp3(block) n;
SOp3 => unaryOperator(block, op, n) : "~" @op, SOp3(block) n;
SOp3 => NegateOp(pos, block, n) : "!", SOp3(block) n;
SOp3 => unaryOperator(block, op, n) : "\+" @op, SOp2(block) n;
SOp3 => unaryOperator(block, op, n) : "-" @op, SOp2(block) n;
SOp3 => operatorNoConst(block, op, n) : "\+\+" @op, SOp3(block) n;
SOp3 => operatorNoConst(block, op, n) : "--" @op, SOp3(block) n;
SOp3 => NewExpr(pos, block, type) : "new" #keyword ~ STypeNamePlain(block.scope) type;
SOp3 => NewExpr(pos, block, type, actuals) : "new" #keyword ~ STypeNamePlain(block.scope) type, "(", SActuals(block) actuals, ")";
SOp3 => NewExpr(pos, block, type, actuals) : "new" #keyword ~ STypeNamePlain(block.scope) type, "{", SActuals(block) actuals, "}";
SOp3 => NewExpr(pos, block, type, count) : "new" #keyword ~ STypeNamePlain(block.scope) type, "\[", SExpr(block) count, "\]";
SOp3[10] => sizeofExpr(pos, x) : "sizeof" #keyword - SSizeofParam(block) x;
SOp3 => castError(pos) : "(", STypeNamePlain, ")", SOp3; // C-style cast.

SizeofParam SSizeofParam(Block block);
SSizeofParam => x : ~ SSizeofInner(block) x;
SSizeofParam => x : , "(", SSizeofRec(block) x, ")";
SSizeofParam => SizeofParam(x) : ~ SOp3(block) x;

SizeofParam SSizeofRec(Block block);
SSizeofRec => x : SSizeofInner(block) x;
SSizeofRec => x : , "(", SSizeofRec(block) x, ")";
SSizeofRec => SizeofParam(x) : SExpr(block) x;

SizeofParam SSizeofInner(Block block);
SSizeofInner => SizeofParam(t) : STypeNamePlain(block.scope) t;
SSizeofInner => SizeofParam(expr) : SOp3(block) expr; // Expressions, but proper priority.
SSizeofInner[100] => SizeofParam(block, name) : SIdentifier name #varName; // Ambiguous case: we don't know if it is an expression or a type.
SSizeofInner[500] => SizeofParam(t) : SPrimitiveType t; // For proper syntax highlighting, and resolution of standard types.

Expr SOp2(Block block);
SOp2 => a : SAtom(block) a;
SOp2 => operatorNoConst(block, n, op) : SOp2(block) n, "\+\+" @op;
SOp2 => operatorNoConst(block, n, op) : SOp2(block) n, "--" @op;
SOp2 => dotOperator(block, lhs, rhs, false) : SOp2(block) lhs, "\.", SIdentifier rhs;
SOp2 => dotOperator(block, lhs, rhs, true) : SOp2(block) lhs, "->", SIdentifier rhs;
SOp2 => arrayOperator(pos, block, base, index) : SOp2(block) base, "\[", SExpr(block) index, "\]";
SOp2 => fnCall(pos, lhs, params) : SOp2(block) lhs, "(", SActuals(block) params, ")";
SOp2 => ctorCall(pos, lhs, params) : SOp2(block) lhs, "{", SActuals(block) params, "}";


Expr SAtom(Block block);
SAtom => v : "(", SExpr(block) v, ")";
SAtom => IntLiteral(v) : "[0-9]+" @v #constant;
SAtom => charLiteral(v) : "'" #string - (SCharContent) @v - "'" #string;
SAtom => StrLiteral(pos, v) : "\"" #string - (SStrContent) v - "\"" #string;
SAtom => NameExpr(pos, block.scope, name) : SCompoundName(block.scope) name;
SAtom[10] => ThisPtr(pos, block.scope) : "this" #keyword;
SAtom[10] => BoolLiteral(pos, true) : "true" #keyword;
SAtom[10] => BoolLiteral(pos, false) : "false" #keyword;
SAtom[10] => Nullptr(pos) : "nullptr" #keyword;
SAtom[10] => Nullptr(pos) : "NULL" #keyword;

void SStrContent() #string;
SStrContent : "[^\"\\\n]*"; // All except end of string and backslash.
SStrContent : SStrContent - "\\." - SStrContent; // Gobble escape sequences (we could translate them here...)

void SCharContent() #string;
SCharContent : "[^'\\\n]*"; // All except end of string and backslash.
SCharContent : SCharContent - "\\." - SCharContent; // Gobble escape sequences (we could translate them here...)

// Special syntax for spawning threads (since we don't have proper support for void * and function pointers).
SOp2[10] => SpawnCall(pos, block, fn) : "thread_new", "(", "&?", SCompoundName(block.scope) fn, ")";
SOp2[10] => SpawnCall(pos, block, fn, params) : "thread_new", "(", "&?", SCompoundName(block.scope) fn, ",", SActuals(block) params, ")";

// Special syntax for printf. We don't support varargs.
SOp2[10] => printf(pos, block, fmt) : "printf", "(", "\"" #string - SPrintfStr fmt #string - "\"" #string, ")";
SOp2[10] => Printf(pos, block, fmt, params) : "printf", "(", "\"" #string - SPrintfStr fmt #string - "\"" #string, ",", SActuals(block) params, ")";

// The format string for printf.
Array<SStr> SPrintfStr();
SPrintfStr => x : SPrintfStr x - SPrintfFmt -> push - (SPrintfPart -> push)?;
SPrintfStr => Array<SStr>() : ;
SPrintfStr => Array<SStr>() : SPrintfPart -> push;

SStr SPrintfPart();
SPrintfPart => v : "[^\"\\\n%]*" @v;
SPrintfPart => v : (SPrintfPart - "\\." - SPrintfPart) @v;

SStr SPrintfFmt();
SPrintfFmt => v : "%-?[0-9]*\.?[0-9]*[a-z]" @v;
SPrintfFmt => v : "%%" @v;

// Malloc and free.

// Free is equivalent to delete[], which works for both arrays and non-arrays.
SOp2[10] => DeleteStmt(pos, expr, true) : "free" #fnName, "(", SExpr(block) expr, ")";

// We special-case "malloc" for regular variables and for arrays.
SOp2[10] => NewExpr(pos, block, type.extract) : "malloc" #fnName, "(", "sizeof" #keyword - SSizeofParam(block) type, ")";
SOp2[10] => NewExpr(pos, block, type.extract, count) : "malloc" #fnName, "(", SOp5(block) count, "\*", "sizeof" #keyword - SSizeofParam(block) type, ")";
SOp2[10] => NewExpr(pos, block, type.extract, count) : "malloc" #fnName, "(", "sizeof" #keyword - SSizeofParam(block) type, "\*", SOp5(block) count, ")";

// Fallback to give decent error messages.
SOp2[5] => mallocError(pos) : "malloc" #fnName, "(", SExpr, ")";


SimpleName SCompoundName(Scope scope);
SCompoundName => SimpleName() : SNamePart(scope) -> add, ("::", SNamePart(scope) -> add)*;

Array<Expr> SActuals(Block block);
SActuals => Array<Expr>() : ;
SActuals => Array<Expr>() : SExprNoComma(block) -> push, (",", SExprNoComma(block) -> push)*;