New submission from Ben Franksen <[email protected]>:

2 patches for repository https://darcs.net/screened:

patch 66f1028034d063bf5cb1a0cfc3d461b1d91b152e
Author: Ben Franksen <[email protected]>
Date:   Wed Apr  5 09:59:32 CEST 2023
  * refactor and cleanup parsing of match expressions

  This concentrates all the parsing the functionality into the single function
  'matchParser' and increases readability of the parsing code in various ways:
  naming of subparsers, code layout, comments, etc.

patch 80233fd730f31864932f0087de703a23f6aff69c
Author: Ben Franksen <[email protected]>
Date:   Thu Apr  6 00:17:09 CEST 2023
  * D.P.Match: rename matchPattern to patternmatch

  This is for consistency of naming with the other prim matchers.

----------
files: patch-preview.txt, 
refactor-and-cleanup-parsing-of-match-expressions.dpatch
messages: 24156
nosy: bfrk
status: needs-screening
title: refactor and cleanup parsing of match ex... (and 1 more)

__________________________________
Darcs bug tracker <[email protected]>
<https://bugs.darcs.net/patch2449>
__________________________________
[refactor and cleanup parsing of match expressions
Ben Franksen <[email protected]>**20230405075932
 Ignore-this: f97c50c5ff49bb9f8d33e5f4d11cb3b94ebeaa39879e7e40929f3db74a87668e0d79f89fdcf945dc
 
 This concentrates all the parsing the functionality into the single function
 'matchParser' and increases readability of the parsing code in various ways:
 naming of subparsers, code layout, comments, etc.
] hunk ./src/Darcs/Patch/Match.hs 204
-matchParser = submatcher <?> helpfulErrorMsg
+matchParser = (option matchAnyPatch expr <* eof) <?> helpfulErrorMsg
hunk ./src/Darcs/Patch/Match.hs 206
-    submatcher = do
-        m <- option matchAnyPatch submatch
-        eof
-        return m
-
hunk ./src/Darcs/Patch/Match.hs 208
-    helpfulErrorMsg = "valid expressions over: "
-                      ++ intercalate ", " (map (\(name, _, _, _, _) -> name) ps)
-                      ++ "\nfor more help, see `darcs help patterns`."
-
-    ps = primitiveMatchers
-
-    -- matchAnyPatch is returned if submatch fails without consuming any
-    -- input, i.e. if we pass --match '', we want to match anything.
+    helpfulErrorMsg =
+      "valid expressions over: "
+      ++ intercalate ", " (map (\(name, _, _, _, _) -> name) primitiveMatchers)
+      ++ "\nfor more help, see `darcs help patterns`."
+
+    -- matchAnyPatch is returned if expr fails without consuming any
+    -- input, i.e. if we pass --match '', we want to match anything
hunk ./src/Darcs/Patch/Match.hs 217
-submatch :: CharParser st MatchFun
-submatch = buildExpressionParser table match
-
-table :: OperatorTable Char st MatchFun
-table   = [ [prefix "not" negate_match,
-             prefix "!" negate_match ]
-          , [binary "||" or_match,
-             binary "or" or_match,
-             binary "&&" and_match,
-            binary "and" and_match ]
-          ]
-    where binary name fun = Infix (tryNameAndUseFun name fun) AssocLeft
-          prefix name fun = Prefix $ tryNameAndUseFun name fun
-          tryNameAndUseFun name fun = do _ <- trystring name
-                                         spaces
-                                         return fun
-          negate_match (MatchFun m) = MatchFun $ \p -> not (m p)
-          or_match (MatchFun m1) (MatchFun m2) = MatchFun $ \p -> m1 p || m2 p
-          and_match (MatchFun m1) (MatchFun m2) = MatchFun $ \p -> m1 p && m2 p
-
-trystring :: String -> CharParser st String
-trystring s = try $ string s
-
-match :: CharParser st MatchFun
-match = between spaces spaces (parens submatch <|> choice matchers_)
-  where
-    matchers_ = map createMatchHelper primitiveMatchers
-
-createMatchHelper :: (String, String, String, [String], String -> MatchFun)
-                  -> CharParser st MatchFun
-createMatchHelper (key,_,_,_,matcher) =
-  do _ <- trystring key
-     spaces
-     q <- quoted
-     return $ matcher q
+    -- parse a non-empty full match expression
+    expr :: CharParser st MatchFun
+    expr = buildExpressionParser table term
+
+    table :: OperatorTable Char st MatchFun
+    table =
+      [ [ prefix "not" negate_match, prefix "!" negate_match ]
+      , [ binary "||" or_match
+        , binary "or" or_match
+        , binary "&&" and_match
+        , binary "and" and_match
+        ]
+      ]
+      where
+        binary name result = Infix (operator name result) AssocLeft
+        prefix name result = Prefix $ operator name result
+        operator name result = try (string name) >> spaces >> return result
+        negate_match (MatchFun m) = MatchFun $ \p -> not (m p)
+        or_match (MatchFun m1) (MatchFun m2) = MatchFun $ \p -> m1 p || m2 p
+        and_match (MatchFun m1) (MatchFun m2) = MatchFun $ \p -> m1 p && m2 p
+
+    -- parse a term, i.e. anything we can combine with operators:
+    -- an expression in parentheses or a primitive match expression
+    term :: CharParser st MatchFun
+    term = between spaces spaces (parens expr <|> choice prims)
+      where
+        -- the primitive match expression parsers
+        prims = map prim primitiveMatchers
+        -- a primitive match expression is a keyword followed by an argument;
+        -- the result is the passed matcher applied to the argument
+        prim (key, _, _, _, matcher) =
+          fmap matcher $ try (string key) >> spaces >> argument
+        -- an argument in a primitive match expression
+        argument :: CharParser st String
+        argument = quoted <|> unquoted <?> "string"
+        -- quoted string
+        quoted =
+          between (char '"') (char '"') (many $ try escaped <|> noneOf "\"")
+        -- bare (unquoted) string
+        unquoted = between spaces spaces (many $ noneOf " ()")
+        -- backslash escaped double quote or backslash
+        escaped = char '\\' >> oneOf "\\\""
+        -- any expression in parentheses
+        parens = between (string "(") (string ")")
hunk ./src/Darcs/Patch/Match.hs 327
-parens :: CharParser st MatchFun
-       -> CharParser st MatchFun
-parens = between (string "(") (string ")")
-
-quoted :: CharParser st String
-quoted = between (char '"') (char '"')
-                 (many $ do { _ <- char '\\' -- allow escapes
-                            ; try (oneOf "\\\"") <|> return '\\'
-                            }
-                         <|>  noneOf "\"")
-         <|> between spaces spaces (many $ noneOf " ()")
-         <?> "string"
-

[D.P.Match: rename matchPattern to patternmatch
Ben Franksen <[email protected]>**20230405221709
 Ignore-this: ca847aa698d2d88bc80c8b77312a5484ea23057ef6d7b14cb5f82596cb67f8996c48ed26bb39c08c
 
 This is for consistency of naming with the other prim matchers.
] replace ./src/Darcs/Patch/Match.hs [A-Za-z_0-9] matchPattern patternmatch

Attachment: refactor-and-cleanup-parsing-of-match-expressions.dpatch
Description: application/darcs-patch

_______________________________________________
darcs-devel mailing list
[email protected]
https://lists.darcs.net/mailman/listinfo/darcs-devel

Reply via email to