String escaping using backslash

This commit is contained in:
James Eversole 2025-04-15 10:52:53 -05:00
parent f2beb86d8a
commit 25bfe139e8
3 changed files with 33 additions and 8 deletions

View File

@ -41,7 +41,6 @@ tricuLexer = do
, try stringLiteral
, assign
, colon
, backslash
, openParen
, closeParen
, openBracket
@ -94,9 +93,6 @@ assign = char '=' $> LAssign
colon :: Lexer LToken
colon = char ':' $> LColon
backslash :: Lexer LToken
backslash = char '\\' $> LBackslash
openParen :: Lexer LToken
openParen = char '(' $> LOpenParen
@ -126,7 +122,22 @@ integerLiteral = do
stringLiteral :: Lexer LToken
stringLiteral = do
char '"'
content <- many (noneOf ['"'])
char '"' --"
content <- manyTill Lexer.charLiteral (char '"')
return (LStringLiteral content)
charLiteral :: Lexer Char
charLiteral = escapedChar <|> normalChar
where
normalChar = noneOf ['"', '\\']
escapedChar = do
void $ char '\\'
c <- oneOf ['n', 't', 'r', 'f', 'b', '\\', '"', '\'']
return $ case c of
'n' -> '\n'
't' -> '\t'
'r' -> '\r'
'f' -> '\f'
'b' -> '\b'
'\\' -> '\\'
'"' -> '"'
'\'' -> '\''

View File

@ -38,7 +38,6 @@ data LToken
| LAssign
| LColon
| LDot
| LBackslash
| LOpenParen
| LCloseParen
| LOpenBracket

View File

@ -51,7 +51,22 @@ lexer = testGroup "Lexer Tests"
, testCase "Lex escaped characters in strings" $ do
let input = "\"hello\\nworld\""
expect = Right [LStringLiteral "hello\\nworld"]
expect = Right [LStringLiteral "hello\nworld"]
runParser tricuLexer "" input @?= expect
, testCase "Lex multiple escaped characters in strings" $ do
let input = "\"tab:\\t newline:\\n quote:\\\" backslash:\\\\\""
expect = Right [LStringLiteral "tab:\t newline:\n quote:\" backslash:\\"]
runParser tricuLexer "" input @?= expect
, testCase "Lex escaped characters in string literals" $ do
let input = "x = \"line1\\nline2\\tindented\""
expect = Right [LIdentifier "x", LAssign, LStringLiteral "line1\nline2\tindented"]
runParser tricuLexer "" input @?= expect
, testCase "Lex empty string with escape sequence" $ do
let input = "\"\\\"\""
expect = Right [LStringLiteral "\""]
runParser tricuLexer "" input @?= expect
, testCase "Lex mixed literals" $ do