Skip to content

Commit

Permalink
Merge pull request #6704 from haskell/rename-regex
Browse files Browse the repository at this point in the history
Rename Regex to GrammarRegex to avoid confusion
  • Loading branch information
phadej authored Apr 13, 2020
2 parents 4b8abaa + a2e0cb0 commit 65acfca
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ library
Distribution.Types.GivenComponent
Distribution.Types.PackageVersionConstraint
Distribution.Utils.CharSet
Distribution.Utils.Regex
Distribution.Utils.Generic
Distribution.Utils.GrammarRegex
Distribution.Utils.NubList
Distribution.Utils.ShortText
Distribution.Utils.Progress
Expand Down
22 changes: 11 additions & 11 deletions Cabal/Distribution/FieldGrammar/Described.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Distribution.FieldGrammar.Described (
Described (..),
describeDoc,
-- * Regular expressions
Regex (..),
GrammarRegex (..),
reEps,
reChar,
reChars,
Expand Down Expand Up @@ -38,15 +38,15 @@ import Prelude ()
import Distribution.Parsec (Parsec)
import Distribution.Pretty (Pretty)

import Distribution.Utils.Regex
import Distribution.Utils.GrammarRegex

import qualified Distribution.Utils.CharSet as CS
import qualified Text.PrettyPrint as PP

-- | Class describing the pretty/parsec format of a.
class (Pretty a, Parsec a) => Described a where
-- | A pretty document of "regex" describing the field format
describe :: proxy a -> Regex void
describe :: proxy a -> GrammarRegex void

-- | Pretty-print description.
--
Expand All @@ -66,20 +66,20 @@ instance Described a => Described (Identity a) where
-- Lists
------------------------------------------------------------------------------

reSpacedList :: Regex a -> Regex a
reSpacedList :: GrammarRegex a -> GrammarRegex a
reSpacedList = REMunch RESpaces1

reCommaList :: Regex a -> Regex a
reCommaList :: GrammarRegex a -> GrammarRegex a
reCommaList = RECommaList

reOptCommaList :: Regex a -> Regex a
reOptCommaList :: GrammarRegex a -> GrammarRegex a
reOptCommaList = REOptCommaList

-------------------------------------------------------------------------------
-- Specific grammars
-------------------------------------------------------------------------------

reHsString :: Regex a
reHsString :: GrammarRegex a
reHsString = RENamed "hs-string" impl where
impl = reChar '"' <> REMunch reEps (REUnion [strChar, escChar]) <> reChar '"'
strChar = RECharSet $ CS.difference CS.universe (CS.fromList "\"\\")
Expand All @@ -95,7 +95,7 @@ reHsString = RENamed "hs-string" impl where
, REUnion ["\\NUL", RENamed "ascii" "\\NUL"] -- TODO
]

reUnqualComponent :: Regex a
reUnqualComponent :: GrammarRegex a
reUnqualComponent = RENamed "unqual-name" $
REMunch1 (reChar '-') component
where
Expand All @@ -108,13 +108,13 @@ reUnqualComponent = RENamed "unqual-name" $
<> RECharSet CS.alpha
<> REMunch reEps (RECharSet csAlphaNum)

reDot :: Regex a
reDot :: GrammarRegex a
reDot = reChar '.'

reComma :: Regex a
reComma :: GrammarRegex a
reComma = reChar ','

reSpacedComma :: Regex a
reSpacedComma :: GrammarRegex a
reSpacedComma = RESpaces <> reComma <> RESpaces

-------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Cabal/Distribution/ModuleName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ parsecModuleName = state0 DList.empty where

instance Described ModuleName where
describe _ = REMunch1 (reChar '.') component where
component = RECharSet csUpper <> reMunchCS (csAlphaNum <> fromString "_'")
component = RECharSet csUpper <> REMunch reEps (REUnion [RECharSet csAlphaNum, RECharSet (fromString "_'")])

