1
Fork 0

fix handling of lex errors

This commit is contained in:
Jake Wheat 2024-01-10 09:44:12 +00:00
parent 55a7537108
commit 0f307f51c7

View file

@ -187,7 +187,7 @@ module Language.SQL.SimpleSQL.Parse
,parseScalarExpr ,parseScalarExpr
,parseStatement ,parseStatement
,parseStatements ,parseStatements
,ParseError ,ParseError(..)
,prettyError ,prettyError
) where ) where
@ -310,10 +310,16 @@ parseScalarExpr
-> Either ParseError ScalarExpr -> Either ParseError ScalarExpr
parseScalarExpr = wrapParse scalarExpr parseScalarExpr = wrapParse scalarExpr
type ParseError = ParseErrorBundle MyStream Void -- Megaparsec is too clever, so have to create a new type to represent
-- either a lex error or a parse error
data ParseError
= LexError L.ParseError
| ParseError (ParseErrorBundle MyStream Void)
prettyError :: ParseError -> Text prettyError :: ParseError -> Text
prettyError = T.pack . errorBundlePretty prettyError (LexError e) = T.pack $ errorBundlePretty e
prettyError (ParseError e) = T.pack $ errorBundlePretty e
{- {-
This helper function takes the parser given and: This helper function takes the parser given and:
@ -330,10 +336,11 @@ wrapParse :: Parser a
-> Maybe (Int,Int) -> Maybe (Int,Int)
-> Text -> Text
-> Either ParseError a -> Either ParseError a
wrapParse parser d f p src = wrapParse parser d f p src = do
let lx = either (error . show) id $ L.lexSQL d f p src lx <- either (Left . LexError) Right $ L.lexSQL d f p src
in runReader (runParserT (parser <* (eof <?> "")) (T.unpack f) either (Left . ParseError) Right $
$ MyStream (T.unpack src) $ filter notSpace lx) d runReader (runParserT (parser <* (eof <?> "")) (T.unpack f)
$ MyStream (T.unpack src) $ filter notSpace lx) d
where where
notSpace = notSpace' . L.tokenVal notSpace = notSpace' . L.tokenVal
notSpace' (L.Whitespace {}) = False notSpace' (L.Whitespace {}) = False
@ -2307,12 +2314,6 @@ sqlNumberTok intOnly = token test Set.empty <?> ""
test (L.WithPos _ _ _ (L.SqlNumber p)) | not intOnly || T.all isDigit p = Just p test (L.WithPos _ _ _ (L.SqlNumber p)) | not intOnly || T.all isDigit p = Just p
test _ = Nothing test _ = Nothing
{-mytoken (\tok ->
case tok of
L.SqlNumber p | not intOnly || all isDigit p -> Just p
_ -> Nothing)-}
symbolTok :: Maybe Text -> Parser Text symbolTok :: Maybe Text -> Parser Text
symbolTok sym = token test Set.empty <?> "" symbolTok sym = token test Set.empty <?> ""
where where