From 1d0426ac77a35ed504d97fde543447b414968db5 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Thu, 23 Aug 2007 15:51:31 +0000 Subject: [PATCH] Rain: fixed the parsing to pass the new literal tests --- LexRain.x | 3 ++- RainParse.hs | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/LexRain.x b/LexRain.x index 8791e37..b01b023 100644 --- a/LexRain.x +++ b/LexRain.x @@ -42,6 +42,7 @@ $hexDigit = [0-9 a-f A-F] | "sint8" | "sint16" | "sint32" | "sint64" | "uint8" | "uint16" | "uint32" | "uint64" | "int" | "bool" + | "true" | "false" @identifier = [a-z A-Z _] [a-z A-Z 0-9 _]* @@ -49,7 +50,7 @@ $hexDigit = [0-9 a-f A-F] $escapeChar = [cnrts \" \' \\ \n] @escape = \\ ( $escapeChar | \# $hexDigit $hexDigit ) -@stringLiteral = \" ( @escape | [^\"] )* \" +@stringLiteral = \" ( @escape | [^\\\"] )* \" @charLiteral = \' ( @escape | [^\'] ) \' -- Note that occam number literals don't include their signs -- if you say diff --git a/RainParse.hs b/RainParse.hs index 1ab5e88..d3847dd 100644 --- a/RainParse.hs +++ b/RainParse.hs @@ -141,12 +141,17 @@ variableId = do {v <- name ; return $ A.Variable (findMeta v) v} stringLiteral :: RainParser (A.LiteralRepr, A.Dimension) stringLiteral = do (m,str) <- getToken testToken - let aes = [A.ArrayElemExpr $ A.Literal m A.Byte $ A.ByteLiteral m [c] | c <- str] - return (A.ArrayLiteral m aes, A.Dimension $ length str) + let processed = replaceEscapes str + let aes = [A.ArrayElemExpr $ A.Literal m A.Byte $ A.ByteLiteral m [c] | c <- processed] + return (A.ArrayLiteral m aes, A.Dimension $ length processed) "string literal" where testToken (L.TokStringLiteral str) = Just str testToken _ = Nothing + replaceEscapes :: String -> String + replaceEscapes [] = [] + replaceEscapes ('\\':(c:cs)) = if c == 'n' then ('\n':replaceEscapes cs) else (c:replaceEscapes cs) + replaceEscapes (c:cs) = (c:replaceEscapes cs) literalCharacter :: RainParser A.LiteralRepr literalCharacter @@ -167,6 +172,8 @@ integer literal :: RainParser A.Expression literal = do {(lr, dim) <- stringLiteral ; return $ A.Literal (findMeta lr) (A.Array [dim] A.Byte) lr } <|> do {i <- integer ; return $ A.Literal (findMeta i) A.Int i} + <|> do {m <- reserved "true" ; return $ A.True m} + <|> do {m <- reserved "false" ; return $ A.False m} "literal" expression :: RainParser A.Expression