Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ Ken Bateman <novadenizen@gmail.com>
Kido Takahiro <shelarcy@gmail.com>
Krasimir Angelov <kr.angelov@gmail.com>
Krasimir Angelov <kr.angelov@gmail.com> ka2_mail <ka2_mail@yahoo.com>
Léana <leana@jappiesoftware.com> Léana 江 <leana.jiang+git@icloud.com>
Léana <leana@jappiesoftware.com> Léana <leana.jiang+git@icloud.com>
Lennart Kolmodin <kolmodin@gmail.com> <kolmodin@dtek.chalmers.se>
Lennart Kolmodin <kolmodin@gmail.com> <kolmodin@gentoo.org>
Lennart Kolmodin <kolmodin@gmail.com> <kolmodin@google.com>
Expand Down
6 changes: 6 additions & 0 deletions .typos-srcs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ extend-exclude = [
"Cabal-tests/tests/ParserTests/errors/common1.cabal", # import: windo doesn't exist
"Cabal-tests/tests/ParserTests/errors/common1.errors",
"cabal-testsuite/PackageTests/ProjectImport/UniquePathDuplicates/www-stackage-org/lts-21.25.config",

# golden tests
"Cabal-tests/tests/ParserTests/comments/*.cabal",
"Cabal-tests/tests/ParserTests/regressions/*.cabal",
"Cabal-tests/tests/ParserTests/regressions/*.format",
"Cabal-tests/tests/ParserTests/regressions/*.expr",
]

[default]
Expand Down
1 change: 1 addition & 0 deletions Cabal-syntax/Cabal-syntax.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ library
Distribution.Fields
Distribution.Fields.ConfVar
Distribution.Fields.Field
Distribution.Fields.Field.Lens
Distribution.Fields.Lexer
Distribution.Fields.LexerMonad
Distribution.Fields.ParseResult
Expand Down
28 changes: 24 additions & 4 deletions Cabal-syntax/src/Distribution/Fields/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module Distribution.Fields.Field
, SectionArg (..)
, sectionArgAnn

-- * Comment
, Comment (..)
, WithComments (..)

-- * Name
, FieldName
, Name (..)
Expand All @@ -38,11 +42,27 @@ import Prelude ()
-- Cabal file
-------------------------------------------------------------------------------

-- | Store a line comment from field syntax files. @ann@ is usually instantiated as 'Position'.
data Comment ann = Comment !ByteString !ann
Comment thread
leana8959 marked this conversation as resolved.
Resolved
deriving (Show, Generic, Eq, Ord, Functor)

-- | Hold a list of comments along side some annotation.
--
-- @since 3.20
-- Note that this API is considered unstable and is subject to change until Cabal Exactprint is fully implemented.
data WithComments ann = WithComments
Comment thread
leana8959 marked this conversation as resolved.
Resolved
{ priorComments :: ![Comment ann]
, followingComments :: ![Comment ann]
, unComments :: !ann
-- ^ Extract the annotation.
}
deriving (Show, Generic, Eq, Ord, Functor)

-- | A Cabal-like file consists of a series of fields (@foo: bar@) and sections (@library ...@).
data Field ann
= Field !(Name ann) [FieldLine ann]
| Section !(Name ann) [SectionArg ann] [Field ann]
deriving (Eq, Show, Functor, Foldable, Traversable)
deriving (Eq, Show, Functor, Foldable, Traversable, Generic)
Comment thread
ulysses4ever marked this conversation as resolved.
Resolved

-- | @since 3.12.0.0
deriving instance Ord ann => Ord (Field ann)
Expand All @@ -67,7 +87,7 @@ fieldUniverse f@(Field _ _) = [f]
--
-- /Invariant:/ 'ByteString' has no newlines.
data FieldLine ann = FieldLine !ann !ByteString
deriving (Eq, Show, Functor, Foldable, Traversable)
deriving (Eq, Show, Functor, Foldable, Traversable, Generic)

-- | @since 3.12.0.0
deriving instance Ord ann => Ord (FieldLine ann)
Expand All @@ -88,7 +108,7 @@ data SectionArg ann
SecArgStr !ann !ByteString
| -- | everything else, mm. operators (e.g. in if-section conditionals)
SecArgOther !ann !ByteString
deriving (Eq, Show, Functor, Foldable, Traversable)
deriving (Eq, Show, Functor, Foldable, Traversable, Generic)

-- | @since 3.12.0.0
deriving instance Ord ann => Ord (SectionArg ann)
Expand All @@ -109,7 +129,7 @@ type FieldName = ByteString
--
-- /Invariant/: 'ByteString' is lower-case ASCII.
data Name ann = Name !ann !FieldName
deriving (Eq, Show, Functor, Foldable, Traversable)
deriving (Eq, Show, Functor, Foldable, Traversable, Generic)