validModuleChar :: Char -> Bool
validModuleChar c = isAlphaNum c || c == '_' || c == '\''
Expand Down
2 changes: 1 addition & 1 deletion Cabal/Distribution/Parsec/Newtypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Sep sep where

parseSep :: CabalParsing m => Proxy sep -> m a -> m [a]

describeSep :: Proxy sep -> Regex a -> Regex a
describeSep :: Proxy sep -> GrammarRegex a -> GrammarRegex a

instance Sep CommaVCat where
prettySep _ = vcat . punctuate comma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Distribution.Utils.Regex (
module Distribution.Utils.GrammarRegex (
-- * Regular expressions
Regex (..),
GrammarRegex (..),
reEps,
reChar,
reChars,
Expand All @@ -26,31 +26,31 @@ import qualified Distribution.Utils.CharSet as CS
import qualified Text.PrettyPrint as PP

-------------------------------------------------------------------------------
-- Regex
-- GrammarRegex
-------------------------------------------------------------------------------

-- | Recursive regular expressions tuned for 'Described' use-case.
data Regex a
= REAppend [Regex a] -- ^ append @ab@
| REUnion [Regex a] -- ^ union @a|b@
data GrammarRegex a
= REAppend [GrammarRegex a] -- ^ append @ab@
| REUnion [GrammarRegex a] -- ^ union @a|b@

-- repetition
| REMunch (Regex a) (Regex a) -- ^ star @a*@, with a separator
| REMunch1 (Regex a) (Regex a) -- ^ plus @a+@, with a separator
| REMunchR Int (Regex a) (Regex a) -- ^ 1-n, with a separator
| REOpt (Regex a) -- ^ optional @r?@
| REMunch (GrammarRegex a) (GrammarRegex a) -- ^ star @a*@, with a separator
| REMunch1 (GrammarRegex a) (GrammarRegex a) -- ^ plus @a+@, with a separator
| REMunchR Int (GrammarRegex a) (GrammarRegex a) -- ^ 1-n, with a separator
| REOpt (GrammarRegex a) -- ^ optional @r?@

| REString String -- ^ literal string @abcd@
| RECharSet CS.CharSet -- ^ charset @[:alnum:]@
| REVar a -- ^ variable
| RENamed String (Regex a) -- ^ named expression
| RERec String (Regex (Maybe a)) -- ^ recursive expressions
| REString String -- ^ literal string @abcd@
| RECharSet CS.CharSet -- ^ charset @[:alnum:]@
| REVar a -- ^ variable
| RENamed String (GrammarRegex a) -- ^ named expression
| RERec String (GrammarRegex (Maybe a)) -- ^ recursive expressions

-- cabal syntax specifics
| RESpaces -- ^ zero-or-more spaces
| RESpaces1 -- ^ one-or-more spaces
| RECommaList (Regex a) -- ^ comma list (note, leading or trailing commas)
| REOptCommaList (Regex a) -- ^ opt comma list
| RECommaList (GrammarRegex a) -- ^ comma list (note, leading or trailing commas)
| REOptCommaList (GrammarRegex a) -- ^ opt comma list

| RETodo -- ^ unspecified
deriving (Eq, Ord, Show, Functor, Foldable, Traversable)
Expand All @@ -59,45 +59,45 @@ data Regex a
-- Instances
-------------------------------------------------------------------------------

instance IsString (Regex a) where
instance IsString (GrammarRegex a) where
fromString = REString

instance Semigroup (Regex a) where
instance Semigroup (GrammarRegex a) where
x <> y = REAppend (unAppend x ++ unAppend y) where
unAppend (REAppend rs) = rs
unAppend r = [r]

instance Monoid (Regex a) where
instance Monoid (GrammarRegex a) where
mempty = REAppend []
mappend = (<>)

-------------------------------------------------------------------------------
-- Smart constructors
-------------------------------------------------------------------------------

reEps :: Regex a
reEps :: GrammarRegex a
reEps = REAppend []

reChar :: Char -> Regex a
reChar :: Char -> GrammarRegex a
reChar = RECharSet . CS.singleton

reChars :: [Char] -> Regex a
reChars :: [Char] -> GrammarRegex a
reChars = RECharSet . CS.fromList

reMunch1CS :: CS.CharSet -> Regex a
reMunch1CS :: CS.CharSet -> GrammarRegex a
reMunch1CS = REMunch1 reEps . RECharSet

reMunchCS :: CS.CharSet -> Regex a
reMunchCS :: CS.CharSet -> GrammarRegex a
reMunchCS = REMunch reEps . RECharSet

-------------------------------------------------------------------------------
-- Variables
-------------------------------------------------------------------------------

reVar0 :: Regex (Maybe a)
reVar0 :: GrammarRegex (Maybe a)
reVar0 = REVar Nothing

reVar1 :: Regex (Maybe (Maybe a))
reVar1 :: GrammarRegex (Maybe (Maybe a))
reVar1 = REVar (Just Nothing)

-------------------------------------------------------------------------------
Expand All @@ -114,9 +114,9 @@ reVar1 = REVar (Just Nothing)
-- >>> regexDoc $ REString "foo" <> REString "bar"
-- \mathop{\mathord{``}\mathtt{foo}\mathord{"}}\mathop{\mathord{``}\mathtt{bar}\mathord{"}}
--
regexDoc :: Regex Void -> PP.Doc
regexDoc :: GrammarRegex Void -> PP.Doc
regexDoc = go 0 . vacuous where
go :: Int -> Regex PP.Doc -> PP.Doc
go :: Int -> GrammarRegex PP.Doc -> PP.Doc
go _ (REAppend []) = ""
go d (REAppend rs) = parensIf (d > 2) $ PP.hcat (map (go 2) rs)
go d (REUnion [r]) = go d r
Expand Down Expand Up @@ -186,6 +186,7 @@ charsetDoc :: CS.CharSet -> PP.Doc
charsetDoc acs
| acs == CS.alpha = terminalDoc "alpha"
| acs == CS.alphanum = terminalDoc "alpha-num"
| acs == CS.upper = terminalDoc "upper"
charsetDoc acs = case CS.toIntervalList acs of
[] -> "\\emptyset"
[(x,y)] | x == y -> inquotes $ mathtt $ charDoc x
Expand Down
14 changes: 7 additions & 7 deletions Cabal/doc/buildinfo-fields-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module-name
Haskell module name as recognized by Cabal parser.

.. math::
\mathsf{\color{red}{TODO}}
{\left(\mathop{\mathit{upper}}{\left\{ \mathop{\mathit{alpha\text{-}num}}\mid[\mathop{\mathord{``}\mathtt{\text{'}}\mathord{"}}\mathop{\mathord{``}\mathtt{\text{_}}\mathord{"}}] \right\}}^\ast_{}\right)}^+_{\mathop{\mathord{``}\mathtt{\text{.}}\mathord{"}}}
version
Version is to first approximation numbers separated by dots, where leading zero is not allowed and each version digit is consists at most of nine characters.
Expand Down Expand Up @@ -190,14 +190,14 @@ autogen-modules
* Documentation of :pkg-field:`autogen-modules`

.. math::
\mathrm{commalist}\mathsf{\color{red}{TODO}}
\mathrm{commalist}\left({\left(\mathop{\mathit{upper}}{\left\{ \mathop{\mathit{alpha\text{-}num}}\mid[\mathop{\mathord{``}\mathtt{\text{'}}\mathord{"}}\mathop{\mathord{``}\mathtt{\text{_}}\mathord{"}}] \right\}}^\ast_{}\right)}^+_{\mathop{\mathord{``}\mathtt{\text{.}}\mathord{"}}}\right)
build-depends
* Monoidal field
* Documentation of :pkg-field:`build-depends`

.. math::
\mathrm{commalist}\left(\mathop{\mathit{pkg\text{-}name}}{\left(\circ\mathop{\mathord{``}\mathtt{\text{:}}\mathord{"}}\circ\left\{ \mathop{\mathit{unqual\text{-}name}}\mid\mathop{\mathord{``}\mathtt{\{}\mathord{"}}\circ\mathrm{commalist}\mathop{\mathit{unqual\text{-}name}}\circ\mathop{\mathord{``}\mathtt{\}}\mathord{"}} \right\}\right)}^?{\left(\circ\mathop{\mathit{version\text{-}range}}\right)}^?\right)
\mathrm{commalist}\left(\mathop{\mathit{pkg\text{-}name}}{\left(\mathop{\mathord{``}\mathtt{\text{:}}\mathord{"}}\left\{ \mathop{\mathit{unqual\text{-}name}}\mid\mathop{\mathord{``}\mathtt{\{}\mathord{"}}\circ{\mathop{\mathit{unqual\text{-}name}}}^\ast_{\left(\circ\mathop{\mathord{``}\mathtt{\text{,}}\mathord{"}}\circ\right)}\circ\mathop{\mathord{``}\mathtt{\}}\mathord{"}} \right\}\right)}^?{\left(\bullet\mathop{\mathit{version\text{-}range}}\right)}^?\right)
build-tool-depends
* Monoidal field
Expand Down Expand Up @@ -473,7 +473,7 @@ other-modules
* Documentation of :pkg-field:`other-modules`

.. math::
\mathrm{commalist}\mathsf{\color{red}{TODO}}
\mathrm{commalist}\left({\left(\mathop{\mathit{upper}}{\left\{ \mathop{\mathit{alpha\text{-}num}}\mid[\mathop{\mathord{``}\mathtt{\text{'}}\mathord{"}}\mathop{\mathord{``}\mathtt{\text{_}}\mathord{"}}] \right\}}^\ast_{}\right)}^+_{\mathop{\mathord{``}\mathtt{\text{.}}\mathord{"}}}\right)
pkgconfig-depends
* Monoidal field
Expand All @@ -488,7 +488,7 @@ virtual-modules
* Documentation of :pkg-field:`virtual-modules`

.. math::
\mathrm{commalist}\mathsf{\color{red}{TODO}}
\mathrm{commalist}\left({\left(\mathop{\mathit{upper}}{\left\{ \mathop{\mathit{alpha\text{-}num}}\mid[\mathop{\mathord{``}\mathtt{\text{'}}\mathord{"}}\mathop{\mathord{``}\mathtt{\text{_}}\mathord{"}}] \right\}}^\ast_{}\right)}^+_{\mathop{\mathord{``}\mathtt{\text{.}}\mathord{"}}}\right)
Package description fields
Expand All @@ -511,7 +511,7 @@ build-type
cabal-version
* Optional field
* Default: ``-any``
* Default: ``>=1.0``
* Documentation of :pkg-field:`cabal-version`

.. math::
Expand Down Expand Up @@ -637,7 +637,7 @@ test-module
* Documentation of :pkg-field:`test-module`

.. math::
\mathsf{\color{red}{TODO}}
{\left(\mathop{\mathit{upper}}{\left\{ \mathop{\mathit{alpha\text{-}num}}\mid[\mathop{\mathord{``}\mathtt{\text{'}}\mathord{"}}\mathop{\mathord{``}\mathtt{\text{_}}\mathord{"}}] \right\}}^\ast_{}\right)}^+_{\mathop{\mathord{``}\mathtt{\text{.}}\mathord{"}}}
type
* Optional field
Expand Down
12 changes: 6 additions & 6 deletions Cabal/tests/UnitTests/Distribution/Described.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Test.QuickCheck (Arbitrary (..), Gen, Property, choose, counterexam
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)

import Distribution.FieldGrammar.Described (Described (..), Regex (..), reComma, reSpacedComma, reSpacedList)
import Distribution.FieldGrammar.Described (Described (..), GrammarRegex (..), reComma, reSpacedComma, reSpacedList)
import Distribution.Parsec (eitherParsec)
import Distribution.Pretty (prettyShow)

Expand Down Expand Up @@ -101,9 +101,9 @@ genInt lo hi = choose (lo, hi)
-- Conversion
-------------------------------------------------------------------------------

convert :: Regex Void -> RE.RE Void
convert :: GrammarRegex Void -> RE.RE Void
convert = go id . vacuous where
go :: Ord b => (a -> b) -> Regex a -> RE.RE b
go :: Ord b => (a -> b) -> GrammarRegex a -> RE.RE b
go f (REAppend rs) = foldr (\r acc -> go f r <> acc) RE.Eps rs
go f (REUnion rs) = foldr (\r acc -> go f r RE.\/ acc) RE.Null rs
go _ (RECharSet cs) = RE.Ch (convertCS cs)
Expand Down Expand Up @@ -140,17 +140,17 @@ convert = go id . vacuous where

go _ RETodo = RE.Null

expandedCommaList :: Regex a -> Regex a
expandedCommaList :: GrammarRegex a -> GrammarRegex a
expandedCommaList = REUnion . expandedCommaList'

expandedCommaList' :: Regex a -> [Regex a]
expandedCommaList' :: GrammarRegex a -> [GrammarRegex a]
expandedCommaList' r =
[ REMunch reSpacedComma r
, reComma <> RESpaces <> REMunch1 reSpacedComma r
, REMunch1 reSpacedComma r <> RESpaces <> reComma
]

expandedOptCommaList :: Regex a -> Regex a
expandedOptCommaList :: GrammarRegex a -> GrammarRegex a
expandedOptCommaList r = REUnion $ reSpacedList r : expandedCommaList' r

convertCS :: CS.CharSet -> RE.CharSet
Expand Down
10 changes: 5 additions & 5 deletions buildinfo-reference-generator/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import qualified Text.PrettyPrint as PP
import qualified Zinza as Z

import Distribution.FieldGrammar.Described
import Distribution.Utils.Regex
import Distribution.Utils.GrammarRegex

import Distribution.ModuleName (ModuleName)
import Distribution.Types.Version (Version)
Expand Down Expand Up @@ -78,7 +78,7 @@ main = do
putStrLn "Usage: generator <tmpl>"
exitFailure

zproduction :: String -> Regex Void -> String -> ZProduction
zproduction :: String -> GrammarRegex Void -> String -> ZProduction
zproduction name re desc = ZProduction
{ zprodName = name
, zprodSyntax = show (regexDoc re')
Expand All @@ -90,17 +90,17 @@ zproduction name re desc = ZProduction
_ -> re

-- also in UnitTests.Distribution.Described
expandedCommaList :: Regex a -> Regex a
expandedCommaList :: GrammarRegex a -> GrammarRegex a
expandedCommaList = REUnion . expandedCommaList'

expandedCommaList' :: Regex a -> [Regex a]
expandedCommaList' :: GrammarRegex a -> [GrammarRegex a]
expandedCommaList' r =
[ REMunch reSpacedComma r
, reComma <> RESpaces <> REMunch1 reSpacedComma r
, REMunch1 reSpacedComma r <> RESpaces <> reComma
]

expandedOptCommaList :: Regex a -> Regex a
expandedOptCommaList :: GrammarRegex a -> GrammarRegex a
expandedOptCommaList r = REUnion $ reSpacedList r : expandedCommaList' r

-------------------------------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions buildinfo-reference-generator/template.zinza
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
.. _buildinfo-field-reference:

==================================================
BuildInfo field reference
==================================================
Field Syntax Reference
======================

Notation
---------------
Expand Down
Loading

0 comments on commit 65acfca

Please sign in to comment.