alex-tools 0.3.1 → 0.4
raw patch · 2 files changed
+30/−9 lines, 2 filesdep ~basedep ~template-haskellPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, template-haskell
API changes (from Hackage documentation)
+ AlexTools: prevPos :: SourcePos -> SourcePos
- AlexTools: LexerConfig :: s -> (s -> Int) -> (s -> [Lexeme t]) -> LexerConfig s t
+ AlexTools: LexerConfig :: s -> (s -> Int) -> (s -> SourcePos -> [Lexeme t]) -> LexerConfig s t
- AlexTools: [lexerEOF] :: LexerConfig s t -> s -> [Lexeme t]
+ AlexTools: [lexerEOF] :: LexerConfig s t -> s -> SourcePos -> [Lexeme t]
Files
- alex-tools.cabal +1/−1
- src/AlexTools.hs +29/−8
alex-tools.cabal view
@@ -1,5 +1,5 @@ name: alex-tools-version: 0.3.1+version: 0.4 synopsis: A set of functions for a common use case of Alex. description: This captures a common patter for using Alex. license: ISC
src/AlexTools.hs view
@@ -3,7 +3,7 @@ ( -- * Lexer Basics initialInput, Input(..), inputFile , Lexeme(..)- , SourcePos(..), startPos, beforeStartPos+ , SourcePos(..), startPos, beforeStartPos, prevPos , SourceRange(..) , prettySourcePos, prettySourceRange , prettySourcePosLong, prettySourceRangeLong@@ -207,10 +207,11 @@ lexeme tok = do r <- matchRange txt <- matchText- return [ Lexeme { lexemeRange = r- , lexemeToken = tok- , lexemeText = txt- } ]+ let l = Lexeme { lexemeRange = r+ , lexemeToken = tok+ , lexemeText = txt+ }+ l `seq` return [ l ] -- | Information about the lexer's input. data Input = Input@@ -251,6 +252,26 @@ , sourceFile = file } +{- | Move one position back. Assumes that newlines use a single bytes.++This function is intended to be used when starting the lexing somewhere+in the middle of the input, for example, if we are implementing a quasi+quoter, and the previous part of the input is not in our language.+-}+prevPos :: SourcePos -> SourcePos+prevPos p+ | sourceColumn p > 1 = p { sourceColumn = sourceColumn p - 1+ , sourceIndex = sourceIndex p - 1+ }++ | sourceLine p > 1 = p { sourceLine = sourceLine p - 1+ , sourceColumn = 1+ , sourceIndex = sourceIndex p - 1+ }++ | otherwise = beforeStartPos (sourceFile p)++ -- | The file/thing for the current position. inputFile :: Input -> Text inputFile = sourceFile . inputPos@@ -264,7 +285,7 @@ , lexerStateMode :: s -> Int -- ^ Determine the current lexer mode from the lexer's state. - , lexerEOF :: s -> [Lexeme t]+ , lexerEOF :: s -> SourcePos -> [Lexeme t] -- ^ Emit some lexemes at the end of the input. } @@ -274,7 +295,7 @@ simpleLexer = LexerConfig { lexerInitialState = () , lexerStateMode = \_ -> 0- , lexerEOF = \_ -> []+ , lexerEOF = \_ _ -> [] } @@ -297,7 +318,7 @@ let p ~> e = match p (normalB e) [] body go mode inp cfg = caseE [| $alexScanUser $mode $inp (lexerStateMode $cfg $mode) |]- [ alexEOF ~> [| lexerEOF $cfg $mode |]+ [ alexEOF ~> [| lexerEOF $cfg $mode (inputPrev $inp) |] , alexError ~> [| error "internal error in lexer (AlexTools.hs)" |] , alexSkip ~> [| $go $mode $xE |] , alexToken ~> [| case runA $zE $inp $xE $yE $mode of