-- | @since 3.12.0.0
deriving instance Ord ann => Ord (Name ann)
Expand Down
16 changes: 16 additions & 0 deletions Cabal-syntax/src/Distribution/Fields/Field/Lens.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Distribution.Fields.Field.Lens where

import Distribution.Compat.Lens
import qualified Distribution.Fields.Field as T

priorComments :: Lens' (T.WithComments ann) [T.Comment ann]
priorComments f s = fmap (\x -> s{T.priorComments = x}) (f (T.priorComments s))
{-# INLINE priorComments #-}

followingComments :: Lens' (T.WithComments ann) [T.Comment ann]
followingComments f s = fmap (\x -> s{T.followingComments = x}) (f (T.followingComments s))
{-# INLINE followingComments #-}

unComments :: Lens' (T.WithComments ann) ann
unComments f s = fmap (\x -> s{T.unComments = x}) (f (T.unComments s))
{-# INLINE unComments #-}
30 changes: 21 additions & 9 deletions Cabal-syntax/src/Distribution/Fields/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Distribution.Fields.Lexer
import Prelude ()
import qualified Prelude
import Distribution.Compat.Prelude
import Data.Char (isSpace)

import Distribution.Fields.LexerMonad
import Distribution.Parsec.Position (Position (..), incPos, retPos)
Expand All @@ -31,7 +32,6 @@ import qualified Data.ByteString.Char8 as B.Char8
import qualified Data.Word as Word

#ifdef CABAL_PARSEC_DEBUG
import Debug.Trace
import qualified Data.Vector as V
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
Expand Down Expand Up @@ -84,8 +84,16 @@ tokens :-
<bol_section, bol_field_layout, bol_field_braces> {
@nbspspacetab* @nl { \pos len inp -> checkWhitespace pos len inp >> adjustPos retPos >> lexToken }
-- no @nl here to allow for comments on last line of the file with no trailing \n
$spacetab* "--" $comment* ; -- TODO: check the lack of @nl works here
-- including counting line numbers
$spacetab* "--" $comment* { \pos len inp -> do
-- Remove leading space and move the position to the real start of the comment.
let (sp, cmt) = B.Char8.span isSpace inp
spByteLen = B.length sp
spCharLen <- checkLeadingWhitespace pos spByteLen sp
let pos' = incPos spCharLen pos
toki TokComment pos' (len - spByteLen) cmt
}
-- TODO: check the lack of @nl works here
-- including counting line numbers
}

<bol_section> {
Expand All @@ -105,9 +113,8 @@ tokens :-
}

<in_section> {
$spacetab+ ; --TODO: don't allow tab as leading space

"--" $comment* ;
$spacetab+ ; --TODO: don't allow tab as leading space
"--" $comment* { toki TokComment }
Comment thread
leana8959 marked this conversation as resolved.
Resolved

@name { toki TokSym }
@string { \pos len inp -> return $! L pos (TokStr (B.take (len - 2) (B.tail inp))) }
Expand Down Expand Up @@ -161,6 +168,7 @@ data Token = TokSym !ByteString -- ^ Haskell-like identifier, number or
| Colon
| OpenBrace
| CloseBrace
| TokComment !ByteString
| EOF
| LexicalError InputStream --TODO: add separate string lexical error
deriving Show
Expand Down Expand Up @@ -230,7 +238,9 @@ lexToken = do
setInput inp'
let !len_bytes = B.length inp - B.length inp'
t <- action pos len_bytes inp
--traceShow t $ return tok
#ifdef CABAL_PARSEC_DEBUG
traceShow t $ return t
#endif
return t


Expand All @@ -241,10 +251,12 @@ checkPosition pos@(Position lineno colno) inp inp' len_chars = do
let len_bytes = B.length inp - B.length inp'
pos_txt | lineno-1 < V.length text_lines = T.take len_chars (T.drop (colno-1) (text_lines V.! (lineno-1)))
| otherwise = T.empty
real_txt = B.take len_bytes inp
real_txt :: B.ByteString
real_txt = B.take len_bytes inp
when (pos_txt /= T.decodeUtf8 real_txt) $
traceShow (pos, pos_txt, T.decodeUtf8 real_txt) $
traceShow (take 3 (V.toList text_lines)) $ return ()
traceShow (take 3 (V.toList text_lines)) $
return ()
where
getDbgText = Lex $ \s@LexState{ dbgText = txt } -> LexResult s txt
#else
Expand Down
Loading
Loading