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

73 lines
3.7 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-10-09 17:40:54 -06:00
AssignmentStatement = identifier "=" Expression ";" -- identifier
| StructGetter "=" Expression ";" -- getter
2025-08-18 22:53:36 -06:00
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 | "_")*
StructGetter = Expression "." identifier
2025-08-18 22:53:36 -06:00
IfExpression = "if" "(" Expression ")" Block ("else" Block)?
2025-10-09 17:40:54 -06:00
Path = Path "::" identifier -- nested
| identifier -- base
PrimaryExpression = LiteralInt
2025-08-18 22:53:36 -06:00
| LiteralFloat
| LiteralBool
| LiteralString
2025-10-09 17:40:54 -06:00
| Path -- path
2025-08-18 22:53:36 -06:00
| "(" Expression ")" -- parens
2025-10-09 17:40:54 -06:00
StructExpression = LiteralStruct
| Block
| IfExpression
| PrimaryExpression
MemberExpression = MemberExpression "." identifier -- structGetter
| StructExpression
CallExpression = CallExpression "." identifier -- structGetter
| CallExpression "(" ListOf<Expression, ","> ")" -- functionCall
| MemberExpression "(" ListOf<Expression, ","> ")" -- memberFunctionCall
| MemberExpression
MultExpression = MultExpression "*" CallExpression -- mult
| MultExpression "/" CallExpression -- div
| CallExpression
AddExpression = Expression "+" MultExpression -- plus
| Expression "-" MultExpression -- minus
| MultExpression
Expression = AddExpression
2025-08-18 22:53:36 -06:00
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
}
`);