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"
67reservedOpNames :: [String]
68reservedOpNames = [ "!", "~", "-", "<=", ">=", "<", ">", "*", "/", "%", "++", "+", "<<", ">>>", ">>"
69 , "==", "!=", "&", "^", "|", "&&", "||" ]
71identStart :: P.Parsec String u Char
72identStart = P.lower <|> P.oneOf "_$"
74properNameStart :: P.Parsec String u Char
75properNameStart = P.upper
77identLetter :: P.Parsec String u Char
78identLetter = P.alphaNum <|> P.oneOf "_'"
80opStart :: P.Parsec String u Char
81opStart = P.oneOf "!#$%&*+/<=>?@^|-~"
83opLetter :: P.Parsec String u Char
84opLetter = P.oneOf ":#$%&*+./<=>?@^|"
86langDef = PT.LanguageDef
87 { PT.reservedNames = reservedNames
88 , PT.reservedOpNames = reservedOpNames
89 , PT.commentStart = "{-"
90 , PT.commentEnd = "-}"
91 , PT.commentLine = "--"
92 , PT.nestedComments = True
93 , PT.identStart = identStart
94 , PT.identLetter = identLetter
95 , PT.opStart = opStart
96 , PT.opLetter = opLetter
97 , PT.caseSensitive = True
100tokenParser = PT.makeTokenParser langDef
102lexeme = PT.lexeme tokenParser
103identifier = PT.identifier tokenParser
104reserved = PT.reserved tokenParser
105reservedOp = PT.reservedOp tokenParser
106operator = PT.operator tokenParser
107stringLiteral = PT.stringLiteral tokenParser
108whiteSpace = PT.whiteSpace tokenParser
109parens = PT.parens tokenParser
110braces = PT.braces tokenParser
111angles = PT.angles tokenParser
112squares = PT.squares tokenParser
113semi = PT.semi tokenParser
114comma = PT.comma tokenParser
115colon = PT.colon tokenParser
116dot = PT.dot tokenParser
117semiSep = PT.semiSep tokenParser
118semiSep1 = PT.semiSep1 tokenParser
119commaSep = PT.commaSep tokenParser
120commaSep1 = PT.commaSep1 tokenParser
122tick :: P.Parsec String u Char
123tick = lexeme $ P.char '`'
125properName :: P.Parsec String u String
126properName = lexeme $ P.try ((:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name")
128integerOrFloat :: P.Parsec String u (Either Integer Double)
129integerOrFloat = Left <$> P.try (PT.integer tokenParser) <|>
130 Right <$> P.try (PT.float tokenParser)
132augment :: 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
133augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
135fold :: 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
136fold first more combine = do
139 return $ foldl combine a bs
141buildPostfixParser :: P.Stream s m t => [P.ParsecT s u m (a -> a)] -> P.ParsecT s u m a -> P.ParsecT s u m a
142buildPostfixParser f x = fold x (P.choice (map P.try f)) (flip ($))
144parseIdent :: P.Parsec String u Ident
145parseIdent = (Ident <$> identifier) <|> (Op <$> parens operator)
147parseIdentInfix :: P.Parsec String u Ident
148parseIdentInfix = (Ident <$> P.between tick tick identifier) <|> (Op <$> operator)
150mark :: P.Parsec String P.Column a -> P.Parsec String P.Column a
152 current <- P.getState
153 pos <- P.sourceColumn <$> P.getPosition
159checkIndentation :: (P.Column -> P.Column -> Bool) -> P.Parsec String P.Column ()
160checkIndentation rel = (do
161 col <- P.sourceColumn <$> P.getPosition
162 current <- P.getState
163 guard $ col `rel` current) <|> P.parserFail "Indentation check"
165indented :: P.Parsec String P.Column ()
166indented = checkIndentation (>)
168same :: P.Parsec String P.Column ()
169same = checkIndentation (==)
171runIndentParser :: P.Column -> P.Parsec String P.Column a -> String -> Either P.ParseError a
172runIndentParser 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