git.iko.soy/rowtype-yoga/purescript-native
on 79aaba9 help sign in
73 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 , "extern" ]
36 }
38tokenParser = PT.makeTokenParser langDef
39lexeme = PT.lexeme tokenParser
40identifier = PT.identifier tokenParser
41reserved = PT.reserved tokenParser
42operator = PT.operator tokenParser
43stringLiteral = PT.stringLiteral tokenParser
44natural = PT.natural tokenParser
45naturalOrFloat = PT.naturalOrFloat tokenParser
46whiteSpace = PT.whiteSpace tokenParser
47parens = PT.parens tokenParser
48braces = PT.braces tokenParser
49angles = PT.angles tokenParser
50brackets = PT.brackets tokenParser
51squares = PT.squares tokenParser
52semi = PT.semi tokenParser
53comma = PT.comma tokenParser
54colon = PT.colon tokenParser
55dot = PT.dot tokenParser
56semiSep = PT.semiSep tokenParser
57semiSep1 = PT.semiSep1 tokenParser
58commaSep = PT.commaSep tokenParser
59commaSep1 = PT.commaSep1 tokenParser
61properName :: P.Parsec String u String
62properName = lexeme $ P.try properName'
63 where
64 properName' = (:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name"
66augment :: 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
67augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
69fold :: 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
70fold first more combine = do
71 a <- first
72 bs <- P.many more
73 return $ foldl combine a bs