Repository navigation
Open
Description
-
The grammar is written in
cabal/Cabal/Distribution/Parsec/Parser.hs
Lines 159 to 206 in 76183b4
-- $grammar -- -- @ -- CabalStyleFile ::= SecElems -- -- SecElems ::= SecElem* '\n'? -- SecElem ::= '\n' SecElemLayout | SecElemBraces -- SecElemLayout ::= FieldLayout | FieldBraces | SectionLayout | SectionBraces -- SecElemBraces ::= FieldInline | FieldBraces | SectionBraces -- FieldLayout ::= name ':' line? ('\n' line)* -- FieldBraces ::= name ':' '\n'? '{' content '}' -- FieldInline ::= name ':' content -- SectionLayout ::= name arg* SecElems -- SectionBraces ::= name arg* '\n'? '{' SecElems '}' -- @ -- -- and the same thing but left factored... -- -- @ -- SecElems ::= SecElem* -- SecElem ::= '\n' name SecElemLayout -- | name SecElemBraces -- SecElemLayout ::= ':' FieldLayoutOrBraces -- | arg* SectionLayoutOrBraces -- FieldLayoutOrBraces ::= '\n'? '{' content '}' -- | line? ('\n' line)* -- SectionLayoutOrBraces ::= '\n'? '{' SecElems '\n'? '}' -- | SecElems -- SecElemBraces ::= ':' FieldInlineOrBraces -- | arg* '\n'? '{' SecElems '\n'? '}' -- FieldInlineOrBraces ::= '\n'? '{' content '}' -- | content -- @ -- -- Note how we have several productions with the sequence: -- -- > '\n'? '{' -- -- That is, an optional newline (and indent) followed by a @{@ token. -- In the @SectionLayoutOrBraces@ case you can see that this makes it -- not fully left factored (because @SecElems@ can start with a @\n@). -- Fully left factoring here would be ugly, and though we could use a -- lookahead of two tokens to resolve the alternatives, we can't -- conveniently use Parsec's 'try' here to get a lookahead of only two. -- So instead we deal with this case in the lexer by making a line -- where the first non-space is @{@ lex as just the @{@ token, without -- the usual indent token. Then in the parser we can resolve everything -- with just one token of lookahead and so without using 'try'. - it uses ad-hoc formalism
- something like Indentation-Sensitive PEG from Indendation Sensitive Parsing for Parsec* https://dl.acm.org/citation.cfm?doid=2633357.2633369 should work better
- https://github.com/phadej/cabal-refact/blob/master/src/Distribution/Refact/Parser.hs is one my experiment, it doesn't support
{ }(cabal files could have braces) IIRC. - https://github.com/haskell/cabal/blob/master/boot/Lexer.x is useful too
- I think both
trifecta-indentationandmegaparsecshould work.
-
This is better done as a separate package in this repo
cabal-trifectaorcabal-megaparsec, as there are technical limitations of having this as test-suite inCabalpackage.
cc @sboosali
Activity
hvr commentedon Dec 20, 2017
Could we use that as basis for a exactprint-style parser?
phadej commentedon Dec 20, 2017
@hvr,
Fieldomits comments. I'd rather not to deal with them in this.exactprint-style parser can of course be a fork, if it's suitable, but it's not a goal.sboosali commentedon Dec 20, 2017