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