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