avoid list lookup by parseToken
Minor optimisation to parsing of a preferred content expression.
This commit is contained in:
parent
c1957b6aeb
commit
aa7710982b
5 changed files with 17 additions and 21 deletions
|
@ -84,9 +84,9 @@ data ParseToken t
|
|||
type ParseResult t = Either String (Token t)
|
||||
|
||||
parseToken :: [ParseToken t] -> String -> ParseResult t
|
||||
parseToken l t
|
||||
| t `elem` tokens = Right $ token t
|
||||
| otherwise = go l
|
||||
parseToken l t = case syntaxToken t of
|
||||
Right st -> Right st
|
||||
Left _ -> go l
|
||||
where
|
||||
go [] = Left $ "near " ++ show t
|
||||
go (SimpleToken s r : _) | s == t = r
|
||||
|
|
|
@ -324,8 +324,8 @@ combiningOptions =
|
|||
, shortopt ')' "close group of options"
|
||||
]
|
||||
where
|
||||
longopt o h = globalFlag (Limit.addToken o) ( long o <> help h <> hidden )
|
||||
shortopt o h = globalFlag (Limit.addToken [o]) ( short o <> help h <> hidden )
|
||||
longopt o h = globalFlag (Limit.addSyntaxToken o) ( long o <> help h <> hidden )
|
||||
shortopt o h = globalFlag (Limit.addSyntaxToken [o]) ( short o <> help h <> hidden )
|
||||
|
||||
jsonOptions :: [GlobalOption]
|
||||
jsonOptions =
|
||||
|
|
6
Limit.hs
6
Limit.hs
|
@ -63,9 +63,9 @@ add l = Annex.changeState $ \s -> s { Annex.limit = prepend $ Annex.limit s }
|
|||
prepend (BuildingMatcher ls) = BuildingMatcher $ l:ls
|
||||
prepend _ = error "internal"
|
||||
|
||||
{- Adds a new token. -}
|
||||
addToken :: String -> Annex ()
|
||||
addToken = add . Utility.Matcher.token
|
||||
{- Adds a new syntax token. -}
|
||||
addSyntaxToken :: String -> Annex ()
|
||||
addSyntaxToken = either error add . Utility.Matcher.syntaxToken
|
||||
|
||||
{- Adds a new limit. -}
|
||||
addLimit :: Either String (MatchFiles Annex) -> Annex ()
|
||||
|
|
|
@ -34,7 +34,7 @@ import qualified Annex.Branch
|
|||
import qualified Annex
|
||||
import Logs
|
||||
import Logs.UUIDBased
|
||||
import Utility.Matcher hiding (tokens)
|
||||
import Utility.Matcher
|
||||
import Annex.FileMatcher
|
||||
import Annex.UUID
|
||||
import Types.Group
|
||||
|
|
|
@ -20,8 +20,7 @@
|
|||
module Utility.Matcher (
|
||||
Token(..),
|
||||
Matcher(..),
|
||||
token,
|
||||
tokens,
|
||||
syntaxToken,
|
||||
generate,
|
||||
match,
|
||||
matchM,
|
||||
|
@ -47,16 +46,13 @@ data Matcher op = MAny
|
|||
deriving (Show, Eq)
|
||||
|
||||
{- Converts a word of syntax into a token. Doesn't handle operations. -}
|
||||
token :: String -> Token op
|
||||
token "and" = And
|
||||
token "or" = Or
|
||||
token "not" = Not
|
||||
token "(" = Open
|
||||
token ")" = Close
|
||||
token t = error $ "unknown token " ++ t
|
||||
|
||||
tokens :: [String]
|
||||
tokens = words "and or not ( )"
|
||||
syntaxToken :: String -> Either String (Token op)
|
||||
syntaxToken "and" = Right And
|
||||
syntaxToken "or" = Right Or
|
||||
syntaxToken "not" = Right Not
|
||||
syntaxToken "(" = Right Open
|
||||
syntaxToken ")" = Right Close
|
||||
syntaxToken t = Left $ "unknown token " ++ t
|
||||
|
||||
{- Converts a list of Tokens into a Matcher. -}
|
||||
generate :: [Token op] -> Matcher op
|
||||
|
|
Loading…
Reference in a new issue