Rain: fixed the parsing to pass the new literal tests

This commit is contained in:
Neil Brown 2007-08-23 15:51:31 +00:00
parent bb77f3fb91
commit 1d0426ac77
2 changed files with 11 additions and 3 deletions

View File

@ -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

View File

@ -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