diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-01-24 18:59:13 -0700 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2024-01-24 18:59:13 -0700 |
commit | 203925d9a48d34537bdf6cd25502134df5e91ae7 (patch) | |
tree | afe8a12679568ba00bfa019b0a5b6f13a3778c00 /src/parser/grammar.peg | |
download | tabloid-compiler-203925d9a48d34537bdf6cd25502134df5e91ae7.tar.gz tabloid-compiler-203925d9a48d34537bdf6cd25502134df5e91ae7.zip |
Diffstat (limited to 'src/parser/grammar.peg')
-rw-r--r-- | src/parser/grammar.peg | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/parser/grammar.peg b/src/parser/grammar.peg new file mode 100644 index 0000000..4bb1454 --- /dev/null +++ b/src/parser/grammar.peg @@ -0,0 +1,67 @@ +Program := Statement* PROGRAM_END + +Statement := Enum | Interface | Module | Expression + +Module := 'EVERYTHING' _ 'CHANGED' _ 'WHEN' _ name=PASCAL_CASE _ 'EXISTS' _ SCOPE_START _ body=ModuleBody _ SCOPE_END +ModuleBody := Constructor? _? Declaration* +Constructor := 'PRACTICALLY' _ 'IMPOSSIBLE' _ 'TO' _ 'HAVE' _ module=Identifier _ 'WITHOUT' _ args=ArgList _ SCOPE_START _ body=FunctionBody _ SCOPE_END + +Expression := FunctionCall | BinaryExpression | UnaryOperation | Literal | Declaration + +FunctionCall := name=Identifier _ 'OF' _ args={{Expression _? COMMA _?}* Expression} + +BinaryExpression := left=Expression op=BinaryOperation right=Expression +BinaryOperation := ArithmeticOperation | ComparisonOperation | BooleanOperation +ArithmeticOperation := '\+' | '\-' | '/' | '\%' | '\*\*' | '\*' | '>>' | '<<' +ComparisonOperation := '<' | '>' | '<=' | '>=' +BooleanOperation := '||' | '|' | '&&' | '&' + +Declaration := VarDeclaration | FunctionDeclaration +VarDeclaration := 'EXPERTS' _ 'CLAIM' _ var=CAMEL_CASE _ 'TO' _ 'BE' _ Type _ val={option=OptionMatch | 'OF' _ expr=Expression} +OptionMatch := 'WHO' _ 'IS' _ 'DATING' _ SCOPE_START _ {some=SomeMatch _ none=NoneMatch} | {none=NoneMatch _ some=SomeMatch} _ SCOPE_END +SomeMatch := 'Someone' LPAREN name=Identifier RPAREN _ 'SO' _ expr=Expression +NoneMatch := 'Nobody' _ 'SO' _ expr=Expression + +Literal := String | Integer | Float | StructInit | Identifier +String := '(["\'])(?:(?=(\\?))\2.)*?\1' +Integer := '^-?0*\d+$' +Float := '^-?\d+(\.\d+)?$' + +UnaryOperation := PrefixOperation Expression | Expression PostfixOperation +PrefixOperation := '!' | '~' +PostfixOperation := LBRACKET index=POSITIVE_INT RBRACKET + +StructInit := 'STUFF' _ 'MADE' _ 'OF' _ SCOPE_START _ init=Declaration* _ SCOPE_END + +Type := name={PASCAL_CASE} | {name=PASCAL_CASE _ generic=Generic} | {tupleType=Type _ LBRACKET _ tupleLength=POSITIVE_INT _ RBRACKET} | union={Type _ 'OR' _ Type} +Generic := LPAREN _? generic={{Type _? COMMA _?}* Type} RPAREN + +FunctionDeclaration := FunctionSignature _ SCOPE_START _ body=FunctionBody _ SCOPE_END +FunctionBody := {Expression | ReturnStatement}* +FunctionSignature := 'DISCOVER' _ 'HOW' _ 'TO' _ name=CAMEL_CASE _ 'WITH' _ args=ArgList _ 'GIVES' _ 'YOU' _ return=Type +ArgList := args={{type=Type name=CAMEL_CASE COMMA _?}* type=Type name=CAMEL_CASE} + +ReturnStatement := 'SHOCKING' _ 'DEVELOPMENT' _ Expression + +Interface := 'STUFF' _ 'OF' _ name=PASCAL_CASE _ 'LOOKS' _ 'LIKE' SCOPE_START methods={ FunctionSignature* } + +Enum := 'ONLY' _ 'OPTIONS' _ 'OF' name=PASCAL_CASE _ 'ARE' _ SCOPE_START _ options=EnumBody* _ SCOPE_END +EnumBody := name=Identifier COMMA + +Identifier := '[a-zA-Z][\w\d|\.]*' + +SCOPE_START := 'RUMOR' _ 'HAS' _ 'IT' +SCOPE_END := 'END' _ 'OF' _ 'STORY' +PROGRAM_END := 'PLEASE' _ 'LIKE' _ 'AND' _ 'SUBSCRIBE' +PASCAL_CASE := '[A-Z][\w\d]*' +CAMEL_CASE := '[a-z][\w\d]*' + +LPAREN := '\(' +RPAREN := '\)' +LBRACKET := '\[' +RBRACKET := '\]' +COMMA := ',' + +POSITIVE_INT := '^[1-9][0-9]*$' + +_ := '\s'
\ No newline at end of file |