From ea0baf1bb82d40ebfcaca1de6a2a0f6d56c709e7 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 24 Jan 2022 22:06:03 +0530 Subject: feat(parser): implements regex literal parser --- src/parser/index.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/parser') 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 = 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 = 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 = delimited( whitespaces0, - or([booleanLiteral, numberLiteral, stringLiteral]), + or([booleanLiteral, numberLiteral, stringLiteral, regexLiteral]), whitespaces0, ) -- cgit v1.3.1