Xarpeg Kotlin PEG Parser

Online Parser Sample

View on GitHub

Enter mathematical expressions in the input field below and click "Evaluate" to see the results. This interactive demo lets you test arithmetic operations, variables, lambda functions, Python-style indent blocks, heredoc string literals, and more. Try the examples on the right to get started, or write your own expressions!

Tip: Press Ctrl+Enter (or Cmd+Enter on Mac) to evaluate

The parser evaluates programs with arithmetic operations, comparison operators, ternary conditionals, variable declarations, lambda expressions, function calls, indent-based function definitions, and heredoc string literals. Stack traces are shown for runtime errors.

  • Enter expressions using +, -, *, /, and parentheses.
  • Compare values: 5 > 3, 10 == 10, x != y, a <= b
  • Use ternary operator: condition ? valueIfTrue : valueIfFalse
  • Declare variables: var x = 10
  • Reassign variables: x = 20
  • Define lambdas: var add = (a, b) -> a + b
  • Define indent-style functions: name(params): followed by an indented body.
  • Use heredoc string literals: start with << DELIMITER, then content lines, and end with a closing line that begins with the same DELIMITER at the start of the line, followed only by a line break or the end of the file.
  • Call functions: add(5, 3)
  • Identifiers can use letters, digits, and underscore (but cannot start with a digit).
  • Comparison operators return boolean values (true/false) with strict type checking.
  • See the evaluated result or read error messages with stack traces.

This grammar includes arithmetic operations, comparison operators, ternary conditionals, variable declarations, reassignment, lambda expressions, function calls, indent-based function definitions, and heredoc string literals.

Program         <- _ Statement (NL Statement)* _
Statement       <- IndentFunction / VarDeclaration / Expression
VarDeclaration  <- 'var' _ Identifier _ '=' _ Expression
IndentFunction  <- Identifier _ ParamList _ ':' NL INDENT Program
Expression      <- (Identifier _ '=' _ Expression) / Ternary
Ternary         <- (Equality _ '?' _ Equality _ ':' _ Equality) / Equality
Equality        <- Ordering (_ ('==' / '!=') _ Ordering)*
Ordering        <- Sum (_ ('<=' / '>=' / '<' / '>') _ Sum)*
Sum             <- Product (_ ('+' / '-') _ Product)*
Product         <- Factor (_ ('*' / '/') _ Factor)*
Factor          <- Lambda / FunctionCall / Variable / Heredoc / Number / '(' _ Expression _ ')'
Lambda          <- ParamList _ '->' _ Expression
ParamList       <- '(' _ (Identifier (',' _ Identifier)*)? _ ')'
FunctionCall    <- Identifier _ '(' _ (Expression (',' _ Expression)*)? _ ')'
Variable        <- Identifier
Heredoc         <- '<<' _ Delimiter NL (!CloseTag Line)* CloseTag
Delimiter       <- [a-zA-Z0-9_]+
CloseTag        <- Delimiter (NL / !.)  -- Delimiter is matched against the opening tag in parser context; NL or EOF may follow
Line            <- (!('\r\n' / [\r\n]) .)* NL
Identifier      <- [a-zA-Z_][a-zA-Z0-9_]*
Number          <- [0-9]+ ('.' [0-9]+)?
_               <- [ \t]*
NL              <- ('\r\n' / [\r\n]) _