escape regex tokens
This commit is contained in:
parent
01cf81f930
commit
0b163e8ffd
24
calc.js
24
calc.js
@ -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({
|
||||
|
Loading…
Reference in New Issue
Block a user