git.iko.soy/rowtype-yoga/purescript-native
on 5d428e9 help sign in
226 lines 9.3 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : PureScript.Parser.Values
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.Values (
16 parseValue,
17 parseBinder
18) where
20import PureScript.Values
21import qualified PureScript.Parser.Common as C
22import Control.Applicative
23import qualified Text.Parsec as P
24import Text.Parsec.Expr
25import Control.Monad
26import Control.Arrow (Arrow(..))
27import PureScript.Parser.Types
28import PureScript.Types
30booleanLiteral :: P.Parsec String () Bool
31booleanLiteral = (C.reserved "true" >> return True) P.<|> (C.reserved "false" >> return False)
33parseNumericLiteral :: P.Parsec String () Value
34parseNumericLiteral = NumericLiteral <$> C.naturalOrFloat
36parseStringLiteral :: P.Parsec String () Value
37parseStringLiteral = StringLiteral <$> C.stringLiteral
39parseBooleanLiteral :: P.Parsec String () Value
40parseBooleanLiteral = BooleanLiteral <$> booleanLiteral
42parseArrayLiteral :: P.Parsec String () Value
43parseArrayLiteral = ArrayLiteral <$> (C.squares $ C.commaSep parseValue)
45parseObjectLiteral :: P.Parsec String () Value
46parseObjectLiteral = ObjectLiteral <$> (C.braces $ C.commaSep parseIdentifierAndValue)
48parseIdentifierAndValue :: P.Parsec String () (String, Value)
49parseIdentifierAndValue = do
50 name <- C.lexeme C.identifier
51 C.colon
52 value <- parseValue
53 return (name, value)
55parseAbs :: P.Parsec String () Value
56parseAbs = do
57 C.lexeme $ P.char '\\'
58 args <- C.commaSep C.identifier
59 C.lexeme $ P.string "->"
60 value <- parseValue
61 return $ Abs args value
63parseApp :: P.Parsec String () Value
64parseApp = App <$> parseValue
65 <*> (C.parens $ C.commaSep parseValue)
67parseVar :: P.Parsec String () Value
68parseVar = Var <$> C.identifier
70parseConstructor :: P.Parsec String () Value
71parseConstructor = Constructor <$> C.properName
73parseCase :: P.Parsec String () Value
74parseCase = Case <$> P.between (C.reserved "case") (C.reserved "of") parseValue
75 <*> C.braces (C.semiSep parseCaseAlternative)
77parseCaseAlternative :: P.Parsec String () (Binder, Value)
78parseCaseAlternative = (,) <$> (parseBinder <* C.lexeme (P.string "->")) <*> parseValue
80parseBlock :: P.Parsec String () Value
81parseBlock = Block <$> (C.braces $ P.many (parseStatement True))
83parseValueAtom :: P.Parsec String () Value
84parseValueAtom = P.choice $ map P.try
85 [ parseNumericLiteral
86 , parseStringLiteral
87 , parseBooleanLiteral
88 , parseArrayLiteral
89 , parseObjectLiteral
90 , parseAbs
91 , parseVar
92 , parseConstructor
93 , parseBlock
94 , parseCase
95 , C.parens parseValue ]
97parseValue :: P.Parsec String () Value
98parseValue = buildExpressionParser operators $ C.fold (C.lexeme typedValue) (C.lexeme funArgs) App
99 where
100 typedValue = C.augment parseValueAtom parseTypeAnnotation TypedValue
101 funArgs = C.parens $ C.commaSep parseValue
102 parseTypeAnnotation = C.lexeme (P.string "::") *> parsePolyType
103 operators = [ [ Postfix $ Accessor <$> (C.dot *> C.identifier)
104 , Postfix $ Indexer <$> C.squares parseValue ]
105 , [ Prefix $ C.lexeme (P.char '!') >> return (Unary Not)
106 , Prefix $ C.lexeme (P.char '~') >> return (Unary BitwiseNot)
107 , Prefix $ C.lexeme (P.char '-') >> return (Unary Negate) ]
108 , [ Infix (C.lexeme (P.try (P.string "<=")) >> return (Binary LessThanOrEqualTo)) AssocRight
109 , Infix (C.lexeme (P.try (P.string ">=")) >> return (Binary GreaterThanOrEqualTo)) AssocRight
110 , Infix (C.lexeme (P.try (P.char '<' <* P.notFollowedBy (P.char '<'))) >> return (Binary LessThan)) AssocRight ]
111 , [ Infix (C.lexeme (P.try (P.char '>' <* P.notFollowedBy (P.char '>'))) >> return (Binary GreaterThan)) AssocRight
112 , Infix (C.lexeme (P.char '/') >> return (Binary Divide)) AssocRight
113 , Infix (C.lexeme (P.char '%') >> return (Binary Modulus)) AssocRight ]
114 , [ Infix (C.lexeme (P.try (P.string "++")) >> return (Binary Concat)) AssocRight
115 , Infix (C.lexeme (P.char '+') >> return (Binary Add)) AssocRight
116 , Infix (C.lexeme (P.char '-') >> return (Binary Subtract)) AssocRight ]
117 , [ Infix (C.lexeme (P.string "<<") >> return (Binary ShiftLeft)) AssocRight
118 , Infix (C.lexeme (P.try (P.string ">>>")) >> return (Binary ZeroFillShiftRight)) AssocRight
119 , Infix (C.lexeme (P.string ">>") >> return (Binary ShiftRight)) AssocRight ]
120 , [ Infix (C.lexeme (P.string "==") >> return (Binary EqualTo)) AssocRight
121 , Infix (C.lexeme (P.try (P.string "!=")) >> return (Binary NotEqualTo)) AssocRight ]
122 , [ Infix (C.lexeme (P.try (P.char '&' <* P.notFollowedBy (P.char '&'))) >> return (Binary BitwiseAnd)) AssocRight ]
123 , [ Infix (C.lexeme (P.char '^') >> return (Binary BitwiseXor)) AssocRight ]
124 , [ Infix (C.lexeme (P.try (P.char '|' <* P.notFollowedBy (P.char '|'))) >> return (Binary BitwiseOr)) AssocRight ]
125 , [ Infix (C.lexeme (P.string "&&") >> return (Binary And)) AssocRight ]
126 , [ Infix (C.lexeme (P.string "||") >> return (Binary Or)) AssocRight ]
127 ]
129parseVariableIntroduction :: Bool -> P.Parsec String () Statement
130parseVariableIntroduction requireSemi = do
131 C.reserved "var"
132 name <- C.identifier
133 C.lexeme $ P.char '='
134 value <- parseValue
135 when requireSemi (void C.semi)
136 return $ VariableIntroduction name value
138parseAssignment :: Bool -> P.Parsec String () (Statement)
139parseAssignment requireSemi = do
140 tgt <- parseAssignmentTarget
141 C.lexeme $ P.char '='
142 value <- parseValue
143 when requireSemi (void C.semi)
144 return $ Assignment tgt value
146parseManyStatements :: P.Parsec String () [Statement]
147parseManyStatements = C.braces $ P.many (parseStatement True)
149parseWhile :: P.Parsec String () Statement
150parseWhile = While <$> (C.reserved "while" *> C.parens parseValue)
151 <*> parseManyStatements
153parseFor :: P.Parsec String () Statement
154parseFor = For <$> (C.reserved "for" *> C.parens forIntro)
155 <*> parseManyStatements
156 where
157 forIntro = (,,) <$> parseStatement False
158 <*> (C.semi *> parseValue)
159 <*> (C.semi *> parseStatement False)
161parseIfThenElse :: P.Parsec String () Statement
162parseIfThenElse = IfThenElse
163 <$> (C.reserved "if" *> C.parens parseValue)
164 <*> parseManyStatements
165 <*> P.optionMaybe (C.reserved "else" *> parseManyStatements)
167parseReturn :: Bool -> P.Parsec String () Statement
168parseReturn requireSemi = Return <$> (C.reserved "return" *> parseValue <* when requireSemi (void C.semi))
170parseStatement :: Bool -> P.Parsec String () Statement
171parseStatement requireSemi = P.choice $ map P.try
172 [ parseVariableIntroduction requireSemi
173 , parseAssignment requireSemi
174 , parseWhile
175 , parseFor
176 , parseIfThenElse
177 , parseReturn requireSemi ]
179parseStringBinder :: P.Parsec String () Binder
180parseStringBinder = StringBinder <$> C.stringLiteral
182parseBooleanBinder :: P.Parsec String () Binder
183parseBooleanBinder = BooleanBinder <$> booleanLiteral
185parseNumberBinder :: P.Parsec String () Binder
186parseNumberBinder = NumberBinder <$> C.naturalOrFloat
188parseVarBinder :: P.Parsec String () Binder
189parseVarBinder = VarBinder <$> C.lexeme C.identifier
191parseNullaryBinder :: P.Parsec String () Binder
192parseNullaryBinder = NullaryBinder <$> C.lexeme C.properName
194parseUnaryBinder :: P.Parsec String () Binder
195parseUnaryBinder = UnaryBinder <$> C.lexeme C.properName <*> parseBinder
197parseObjectBinder :: P.Parsec String () Binder
198parseObjectBinder = ObjectBinder <$> C.braces (C.commaSep parseIdentifierAndBinder)
200parseArrayBinder :: P.Parsec String () Binder
201parseArrayBinder = C.squares $ ArrayBinder <$> (C.commaSep parseBinder) <*> P.optionMaybe (C.colon *> parseBinder)
203parseIdentifierAndBinder :: P.Parsec String () (String, Binder)
204parseIdentifierAndBinder = do
205 name <- C.lexeme C.identifier
206 C.lexeme $ P.char '='
207 binder <- parseBinder
208 return (name, binder)
210parseBinder :: P.Parsec String () Binder
211parseBinder = P.choice $ map P.try
212 [ parseStringBinder
213 , parseBooleanBinder
214 , parseNumberBinder
215 , parseVarBinder
216 , parseUnaryBinder
217 , parseNullaryBinder
218 , parseObjectBinder
219 , parseArrayBinder
220 , C.parens parseBinder ]
222parseAssignmentTarget :: P.Parsec String () AssignmentTarget
223parseAssignmentTarget = buildExpressionParser operators (AssignVariable <$> C.identifier)
224 where
225 operators = [ [ Postfix $ AssignArrayIndex <$> C.squares parseValue
226 , Postfix $ AssignObjectProperty <$> (C.dot *> C.identifier) ] ]