git.iko.soy/rowtype-yoga/purescript-native
on f96c9a1 help sign in
75 lines 2.8 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 , "type"
27 , "var"
28 , "while"
29 , "for"
30 , "if"
31 , "then"
32 , "else"
33 , "return"
34 , "true"
35 , "false"
36 , "extern"
37 , "forall" ]
38 }
40tokenParser = PT.makeTokenParser langDef
41lexeme = PT.lexeme tokenParser
42identifier = PT.identifier tokenParser
43reserved = PT.reserved tokenParser
44operator = PT.operator tokenParser
45stringLiteral = PT.stringLiteral tokenParser
46natural = PT.natural tokenParser
47naturalOrFloat = PT.naturalOrFloat tokenParser
48whiteSpace = PT.whiteSpace tokenParser
49parens = PT.parens tokenParser
50braces = PT.braces tokenParser
51angles = PT.angles tokenParser
52brackets = PT.brackets tokenParser
53squares = PT.squares tokenParser
54semi = PT.semi tokenParser
55comma = PT.comma tokenParser
56colon = PT.colon tokenParser
57dot = PT.dot tokenParser
58semiSep = PT.semiSep tokenParser
59semiSep1 = PT.semiSep1 tokenParser
60commaSep = PT.commaSep tokenParser
61commaSep1 = PT.commaSep1 tokenParser
63properName :: P.Parsec String u String
64properName = lexeme $ P.try properName'
65 where
66 properName' = (:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name"
68augment :: 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
69augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
71fold :: 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
72fold first more combine = do
73 a <- first
74 bs <- P.many more
75 return $ foldl combine a bs