-
Notifications
You must be signed in to change notification settings - Fork 696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fields parser #4702
Fields parser #4702
Conversation
@phadej, thanks for your PR! By analyzing the history of the files in this pull request, we identified @ezyang, @23Skidoo and @Ericson2314 to be potential reviewers. |
|
||
sourceRepoFieldDescrs :: [FieldDescr SourceRepo] | ||
sourceRepoFieldDescrs = | ||
[ simpleField "type" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like this boilerplate
<*> affineFieldAla "module" Token repoModule | ||
<*> affineFieldAla "branch" Token repoBranch | ||
<*> affineFieldAla "tag" Token repoTag | ||
<*> affineFieldAla "subdir" FilePathNT repoSubdir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the applicative value will live somewhere else, as we'll need it for pretty-printer
uniqueField = error "define me" | ||
|
||
-- | Field which can be defined at most once. | ||
affineField |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll rename to optionalField
parser fields = case Map.lookup fn fields of | ||
Nothing -> pure Nothing | ||
Just [] -> pure Nothing | ||
Just [MkNamelessField pos fls] -> Just . (unpack :: b -> a) <$> runFieldParser pos parsec fls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: if many fields, take last.
-- Newtype | ||
------------------------------------------------------------------------------- | ||
|
||
class Newtype n o | n -> o where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The core point is that "if you add a field, |
@@ -297,7 +300,18 @@ parseGenericPackageDescription' lexWarnings fs = do | |||
_ -> do | |||
parseFailure pos $ "Invalid source-repository kind " ++ show args | |||
pure RepoHead | |||
sr <- parseFields sourceRepoFieldDescrs warnUnrec (emptySourceRepo kind) fields |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also note that one won't need fishy emptySourceRepo
anymore, and similars for other types!
pack = FilePathNT | ||
unpack = getFilePathNT | ||
|
||
instance Text FilePathNT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Text instances will be replaced with Display
class (I once tried to add, but I have to try again!)
Cabal/Distribution/Compat/Newtype.hs
Outdated
-- functions such as 'ala'. | ||
module Distribution.Compat.Newtype where | ||
|
||
-- TODO: export only Newtype (..), ala |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please!
Cabal/Distribution/Compat/Newtype.hs
Outdated
ala' _ hof f = unpack . hof (pack . f) | ||
|
||
------------------------------------------------------------------------------- | ||
-- Move to own module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please.
f2dc34a
to
73304e6
Compare
<*> optionalFieldAla "module" Token repoModule | ||
<*> optionalFieldAla "branch" Token repoBranch | ||
<*> optionalFieldAla "tag" Token repoTag | ||
<*> optionalFieldAla "subdir" FilePathNT repoSubdir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this patch thunk is the reason for new functionality.
f1a7752
to
60e0f79
Compare
690a725
to
4d6057b
Compare
This PR is ready. I'll write a changelog stub separately. |
4d6057b
to
80c19b4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a big patch, and tbh, it had a bit of noise (import-list & pragma-list normalisations) that made it more difficult to focus on what matters.
Btw, now I understand why you'd want a parser abstraction along the lines of http://hackage.haskell.org/package/parsers...
However, I couldn't see anything obviously bad, so this looks good for me. Afaik, @23Skidoo wanted to give this PR a review as well.
I usually just ignore this stuff. If something's wrong with it, CI will tell you. |
@23Skidoo are you planning to review this, or should I merge? |
@phadej Sorry for the delay, I'll try to finish the review today. |
-- Fields can be specified multiple times in the .cabal files. The order of | ||
-- such entries is important, but the mutual ordering of different fields is | ||
-- not.Also conditional sections are considered after non-conditional data. | ||
-- The example of this silent-commutation quirck is the fact that |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/quirck/quirk/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing job! LGTM modulo minor comments. Sorry for procrastinating with the review.
@@ -274,18 +277,22 @@ library | |||
parsec >= 3.1.9 && <3.2 | |||
exposed-modules: | |||
Distribution.Compat.Parsec | |||
Distribution.FieldGrammar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, can this stuff live under e.g. Distribution.Parsec.FieldGrammar
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's both related to Parsing and Pretty-printing, and actually will be a little related to command line parsing too.
Cabal/tests/ParserTests.hs
Outdated
x <- parse contents | ||
let contents' = showGenericPackageDescription x | ||
y <- parse (toUTF8BS contents') | ||
let y' = if x ^. L.packageDescription . L.license == UnspecifiedLicense |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a comment here explaining why special-casing of UnknownLicense "UnspecifiedLicense"
is needed here and whether we can make it go away in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will hopefully go away in SPDX change. The short explanation is that parse . pretty
roundtrip on License
doesn't hold ATM. Yet Hackage never accepts a package with UnspecifiedLicense
, so I didn't bother to refactor it yet.
Cabal/tests/ParserHackageTests.hs
Outdated
y0 <- parse "2nd" (toUTF8BS bs') | ||
|
||
-- unspecified license | ||
let y1 = if x0 ^. L.packageDescription . L.license == UnspecifiedLicense |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some duplication here with the ParserTests
module. Maybe worth consolidating various data scrubbing utilities used in the parser test suite in a single place somewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll think about license stuff particularly when doing SPDX change, but yeah, there are commonalities. I'll factor them out in separate PR.
Cabal/Distribution/Pretty.hs
Outdated
|
||
showToken :: String -> PP.Doc | ||
showToken str | ||
| "--" `isPrefixOf` str = PP.text (show str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice with a comment here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a comment, but --foo
looks like a comment.
Cabal/Distribution/FieldGrammar.hs
Outdated
(^^^) :: a -> (a -> b) -> b | ||
x ^^^ f = f x | ||
|
||
-- | Partitionin state |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Partititionin/Partitioning/.
I'll fix typos and add comments tomorrow. If there is no objection I'll merge this after that. |
This commit reworks how GenericPackageDescription is parsed from `[Field Position]` and pretty-printed to `Doc`. This also fixes few issues: - Fix haskell#4697: `cabal format` doesn't output custom-setup stanza (nor foreign-lib stanzas) - Fix haskell#4719: `parse . pretty . parse = parse` for all Hackage cabal files. - `parser-hackage-tests roundtrip` is the test program. The handling of `license-file` and `license-files` is changed. Now they behave the same.
80c19b4
to
66e2232
Compare
this is PoC of approach to parse fields
The difference is visible in
FieldDescr.hs
andCabal/Distribution/PackageDescription/Parsec.hs
Please include the following checklist in your PR:
Please also shortly describe how you tested your change. Bonus points for added tests!