git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Better parser errors using <?>
a17d147Phil Freeman
src/PureScript/Parser/Common.hs+6−6
@@ -135,8 +135,8 @@ properName :: P.Parsec String u String
135properName = lexeme $ P.try ((:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name")
135properName = lexeme $ P.try ((:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name")
136
136
137integerOrFloat :: P.Parsec String u (Either Integer Double)
137integerOrFloat :: P.Parsec String u (Either Integer Double)
138−integerOrFloat = Left <$> P.try (PT.natural tokenParser) <|>
138+integerOrFloat = (Left <$> P.try (PT.natural tokenParser) <|>
139− Right <$> P.try (PT.float tokenParser)
139+ Right <$> P.try (PT.float tokenParser)) P.<?> "number"
140
140
141augment :: 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
141augment :: 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
142augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
142augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
@@ -166,16 +166,16 @@ mark p = do
166 return a
166 return a
167
167
168checkIndentation :: (P.Column -> P.Column -> Bool) -> P.Parsec String ParseState ()
168checkIndentation :: (P.Column -> P.Column -> Bool) -> P.Parsec String ParseState ()
169−checkIndentation rel = (do
169+checkIndentation rel = do
170 col <- P.sourceColumn <$> P.getPosition
170 col <- P.sourceColumn <$> P.getPosition
171 current <- indentationLevel <$> P.getState
171 current <- indentationLevel <$> P.getState
172− guard $ col `rel` current) <|> P.parserFail "Indentation check"
172+ guard (col `rel` current)
173
173
174indented :: P.Parsec String ParseState ()
174indented :: P.Parsec String ParseState ()
175−indented = checkIndentation (>)
175+indented = checkIndentation (>) P.<?> "indentation"
176
176
177same :: P.Parsec String ParseState ()
177same :: P.Parsec String ParseState ()
178−same = checkIndentation (==)
178+same = checkIndentation (==) P.<?> "no indentation"
179
179
180runIndentParser :: P.Parsec String ParseState a -> String -> Either P.ParseError a
180runIndentParser :: P.Parsec String ParseState a -> String -> Either P.ParseError a
181runIndentParser p = P.runParser p (ParseState 0 M.empty) ""
181runIndentParser p = P.runParser p (ParseState 0 M.empty) ""
src/PureScript/Parser/Declarations.hs+2−2
@@ -88,13 +88,13 @@ parseFixityDeclaration = do
88 return $ FixityDeclaration fixity name
88 return $ FixityDeclaration fixity name
89
89
90parseDeclaration :: P.Parsec String ParseState Declaration
90parseDeclaration :: P.Parsec String ParseState Declaration
91−parseDeclaration = P.choice $ map P.try
91+parseDeclaration = P.choice (map P.try
92 [ parseDataDeclaration
92 [ parseDataDeclaration
93 , parseTypeDeclaration
93 , parseTypeDeclaration
94 , parseTypeSynonymDeclaration
94 , parseTypeSynonymDeclaration
95 , parseValueDeclaration
95 , parseValueDeclaration
96 , parseExternDeclaration
96 , parseExternDeclaration
97− , parseFixityDeclaration ]
97+ , parseFixityDeclaration ]) P.<?> "declaration"
98
98
99parseDeclarations :: P.Parsec String ParseState [Declaration]
99parseDeclarations :: P.Parsec String ParseState [Declaration]
100parseDeclarations = whiteSpace *> mark (same *> P.many parseDeclaration) <* P.eof
100parseDeclarations = whiteSpace *> mark (same *> P.many parseDeclaration) <* P.eof
src/PureScript/Parser/Kinds.hs+1−1
@@ -36,6 +36,6 @@ parseTypeAtom = indented *> P.choice (map P.try
36 , parens parseKind ])
36 , parens parseKind ])
37
37
38parseKind :: P.Parsec String ParseState Kind
38parseKind :: P.Parsec String ParseState Kind
39−parseKind = P.buildExpressionParser operators $ parseTypeAtom
39+parseKind = P.buildExpressionParser operators parseTypeAtom P.<?> "kind"
40 where
40 where
41 operators = [ [ P.Infix (lexeme (P.try (P.string "->")) >> return FunKind) P.AssocRight ] ]
41 operators = [ [ P.Infix (lexeme (P.try (P.string "->")) >> return FunKind) P.AssocRight ] ]
src/PureScript/Parser/Types.hs+4−4
@@ -67,11 +67,11 @@ parseTypeAtom = indented *> P.choice (map P.try
67 , parens parseType ])
67 , parens parseType ])
68
68
69parsePolyType :: P.Parsec String ParseState PolyType
69parsePolyType :: P.Parsec String ParseState PolyType
70−parsePolyType = PolyType <$> (P.option [] (indented *> reserved "forall" *> many (indented *> identifier) <* indented <* dot))
70+parsePolyType = (PolyType <$> (P.option [] (indented *> reserved "forall" *> many (indented *> identifier) <* indented <* dot))
71− <*> parseType
71+ <*> parseType) P.<?> "polymorphic type"
72
72
73parseType :: P.Parsec String ParseState Type
73parseType :: P.Parsec String ParseState Type
74−parseType = P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom
74+parseType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
75 where
75 where
76 postfixTable :: [P.Parsec String ParseState (Type -> Type)]
76 postfixTable :: [P.Parsec String ParseState (Type -> Type)]
77 postfixTable = [ flip TypeApp <$> (indented *> parseTypeAtom) ]
77 postfixTable = [ flip TypeApp <$> (indented *> parseTypeAtom) ]
@@ -84,7 +84,7 @@ parseRowEnding :: P.Parsec String ParseState Row
84parseRowEnding = P.option REmpty (RowVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
84parseRowEnding = P.option REmpty (RowVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
85
85
86parseRow :: P.Parsec String ParseState Row
86parseRow :: P.Parsec String ParseState Row
87−parseRow = fromList <$> (parseNameAndType `P.sepBy` (indented *> semi)) <*> parseRowEnding
87+parseRow = (fromList <$> (parseNameAndType `P.sepBy` (indented *> semi)) <*> parseRowEnding) P.<?> "row"
88 where
88 where
89 fromList :: [(String, Type)] -> Row -> Row
89 fromList :: [(String, Type)] -> Row -> Row
90 fromList [] r = r
90 fromList [] r = r
src/PureScript/Parser/Values.hs+11−7
@@ -79,7 +79,9 @@ parseCase = Case <$> (P.between (C.reserved "case") (C.indented *> C.reserved "o
79 <*> (C.indented *> C.mark (P.many (C.same *> C.mark parseCaseAlternative)))
79 <*> (C.indented *> C.mark (P.many (C.same *> C.mark parseCaseAlternative)))
80
80
81parseCaseAlternative :: P.Parsec String ParseState (Binder, Value)
81parseCaseAlternative :: P.Parsec String ParseState (Binder, Value)
82−parseCaseAlternative = (,) <$> (parseGuardedBinder <* C.lexeme (P.string "->")) <*> parseValue
82+parseCaseAlternative = (,) <$> (parseGuardedBinder <* C.lexeme (P.string "->"))
83+ <*> parseValue
84+ P.<?> "case alternative"
83
85
84parseIfThenElse :: P.Parsec String ParseState Value
86parseIfThenElse :: P.Parsec String ParseState Value
85parseIfThenElse = IfThenElse <$> (C.reserved "if" *> C.indented *> parseValue)
87parseIfThenElse = IfThenElse <$> (C.reserved "if" *> C.indented *> parseValue)
@@ -90,7 +92,7 @@ parseBlock :: P.Parsec String ParseState Value
90parseBlock = Block <$> (C.reserved "do" *> parseManyStatements)
92parseBlock = Block <$> (C.reserved "do" *> parseManyStatements)
91
93
92parseManyStatements :: P.Parsec String ParseState [Statement]
94parseManyStatements :: P.Parsec String ParseState [Statement]
93−parseManyStatements = C.indented *> (C.mark $ P.many $ C.same *> C.mark parseStatement)
95+parseManyStatements = C.indented *> (C.mark $ P.many $ C.same *> C.mark parseStatement) P.<?> "block"
94
96
95parseValueAtom :: P.Parsec String ParseState Value
97parseValueAtom :: P.Parsec String ParseState Value
96parseValueAtom = C.indented *> P.choice (map P.try
98parseValueAtom = C.indented *> P.choice (map P.try
@@ -110,7 +112,9 @@ parseValueAtom = C.indented *> P.choice (map P.try
110parseValue :: P.Parsec String ParseState Value
112parseValue :: P.Parsec String ParseState Value
111parseValue = do
113parseValue = do
112 customOps <- fixities <$> P.getState
114 customOps <- fixities <$> P.getState
113− buildExpressionParser (operators customOps) . C.buildPostfixParser postfixTable2 $ indexersAndAccessors
115+ (buildExpressionParser (operators customOps)
116+ . C.buildPostfixParser postfixTable2
117+ $ indexersAndAccessors) P.<?> "expression"
114 where
118 where
115 indexersAndAccessors = C.buildPostfixParser postfixTable1 $ parseValueAtom
119 indexersAndAccessors = C.buildPostfixParser postfixTable1 $ parseValueAtom
116 postfixTable1 = [ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
120 postfixTable1 = [ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
@@ -124,7 +128,7 @@ parseValue = do
124 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "-") >> return (Unary Negate)
128 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "-") >> return (Unary Negate)
125 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "+") >> return id ]
129 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "+") >> return id ]
126 ] ++ customOperatorTable user ++
130 ] ++ customOperatorTable user ++
127− [ [ Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix) >>= \ident -> return $ \t1 t2 -> App (App (Var ident) [t1]) [t2])) AssocRight ]
131+ [ [ Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix P.<?> "operator") >>= \ident -> return $ \t1 t2 -> App (App (Var ident) [t1]) [t2])) AssocRight ]
128 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<=") >> return (Binary LessThanOrEqualTo)) AssocRight
132 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<=") >> return (Binary LessThanOrEqualTo)) AssocRight
129 , Infix (C.lexeme (P.try $ C.indented *> C.reservedOp ">=") >> return (Binary GreaterThanOrEqualTo)) AssocRight ]
133 , Infix (C.lexeme (P.try $ C.indented *> C.reservedOp ">=") >> return (Binary GreaterThanOrEqualTo)) AssocRight ]
130 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<") >> return (Binary LessThan)) AssocRight
134 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<") >> return (Binary LessThan)) AssocRight
@@ -157,7 +161,7 @@ customOperatorTable fixities =
157 flip map levels (map (\(name, (a, _)) -> flip Infix (toAssoc a) $ do
161 flip map levels (map (\(name, (a, _)) -> flip Infix (toAssoc a) $ do
158 C.lexeme $ P.try $ do
162 C.lexeme $ P.try $ do
159 C.indented
163 C.indented
160− C.reservedOp name
164+ C.reservedOp name P.<?> "operator"
161 return $ \t1 t2 -> App (App (Var (Op name)) [t1]) [t2]))
165 return $ \t1 t2 -> App (App (Var (Op name)) [t1]) [t2]))
162
166
163toAssoc :: Associativity -> Assoc
167toAssoc :: Associativity -> Assoc
@@ -218,7 +222,7 @@ parseStatement = P.choice (map P.try
218 , parseFor
222 , parseFor
219 , parseForEach
223 , parseForEach
220 , parseIf
224 , parseIf
221− , parseReturn ])
225+ , parseReturn ]) P.<?> "statement"
222
226
223parseStringBinder :: P.Parsec String ParseState Binder
227parseStringBinder :: P.Parsec String ParseState Binder
224parseStringBinder = StringBinder <$> C.stringLiteral
228parseStringBinder = StringBinder <$> C.stringLiteral
@@ -271,7 +275,7 @@ parseBinder = P.choice (map P.try
271 , parseNullaryBinder
275 , parseNullaryBinder
272 , parseObjectBinder
276 , parseObjectBinder
273 , parseArrayBinder
277 , parseArrayBinder
274− , C.parens parseBinder ])
278+ , C.parens parseBinder ]) P.<?> "binder"
275
279
276parseGuardedBinder :: P.Parsec String ParseState Binder
280parseGuardedBinder :: P.Parsec String ParseState Binder
277parseGuardedBinder = flip ($) <$> parseBinder <*> P.option id (GuardedBinder <$> (C.indented *> C.lexeme (P.char '|') *> C.indented *> parseValue))
281parseGuardedBinder = flip ($) <$> parseBinder <*> P.option id (GuardedBinder <$> (C.indented *> C.lexeme (P.char '|') *> C.indented *> parseValue))