lex-applicative 0.0.0.0 → 0.0.0.1
raw patch · 4 files changed
+31/−12 lines, 4 filesdep +dlist
Dependencies added: dlist
Files
- lex-applicative.cabal +4/−2
- src/Text/Lexer.hs +4/−4
- src/Text/Regex/Applicative/Lex.hs +6/−6
- src/Util/Private.hs +17/−0
lex-applicative.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: lex-applicative-version: 0.0.0.0+version: 0.0.0.1 synopsis: See README for more info description: See README for more info homepage: https://github.com/strake/lex-applicative.hs@@ -52,7 +52,9 @@ exposed-modules: Data.TextPos , Text.Lexer , Text.Regex.Applicative.Lex- build-depends: hs-functors ^>= 0.1.4+ other-modules: Util.Private+ build-depends: dlist ^>= 0.8.0.7+ , hs-functors ^>= 0.1.4 , parser-combinators ^>= 1.2 , regex-applicative ^>= 0.3.3 , text ^>= 1.2.3
src/Text/Lexer.hs view
@@ -8,7 +8,7 @@ import qualified Data.Char as Char import Data.Semigroup (Min (..), Max (..)) import qualified Data.TextPos as Text-import Text.Regex.Applicative (RE, findLongestPrefix')+import Text.Regex.Applicative (RE, findLongestPrefix', reFoldl) import qualified Text.Regex.Applicative as RE import Util @@ -23,7 +23,7 @@ defaultLexerSpec :: LexerSpec Text.Pos Char t defaultLexerSpec = LexerSpec { token = empty- , space = () <$ many (RE.psym Char.isSpace)+ , space = () <$ RE.psym Char.isSpace , blockComment = empty , move = Text.move , init = Text.Pos 0 1 0@@ -32,7 +32,7 @@ lex :: LexerSpec p x t -> [x] -> Free ((,,) (Min p, Max p) t) (Maybe (Error p)) lex LexerSpec {..} = go' [] . annotate move init where- go bs = stripLongestPrefix' stripAnnotation (many space) & go' bs+ go bs = stripLongestPrefix' stripAnnotation (reFoldl RE.Greedy pure () space) & go' bs go' bs' xs@(AList p l) = case (bs', l) of ([], Nothing) -> Pure Nothing (_, Nothing) -> Pure (Just (Error p))@@ -62,4 +62,4 @@ deriving (Eq, Ord, Read, Show) stripLongestPrefix' :: (xs -> Maybe (x, xs)) -> RE x a -> xs -> xs-stripLongestPrefix' uncons re = flip maybe snd <*> findLongestPrefix' uncons re+stripLongestPrefix' uncons re = flip maybe snd <*> findLongestPrefix' uncons (() <$ re)
src/Text/Regex/Applicative/Lex.hs view
@@ -5,15 +5,17 @@ import Control.Applicative.Combinators (between, count') import Control.Monad ((>=>), guard, replicateM) import qualified Data.Char.Properties.DerivedCore as UC+import qualified Data.DList as DList import Data.Foldable import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Lazy as TL import qualified Data.Text.Normalize as T import Numeric.Natural-import Text.Regex.Applicative hiding (string)+import Text.Regex.Applicative hiding (string, some, many) import qualified Text.Regex.Applicative as RE import Util hiding (some)+import Util.Private natural' :: RE Char Natural natural' = asum@@ -40,14 +42,12 @@ ident' = ident defaultIdentSpec ident :: IdentSpec -> RE Char Text-ident IdentSpec {..} = normalize . T.pack <$> start <:> many cont <++> (fmap asum . many) (med <:> some cont)+ident IdentSpec {..} = normalize . T.pack . DList.toList <$>+ start <:> many cont <++> (fmap asum . RE.many) (med <:> some cont) where start = psym isStart cont = psym isContinue med = psym isMedial- infixr 5 <:>, <++>- (<:>) = liftA2 (:)- (<++>) = liftA2 (++) data IdentSpec = IdentSpec { isStart, isContinue, isMedial :: Char -> Bool@@ -70,7 +70,7 @@ x -> x string :: RE Char TL.Text-string = TL.pack <$> between (sym '"') (sym '"') (many xre)+string = TL.pack . DList.toList <$> between (sym '"') (sym '"') (many xre) where xre = psym (∉ "\"\\") <|> sym '\\' *> (psym (∈ "\"\\") <|> esc) esc :: RE Char Char
+ src/Util/Private.hs view
@@ -0,0 +1,17 @@+module Util.Private where++import Control.Applicative hiding (some, many)+import Text.Regex.Applicative hiding (some, many)+import Util hiding (some)++infixr 5 <:>, <++>+(<:>) :: (Applicative p, Alternative q) => p a -> p (q a) -> p (q a)+(<++>) :: (Applicative p, Alternative q) => p (q a) -> p (q a) -> p (q a)+(<:>) = liftA2 (<|)+(<++>) = liftA2 (<|>)++many :: Alternative q => RE x a -> RE x (q a)+many = reFoldl Greedy (|>) empty++some :: Alternative q => RE x a -> RE x (q a)+some a = a <:> many a