From f8733b1f75de27ee2da04d60aed81c6048afd296 Mon Sep 17 00:00:00 2001 From: Falco Peijnenburg Date: Sat, 15 Apr 2023 12:26:24 +0200 Subject: [PATCH] Fix lex error on string like "\z" --- src/GLua/Lexer.hs | 9 ++++++--- src/GLua/PSLexer.hs | 8 ++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/GLua/Lexer.hs b/src/GLua/Lexer.hs index a4b00a1..5063f6c 100644 --- a/src/GLua/Lexer.hs +++ b/src/GLua/Lexer.hs @@ -40,10 +40,13 @@ import Text.ParserCombinators.UU.BasicInstances -- | String parser that maintains positions. type LParser a = P (Str Char String LineColPos) a --- | Whitespace parser. +-- | Whitespace parser that requires at least one whitespace character parseWhitespace :: LParser String parseWhitespace = pSome $ pSatisfy (`elem` " \r\n\t") (Insertion "Whitespace" ' ' 5) - --MToken <$> pPos <*> (Whitespace <$> pMunch (`elem` " \r\n\t")) + +-- | Whitespace parser that requires 0 or more whitespace characters +parseOptionalWhitespace :: LParser String +parseOptionalWhitespace = pMany $ pSatisfy (`elem` " \r\n\t") (Insertion "Whitespace" ' ' 5) -- | Blanco parser. Parses anything. Used in parsing comments. parseAnyChar :: LParser Char @@ -148,7 +151,7 @@ parseLineString c = pSym c *> innerString (:) <$> pNoNewline <*> innerString -- the next character in the string escapeSequence :: LParser String - escapeSequence = (:) <$> pSym 'z' <*> parseWhitespace <<|> (:[]) <$> parseAnyChar + escapeSequence = (:) <$> pSym 'z' <*> parseOptionalWhitespace <<|> (:[]) <$> parseAnyChar pNoNewline :: LParser Char pNoNewline = pSatisfy (/= '\n') (Insertion "Anything but a newline" c 5) diff --git a/src/GLua/PSLexer.hs b/src/GLua/PSLexer.hs index ddb97d1..413e5b4 100644 --- a/src/GLua/PSLexer.hs +++ b/src/GLua/PSLexer.hs @@ -90,16 +90,20 @@ cBetween c left right = c >= left && c <= right pUntilEnd :: Parser String pUntilEnd = manyTill anyChar (lookAhead (endOfLine <|> const '\n' <$> eof)) --- | Whitespace parser. +-- | Whitespace parser that requires at least one whitespace character parseWhitespace :: Parser String parseWhitespace = many1 space "whitespace" +-- | Whitespace parser that requires 0 or more whitespace characters +parseOptionalWhitespace :: Parser String +parseOptionalWhitespace = many space "whitespace" + -- | An escaped character of a single line string -- Takes the delimiter of the string as parameter escapedStringChar :: Char -> Parser String escapedStringChar delim = (\escaped -> '\\' : escaped) <$ char '\\' <*> escapedChar <|> (:[]) <$> noneOf ['\n', delim] where - escapedChar = (:) <$> char 'z' <*> parseWhitespace <|> (:[]) <$> anyChar + escapedChar = (:) <$> char 'z' <*> parseOptionalWhitespace <|> (:[]) <$> anyChar -- | The start of a nested string -- Returns the amount of =-signs at the start of the nested string