forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roll-grammar.peggy
169 lines (145 loc) · 4.39 KB
/
roll-grammar.peggy
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{{
function createNumericTerm(number, { flavor = null, truncate = false } = {}) {
number = truncate ? Math.trunc(number) : number;
const formula = flavor ? `${number}[${flavor}]` : number.toString();
const obj = {
class: "NumericTerm",
number,
formula,
evaluated: false,
};
if (flavor) obj.options = { flavor };
return obj;
}
}}
Expression = head:(Pool / Term) tail:(_ ("+" / "-") _ (Pool / Term))* {
return tail.reduce((result, elements) => {
const operator = elements[1];
const operands = [
result,
elements.find((e) => typeof e === "number" || e instanceof Object && e.class)
].map((e) => typeof e === "number" ? createNumericTerm(e, { truncate: true }) : e);
return {
class: "ArithmeticExpression",
formula: text(),
operator,
operands,
evaluated: false,
};
}, head);
};
Term = head:TermOperand tail:(_ ("*" / "/") _ TermOperand)* {
return tail.reduce((result, elements) => {
const operator = elements[1];
const operands = [
result,
elements.find((e) => typeof e === "number" || e instanceof Object && e.class)
].map((e) => typeof e === "number" ? createNumericTerm(e) : e);
return {
class: "ArithmeticExpression",
formula: text(),
operator,
operands,
evaluated: false,
};
}, head);
};
TermOperand = DiceTerm / FunctionTerm / Grouping / FlavoredNumber / Number;
FunctionTerm = fn:Identifier "(" _ head:Expression? _ tail:(_ "," _ @Expression)* _ ")" {
const terms = head !== null
? [head, ...tail].map((t) => typeof t === "number" ? t.toString() : t.formula)
: [];
return {
class: "FunctionTerm",
formula: text(),
fn,
terms,
evaluated: false,
};
};
DiceTerm = number:(Grouping / FunctionTerm / Number)? [dD] faces:(Grouping / FunctionTerm / Number / Identifier) modifiers:Modifiers? flavor:Flavor? {
const obj =
number instanceof Object || faces instanceof Object
? {
class: "IntermediateDie",
formula: text(),
number: typeof number === "number" ? createNumericTerm(number) : number,
faces: typeof faces === "number" ? createNumericTerm(faces) : faces,
evaluated: false,
}
: {
class: "DiceTerm",
formula: text(),
number: number ?? 1,
faces,
evaluated: false,
};
if (modifiers) obj.modifiers = modifiers;
if (flavor) obj.options = { flavor };
return obj;
};
Grouping = "(" _ expression:Expression _ ")" flavor:Flavor? {
const obj = {
class: "Grouping",
formula: text(),
term: typeof expression === "number" ? createNumericTerm(expression) : expression,
evaluated: false,
};
if (flavor) obj.options = { flavor };
return obj;
};
Pool = "{" _ head:Expression tail:("," _ @Expression)* "}" modifiers:Modifiers? flavor:Flavor? {
const headTerm = typeof head === "number" ? createNumericTerm(head) : head;
const tailTerms = tail.map((t) => typeof t === "number" ? createNumericTerm(t) : t);
const terms = [headTerm.formula, ...tailTerms.map((t) => t.formula)];
const firstRoll = {
class: "DamageInstance",
formula: headTerm.formula,
terms: [headTerm],
evaluated: false
};
if (headTerm.options?.flavor) {
firstRoll.options = { flavor: headTerm.options.flavor };
}
const rolls = [
firstRoll,
...tailTerms.map((term) => {
const obj = {
class: "DamageInstance",
formula: term.formula,
terms: [term],
evaluated: false,
};
if (term.options?.flavor) {
obj.options = { flavor: term.options.flavor };
}
return obj;
}),
];
const obj = {
class: "InstancePool",
formula: text(),
terms,
rolls,
evaluated: false,
};
if (flavor) obj.options = { flavor };
if (modifiers) obj.modifiers = modifiers;
return obj;
};
Flavor = "[" string:$(FlavorString) "]" {
return string;
};
Number = _ [-+]? [0-9]+ ("." [0-9]+)? {
const numified = Number(text());
return input === text() ? createNumericTerm(numified) : numified;
};
FlavoredNumber = number:Number flavor:Flavor {
return createNumericTerm(number, { flavor });
};
Identifier = $([a-z$_]i [a-z$_0-9]i*);
Modifiers = [^ (){}\[\]+\-*/,]+ {
return Array.from(text().matchAll(/([A-z]+)([^A-z\s()+\-*\/]+)?/g)).map((m) => m[0]);
};
FlavorString "string" = [^\[\]]+;
_ "whitespace" = [ ]*;