From aa7710982ba96ed4ac3246d3c04b85ffb66ee6f0 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 14 May 2019 13:08:51 -0400 Subject: [PATCH] avoid list lookup by parseToken Minor optimisation to parsing of a preferred content expression. --- Annex/FileMatcher.hs | 6 +++--- CmdLine/GitAnnex/Options.hs | 4 ++-- Limit.hs | 6 +++--- Logs/PreferredContent.hs | 2 +- Utility/Matcher.hs | 20 ++++++++------------ 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Annex/FileMatcher.hs b/Annex/FileMatcher.hs index b2c626c5a6..162161984e 100644 --- a/Annex/FileMatcher.hs +++ b/Annex/FileMatcher.hs @@ -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 diff --git a/CmdLine/GitAnnex/Options.hs b/CmdLine/GitAnnex/Options.hs index a1beef7e98..760529013c 100644 --- a/CmdLine/GitAnnex/Options.hs +++ b/CmdLine/GitAnnex/Options.hs @@ -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 = diff --git a/Limit.hs b/Limit.hs index bb1b64f5a4..6c48100564 100644 --- a/Limit.hs +++ b/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 () diff --git a/Logs/PreferredContent.hs b/Logs/PreferredContent.hs index c8605b083f..5101575f3a 100644 --- a/Logs/PreferredContent.hs +++ b/Logs/PreferredContent.hs @@ -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 diff --git a/Utility/Matcher.hs b/Utility/Matcher.hs index badf72acfd..faf53c3e9a 100644 --- a/Utility/Matcher.hs +++ b/Utility/Matcher.hs @@ -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