Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report multiple lexical errors. Fixes #144 #145

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions templates/wrappers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ data AlexState = AlexState {
alex_inp :: ByteString.ByteString, -- the current input
alex_chr :: !Char, -- the character before the input
#endif /* ALEX_MONAD_BYTESTRING */
alex_scd :: !Int -- the current startcode
alex_scd :: !Int, -- the current startcode
alex_errs :: [String] -- lexical errors so far
#ifdef ALEX_MONAD_USER_STATE
, alex_ust :: AlexUserState -- AlexUserState will be defined in the user program
#endif
Expand All @@ -199,6 +200,7 @@ runAlex input__ (Alex f)
alex_pos = alexStartPos,
alex_inp = input__,
alex_chr = '\n',
alex_errs = [],
#ifdef ALEX_MONAD_USER_STATE
alex_ust = alexInitUserState,
#endif
Expand Down Expand Up @@ -252,6 +254,15 @@ alexSetInput (pos,c,inp__,bpos)
alexError :: String -> Alex a
alexError message = Alex $ const $ Left message

alexPushError :: String -> Alex ()
alexPushError message = Alex $ \s@AlexState{alex_errs=errs} -> Right (s{ alex_errs=message:errs }, ())

-- Use this function for returning all lexical errors that have occurred
alexThrowErrors :: Alex ()
alexThrowErrors = Alex $ \s@AlexState{alex_errs=errs} -> case errs of
[] -> Right (s, ())
_ -> Left $ init $ concatMap (++ "\n") $ reverse errs

alexGetStartCode :: Alex Int
alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc)

Expand All @@ -274,8 +285,29 @@ alexMonadScan = do
#endif /* ALEX_MONAD_BYTESTRING */
sc <- alexGetStartCode
case alexScan inp__ sc of
AlexEOF -> alexEOF
AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)
AlexEOF -> do
alexThrowErrors
alexEOF

#if defined(ALEX_BASIC)
AlexError (p@(AlexPn _ line column), x, s) -> do
#elif !defined(ALEX_MONAD_BYTESTRING)
AlexError (p@(AlexPn _ line column),x,y,s) -> do
#else
AlexError (p@(AlexPn _ line column), x, s, y) -> do
#endif

alexPushError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column)

#if defined(ALEX_BASIC)
alexSetInput (p, x, tail s)
#elif !defined(ALEX_MONAD_BYTESTRING)
alexSetInput (p, x, y, tail s)
#else
alexSetInput (p, x, ByteString.tail s, y)
#endif
Comment on lines +302 to +308
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @simonmar would prefer another approach for shifting instead of using tail function. I'd be glad hearing any suggestion ;)


alexMonadScan
AlexSkip inp__' _len -> do
alexSetInput inp__'
alexMonadScan
Expand Down