Files
boring-lang/packages/boringlang/src/parse/grammar.ts

67 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-08-18 22:53:36 -06:00
import * as ohm from "ohm-js";
export const boringGrammar = ohm.grammar(String.raw`
Boringlang {
ReturnStatement = "return" Expression ";"
2025-08-19 21:54:06 -06:00
LetStatement = "let" identifier (":" TypeUsage)? "=" Expression ";"
2025-08-18 22:53:36 -06:00
AssignmentStatement = VariableUsage "=" Expression ";"
| StructGetter "=" Expression ";"
ExpressionStatement = Expression ";"
Statement = ExpressionStatement
| LetStatement
| ReturnStatement
| AssignmentStatement
LiteralInt = digit+
LiteralFloat = digit* "." digit+
LiteralBool = "true" | "false"
LiteralString = "\"" (~"\"" any)* "\""
| "'" (~"'" any)* "'"
2025-08-19 21:54:06 -06:00
LiteralStructField = identifier ":" Expression
LiteralStruct = identifier "{" ListOf<LiteralStructField, ","> "}"
identifier = (letter | "_")+(letter | digit | "_")*
2025-08-18 22:53:36 -06:00
FunctionCall = Expression "(" ListOf<Expression, ","> ")"
2025-08-19 21:54:06 -06:00
StructGetter = Expression "." identifier
VariableUsage = identifier
2025-08-18 22:53:36 -06:00
IfExpression = "if" "(" Expression ")" Block ("else" Block)?
Term = LiteralInt
| LiteralFloat
| LiteralBool
| LiteralString
| LiteralStruct
| IfExpression
| Block
| "(" Expression ")" -- parens
| VariableUsage
Factor = Factor "*" Term -- mult
| Factor "/" Term -- div
| Term
Expression = Expression "+" Factor -- plus
| Expression "-" Factor -- minus
| StructGetter
| FunctionCall
| Factor
Block = "{" Statement* Expression? "}"
2025-08-19 21:54:06 -06:00
NamedTypeUsage = identifier
2025-08-18 22:53:36 -06:00
TypeUsage = NamedTypeUsage
| "fn" "(" ListOf<TypeUsage, ","> ")" ":" TypeUsage -- function_tu
2025-08-19 21:54:06 -06:00
FunctionArgument = identifier ":" TypeUsage
FunctionDeclaration = "fn" identifier "(" ListOf<FunctionArgument, ","> ")" ":" TypeUsage
2025-08-18 22:53:36 -06:00
Function = FunctionDeclaration Block
2025-08-19 21:54:06 -06:00
StructTypeField = identifier ":" TypeUsage
StructTypeDeclaration = "type" identifier "struct" "{" ListOf<StructTypeField, ","> "}"
2025-08-18 22:53:36 -06:00
TraitMethod = FunctionDeclaration ";"
2025-08-19 21:54:06 -06:00
TraitTypeDeclaration = "type" identifier "trait" "{" TraitMethod* "}"
2025-08-18 22:53:36 -06:00
TypeDeclaration = StructTypeDeclaration
| TraitTypeDeclaration
Impl = "impl" (NamedTypeUsage "for")? NamedTypeUsage "{" Function* "}"
ModuleItem = Function
| TypeDeclaration
| Impl
Module = ModuleItem*
lineTerminator = "\n" | "\r" | "\u2028" | "\u2029"
comment = "//" (~lineTerminator any)* lineTerminator
space += comment
}
`);