diff options
author | elizabeth.hunt@simponic.xyz <elizabeth.hunt@simponic.xyz> | 2024-01-27 14:22:32 -0700 |
---|---|---|
committer | elizabeth.hunt@simponic.xyz <elizabeth.hunt@simponic.xyz> | 2024-01-27 14:22:32 -0700 |
commit | a064ed39edf34be7d17e415c6d15737f6145c162 (patch) | |
tree | 973234a8e917b76e4825c6824904cbebce86d1c2 | |
parent | 87be72ffec0f7759cf5d7de161262ec649fe0eea (diff) | |
download | tabloid-compiler-parser.tar.gz tabloid-compiler-parser.zip |
more peggy progressparser
-rw-r--r-- | src/parser/grammar.peggy | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/parser/grammar.peggy b/src/parser/grammar.peggy index 4a06041..1d1f142 100644 --- a/src/parser/grammar.peggy +++ b/src/parser/grammar.peggy @@ -4,12 +4,25 @@ Statement = Definition / Expression Definition = EnumDefinition / InterfaceDefinition / FunctionDefinition Expression = BinaryExpression / UnaryExpression / Literal +Literal = String / Integer / Float / StructInit / Identifier + +String = '"' chars:((![\\"]. / "\\\"")*) '"' { return chars.map(([_, match]) => match).join(""); } +Integer = digits:[0-9]+ !'.' { return parseInt(digits.join(''), 10); } +Float = digits:[0-9]+ '.' fractionalDigits:[0-9]+ { return parseFloat(digits.join('') + '.' + fractionalDigits.join('')); } + +StructInit = STRUCT_START _ SCOPE_START _ init:Declaration+ _ SCOPE_END +Declaration = VarDeclaration / FunctionDefinition +VarDeclaration = VAR_DEF _ name:CAMEL_CASE _ VAR_CONT _ 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 + // 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 = "||" / "&&" +ArithmeticOperation = "PLUS" / "MINUS" / "DIVIDED BY" / "REMAINDER" / "TIMES" / "TO THE POWER OF" / "MOVED A BIT RIGHT" / "MOVED A BIT LEFT" / "IS WITH" / "MASKED BY" +ComparisonOperation = "LESS THAN" / "GREATER THAN" / "IS NOT EQUAL TO" / "IS EQUAL TO" / "LESS THAN OR EQUAL" / "GREATER THAN OR EQUAL" +BooleanOperation = "OR" / "AND" UnaryExpression = PrefixOperation Expression / LPAREN Expression PostfixOperation PrefixOperation = "!" / "~" @@ -34,13 +47,18 @@ TupleType = tupleType:PASCAL_CASE _? LBRACKET _? tupleLength:PositiveInteger _? UnionType = firstType:PASCAL_CASE _ TYPE_UNION _ secondType:(UnionType / Type) Generic = LPAREN _? generic:((Type _? COMMA _?)* Type _?) RPAREN -Identifier = PASCAL_CASE / CAMEL_CASE +Identifier = !KEYWORD name:(PASCAL_CASE / CAMEL_CASE) { return { name }; } PASCAL_CASE = [A-Z](LETTER / DIGIT / SAFE_SYMBOL)+ CAMEL_CASE = [a-z](LETTER / DIGIT / SAFE_SYMBOL)+ PositiveInteger = [1-9][0-9]* +KEYWORD = TYPE_UNION / FUNCTION_DEF / FUNCTION_DEF_ARGS / FUNCTION_RETURN_TYPE + / INTERFACE_DEF / INTERFACE_CONT / ENUM_DEF / ENUM_CONT / STRUCT_START + / SCOPE_START / SCOPE_END / PROGRAM_END + / ArithmeticOperation / BinaryOperation / BooleanOperation + TYPE_UNION = "OR" FUNCTION_DEF = "DISCOVER" _ "HOW" _ "TO" FUNCTION_DEF_ARGS = "WITH" @@ -49,6 +67,9 @@ INTERFACE_DEF = "STUFF" _ "OF" INTERFACE_CONT = "LOOKS" _ "LIKE" ENUM_DEF = "ONLY" _ "OPTIONS" _ "OF" ENUM_CONT = "ARE" +STRUCT_START = "STUFF" _ "MADE" _ "OF" +VAR_DEF = "EXPERTS" _ "CLAIM" +VAR_CONT = "TO" _ "BE" SCOPE_START = "RUMOR" _ "HAS" _ "IT" SCOPE_END = "END" _ "OF" _ "STORY" @@ -64,4 +85,4 @@ _ = (" " / "\n" / "\t" / "\r\n")+ LETTER = [A-Za-z] SAFE_SYMBOL = "_" -DIGIT = [0-9] +DIGIT = [0-9]
\ No newline at end of file |