Skip to content

Commit

Permalink
fix: adjusted descriptions to match parsley:5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jan 15, 2024
1 parent 8bfe439 commit 26e1e66
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
37 changes: 20 additions & 17 deletions src/Text/Gigaparsec/Token/Descriptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,25 @@ plainNumeric = NumericDesc { literalBreakChar = NoBreakChar
, chars = ['e', 'E']
, base = 10
, expSign = PlusOptional
, expLeadingZerosAllowd = True
}
, hexadecimalExponentDesc = ExponentsSupported { compulsory = True
, chars = ['p', 'P']
, base = 2
, expSign = PlusOptional
, expLeadingZerosAllowd = True
}
, octalExponentDesc = ExponentsSupported { compulsory = True
, chars = ['e', 'E', 'p', 'P']
, base = 2
, expSign = PlusOptional
, expLeadingZerosAllowd = True
}
, binaryExponentDesc = ExponentsSupported { compulsory = True
, chars = ['e', 'E', 'p', 'P']
, base = 2
, expSign = PlusOptional
, expLeadingZerosAllowd = True
}
}

Expand All @@ -120,6 +124,7 @@ data ExponentDesc = NoExponents
, chars :: !(Set Char)
, base :: !Int
, expSign :: !PlusSignPresence
, expLeadingZerosAllowd :: !Bool
}

type BreakCharDesc :: *
Expand All @@ -134,24 +139,23 @@ data PlusSignPresence = PlusRequired | PlusOptional | PlusIllegal
type TextDesc :: *
data TextDesc = TextDesc { escapeSequences :: {-# UNPACK #-} !EscapeDesc
, characterLiteralEnd :: !Char
, stringEnds :: !(Set String)
, multiStringEnds :: !(Set String)
, stringEnds :: !(Set (String, String))
, multiStringEnds :: !(Set (String, String))
, graphicCharacter :: !CharPredicate
}

plainText :: TextDesc
plainText = TextDesc { escapeSequences = plainEscape
, characterLiteralEnd = '\''
, stringEnds = ["\""]
, stringEnds = [("\"", "\"")]
, multiStringEnds = []
, graphicCharacter = Just (>= ' ')
}

type EscapeDesc :: *
data EscapeDesc = EscapeDesc { escBegin :: !Char
, literals :: !(Set Char)
, singleMap :: !(Map Char Char)
, multiMap :: !(Map String Char)
, mapping :: !(Map String Char)
, decimalEscape :: !NumericEscape
, hexadecimalEscape :: !NumericEscape
, octalEscape :: !NumericEscape
Expand All @@ -163,8 +167,7 @@ data EscapeDesc = EscapeDesc { escBegin :: !Char
plainEscape :: EscapeDesc
plainEscape = EscapeDesc { escBegin = '\\'
, literals = ['\\']
, singleMap = []
, multiMap = []
, mapping = []
, decimalEscape = NumericIllegal
, hexadecimalEscape = NumericIllegal
, octalEscape = NumericIllegal
Expand All @@ -186,21 +189,21 @@ type NumberOfDigits :: *
data NumberOfDigits = Unbounded | Exactly !(NonEmpty Word) | AtMost !Word

type SpaceDesc :: *
data SpaceDesc = SpaceDesc { commentStart :: !String
, commentEnd :: !String
, commentLine :: !String
, commentLineAllowsEOF :: !Bool
, nestedComments :: !Bool
data SpaceDesc = SpaceDesc { lineCommentStart :: !String
, lineCommentAllowsEOF :: !Bool
, multiLineCommentStart :: !String
, multiLineCommentEnd :: !String
, multiLineNestedComments :: !Bool
, space :: !CharPredicate
, whitespaceIsContextDependent :: !Bool
}

plainSpace :: SpaceDesc
plainSpace = SpaceDesc { commentStart = ""
, commentEnd = ""
, commentLine = ""
, commentLineAllowsEOF = True
, nestedComments = False
plainSpace = SpaceDesc { lineCommentStart = ""
, lineCommentAllowsEOF = True
, multiLineCommentStart = ""
, multiLineCommentEnd = ""
, multiLineNestedComments = False
, space = Just isSpace
, whitespaceIsContextDependent = False
}
Expand Down
18 changes: 9 additions & 9 deletions src/Text/Gigaparsec/Token/Lexer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,33 +153,33 @@ We have the following invariances to be checked up front:
commentParser :: Desc.SpaceDesc -> Parsec ()
commentParser Desc.SpaceDesc{..} =
require (multiEnabled || singleEnabled) "skipComments" noComments $
require (not (multiEnabled && isPrefixOf commentStart commentLine)) "skipComments" noOverlap $
require (not (multiEnabled && isPrefixOf multiLineCommentStart lineCommentStart)) "skipComments" noOverlap $
hide (multiLine <|> singleLine)
where
-- can't make these strict until guard is gone
openComment = atomic (string commentStart)
closeComment = atomic (string commentEnd)
openComment = atomic (string multiLineCommentStart)
closeComment = atomic (string multiLineCommentEnd)
multiLine = guard multiEnabled *> openComment *> wellNested 1
wellNested :: Int -> Parsec ()
wellNested 0 = unit
wellNested n = closeComment *> wellNested (n - 1)
<|> guard nestedComments *> openComment *> wellNested (n + 1)
<|> guard multiLineNestedComments *> openComment *> wellNested (n + 1)
<|> item *> wellNested n
singleLine = guard singleEnabled
*> atomic (string commentLine)
*> atomic (string lineCommentStart)
*> skipManyTill item endOfLineComment

endOfLineComment
| commentLineAllowsEOF = void endOfLine <|> eof
| lineCommentAllowsEOF = void endOfLine <|> eof
| otherwise = void endOfLine

multiEnabled = not (null commentStart || null commentEnd)
singleEnabled = not (null commentLine)
multiEnabled = not (null multiLineCommentStart || null multiLineCommentEnd)
singleEnabled = not (null lineCommentStart)
noComments = "one of single- or multi-line comments must be enabled"
noOverlap = "single-line comments must not overlap with multi-line comments"

supportsComments :: Desc.SpaceDesc -> Bool
supportsComments Desc.SpaceDesc{..} = not (null commentLine && null commentStart)
supportsComments Desc.SpaceDesc{..} = not (null lineCommentStart && null multiLineCommentStart)

type UnsupportedOperation :: *
newtype UnsupportedOperation = UnsupportedOperation String deriving stock Eq
Expand Down
2 changes: 1 addition & 1 deletion src/Text/Gigaparsec/Token/Numeric.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE Safe #-}
{-# LANGUAGE DataKinds, KindSignatures, ConstraintKinds, MultiParamTypeClasses, AllowAmbiguousTypes, FlexibleInstances, FlexibleContexts, UndecidableInstances, ApplicativeDo #-}
-- TODO: refine
-- TODO: refine, move to Internal
module Text.Gigaparsec.Token.Numeric (module Text.Gigaparsec.Token.Numeric) where

import Text.Gigaparsec (Parsec, mapMaybeS, unit, void, atomic, (<|>), ($>))
Expand Down

0 comments on commit 26e1e66

Please sign in to comment.