escape regex tokens

This commit is contained in:
xeals 2023-04-27 11:57:44 +10:00
parent 01cf81f930
commit 0b163e8ffd
Signed by: xeals
GPG Key ID: A498C7AF27EC6B5C

24
calc.js
View File

@ -12,6 +12,14 @@ import { createToken, Lexer } from "chevrotain";
const gen = Generations.get(9);
/**
* @param {string} text
* @return {string}
*/
function escapeRegExp(text) {
return text.replace(/[\\^$*+?.()|[\]{}]/g, "\\$&");
}
/**
* Creates a lexer.
*
@ -27,20 +35,28 @@ function buildLexer() {
const Item = createToken({
name: "Item",
pattern: new RegExp([...gen.items].map((i) => i.name).join("|")),
pattern: new RegExp(
[...gen.items].map((i) => escapeRegExp(i.name)).join("|")
),
});
const Ability = createToken({
name: "Ability",
pattern: new RegExp([...gen.abilities].map((a) => a.name).join("|")),
pattern: new RegExp(
[...gen.abilities].map((a) => escapeRegExp(a.name)).join("|")
),
});
const Pokemon = createToken({
name: "Pokemon",
pattern: new RegExp([...gen.species].map((s) => s.name).join("|")),
pattern: new RegExp(
[...gen.species].map((s) => escapeRegExp(s.name)).join("|")
),
});
const Move = createToken({
name: "Move",
pattern: new RegExp([...gen.moves].map((m) => m.name).join("|")),
pattern: new RegExp(
[...gen.moves].map((m) => escapeRegExp(m.name)).join("|")
),
});
const whitespace = createToken({