diff options
author | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2024-01-24 22:42:50 -0700 |
---|---|---|
committer | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2024-01-24 22:42:50 -0700 |
commit | 87be72ffec0f7759cf5d7de161262ec649fe0eea (patch) | |
tree | 7436f40b0b789d63c86661237e7acb067b6a7db6 | |
parent | 203925d9a48d34537bdf6cd25502134df5e91ae7 (diff) | |
download | tabloid-compiler-87be72ffec0f7759cf5d7de161262ec649fe0eea.tar.gz tabloid-compiler-87be72ffec0f7759cf5d7de161262ec649fe0eea.zip |
move to peggy. fix some recursion. have to come back and check out some more though
-rw-r--r-- | src/parser/grammar.peg | 66 | ||||
-rw-r--r-- | src/parser/grammar.peggy | 67 |
2 files changed, 67 insertions, 66 deletions
diff --git a/src/parser/grammar.peg b/src/parser/grammar.peg index 4bb1454..05368b2 100644 --- a/src/parser/grammar.peg +++ b/src/parser/grammar.peg @@ -1,67 +1 @@ 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 diff --git a/src/parser/grammar.peggy b/src/parser/grammar.peggy new file mode 100644 index 0000000..4a06041 --- /dev/null +++ b/src/parser/grammar.peggy @@ -0,0 +1,67 @@ +Program = (Statement / _)* end:PROGRAM_END _? + +Statement = Definition / Expression +Definition = EnumDefinition / InterfaceDefinition / FunctionDefinition +Expression = BinaryExpression / UnaryExpression / Literal + +// recursion here after removing LPAREN - maybe move to prefix notation? +BinaryExpression = LPAREN left:Expression _ op:BinaryOperation _ right:Expression +BinaryOperation = ArithmeticOperation / ComparisonOperation / BooleanOperation +ArithmeticOperation = "+" / "-" / "/" / "%" / "*" / "**" / ">>" / "<<" / "|" / "&" +ComparisonOperation = "<" / ">" / "<=" / ">=" / "==" / "!=" +BooleanOperation = "||" / "&&" + +UnaryExpression = PrefixOperation Expression / LPAREN Expression PostfixOperation +PrefixOperation = "!" / "~" +PostfixOperation = LBRACKET _? index:PositiveInteger _? RBRACKET + +EnumDefinition = ENUM_DEF _ name:PASCAL_CASE _ ENUM_CONT _ SCOPE_START + _ options:EnumBody+ _ SCOPE_END +EnumBody = name:Identifier "," + +InterfaceDefinition = INTERFACE_DEF _ name:PASCAL_CASE _ INTERFACE_CONT + _ signatures:FunctionSignature* _ SCOPE_END + +FunctionDefinition = FunctionSignature _ SCOPE_START _ body:Expression* _ SCOPE_END +FunctionSignature = FUNCTION_DEF _ name:CAMEL_CASE _ + (FUNCTION_DEF_ARGS _ args:FunctionArgsList _)? + FUNCTION_RETURN_TYPE _ result:Type +FunctionArgsList = (type:Type _ name:CAMEL_CASE _? "," _?)? + type:Type _ name:CAMEL_CASE + +Type = name:PASCAL_CASE (_ generic:Generic)? / TupleType / UnionType +TupleType = tupleType:PASCAL_CASE _? LBRACKET _? tupleLength:PositiveInteger _? RBRACKET +UnionType = firstType:PASCAL_CASE _ TYPE_UNION _ secondType:(UnionType / Type) + +Generic = LPAREN _? generic:((Type _? COMMA _?)* Type _?) RPAREN +Identifier = PASCAL_CASE / CAMEL_CASE + +PASCAL_CASE = [A-Z](LETTER / DIGIT / SAFE_SYMBOL)+ +CAMEL_CASE = [a-z](LETTER / DIGIT / SAFE_SYMBOL)+ + +PositiveInteger = [1-9][0-9]* + +TYPE_UNION = "OR" +FUNCTION_DEF = "DISCOVER" _ "HOW" _ "TO" +FUNCTION_DEF_ARGS = "WITH" +FUNCTION_RETURN_TYPE = "GIVES" _ "YOU" +INTERFACE_DEF = "STUFF" _ "OF" +INTERFACE_CONT = "LOOKS" _ "LIKE" +ENUM_DEF = "ONLY" _ "OPTIONS" _ "OF" +ENUM_CONT = "ARE" + +SCOPE_START = "RUMOR" _ "HAS" _ "IT" +SCOPE_END = "END" _ "OF" _ "STORY" +PROGRAM_END = "PLEASE" _ "LIKE" _ "AND" _ "SUBSCRIBE" + +COMMA = "," +LBRACKET = "[" +RBRACKET = "]" +LPAREN = "(" +RPAREN = ")" + +_ = (" " / "\n" / "\t" / "\r\n")+ + +LETTER = [A-Za-z] +SAFE_SYMBOL = "_" +DIGIT = [0-9] |