Grammar

  1. Root <- skip ContainerMembers eof
  2. # *** Top level ***
  3. ContainerMembers
  4. <- TestDecl ContainerMembers
  5. / TopLevelComptime ContainerMembers
  6. / KEYWORD_pub? TopLevelDecl ContainerMembers
  7. / KEYWORD_pub? ContainerField COMMA ContainerMembers
  8. / KEYWORD_pub? ContainerField
  9. /
  10. TestDecl <- KEYWORD_test STRINGLITERAL Block
  11. TopLevelComptime <- KEYWORD_comptime BlockExpr
  12. TopLevelDecl
  13. <- (KEYWORD_export / KEYWORD_extern STRINGLITERAL? / KEYWORD_inline)? FnProto (SEMICOLON / Block)
  14. / (KEYWORD_export / KEYWORD_extern STRINGLITERAL?)? KEYWORD_threadlocal? VarDecl
  15. / KEYWORD_use Expr SEMICOLON
  16. FnProto <- FnCC? KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? EXCLAMATIONMARK? (KEYWORD_var / TypeExpr)
  17. VarDecl <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteAlign? LinkSection? (EQUAL Expr)? SEMICOLON
  18. ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)?
  19. # *** Block Level ***
  20. Statement
  21. <- KEYWORD_comptime? VarDecl
  22. / KEYWORD_comptime BlockExprStatement
  23. / KEYWORD_suspend (SEMICOLON / BlockExprStatement)
  24. / KEYWORD_defer BlockExprStatement
  25. / KEYWORD_errdefer BlockExprStatement
  26. / IfStatement
  27. / LabeledStatement
  28. / SwitchExpr
  29. / AssignExpr SEMICOLON
  30. IfStatement
  31. <- IfPrefix BlockExpr ( KEYWORD_else Payload? Statement )?
  32. / IfPrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )
  33. LabeledStatement <- BlockLabel? (Block / LoopStatement)
  34. LoopStatement <- KEYWORD_inline? (ForStatement / WhileStatement)
  35. ForStatement
  36. <- ForPrefix BlockExpr ( KEYWORD_else Statement )?
  37. / ForPrefix AssignExpr ( SEMICOLON / KEYWORD_else Statement )
  38. WhileStatement
  39. <- WhilePrefix BlockExpr ( KEYWORD_else Payload? Statement )?
  40. / WhilePrefix AssignExpr ( SEMICOLON / KEYWORD_else Payload? Statement )
  41. BlockExprStatement
  42. <- BlockExpr
  43. / AssignExpr SEMICOLON
  44. BlockExpr <- BlockLabel? Block
  45. # *** Expression Level ***
  46. AssignExpr <- Expr (AssignOp Expr)?
  47. Expr <- KEYWORD_try* BoolOrExpr
  48. BoolOrExpr <- BoolAndExpr (KEYWORD_or BoolAndExpr)*
  49. BoolAndExpr <- CompareExpr (KEYWORD_and CompareExpr)*
  50. CompareExpr <- BitwiseExpr (CompareOp BitwiseExpr)?
  51. BitwiseExpr <- BitShiftExpr (BitwiseOp BitShiftExpr)*
  52. BitShiftExpr <- AdditionExpr (BitShiftOp AdditionExpr)*
  53. AdditionExpr <- MultiplyExpr (AdditionOp MultiplyExpr)*
  54. MultiplyExpr <- PrefixExpr (MultiplyOp PrefixExpr)*
  55. PrefixExpr <- PrefixOp* PrimaryExpr
  56. PrimaryExpr
  57. <- AsmExpr
  58. / IfExpr
  59. / KEYWORD_break BreakLabel? Expr?
  60. / KEYWORD_cancel Expr
  61. / KEYWORD_comptime Expr
  62. / KEYWORD_continue BreakLabel?
  63. / KEYWORD_resume Expr
  64. / KEYWORD_return Expr?
  65. / BlockLabel? LoopExpr
  66. / Block
  67. / CurlySuffixExpr
  68. IfExpr <- IfPrefix Expr (KEYWORD_else Payload? Expr)?
  69. LabeledExpr <- BlockLabel? (Block / LoopExpr)
  70. Block <- LBRACE Statement* RBRACE
  71. LoopExpr <- KEYWORD_inline? (ForExpr / WhileExpr)
  72. ForExpr <- ForPrefix Expr (KEYWORD_else Expr)?
  73. WhileExpr <- WhilePrefix Expr (KEYWORD_else Payload? Expr)?
  74. InitList
  75. <- LBRACE FieldInit (COMMA FieldInit)* COMMA? RBRACE
  76. / LBRACE Expr (COMMA Expr)* COMMA? RBRACE
  77. / LBRACE RBRACE
  78. TypeExpr <- PrefixTypeOp* ErrorUnionExpr
  79. ErrorUnionExpr <- SuffixExpr (EXCLAMATIONMARK TypeExpr)?
  80. SuffixExpr
  81. <- AsyncPrefix PrimaryTypeExpr SuffixOp* FnCallArguments
  82. / PrimaryTypeExpr (SuffixOp / FnCallArguments)*
  83. PrimaryTypeExpr
  84. <- BUILTINIDENTIFIER FnCallArguments
  85. / CHAR_LITERAL
  86. / ContainerDecl
  87. / ErrorSetDecl
  88. / FLOAT
  89. / FnProto
  90. / GroupedExpr
  91. / LabeledTypeExpr
  92. / IDENTIFIER
  93. / IfTypeExpr
  94. / INTEGER
  95. / KEYWORD_anyerror
  96. / KEYWORD_comptime TypeExpr
  97. / KEYWORD_error DOT IDENTIFIER
  98. / KEYWORD_false
  99. / KEYWORD_null
  100. / KEYWORD_promise
  101. / KEYWORD_true
  102. / KEYWORD_undefined
  103. / KEYWORD_unreachable
  104. / STRINGLITERAL
  105. / SwitchExpr
  106. ContainerDecl <- (KEYWORD_extern / KEYWORD_packed)? ContainerDeclAuto
  107. ErrorSetDecl <- KEYWORD_error LBRACE IdentifierList RBRACE
  108. GroupedExpr <- LPAREN Expr RPAREN
  109. IfTypeExpr <- IfPrefix TypeExpr (KEYWORD_else Payload? TypeExpr)?
  110. LabeledTypeExpr
  111. <- BlockLabel Block
  112. / BlockLabel? LoopTypeExpr
  113. LoopTypeExpr <- KEYWORD_inline? (ForTypeExpr / WhileTypeExpr)
  114. ForTypeExpr <- ForPrefix TypeExpr (KEYWORD_else TypeExpr)?
  115. WhileTypeExpr <- WhilePrefix TypeExpr (KEYWORD_else Payload? TypeExpr)?
  116. SwitchExpr <- KEYWORD_switch LPAREN Expr RPAREN LBRACE SwitchProngList RBRACE
  117. # *** Assembly ***
  118. AsmExpr <- KEYWORD_asm KEYWORD_volatile? LPAREN STRINGLITERAL AsmOutput? RPAREN
  119. AsmOutput <- COLON AsmOutputList AsmInput?
  120. AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPAREN
  121. AsmInput <- COLON AsmInputList AsmClobbers?
  122. AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN
  123. AsmClobbers <- COLON StringList
  124. # *** Helper grammar ***
  125. BreakLabel <- COLON IDENTIFIER
  126. BlockLabel <- IDENTIFIER COLON
  127. FieldInit <- DOT IDENTIFIER EQUAL Expr
  128. WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN
  129. LinkSection <- KEYWORD_linksection LPAREN Expr RPAREN
  130. # Fn specific
  131. FnCC
  132. <- KEYWORD_nakedcc
  133. / KEYWORD_stdcallcc
  134. / KEYWORD_extern
  135. / KEYWORD_async (LARROW TypeExpr RARROW)?
  136. ParamDecl <- (KEYWORD_noalias / KEYWORD_comptime)? (IDENTIFIER COLON)? ParamType
  137. ParamType
  138. <- KEYWORD_var
  139. / DOT3
  140. / TypeExpr
  141. # Control flow prefixes
  142. IfPrefix <- KEYWORD_if LPAREN Expr RPAREN PtrPayload?
  143. WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr?
  144. ForPrefix <- KEYWORD_for LPAREN Expr RPAREN PtrIndexPayload
  145. # Payloads
  146. Payload <- PIPE IDENTIFIER PIPE
  147. PtrPayload <- PIPE ASTERISK? IDENTIFIER PIPE
  148. PtrIndexPayload <- PIPE ASTERISK? IDENTIFIER (COMMA IDENTIFIER)? PIPE
  149. # Switch specific
  150. SwitchProng <- SwitchCase EQUALRARROW PtrPayload? AssignExpr
  151. SwitchCase
  152. <- SwitchItem (COMMA SwitchItem)* COMMA?
  153. / KEYWORD_else
  154. SwitchItem <- Expr (DOT3 Expr)?
  155. # Operators
  156. AssignOp
  157. <- ASTERISKEQUAL
  158. / SLASHEQUAL
  159. / PERCENTEQUAL
  160. / PLUSEQUAL
  161. / MINUSEQUAL
  162. / LARROW2EQUAL
  163. / RARROW2EQUAL
  164. / AMPERSANDEQUAL
  165. / CARETEQUAL
  166. / PIPEEQUAL
  167. / ASTERISKPERCENTEQUAL
  168. / PLUSPERCENTEQUAL
  169. / MINUSPERCENTEQUAL
  170. / EQUAL
  171. CompareOp
  172. <- EQUALEQUAL
  173. / EXCLAMATIONMARKEQUAL
  174. / LARROW
  175. / RARROW
  176. / LARROWEQUAL
  177. / RARROWEQUAL
  178. BitwiseOp
  179. <- AMPERSAND
  180. / CARET
  181. / PIPE
  182. / KEYWORD_orelse
  183. / KEYWORD_catch Payload?
  184. BitShiftOp
  185. <- LARROW2
  186. / RARROW2
  187. AdditionOp
  188. <- PLUS
  189. / MINUS
  190. / PLUS2
  191. / PLUSPERCENT
  192. / MINUSPERCENT
  193. MultiplyOp
  194. <- PIPE2
  195. / ASTERISK
  196. / SLASH
  197. / PERCENT
  198. / ASTERISK2
  199. / ASTERISKPERCENT
  200. PrefixOp
  201. <- EXCLAMATIONMARK
  202. / MINUS
  203. / TILDE
  204. / MINUSPERCENT
  205. / AMPERSAND
  206. / KEYWORD_try
  207. / KEYWORD_await
  208. PrefixTypeOp
  209. <- QUESTIONMARK
  210. / KEYWORD_promise MINUSRARROW
  211. / ArrayTypeStart (ByteAlign / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
  212. / PtrTypeStart (KEYWORD_align LPAREN Expr (COLON INTEGER COLON INTEGER)? RPAREN / KEYWORD_const / KEYWORD_volatile / KEYWORD_allowzero)*
  213. SuffixOp
  214. <- LBRACKET Expr (DOT2 Expr?)? RBRACKET
  215. / DOT IDENTIFIER
  216. / DOTASTERISK
  217. / DOTQUESTIONMARK
  218. AsyncPrefix <- KEYWORD_async (LARROW PrefixExpr RARROW)?
  219. FnCallArguments <- LPAREN ExprList RPAREN
  220. # Ptr specific
  221. ArrayTypeStart <- LBRACKET Expr? RBRACKET
  222. PtrTypeStart
  223. <- ASTERISK
  224. / ASTERISK2
  225. / PTRUNKNOWN
  226. / PTRC
  227. # ContainerDecl specific
  228. ContainerDeclAuto <- ContainerDeclType LBRACE ContainerMembers RBRACE
  229. ContainerDeclType
  230. <- (KEYWORD_struct / KEYWORD_enum) (LPAREN Expr RPAREN)?
  231. / KEYWORD_union (LPAREN (KEYWORD_enum (LPAREN Expr RPAREN)? / Expr) RPAREN)?
  232. # Alignment
  233. ByteAlign <- KEYWORD_align LPAREN Expr RPAREN
  234. # Lists
  235. IdentifierList <- (IDENTIFIER COMMA)* IDENTIFIER?
  236. SwitchProngList <- (SwitchProng COMMA)* SwitchProng?
  237. AsmOutputList <- (AsmOutputItem COMMA)* AsmOutputItem?
  238. AsmInputList <- (AsmInputItem COMMA)* AsmInputItem?
  239. StringList <- (STRINGLITERAL COMMA)* STRINGLITERAL?
  240. ParamDeclList <- (ParamDecl COMMA)* ParamDecl?
  241. ExprList <- (Expr COMMA)* Expr?
  242. # *** Tokens ***
  243. eof <- !.
  244. hex <- [0-9a-fA-F]
  245. char_escape
  246. <- "\\x" hex hex
  247. / "\\u" hex hex hex hex
  248. / "\\U" hex hex hex hex hex hex
  249. / "\\" [nr\\t'"]
  250. char_char
  251. <- char_escape
  252. / [^\\'\n]
  253. string_char
  254. <- char_escape
  255. / [^\\"\n]
  256. line_comment <- '//'[^\n]*
  257. line_string <- ("\\\\" [^\n]* [ \n]*)+
  258. line_cstring <- ("c\\\\" [^\n]* [ \n]*)+
  259. skip <- ([ \n] / line_comment)*
  260. CHAR_LITERAL <- "'" char_char "'" skip
  261. FLOAT
  262. <- "0x" hex+ "." hex+ ([pP] [-+]? hex+)? skip
  263. / [0-9]+ "." [0-9]+ ([eE] [-+]? [0-9]+)? skip
  264. / "0x" hex+ "."? [pP] [-+]? hex+ skip
  265. / [0-9]+ "."? [eE] [-+]? [0-9]+ skip
  266. INTEGER
  267. <- "0b" [01]+ skip
  268. / "0o" [0-7]+ skip
  269. / "0x" hex+ skip
  270. / [0-9]+ skip
  271. STRINGLITERAL
  272. <- "c"? "\"" string_char* "\"" skip
  273. / line_string skip
  274. / line_cstring skip
  275. IDENTIFIER
  276. <- !keyword ("c" !["\\] / [A-Zabd-z_]) [A-Za-z0-9_]* skip
  277. / "@\"" string_char* "\"" skip
  278. BUILTINIDENTIFIER <- "@"[A-Za-z_][A-Za-z0-9_]* skip
  279. AMPERSAND <- '&' ![=] skip
  280. AMPERSANDEQUAL <- '&=' skip
  281. ASTERISK <- '*' ![*%=] skip
  282. ASTERISK2 <- '**' skip
  283. ASTERISKEQUAL <- '*=' skip
  284. ASTERISKPERCENT <- '*%' ![=] skip
  285. ASTERISKPERCENTEQUAL <- '*%=' skip
  286. CARET <- '^' ![=] skip
  287. CARETEQUAL <- '^=' skip
  288. COLON <- ':' skip
  289. COMMA <- ',' skip
  290. DOT <- '.' ![*.?] skip
  291. DOT2 <- '..' ![.] skip
  292. DOT3 <- '...' skip
  293. DOTASTERISK <- '.*' skip
  294. DOTQUESTIONMARK <- '.?' skip
  295. EQUAL <- '=' ![>=] skip
  296. EQUALEQUAL <- '==' skip
  297. EQUALRARROW <- '=>' skip
  298. EXCLAMATIONMARK <- '!' ![=] skip
  299. EXCLAMATIONMARKEQUAL <- '!=' skip
  300. LARROW <- '<' ![<=] skip
  301. LARROW2 <- '<<' ![=] skip
  302. LARROW2EQUAL <- '<<=' skip
  303. LARROWEQUAL <- '<=' skip
  304. LBRACE <- '{' skip
  305. LBRACKET <- '[' ![*] skip
  306. LPAREN <- '(' skip
  307. MINUS <- '-' ![%=>] skip
  308. MINUSEQUAL <- '-=' skip
  309. MINUSPERCENT <- '-%' ![=] skip
  310. MINUSPERCENTEQUAL <- '-%=' skip
  311. MINUSRARROW <- '->' skip
  312. PERCENT <- '%' ![=] skip
  313. PERCENTEQUAL <- '%=' skip
  314. PIPE <- '|' ![|=] skip
  315. PIPE2 <- '||' skip
  316. PIPEEQUAL <- '|=' skip
  317. PLUS <- '+' ![%+=] skip
  318. PLUS2 <- '++' skip
  319. PLUSEQUAL <- '+=' skip
  320. PLUSPERCENT <- '+%' ![=] skip
  321. PLUSPERCENTEQUAL <- '+%=' skip
  322. PTRC <- '[*c]' skip
  323. PTRUNKNOWN <- '[*]' skip
  324. QUESTIONMARK <- '?' skip
  325. RARROW <- '>' ![>=] skip
  326. RARROW2 <- '>>' ![=] skip
  327. RARROW2EQUAL <- '>>=' skip
  328. RARROWEQUAL <- '>=' skip
  329. RBRACE <- '}' skip
  330. RBRACKET <- ']' skip
  331. RPAREN <- ')' skip
  332. SEMICOLON <- ';' skip
  333. SLASH <- '/' ![=] skip
  334. SLASHEQUAL <- '/=' skip
  335. TILDE <- '~' skip
  336. end_of_word <- ![a-zA-Z0-9_] skip
  337. KEYWORD_align <- 'align' end_of_word
  338. KEYWORD_allowzero <- 'allowzero' end_of_word
  339. KEYWORD_and <- 'and' end_of_word
  340. KEYWORD_anyerror <- 'anyerror' end_of_word
  341. KEYWORD_asm <- 'asm' end_of_word
  342. KEYWORD_async <- 'async' end_of_word
  343. KEYWORD_await <- 'await' end_of_word
  344. KEYWORD_break <- 'break' end_of_word
  345. KEYWORD_cancel <- 'cancel' end_of_word
  346. KEYWORD_catch <- 'catch' end_of_word
  347. KEYWORD_comptime <- 'comptime' end_of_word
  348. KEYWORD_const <- 'const' end_of_word
  349. KEYWORD_continue <- 'continue' end_of_word
  350. KEYWORD_defer <- 'defer' end_of_word
  351. KEYWORD_else <- 'else' end_of_word
  352. KEYWORD_enum <- 'enum' end_of_word
  353. KEYWORD_errdefer <- 'errdefer' end_of_word
  354. KEYWORD_error <- 'error' end_of_word
  355. KEYWORD_export <- 'export' end_of_word
  356. KEYWORD_extern <- 'extern' end_of_word
  357. KEYWORD_false <- 'false' end_of_word
  358. KEYWORD_fn <- 'fn' end_of_word
  359. KEYWORD_for <- 'for' end_of_word
  360. KEYWORD_if <- 'if' end_of_word
  361. KEYWORD_inline <- 'inline' end_of_word
  362. KEYWORD_nakedcc <- 'nakedcc' end_of_word
  363. KEYWORD_noalias <- 'noalias' end_of_word
  364. KEYWORD_null <- 'null' end_of_word
  365. KEYWORD_or <- 'or' end_of_word
  366. KEYWORD_orelse <- 'orelse' end_of_word
  367. KEYWORD_packed <- 'packed' end_of_word
  368. KEYWORD_promise <- 'promise' end_of_word
  369. KEYWORD_pub <- 'pub' end_of_word
  370. KEYWORD_resume <- 'resume' end_of_word
  371. KEYWORD_return <- 'return' end_of_word
  372. KEYWORD_linksection <- 'linksection' end_of_word
  373. KEYWORD_stdcallcc <- 'stdcallcc' end_of_word
  374. KEYWORD_struct <- 'struct' end_of_word
  375. KEYWORD_suspend <- 'suspend' end_of_word
  376. KEYWORD_switch <- 'switch' end_of_word
  377. KEYWORD_test <- 'test' end_of_word
  378. KEYWORD_threadlocal <- 'threadlocal' end_of_word
  379. KEYWORD_true <- 'true' end_of_word
  380. KEYWORD_try <- 'try' end_of_word
  381. KEYWORD_undefined <- 'undefined' end_of_word
  382. KEYWORD_union <- 'union' end_of_word
  383. KEYWORD_unreachable <- 'unreachable' end_of_word
  384. KEYWORD_use <- 'use' end_of_word
  385. KEYWORD_var <- 'var' end_of_word
  386. KEYWORD_volatile <- 'volatile' end_of_word
  387. KEYWORD_while <- 'while' end_of_word
  388. keyword <- KEYWORD_align / KEYWORD_and / KEYWORD_allowzero / KEYWORD_anyerror / KEYWORD_asm
  389. / KEYWORD_async / KEYWORD_await / KEYWORD_break / KEYWORD_cancel
  390. / KEYWORD_catch / KEYWORD_comptime / KEYWORD_const / KEYWORD_continue
  391. / KEYWORD_defer / KEYWORD_else / KEYWORD_enum / KEYWORD_errdefer
  392. / KEYWORD_error / KEYWORD_export / KEYWORD_extern / KEYWORD_false
  393. / KEYWORD_fn / KEYWORD_for / KEYWORD_if / KEYWORD_inline
  394. / KEYWORD_nakedcc / KEYWORD_noalias / KEYWORD_null / KEYWORD_or
  395. / KEYWORD_orelse / KEYWORD_packed / KEYWORD_promise / KEYWORD_pub
  396. / KEYWORD_resume / KEYWORD_return / KEYWORD_linksection
  397. / KEYWORD_stdcallcc / KEYWORD_struct / KEYWORD_suspend
  398. / KEYWORD_switch / KEYWORD_test / KEYWORD_threadlocal / KEYWORD_true / KEYWORD_try
  399. / KEYWORD_undefined / KEYWORD_union / KEYWORD_unreachable
  400. / KEYWORD_use / KEYWORD_var / KEYWORD_volatile / KEYWORD_while