Files
boring-lang/packages/boringlang/src/parse/grammar.ts
2026-01-20 17:47:36 -07:00

77 lines
4.1 KiB
TypeScript

import * as ohm from "ohm-js";
export const boringGrammar = ohm.grammar(String.raw`
Boringlang {
GenericUsage = "[" ListOf<TypeUsage, ","> "]"
GenericParameter = identifier ":" ListOf<TypeUsage, "+"> -- conditions
| identifier
GenericDeclaration = "[" ListOf<GenericParameter, ","> "]"
ReturnStatement = "return" Expression ";"
LetStatement = "let" identifier (":" TypeUsage)? "=" Expression ";"
AssignmentStatement = identifier "=" Expression ";" -- identifier
| StructGetter "=" Expression ";" -- getter
ExpressionStatement = Expression ";"
Statement = ExpressionStatement
| LetStatement
| ReturnStatement
| AssignmentStatement
LiteralInt = digit+
LiteralFloat = digit* "." digit+
LiteralBool = "true" | "false"
LiteralString = "\"" (~"\"" any)* "\""
| "'" (~"'" any)* "'"
LiteralStructField = identifier ":" Expression
LiteralStruct = identifier (GenericUsage)? "{" ListOf<LiteralStructField, ","> "}"
identifier = (letter | "_")+(letter | digit | "_")*
StructGetter = Expression "." identifier
IfExpression = "if" "(" Expression ")" Block ("else" Block)?
Path = Path "::" identifier (GenericUsage)? -- nested
| identifier (GenericUsage)? -- base
PrimaryExpression = LiteralInt
| LiteralFloat
| LiteralBool
| LiteralString
| Path -- path
| "(" Expression ")" -- parens
StructExpression = LiteralStruct
| Block
| IfExpression
| PrimaryExpression
MemberExpression = MemberExpression "." identifier (GenericUsage)? -- structGetter
| StructExpression
CallExpression = CallExpression "." identifier (GenericUsage)? -- 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
Block = "{" Statement* Expression? "}"
NamedTypeUsage = identifier (GenericUsage)?
TypeUsage = NamedTypeUsage
| "fn" "(" ListOf<TypeUsage, ","> ")" ":" TypeUsage -- function_tu
FunctionArgument = identifier ":" TypeUsage
FunctionDeclaration = "fn" identifier (GenericDeclaration)? "(" ListOf<FunctionArgument, ","> ")" ":" TypeUsage
Function = FunctionDeclaration Block
StructTypeField = identifier ":" TypeUsage
StructTypeDeclaration = "type" identifier (GenericDeclaration)? "struct" "{" ListOf<StructTypeField, ","> "}"
TraitMethod = FunctionDeclaration ";"
TraitTypeDeclaration = "type" identifier (GenericDeclaration)? "trait" "{" TraitMethod* "}"
TypeDeclaration = StructTypeDeclaration
| TraitTypeDeclaration
Impl = "impl" (GenericDeclaration)? (NamedTypeUsage "for")? NamedTypeUsage "{" Function* "}"
ModuleItem = Function
| TypeDeclaration
| Impl
Module = ModuleItem*
lineTerminator = "\n" | "\r" | "\u2028" | "\u2029"
comment = "//" (~lineTerminator any)* lineTerminator
space += comment
}
`);