Skip to content

Commit 8011a4b

Browse files
authored
Merge pull request #38 from scmlab/remove_unused_either_in_lexer
Remove unused either in lexer
2 parents 0006d3b + 87d9916 commit 8011a4b

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

gcl/src/Server/Handler/GCL/Refine.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,11 @@ digImplHoles parseStart filePath implText =
279279
parseFragment :: Pos -> Text -> Either ParseError [C.Stmt]
280280
parseFragment fragmentStart fragment = do
281281
let Pos filePath _ _ _ = fragmentStart
282-
case Syntax.Parser.Lexer.scan filePath fragment of
283-
Left err -> Left (LexicalError err)
284-
Right tokens -> do
285-
let tokens' = translateTokStream fragmentStart tokens
286-
case Parser.parse Parser.statements filePath tokens' of
287-
Left (errors, logMsg) -> Left (SyntacticError errors logMsg)
288-
Right val -> Right val
282+
let tokens = Syntax.Parser.Lexer.scan filePath fragment
283+
let tokens' = translateTokStream fragmentStart tokens
284+
case Parser.parse Parser.statements filePath tokens' of
285+
Left (errors, logMsg) -> Left (SyntacticError errors logMsg)
286+
Right val -> Right val
289287
where
290288
translateRange :: Pos -> Pos -> Pos
291289
translateRange

gcl/src/Syntax/Parser.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ type Parser = ParsecT Void TokStream M
4444
--------------------------------------------------------------------------------
4545

4646
scanAndParse :: Parser a -> FilePath -> Text -> Either ParseError a
47-
scanAndParse parser filepath source = case scan filepath source of
48-
Left err -> throwError (LexicalError err)
49-
Right tokens -> case parse parser filepath tokens of
50-
Left (errors, logMsg) -> throwError (SyntacticError errors logMsg)
51-
Right val -> return val
47+
scanAndParse parser filepath source =
48+
let tokens = scan filepath source
49+
in case parse parser filepath tokens of
50+
Left (errors, logMsg) -> throwError (SyntacticError errors logMsg)
51+
Right val -> return val
5252

5353
parse :: Parser a -> FilePath -> TokStream -> Either (NonEmpty (Loc, String), String) a
5454
parse parser filepath tokenStream =

gcl/src/Syntax/Parser/Lexer.hs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -483,23 +483,20 @@ lexer =
483483
--------------------------------------------------------------------------------
484484
type LexicalError = Pos
485485

486-
scan :: FilePath -> Text -> Either LexicalError TokStream
486+
scan :: FilePath -> Text -> TokStream
487487
scan filepath =
488-
translateLoc . Right . runLexer lexer filepath . Text.unpack
488+
translateLoc . runLexer lexer filepath . Text.unpack
489489
where
490490
-- According to the document in Data.Loc.Range, the original meaning of Loc is
491491
-- different from how we use it as Range (to simply put, Range extends 1 in col and charOffset).
492492
-- The lexer records tokens' ranges in Loc, and we use translateLoc to make it Range.
493-
translateLoc :: Either LexicalError TokStream -> Either LexicalError TokStream
494-
translateLoc (Left x) = Left x
495-
translateLoc (Right toks) = Right (f toks)
493+
translateLoc :: TokStream -> TokStream
494+
translateLoc (TsToken (L loc x) rest) = TsToken (L (update loc) x) (translateLoc rest)
496495
where
497-
f (TsToken (L loc x) rest) = TsToken (L (update loc) x) (f rest)
498-
where
499-
update NoLoc = NoLoc
500-
update (Loc start (Pos path l c co)) = Loc start (Pos path l (c + 1) (co + 1))
501-
f TsEof = TsEof
502-
f (TsError e) = TsError e
496+
update NoLoc = NoLoc
497+
update (Loc start (Pos path l c co)) = Loc start (Pos path l (c + 1) (co + 1))
498+
translateLoc TsEof = TsEof
499+
translateLoc (TsError e) = TsError e
503500

504501
-- | Instances of PrettyToken
505502
instance PrettyToken Tok where

0 commit comments

Comments
 (0)