From 950faa740b86af5f264717898f24ae8fb65c036d Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 14 Jan 2022 21:15:29 +0530 Subject: feat: implements string literal parser --- src/parser/index.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/parser/index.ts') diff --git a/src/parser/index.ts b/src/parser/index.ts index a2276bb..2db91a4 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -5,15 +5,18 @@ import { digits, many0, many1, + manyTill, mapTo, matchChar, + matchString, oneOf, optional, or, pair, Parser, ParserResult, - satifyChar, + prefixed, + satisfyChar, sepBy1, suffixed, symbol, @@ -49,7 +52,7 @@ const wrapQuantifiers: (e: ParserResult) => ParserResult = const propRegex = /^[A-Za-z0-9_-]$/ export const propertyName: Parser = pipe( - satifyChar(c => propRegex.test(c)), + satisfyChar(c => propRegex.test(c)), many1, p => mapTo(p, xs => xs.join('')), p => suffixed(p, whitespaces0), @@ -94,9 +97,22 @@ const booleanLiteral: Parser = mapTo(oneOf(['true', 'false']), b => Literal.Boolean(b === 'true' ? true : false), ) +const stringDelimiter = matchChar('"') + +const stringLiteral: Parser = mapTo( + prefixed( + stringDelimiter, + manyTill( + satisfyChar(_ => true), + stringDelimiter, + ), + ), + s => Literal.String(s.join('')), +) + const literalP: Parser = delimited( whitespaces0, - or([booleanLiteral, numberLiteral]), + or([booleanLiteral, numberLiteral, stringLiteral]), whitespaces0, ) -- cgit v1.3.1