1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 14:21:31 +03:00

Use Alternative to combine our non-skipped parsers.

This commit is contained in:
Rob Rix 2015-12-14 16:23:09 -05:00
parent 835a3a0245
commit e9f21668a1

View File

@ -1,5 +1,6 @@
module Range where module Range where
import Control.Applicative ((<|>))
import qualified Data.Char as Char import qualified Data.Char as Char
data Range = Range { start :: Int, end :: Int } data Range = Range { start :: Int, end :: Int }
@ -17,15 +18,12 @@ offsetRange i (Range start end) = Range (i + start) (i + end)
rangesAndWordsFrom :: Int -> String -> [(Range, String)] rangesAndWordsFrom :: Int -> String -> [(Range, String)]
rangesAndWordsFrom _ "" = [] rangesAndWordsFrom _ "" = []
rangesAndWordsFrom startIndex string = rangesAndWordsFrom startIndex string =
case parse isWord string of case (parse isWord string <|> parse (not . isWordOrSeparator) string) of
Just parsed -> takeAndContinue parsed Just parsed -> takeAndContinue parsed
Nothing -> Nothing ->
case parse (not . isWordOrSeparator) string of case parse Char.isSeparator string of
Just parsed -> takeAndContinue parsed Just (space, rest) -> rangesAndWordsFrom (startIndex + length space) rest
Nothing -> Nothing -> []
case parse Char.isSeparator string of
Just (space, rest) -> rangesAndWordsFrom (startIndex + length space) rest
Nothing -> []
where where
takeAndContinue (parsed, rest) = (Range startIndex $ startIndex + length parsed, parsed) : rangesAndWordsFrom (startIndex + length parsed) rest takeAndContinue (parsed, rest) = (Range startIndex $ startIndex + length parsed, parsed) : rangesAndWordsFrom (startIndex + length parsed) rest
parse predicate string = case span predicate string of parse predicate string = case span predicate string of