Skip to content

Commit

Permalink
Adding more robust variable syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bytbox committed Apr 22, 2012
1 parent 4e4a69f commit 2907197
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
all: main

main: main.hs
main: main.hs Text/Format.lhs
ghc --make $@

clean:
Expand Down
8 changes: 6 additions & 2 deletions Text/Format.lhs
Expand Up @@ -12,10 +12,14 @@

> ident :: Parsec String () FormatPart
> ident = do
> char '$'
> x <- many1 (alphaNum <|> char '_')
> return $ Var x

> variable =
> try (char '$' >> ident)
> <|>
> try (char '$' >> between (char '{') (char '}') ident)

> special = do
> char '$' >> char '$'
> return $ Raw '$'
Expand All @@ -25,7 +29,7 @@

> formatParser :: Parsec String () Format
> formatParser = do
> xs <- many (try raw <|> try special <|> ident)
> xs <- many (try raw <|> try special <|> variable)
> eof
> return xs

Expand Down
2 changes: 1 addition & 1 deletion main.hs
Expand Up @@ -6,7 +6,7 @@ import Data.Maybe (fromJust)

import Text.Format

formatString = "$p1 - $p2 [$$$cost]"
formatString = "$p1 - ${p2} [$$$cost]"

data Record = Record
{ p1 :: Int
Expand Down

0 comments on commit 2907197

Please sign in to comment.