Skip to content

Commit

Permalink
feat(Token): numeric escape valid for unbounded
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jan 16, 2024
1 parent 626a1b0 commit 7c49140
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Text/Gigaparsec/Token/Descriptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ type NumericEscape :: *
data NumericEscape = NumericIllegal
| NumericSupported { prefix :: !(Maybe Char)
, numDigits :: !NumberOfDigits
, maxValue :: !Int
, maxValue :: !Char
}

type NumberOfDigits :: *
Expand Down
31 changes: 23 additions & 8 deletions src/Text/Gigaparsec/Token/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
-- TODO: refine, move to Internal
module Text.Gigaparsec.Token.Text (module Text.Gigaparsec.Token.Text) where

import Text.Gigaparsec (Parsec, void, (<|>), empty, filterS)
import Text.Gigaparsec (Parsec, void, (<|>), empty, filterS, mapMaybeS)
import Text.Gigaparsec.Char (char, digit, hexDigit, octDigit, bit, satisfy, trie)
import Text.Gigaparsec.Token.Descriptions (TextDesc(..), EscapeDesc(..), NumericEscape, CharPredicate)
import Text.Gigaparsec.Token.Descriptions (TextDesc(..), EscapeDesc(..), NumericEscape (NumericSupported, NumericIllegal, numDigits, maxValue, prefix), CharPredicate, NumberOfDigits (Exactly, AtMost, Unbounded))

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.2, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 8.10, Cabal 3.6

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC latest, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘EscapeDesc(..)’ does not have an explicit import list

Check warning on line 7 in src/Text/Gigaparsec/Token/Text.hs

View workflow job for this annotation

GitHub Actions / GHC 9.4, Cabal latest

The import item ‘TextDesc(..)’ does not have an explicit import list
import Text.Gigaparsec.Token.Generic (GenericNumeric(zeroAllowedDecimal, zeroAllowedHexadecimal, zeroAllowedOctal, zeroAllowedBinary))
import Data.Char (isSpace)
import Data.Map qualified as Map (insert, null, map)
import Data.Char (isSpace, chr, ord)
import Data.Map qualified as Map (insert, map)
import Data.List.NonEmpty (NonEmpty((:|)))

type TextParsers :: * -> *
data TextParsers t = TextParsers { unicode :: Parsec t
Expand Down Expand Up @@ -54,9 +55,7 @@ mkEscape EscapeDesc{..} gen = Escape {..}
escapeChar = escapeBegin *> escapeCode

escs = foldr (\c -> Map.insert [c] c) mapping literals
escMapped
| Map.null escs = empty
| otherwise = trie (Map.map pure escs)
escMapped = trie (Map.map pure escs)

numericEscape = decimalEsc <|> hexadecimalEsc <|> octalEsc <|> binaryEsc

Expand All @@ -65,8 +64,24 @@ mkEscape EscapeDesc{..} gen = Escape {..}
octalEsc = fromDesc 8 octalEscape (zeroAllowedOctal gen) octDigit
binaryEsc = fromDesc 2 binaryEscape (zeroAllowedBinary gen) bit

boundedChar :: Parsec Integer -> Char -> Maybe Char -> Int -> Parsec Char
boundedChar p maxValue prefix _radix = foldr (\c t -> char c *> t) (mapMaybeS f p) prefix
where f c
| c < toInteger (ord maxValue) = Just (chr (fromInteger c))
| otherwise = Nothing

atMost :: Word -> Int -> Parsec Char -> Parsec Integer
atMost _ _ _ = empty -- TODO:

oneOfExactly :: Word -> [Word] -> Int -> Parsec Char -> Parsec Integer
oneOfExactly _ _ _ _ = empty --TODO:

fromDesc :: Int -> NumericEscape -> Parsec Integer -> Parsec Char -> Parsec Char
fromDesc radix _ integer digit = undefined --TODO:
fromDesc !_ NumericIllegal !_ !_ = empty
fromDesc radix NumericSupported{..} integer dig = case numDigits of
Unbounded -> boundedChar integer maxValue prefix radix
AtMost n -> boundedChar (atMost n radix dig) maxValue prefix radix
Exactly (n :| ns) -> boundedChar (oneOfExactly n ns radix digit) maxValue prefix radix

lexemeText :: (forall a. Parsec a -> Parsec a) -> TextParsers t -> TextParsers t
lexemeText lexe TextParsers{..} = TextParsers {
Expand Down

0 comments on commit 7c49140

Please sign in to comment.