Skip to content

Instantly share code, notes, and snippets.

@u27a4
Created July 9, 2018 17:17
Show Gist options
  • Save u27a4/d0127ce3cf7400a72ba05a68dadf5415 to your computer and use it in GitHub Desktop.
Save u27a4/d0127ce3cf7400a72ba05a68dadf5415 to your computer and use it in GitHub Desktop.
Dice notation by PEG.js (https://pegjs.org/)
start
= additive
additive
= left:multiplicative __ "+" __ right:additive {
return {
type: "addexpr", left: left, right: right
}
}
/ left:multiplicative __ "-" __ right:additive {
return {
type: "subexpr", left: left, right: right
}
}
/ multiplicative
multiplicative
= left:primary __ "*" __ right:multiplicative {
return {
type: "mulexpr", left: left, right: right
}
}
/ left:primary __ "/" __ right:multiplicative {
return {
type: "divexpr", left: left, right: right
}
}
/ left:primary __ "x" __ right:multiplicative {
return {
type: "repexpr", left: left, right: right
}
}
/ __ "-" __ !"-" __ operand:multiplicative {
return {
type: "minusexpr", operand: operand
}
}
/ __ "+" __ !"+" __ operand:multiplicative {
return operand;
}
/ primary
primary
= dice
/ number:(float / integer) {
return {
type: "number", value: number
}
}
/ __ "(" __ content:additive __ ")" __ {
return {
type: "parenexpr", content: content
}
}
dice "dice"
= times:integer? sides:sides modifiers:modifiers? {
var times = times == null ? 1 : times;
var modifiers = modifiers == null ? [] : modifiers;
return {
type: "dice", times: times, sides: sides, modifiers: modifiers
}
}
sides
= __ "d"i __ sides:integer {
return sides;
}
/ __ "d"i __ "f"i __ {
return -1;
}
/ __ "d"i __ {
return 6;
}
modifiers
= modifier:modifier+ dropper:dropper+ {
return modifier.concat(dropper);
}
/ modifier+
/ dropper+
modifier
= keep
/ highest
/ lowest
dropper
= drophighest
/ droplowest
drophighest
= __ "-" __ highest:highest {
var holds = highest.holds == null ? 1 : highest.holds;
return {
type: "drophighest", holds: holds
}
}
droplowest
= __ "-" __ lowest:lowest {
var holds = lowest.holds == null ? 1 : lowest.holds;
return {
type: "droplowest", holds: holds
}
}
keep
= __ "k"i __ holds:integer? {
var holds = holds == null ? 1 : holds;
return {
type: "keep", holds: holds
}
}
highest
= __ "h"i __ holds:integer? {
var holds = holds == null ? 1 : holds;
return {
type: "highest", holds: holds
}
}
lowest
= __ "l"i __ holds:integer? {
var holds = holds == null ? 1 : holds;
return {
type: "lowest", holds: holds
}
}
integer "integer"
= __ [0-9]+ __ {
return parseInt(text(), 10);
}
float "float"
= __ [0-9]+ "." [0-9]* exponent? __ {
return parseFloat(text());
}
/ __ digits:("." [0-9]+ exponent?) __ {
return parseFloat(text());
}
/ __ digits:([0-9]+ exponent) __ {
return parseFloat(text());
}
exponent
= "e"i [+-]? [0-9]+
__
= (WS / EOL)*
WS "whitespace"
= "\t"
/ "\v"
/ "\f"
/ " "
/ "\u00A0"
/ "\uFEFF"
/ [\u0020\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]
EOL "end of line"
= "\n"
/ "\r\n"
/ "\r"
/ "\u2028"
/ "\u2029"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment