git.iko.soy/rowtype-yoga/purescript-native
on 0c961be help sign in
87 lines 3.2 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
22import PureScript.Names
24langDef = PL.haskellStyle
25 { PT.identStart = P.lower <|> P.char '_'
26 , PT.reservedNames = [ "case"
27 , "data"
28 , "type"
29 , "var"
30 , "while"
31 , "for"
32 , "if"
33 , "then"
34 , "else"
35 , "return"
36 , "true"
37 , "false"
38 , "extern"
39 , "forall" ]
40 , PT.reservedOpNames = [ "!", "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
41 , "==", "!=", "&", "^", "|", "&&", "||" ]
42 }
44tokenParser = PT.makeTokenParser langDef
45lexeme = PT.lexeme tokenParser
46identifier = PT.identifier tokenParser
47reserved = PT.reserved tokenParser
48operator = PT.operator tokenParser
49stringLiteral = PT.stringLiteral tokenParser
50natural = PT.natural tokenParser
51naturalOrFloat = PT.naturalOrFloat tokenParser
52whiteSpace = PT.whiteSpace tokenParser
53parens = PT.parens tokenParser
54braces = PT.braces tokenParser
55angles = PT.angles tokenParser
56brackets = PT.brackets tokenParser
57squares = PT.squares tokenParser
58semi = PT.semi tokenParser
59comma = PT.comma tokenParser
60colon = PT.colon tokenParser
61dot = PT.dot tokenParser
62semiSep = PT.semiSep tokenParser
63semiSep1 = PT.semiSep1 tokenParser
64commaSep = PT.commaSep tokenParser
65commaSep1 = PT.commaSep1 tokenParser
67tick = lexeme (P.char '`')
69properName :: P.Parsec String u String
70properName = lexeme $ P.try properName'
71 where
72 properName' = (:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name"
74augment :: 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
75augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
77fold :: 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
78fold first more combine = do
79 a <- first
80 bs <- P.many more
81 return $ foldl combine a bs
83parseIdent :: P.Parsec String () Ident
84parseIdent = (Ident <$> identifier) <|> (Op <$> parens operator)
86parseIdentInfix :: P.Parsec String () Ident
87parseIdentInfix = (Ident <$> P.between tick tick identifier) <|> (Op <$> operator)