git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Better parser errors using <?>
commita17d1474e9720482a42d24b90f5bf7058cc0c25c authorPhil Freeman <paf31@cantab.net> dateTue 29 Oct 2013, 20:18 (13 years ago) parent5c9e25e
5 files changed+24−20
src/PureScript/Parser/Common.hs+6−6 src/PureScript/Parser/Declarations.hs+2−2 src/PureScript/Parser/Kinds.hs+1−1 src/PureScript/Parser/Types.hs+4−4 src/PureScript/Parser/Values.hs+11−7
src/PureScript/Parser/Common.hs+6−6
@@ -135,8 +135,8 @@ properName :: P.Parsec String u String
135135properName = lexeme $ P.try ((:) <$> P.upper <*> many (PT.identLetter langDef) P.<?> "name")
136136
137137integerOrFloat :: P.Parsec String u (Either Integer Double)
138−integerOrFloat = Left <$> P.try (PT.natural tokenParser) <|>
139− Right <$> P.try (PT.float tokenParser)
138+integerOrFloat = (Left <$> P.try (PT.natural tokenParser) <|>
139+ Right <$> P.try (PT.float tokenParser)) P.<?> "number"
140140
141141augment :: 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
142142augment p q f = (flip $ maybe id $ flip f) <$> p <*> P.optionMaybe q
@@ -166,16 +166,16 @@ mark p = do
166166 return a
167167
168168checkIndentation :: (P.Column -> P.Column -> Bool) -> P.Parsec String ParseState ()
169−checkIndentation rel = (do
169+checkIndentation rel = do
170170 col <- P.sourceColumn <$> P.getPosition
171171 current <- indentationLevel <$> P.getState
172− guard $ col `rel` current) <|> P.parserFail "Indentation check"
172+ guard (col `rel` current)
173173
174174indented :: P.Parsec String ParseState ()
175−indented = checkIndentation (>)
175+indented = checkIndentation (>) P.<?> "indentation"
176176
177177same :: P.Parsec String ParseState ()
178−same = checkIndentation (==)
178+same = checkIndentation (==) P.<?> "no indentation"
179179
180180runIndentParser :: P.Parsec String ParseState a -> String -> Either P.ParseError a
181181runIndentParser p = P.runParser p (ParseState 0 M.empty) ""
src/PureScript/Parser/Declarations.hs+2−2
@@ -88,13 +88,13 @@ parseFixityDeclaration = do
8888 return $ FixityDeclaration fixity name
8989
9090parseDeclaration :: P.Parsec String ParseState Declaration
91−parseDeclaration = P.choice $ map P.try
91+parseDeclaration = P.choice (map P.try
9292 [ parseDataDeclaration
9393 , parseTypeDeclaration
9494 , parseTypeSynonymDeclaration
9595 , parseValueDeclaration
9696 , parseExternDeclaration
97− , parseFixityDeclaration ]
97+ , parseFixityDeclaration ]) P.<?> "declaration"
9898
9999parseDeclarations :: P.Parsec String ParseState [Declaration]
100100parseDeclarations = 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
3636 , parens parseKind ])
3737
3838parseKind :: P.Parsec String ParseState Kind
39−parseKind = P.buildExpressionParser operators $ parseTypeAtom
39+parseKind = P.buildExpressionParser operators parseTypeAtom P.<?> "kind"
4040 where
4141 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
6767 , parens parseType ])
6868
6969parsePolyType :: P.Parsec String ParseState PolyType
70−parsePolyType = PolyType <$> (P.option [] (indented *> reserved "forall" *> many (indented *> identifier) <* indented <* dot))
71− <*> parseType
70+parsePolyType = (PolyType <$> (P.option [] (indented *> reserved "forall" *> many (indented *> identifier) <* indented <* dot))
71+ <*> parseType) P.<?> "polymorphic type"
7272
7373parseType :: P.Parsec String ParseState Type
74−parseType = P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom
74+parseType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
7575 where
7676 postfixTable :: [P.Parsec String ParseState (Type -> Type)]
7777 postfixTable = [ flip TypeApp <$> (indented *> parseTypeAtom) ]
@@ -84,7 +84,7 @@ parseRowEnding :: P.Parsec String ParseState Row
8484parseRowEnding = P.option REmpty (RowVar <$> (lexeme (indented *> P.char '|') *> indented *> identifier))
8585
8686parseRow :: 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"
8888 where
8989 fromList :: [(String, Type)] -> Row -> Row
9090 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
7979 <*> (C.indented *> C.mark (P.many (C.same *> C.mark parseCaseAlternative)))
8080
8181parseCaseAlternative :: 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"
8385
8486parseIfThenElse :: P.Parsec String ParseState Value
8587parseIfThenElse = IfThenElse <$> (C.reserved "if" *> C.indented *> parseValue)
@@ -90,7 +92,7 @@ parseBlock :: P.Parsec String ParseState Value
9092parseBlock = Block <$> (C.reserved "do" *> parseManyStatements)
9193
9294parseManyStatements :: 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"
9496
9597parseValueAtom :: P.Parsec String ParseState Value
9698parseValueAtom = C.indented *> P.choice (map P.try
@@ -110,7 +112,9 @@ parseValueAtom = C.indented *> P.choice (map P.try
110112parseValue :: P.Parsec String ParseState Value
111113parseValue = do
112114 customOps <- fixities <$> P.getState
113− buildExpressionParser (operators customOps) . C.buildPostfixParser postfixTable2 $ indexersAndAccessors
115+ (buildExpressionParser (operators customOps)
116+ . C.buildPostfixParser postfixTable2
117+ $ indexersAndAccessors) P.<?> "expression"
114118 where
115119 indexersAndAccessors = C.buildPostfixParser postfixTable1 $ parseValueAtom
116120 postfixTable1 = [ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
@@ -124,7 +128,7 @@ parseValue = do
124128 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "-") >> return (Unary Negate)
125129 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "+") >> return id ]
126130 ] ++ 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 ]
128132 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<=") >> return (Binary LessThanOrEqualTo)) AssocRight
129133 , Infix (C.lexeme (P.try $ C.indented *> C.reservedOp ">=") >> return (Binary GreaterThanOrEqualTo)) AssocRight ]
130134 , [ Infix (C.lexeme (P.try $ C.indented *> C.reservedOp "<") >> return (Binary LessThan)) AssocRight
@@ -157,7 +161,7 @@ customOperatorTable fixities =
157161 flip map levels (map (\(name, (a, _)) -> flip Infix (toAssoc a) $ do
158162 C.lexeme $ P.try $ do
159163 C.indented
160− C.reservedOp name
164+ C.reservedOp name P.<?> "operator"
161165 return $ \t1 t2 -> App (App (Var (Op name)) [t1]) [t2]))
162166
163167toAssoc :: Associativity -> Assoc
@@ -218,7 +222,7 @@ parseStatement = P.choice (map P.try
218222 , parseFor
219223 , parseForEach
220224 , parseIf
221− , parseReturn ])
225+ , parseReturn ]) P.<?> "statement"
222226
223227parseStringBinder :: P.Parsec String ParseState Binder
224228parseStringBinder = StringBinder <$> C.stringLiteral
@@ -271,7 +275,7 @@ parseBinder = P.choice (map P.try
271275 , parseNullaryBinder
272276 , parseObjectBinder
273277 , parseArrayBinder
274− , C.parens parseBinder ])
278+ , C.parens parseBinder ]) P.<?> "binder"
275279
276280parseGuardedBinder :: P.Parsec String ParseState Binder
277281parseGuardedBinder = flip ($) <$> parseBinder <*> P.option id (GuardedBinder <$> (C.indented *> C.lexeme (P.char '|') *> C.indented *> parseValue))