String escaping using backslash
This commit is contained in:
parent
f2beb86d8a
commit
25bfe139e8
23
src/Lexer.hs
23
src/Lexer.hs
@ -41,7 +41,6 @@ tricuLexer = do
|
|||||||
, try stringLiteral
|
, try stringLiteral
|
||||||
, assign
|
, assign
|
||||||
, colon
|
, colon
|
||||||
, backslash
|
|
||||||
, openParen
|
, openParen
|
||||||
, closeParen
|
, closeParen
|
||||||
, openBracket
|
, openBracket
|
||||||
@ -94,9 +93,6 @@ assign = char '=' $> LAssign
|
|||||||
colon :: Lexer LToken
|
colon :: Lexer LToken
|
||||||
colon = char ':' $> LColon
|
colon = char ':' $> LColon
|
||||||
|
|
||||||
backslash :: Lexer LToken
|
|
||||||
backslash = char '\\' $> LBackslash
|
|
||||||
|
|
||||||
openParen :: Lexer LToken
|
openParen :: Lexer LToken
|
||||||
openParen = char '(' $> LOpenParen
|
openParen = char '(' $> LOpenParen
|
||||||
|
|
||||||
@ -126,7 +122,22 @@ integerLiteral = do
|
|||||||
stringLiteral :: Lexer LToken
|
stringLiteral :: Lexer LToken
|
||||||
stringLiteral = do
|
stringLiteral = do
|
||||||
char '"'
|
char '"'
|
||||||
content <- many (noneOf ['"'])
|
content <- manyTill Lexer.charLiteral (char '"')
|
||||||
char '"' --"
|
|
||||||
return (LStringLiteral content)
|
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'
|
||||||
|
'\\' -> '\\'
|
||||||
|
'"' -> '"'
|
||||||
|
'\'' -> '\''
|
||||||
|
@ -38,7 +38,6 @@ data LToken
|
|||||||
| LAssign
|
| LAssign
|
||||||
| LColon
|
| LColon
|
||||||
| LDot
|
| LDot
|
||||||
| LBackslash
|
|
||||||
| LOpenParen
|
| LOpenParen
|
||||||
| LCloseParen
|
| LCloseParen
|
||||||
| LOpenBracket
|
| LOpenBracket
|
||||||
|
17
test/Spec.hs
17
test/Spec.hs
@ -51,7 +51,22 @@ lexer = testGroup "Lexer Tests"
|
|||||||
|
|
||||||
, testCase "Lex escaped characters in strings" $ do
|
, testCase "Lex escaped characters in strings" $ do
|
||||||
let input = "\"hello\\nworld\""
|
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
|
runParser tricuLexer "" input @?= expect
|
||||||
|
|
||||||
, testCase "Lex mixed literals" $ do
|
, testCase "Lex mixed literals" $ do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user