67 lines
3.1 KiB
TypeScript
67 lines
3.1 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 = VariableUsage "=" Expression ";"
|
|
| StructGetter "=" Expression ";"
|
|
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 | "_")*
|
|
FunctionCall = Expression "(" ListOf<Expression, ","> ")"
|
|
StructGetter = Expression "." identifier
|
|
VariableUsage = identifier
|
|
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? "}"
|
|
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
|
|
}
|
|
`);
|