macro-peg / macro_peg   0.0.3

BSD 3-clause "New" or "Revised" License GitHub

Macro PEG: PEG with macro-like rules

Scala versions: 2.11

Macro PEG

Macro PEG extends Parsing Expression Grammars with macro-like rules and is implemented in Scala 3. It supports lambda-style macros so you can build higher-order grammars.

Grammar Overview

Whitespace is omitted in the grammar below.

Grammar       <- Definition* ";"
Definition    <- Identifier ("(" Arg ("," Arg)* ")")? "=" Expression ";"
Arg           <- Identifier (":" Type)?
Type          <- RuleType / "?"
RuleType      <- ("(" Type ("," Type)* ")" "->" Type)
               / (Type "->" Type)
Expression    <- Sequence ("/" Sequence)*
Sequence      <- Prefix+
Prefix        <- ("&" / "!") Suffix
               / Suffix
Suffix        <- Primary "?"
               / Primary "*"
               / Primary "+"
               / Primary
Primary       <- "(" Expression ")"
               / Call
               / Debug
               / Identifier
               / StringLiteral
               / CharacterClass
               / Lambda
Call          <- Identifier "(" Expression ("," Expression)* ")"
Debug         <- "Debug" "(" Expression ")"
Lambda        <- "(" Identifier ("," Identifier)* "->" Expression ")"
StringLiteral <- '"' (!'"' .)* '"'
CharacterClass<- '[' '^'? (!']' .)+ ']'

Features

  • Macro rules with parameters
  • Lambda macros for higher-order grammars
  • Type annotations for macro parameters
  • Multiple evaluation strategies (call by name, call by value sequential/parallel)
  • Parser combinator library MacroParsers
  • Debug expressions for inspecting matches

Getting Started

Add the library to your build.sbt:

libraryDependencies += "com.github.kmizu" %% "macro_peg" % "0.1.1-SNAPSHOT"

Then parse and evaluate a grammar:

import com.github.kmizu.macro_peg._

val grammar = Parser.parse("""
  S = Double((x -> x x), "aa") !.;
  Double(f: ?, s: ?) = f(f(s));
""")

val evaluator = Evaluator(grammar)
val result = evaluator.evaluate("aaaaaaaa", Symbol("S"))
println(result)

Release Note

0.0.9

0.0.8

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

Running Tests

Execute the following command:

sbt test

License

This project is released under the MIT License.