73 lines
3.7 KiB
TypeScript
73 lines
3.7 KiB
TypeScript
import * as ohm from "ohm-js";
|
|
|
|
export const boringGrammar = ohm.grammar(String.raw`
|
|
Boringlang {
|
|
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 "{" ListOf<LiteralStructField, ","> "}"
|
|
identifier = (letter | "_")+(letter | digit | "_")*
|
|
StructGetter = Expression "." identifier
|
|
IfExpression = "if" "(" Expression ")" Block ("else" Block)?
|
|
Path = Path "::" identifier -- nested
|
|
| identifier -- base
|
|
PrimaryExpression = LiteralInt
|
|
| LiteralFloat
|
|
| LiteralBool
|
|
| LiteralString
|
|
| Path -- path
|
|
| "(" Expression ")" -- parens
|
|
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
|
|
Block = "{" Statement* Expression? "}"
|
|
NamedTypeUsage = identifier
|
|
TypeUsage = NamedTypeUsage
|
|
| "fn" "(" ListOf<TypeUsage, ","> ")" ":" TypeUsage -- function_tu
|
|
FunctionArgument = identifier ":" TypeUsage
|
|
FunctionDeclaration = "fn" identifier "(" ListOf<FunctionArgument, ","> ")" ":" TypeUsage
|
|
Function = FunctionDeclaration Block
|
|
StructTypeField = identifier ":" TypeUsage
|
|
StructTypeDeclaration = "type" identifier "struct" "{" ListOf<StructTypeField, ","> "}"
|
|
TraitMethod = FunctionDeclaration ";"
|
|
TraitTypeDeclaration = "type" identifier "trait" "{" TraitMethod* "}"
|
|
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
|
|
}
|
|
`);
|