From 108dc9c0d3acefb4597619ae7755cf2199e5d8b8 Mon Sep 17 00:00:00 2001 From: Gustavo Castellanos Date: Thu, 3 Oct 2019 04:22:29 -0400 Subject: [PATCH 1/4] Fixes #144 --- templates/wrappers.hs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/templates/wrappers.hs b/templates/wrappers.hs index 4263a18..7214e38 100644 --- a/templates/wrappers.hs +++ b/templates/wrappers.hs @@ -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 @@ -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 @@ -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 $ concatMap (++ "\n") $ reverse errs + alexGetStartCode :: Alex Int alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc) @@ -275,7 +286,10 @@ alexMonadScan = do sc <- alexGetStartCode case alexScan inp__ sc of AlexEOF -> alexEOF - AlexError ((AlexPn _ line column),_,_,_) -> alexError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column) + AlexError (p@(AlexPn _ line column),x,y,s) -> do + alexPushError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column) + alexSetInput (p, x, y, (tail s)) + alexMonadScan AlexSkip inp__' _len -> do alexSetInput inp__' alexMonadScan From a14a6bf5b4f288dde23ff34f169b534e9816c289 Mon Sep 17 00:00:00 2001 From: Gustavo Castellanos Date: Thu, 3 Oct 2019 04:44:12 -0400 Subject: [PATCH 2/4] Solve bug with ALEX_MONAD_BYTESTRING --- templates/wrappers.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/templates/wrappers.hs b/templates/wrappers.hs index 7214e38..eb7a551 100644 --- a/templates/wrappers.hs +++ b/templates/wrappers.hs @@ -288,7 +288,11 @@ alexMonadScan = do AlexEOF -> alexEOF AlexError (p@(AlexPn _ line column),x,y,s) -> do alexPushError $ "lexical error at line " ++ (show line) ++ ", column " ++ (show column) +#ifndef ALEX_MONAD_BYTESTRING alexSetInput (p, x, y, (tail s)) +#else + alexSetInput (p, x, y, s-n) +#endif alexMonadScan AlexSkip inp__' _len -> do alexSetInput inp__' From fce6396e0d41f18cac47a25e010af4d2f09b00e8 Mon Sep 17 00:00:00 2001 From: Gustavo Castellanos Date: Thu, 3 Oct 2019 05:17:14 -0400 Subject: [PATCH 3/4] Some fixes --- templates/wrappers.hs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/templates/wrappers.hs b/templates/wrappers.hs index eb7a551..0ae14e6 100644 --- a/templates/wrappers.hs +++ b/templates/wrappers.hs @@ -285,14 +285,28 @@ alexMonadScan = do #endif /* ALEX_MONAD_BYTESTRING */ sc <- alexGetStartCode case alexScan inp__ sc of - AlexEOF -> alexEOF + 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) -#ifndef ALEX_MONAD_BYTESTRING - alexSetInput (p, x, y, (tail s)) + +#if defined(ALEX_BASIC) + alexSetInput (p, x, tail s) +#elif !defined(ALEX_MONAD_BYTESTRING) + alexSetInput (p, x, y, tail s) #else - alexSetInput (p, x, y, s-n) + alexSetInput (p, x, ByteString.tail s, y) #endif + alexMonadScan AlexSkip inp__' _len -> do alexSetInput inp__' From 90e623115c6c1a8c4aa9035ca0c9af42a3f0cea0 Mon Sep 17 00:00:00 2001 From: Gustavo Castellanos Date: Thu, 3 Oct 2019 05:33:15 -0400 Subject: [PATCH 4/4] Remove last '\n' in final error message --- templates/wrappers.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/wrappers.hs b/templates/wrappers.hs index 0ae14e6..d498074 100644 --- a/templates/wrappers.hs +++ b/templates/wrappers.hs @@ -261,7 +261,7 @@ alexPushError message = Alex $ \s@AlexState{alex_errs=errs} -> Right (s{ alex_er alexThrowErrors :: Alex () alexThrowErrors = Alex $ \s@AlexState{alex_errs=errs} -> case errs of [] -> Right (s, ()) - _ -> Left $ concatMap (++ "\n") $ reverse errs + _ -> Left $ init $ concatMap (++ "\n") $ reverse errs alexGetStartCode :: Alex Int alexGetStartCode = Alex $ \s@AlexState{alex_scd=sc} -> Right (s, sc)