1-----------------------------------------------------------------------------
3-- Module : PureScript.Parser.Common
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15{-# LANGUAGE FlexibleContexts #-}
17module PureScript.Parser.Common where
19import Data.Char (isSpace)
20import Control.Applicative
22import Control.Monad.State
23import qualified Text.Parsec as P
24import qualified Text.Parsec.Pos as P
25import qualified Text.Parsec.Token as PT
27import PureScript.Names
29reservedNames :: [String]
30reservedNames = [ "case"
47reservedOpNames :: [String]
48reservedOpNames = [ "!", "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
49 , "==", "!=", "&", "^", "|", "&&", "||" ]
51identStart :: P.Parsec String u Char
52identStart = P.lower <|> P.oneOf "_$"
54properNameStart :: P.Parsec String u Char
55properNameStart = P.upper
57identLetter :: P.Parsec String u Char
58identLetter = P.alphaNum <|> P.oneOf "_'"
60opStart :: P.Parsec String u Char
61opStart = P.oneOf "!#$%&*+/<=>?@^|-~"
63opLetter :: P.Parsec String u Char
64opLetter = P.oneOf ":#$%&*+./<=>?@^|"
66langDef = PT.LanguageDef
67 { PT.reservedNames = reservedNames
68 , PT.reservedOpNames = reservedOpNames
69 , PT.commentStart = "{-"
70 , PT.commentEnd = "-}"
71 , PT.commentLine = "--"
72 , PT.nestedComments = True
73 , PT.identStart = identStart
74 , PT.identLetter = identLetter
75 , PT.opStart = opStart
76 , PT.opLetter = opLetter
77 , PT.caseSensitive = True
80tokenParser = PT.makeTokenParser langDef
82lexeme = PT.lexeme tokenParser
83identifier = PT.identifier tokenParser
84reserved = PT.reserved tokenParser
85reservedOp = PT.reservedOp tokenParser
86operator = PT.operator tokenParser
87stringLiteral = PT.stringLiteral tokenParser
88whiteSpace = PT.whiteSpace tokenParser
89parens = PT.parens tokenParser
90braces = PT.braces tokenParser
91angles = PT.angles tokenParser
92squares = PT.squares tokenParser
93semi = PT.semi tokenParser
94comma = PT.comma tokenParser
95colon = PT.colon tokenParser
96dot = PT.dot tokenParser
97semiSep = PT.semiSep tokenParser
98semiSep1 = PT.semiSep1 tokenParser
99commaSep = PT.commaSep tokenParser
100commaSep1 = PT.commaSep1 tokenParser
102tick :: P.Parsec String u Char
103tick = lexeme $ P.char '`'
105properName :: P.Parsec String u String
106properName = lexeme $ P.try ((:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name")
108integerOrFloat :: P.Parsec String u (Either Integer Double)
109integerOrFloat = Left <$> P.try (PT.integer tokenParser) <|>
110 Right <$> P.try (PT.float tokenParser)
112augment :: 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
113augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
115fold :: 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
116fold first more combine = do
119 return $ foldl combine a bs
121parseIdent :: P.Parsec String u Ident
122parseIdent = (Ident <$> identifier) <|> (Op <$> parens operator)
124parseIdentInfix :: P.Parsec String u Ident
125parseIdentInfix = (Ident <$> P.between tick tick identifier) <|> (Op <$> operator)
127mark :: P.Parsec String P.Column a -> P.Parsec String P.Column a
129 current <- P.getState
130 pos <- P.sourceColumn <$> P.getPosition
136checkIndentation :: (P.Column -> P.Column -> Bool) -> P.Parsec String P.Column ()
137checkIndentation rel = (do
138 col <- P.sourceColumn <$> P.getPosition
139 current <- P.getState
140 guard $ col `rel` current) <|> P.parserFail "Indentation check"
142indented :: P.Parsec String P.Column ()
143indented = checkIndentation (>)
145same :: P.Parsec String P.Column ()
146same = checkIndentation (==)
148runIndentParser :: P.Column -> P.Parsec String P.Column a -> String -> Either P.ParseError a
149runIndentParser col p = P.runParser p 0 ""
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list