git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #2077 from nwolverson/sourcespan

Tighten source-spans to token end position -> 0.9

commita5eed316443b046d6ed2ef323123edafb533e4ac authorPhil Freeman <paf31@cantab.net> dateMon 2 May 2016, 13:14 (10 years ago) parenta9dd3750100861
3 files changed+23−7
src/Language/PureScript/Parser/Declarations.hs+6−1 src/Language/PureScript/Parser/Lexer.hs+17−4 src/Language/PureScript/Parser/State.hs−2
src/Language/PureScript/Parser/Declarations.hs+6−1
@@ -52,7 +52,11 @@ withSourceSpan f p = do
5252 comments <- C.readComments
5353 x <- p
5454 end <- P.getPosition
55− let sp = SourceSpan (P.sourceName start) (C.toSourcePos start) (C.toSourcePos end)
55+ input <- P.getInput
56+ let end' = case input of
57+ pt:_ -> ptPrevEndPos pt
58+ _ -> Nothing
59+ let sp = SourceSpan (P.sourceName start) (C.toSourcePos start) (C.toSourcePos $ fromMaybe end end')
5660 return $ f sp comments x
5761
5862kindedIdent :: TokenParser (String, Maybe Kind)
@@ -273,6 +277,7 @@ parseModulesFromFiles toFilePath input = do
273277 inParallel :: [Either P.ParseError (k, [Module])] -> [Either P.ParseError (k, [Module])]
274278 inParallel = withStrategy (parList rseq)
275279
280+
276281toPositionedError :: P.ParseError -> ErrorMessage
277282toPositionedError perr = ErrorMessage [ PositionedError (SourceSpan name start end) ] (ErrorParsingModule perr)
278283 where
src/Language/PureScript/Parser/Lexer.hs+17−4
@@ -152,7 +152,12 @@ prettyPrintToken (Number n) = either show show n
152152prettyPrintToken (HoleLit name) = "?" ++ name
153153
154154data PositionedToken = PositionedToken
155− { ptSourcePos :: P.SourcePos
155+ { -- | Start position of this token
156+ ptSourcePos :: P.SourcePos
157+ -- | End position of this token (not including whitespace)
158+ , ptEndPos :: P.SourcePos
159+ -- | End position of the previous token
160+ , ptPrevEndPos :: Maybe P.SourcePos
156161 , ptToken :: Token
157162 , ptComments :: [Comment]
158163 } deriving (Eq)
@@ -162,7 +167,13 @@ instance Show PositionedToken where
162167 show = prettyPrintToken . ptToken
163168
164169lex :: FilePath -> String -> Either P.ParseError [PositionedToken]
165−lex = P.parse parseTokens
170+lex f s = updatePositions <$> P.parse parseTokens f s
171+
172+updatePositions :: [PositionedToken] -> [PositionedToken]
173+updatePositions [] = []
174+updatePositions (x:xs) = x : zipWith update (x:xs) xs
175+ where
176+ update PositionedToken { ptEndPos = pos } pt = pt { ptPrevEndPos = Just pos }
166177
167178parseTokens :: P.Parsec String u [PositionedToken]
168179parseTokens = whitespace *> P.many parsePositionedToken <* P.skipMany parseComment <* P.eof
@@ -184,7 +195,9 @@ parsePositionedToken = P.try $ do
184195 comments <- P.many parseComment
185196 pos <- P.getPosition
186197 tok <- parseToken
187− return $ PositionedToken pos tok comments
198+ pos' <- P.getPosition
199+ whitespace
200+ return $ PositionedToken pos pos' Nothing tok comments
188201
189202parseToken :: P.Parsec String u Token
190203parseToken = P.choice
@@ -221,7 +234,7 @@ parseToken = P.choice
221234 , CharLiteral <$> parseCharLiteral
222235 , StringLiteral <$> parseStringLiteral
223236 , Number <$> parseNumber
224− ] <* whitespace
237+ ]
225238
226239 where
227240 parseLName :: P.Parsec String u String
src/Language/PureScript/Parser/State.hs−2
@@ -26,5 +26,3 @@ data ParseState = ParseState {
2626 --
2727 indentationLevel :: P.Column
2828 } deriving Show
29−
30−