aboutsummaryrefslogtreecommitdiff
path: root/src/parser
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-24 22:06:03 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-24 22:06:03 +0530
commitea0baf1bb82d40ebfcaca1de6a2a0f6d56c709e7 (patch)
tree8df0853d6f081bb730284bb2256c1b12ec43d62f /src/parser
parentf380df108a08bed22b92b9c5355d0b67168375be (diff)
downloadelxr-ea0baf1bb82d40ebfcaca1de6a2a0f6d56c709e7.tar.gz
elxr-ea0baf1bb82d40ebfcaca1de6a2a0f6d56c709e7.zip
feat(parser): implements regex literal parser
Diffstat (limited to 'src/parser')
-rw-r--r--src/parser/index.ts15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/parser/index.ts b/src/parser/index.ts
index a336b83..7f0e945 100644
--- a/src/parser/index.ts
+++ b/src/parser/index.ts
@@ -129,9 +129,22 @@ const stringLiteral: Parser<Literal> = mapTo(
s => Literal.String(s.join('')),
)
+const REGEX_FLAGS = ['m', 'g', 'i', 'd', 's', 'u', 'y']
+const regexDelimiter = prefixed(optional(matchChar('\\')), matchChar('/'))
+const regexLiteral: Parser<Literal> = mapTo(
+ prefixed(
+ regexDelimiter,
+ pair(
+ manyTill(satisfyChar(_ => true), regexDelimiter),
+ many0(satisfyChar(c => REGEX_FLAGS.includes(c)))
+ ),
+ ),
+ ([rs, flags]) => Literal.RegExp(new RegExp(rs.join(''), flags.join(''))),
+)
+
const literalP: Parser<Literal> = delimited(
whitespaces0,
- or([booleanLiteral, numberLiteral, stringLiteral]),
+ or([booleanLiteral, numberLiteral, stringLiteral, regexLiteral]),
whitespaces0,
)