Last active
December 26, 2015 06:40
-
-
Save esnya/6a2fcedd31fffd7bfac1 to your computer and use it in GitHub Desktop.
ダイス式の構文
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DiceCommand: | |
Expression '=' | |
Expression '<' | |
Expression '>' | |
Expression '<=' | |
Expression '>=' | |
Expression: | |
Term | |
Expression '+' Term | |
Expression '-' Term | |
Term: | |
Factor | |
Term '*' Factor | |
Term '/' Factor | |
Factor: | |
Numeric | |
'(' Expression ')' | |
'-' Factor | |
DiceFactor | |
Numeric: | |
[0-9]+ | |
DiceFactor: | |
'd' | |
'd' Factor | |
Factor 'd' | |
Factor 'd' Factor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Parsimmon, { alt, any, digits, eof, regex, seq, string, lazy } from 'parsimmon'; | |
export let | |
DiceText, | |
DiceCommand, | |
DiceExpression, | |
Term, | |
Factor, | |
DiceFactor; | |
let plus = string('+'); | |
let minus = string('-'); | |
let mul = string('*'); | |
let div = string('/'); | |
let lparen = string('('); | |
let rparen = string(')'); | |
let d = string('d'); | |
let equal = string('='); | |
let lt = string('<'); | |
let gt = string('>'); | |
let le = string('<='); | |
let ge = string('>='); | |
DiceText = lazy(function() { | |
return seq( | |
alt(DiceCommand, any), | |
eof | |
); | |
}); | |
DiceCommand = lazy(function() { | |
return alt( | |
seq(DiceExpression, equal), | |
seq(DiceExpression, ge, DiceExpression), | |
seq(DiceExpression, le, DiceExpression), | |
seq(DiceExpression, gt, DiceExpression), | |
seq(DiceExpression, lt, DiceExpression) | |
); | |
}); | |
DiceExpression = lazy(function () { | |
return seq(Term, seq(alt(plus, minus), Term).many()); | |
}); | |
Factor = alt( | |
seq(plus, DiceExpression), | |
seq(minus, DiceExpression), | |
seq(lparen, DiceExpression, rparen), | |
digits.map(function(x) { return parseInt(x); }), | |
); | |
DiceFactor = alt( | |
seq(Factor, d, Factor), | |
seq(Factor, d), | |
seq(d, Factor), | |
d | |
); | |
Term = alt( | |
seq(Factor, mul, Factor), | |
seq(Factor, div, Factor), | |
DiceFactor, | |
Factor | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment