Skip to content

Instantly share code, notes, and snippets.

@phred
Last active October 11, 2015 08:08
Show Gist options
  • Save phred/3828935 to your computer and use it in GitHub Desktop.
Save phred/3828935 to your computer and use it in GitHub Desktop.
EBNF for FoxyCart discount strings
USE: peg.ebnf
#! throw this in the listener to see the example parse tree
"discount_quantity_amount=Discount_Name{repeat|1-5.42|2-5|6-0}"
[EBNF
number = [0-9]+ => [[ string>number ]]
amount = number ("." number)?
type = "allunits" | "incremental" | "repeat" | "single"
tier = number "-" amount
method = "discount" "_" ("quantity" | "price") "_" ("amount" | "percentage")
name = [A-Za-z0-9_ ]+ => [[ >string ]]
discount = method "=" name "{" type "|" tier ( "|" tier )+ "}"
EBNF]
#! example parse tree
V{
V{ "discount" "_" "quantity" "_" "amount" }
"="
"Discount_Name"
"{"
"repeat"
"|"
V{ 1 "-" V{ 5 V{ "." 42 } } }
V{
V{ "|" V{ 2 "-" V{ 5 f } } }
V{ "|" V{ 6 "-" V{ 0 f } } }
}
"}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment