git.iko.soy/rowtype-yoga/purescript-native
on a10616f help sign in
72 lines 2.7 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : PureScript.Parser.Common
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15module PureScript.Parser.Common where
17import Control.Applicative
18import qualified Text.Parsec as P
19import qualified Text.Parsec.Token as PT
20import qualified Text.Parsec.Language as PL
22langDef = PL.haskellStyle
23 { PT.identStart = P.lower <|> P.char '_'
24 , PT.reservedNames = [ "case"
25 , "data"
26 , "var"
27 , "while"
28 , "for"
29 , "if"
30 , "then"
31 , "else"
32 , "return"
33 , "true"
34 , "false" ]
35 }
37tokenParser = PT.makeTokenParser langDef
38lexeme = PT.lexeme tokenParser
39identifier = PT.identifier tokenParser
40reserved = PT.reserved tokenParser
41operator = PT.operator tokenParser
42stringLiteral = PT.stringLiteral tokenParser
43natural = PT.natural tokenParser
44naturalOrFloat = PT.naturalOrFloat tokenParser
45whiteSpace = PT.whiteSpace tokenParser
46parens = PT.parens tokenParser
47braces = PT.braces tokenParser
48angles = PT.angles tokenParser
49brackets = PT.brackets tokenParser
50squares = PT.squares tokenParser
51semi = PT.semi tokenParser
52comma = PT.comma tokenParser
53colon = PT.colon tokenParser
54dot = PT.dot tokenParser
55semiSep = PT.semiSep tokenParser
56semiSep1 = PT.semiSep1 tokenParser
57commaSep = PT.commaSep tokenParser
58commaSep1 = PT.commaSep1 tokenParser
60properName :: P.Parsec String u String
61properName = lexeme $ P.try properName'
62 where
63 properName' = (:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name"
65augment :: P.Stream s m t => P.ParsecT s u m a -> P.ParsecT s u m b -> (a -> b -> a) -> P.ParsecT s u m a
66augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
68fold :: P.Stream s m t => P.ParsecT s u m a -> P.ParsecT s u m b -> (a -> b -> a) -> P.ParsecT s u m a
69fold first more combine = do
70 a <- first
71 bs <- P.many more
72 return $ foldl combine a bs