String escaping using backslash
This commit is contained in:
		
							
								
								
									
										23
									
								
								src/Lexer.hs
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								src/Lexer.hs
									
									
									
									
									
								
							| @ -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' | ||||
|         '\\' -> '\\' | ||||
|         '"'  -> '"' | ||||
|         '\'' -> '\'' | ||||
|  | ||||
| @ -38,7 +38,6 @@ data LToken | ||||
|   | LAssign | ||||
|   | LColon | ||||
|   | LDot | ||||
|   | LBackslash | ||||
|   | LOpenParen | ||||
|   | LCloseParen | ||||
|   | LOpenBracket | ||||
|  | ||||
							
								
								
									
										17
									
								
								test/Spec.hs
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								test/Spec.hs
									
									
									
									
									
								
							| @ -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 | ||||
|  | ||||
		Reference in New Issue
	
	Block a user