-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.jison
110 lines (91 loc) · 2.18 KB
/
grammar.jison
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* description: Parses and executes mathematical expressions. */
%{
let Mods = require('./dice_modifiers.js')
let RollInfo = require('./roll_info.js').RollInfo
let rolling = require('./rolling.js')
%}
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
"help" return 'HELP'
"!".* return 'COMMENT'
"r"[0-9]+ return 'REROLL'
("adv") return 'ADVANTAGE'
("dis") return 'DISADVANTAGE'
d[0-9]+ return 'SHORTDICE'
[0-9]+d[0-9]+ return 'DICE'
[0-9]+("."[0-9]+)? return 'NUMBER'
"-" return '-'
"+" return '+'
"!" return '!'
"(" return '('
")" return ')'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
/* operator associations and precedence */
%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS
%start expressions
%% /* language grammar */
expressions
: e end
{ return $2 + $1.get_output(); }
| help end
{ return $1; }
;
help
: HELP
{$$ = "for help!\nSee: https://ltibbetts.github.io/dungeon-dice/";}
;
e
: e '+' e
{$$ = $1.add_other($3);}
| e '-' e
{$$ = $1.subtract_other($3);}
| '(' e ')'
{$$ = $2;}
| mod
{$$ = $1;}
| dice
{$$ = $1;}
;
mod
: NUMBER
{$$ = new RollInfo(Number(yytext), []);}
;
dice
: DICE
{$$ = rolling.roll($1)}
| SHORTDICE
{$$ = rolling.roll($1)}
| DICE dice_mods
{$$ = rolling.rollWithMods($1, $2)}
| SHORTDICE dice_mods
{$$ = rolling.rollWithMods($1, $2)}
;
dice_mods
: dice_mods dice_mod
{$$ = $1.concat($2)}
| dice_mod
{$$ = $1}
;
dice_mod
: REROLL
{$$ = [new Mods.Reroll(yytext)]}
| ADVANTAGE
{$$ = [new Mods.Advantage()]}
| DISADVANTAGE
{$$ = [new Mods.Disadvantage()]}
;
end
: COMMENT EOF
{$$ = "``" + $1.substring(1).trim() + "``";}
| EOF
{$$ = ""}
;