diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-14 21:15:29 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-14 21:15:29 +0530 |
| commit | 950faa740b86af5f264717898f24ae8fb65c036d (patch) | |
| tree | 86b3e226f8efa9b0004b31a0edf9b852716a59db /src/parser/index.ts | |
| parent | 3b0e7428441f85de5e76a59abbb09931bcf2387b (diff) | |
| download | elxr-950faa740b86af5f264717898f24ae8fb65c036d.tar.gz elxr-950faa740b86af5f264717898f24ae8fb65c036d.zip | |
feat: implements string literal parser
Diffstat (limited to '')
| -rw-r--r-- | src/parser/index.ts | 22 |
1 files changed, 19 insertions, 3 deletions
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<Expr>) => ParserResult<Expr> = const propRegex = /^[A-Za-z0-9_-]$/ export const propertyName: Parser<string> = 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<Literal> = mapTo(oneOf(['true', 'false']), b => Literal.Boolean(b === 'true' ? true : false), ) +const stringDelimiter = matchChar('"') + +const stringLiteral: Parser<Literal> = mapTo( + prefixed( + stringDelimiter, + manyTill( + satisfyChar(_ => true), + stringDelimiter, + ), + ), + s => Literal.String(s.join('')), +) + const literalP: Parser<Literal> = delimited( whitespaces0, - or([booleanLiteral, numberLiteral]), + or([booleanLiteral, numberLiteral, stringLiteral]), whitespaces0, ) |
