summaryrefslogtreecommitdiff
path: root/src/paca.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/paca.mjs')
-rw-r--r--src/paca.mjs49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/paca.mjs b/src/paca.mjs
index ae14538..42c2f88 100644
--- a/src/paca.mjs
+++ b/src/paca.mjs
@@ -15,7 +15,7 @@ const ConcatStep = {
CLASS: "class",
};
-const nonConcatOperators = new Set(["*", "+", "?", "|", ")"]);
+const nonConcatOperators = new Set(["*", "+", "?", "|", ")", "$"]);
const shouldConcat = (char, next) =>
next !== undefined &&
@@ -272,6 +272,44 @@ const TRANSITION_FNS = {
}),
};
+const ANCHOR_FNS = {
+ "^": ({ out, state, context }, _char, index, _next) =>
+ index !== 0
+ ? reduced({
+ out,
+ state,
+ context,
+ error: new SyntaxError(
+ "^ not at the start of the expression",
+ ),
+ })
+ : {
+ out: out.concat({ operator: "caret" }),
+ state,
+ context,
+ },
+ "$": ({ out, state, context }, _char, _index, next) =>
+ next !== undefined
+ ? reduced({
+ out,
+ state,
+ context,
+ error: new SyntaxError(
+ "$ not at the end of the expression",
+ ),
+ })
+ : {
+ out: out.concat({ operator: "dollar" }),
+ state,
+ context,
+ },
+};
+
+const anchors = new Set(Object.keys(ANCHOR_FNS));
+
+const isAnchor = char =>
+ anchors.has(char);
+
const stateTransitionOperators = new Set(Object.keys(TRANSITION_FNS));
const isOperator = char =>
@@ -298,6 +336,15 @@ const tokenizeRegexStep = chars => ({ out, state, context }, char, index) => {
);
}
+ if (isAnchor(char)) {
+ return ANCHOR_FNS[char](
+ { out, state, context },
+ char,
+ index,
+ next,
+ );
+ }
+
const op = isOperator(char) ? { operator: char } : char;
return {
out: out.concat(