Grammar

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