Common.hs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. module Data.SCargot.Common ( -- $intro
  2. -- * Lisp Identifier Syntaxes
  3. parseR5RSIdent
  4. , parseR6RSIdent
  5. , parseR7RSIdent
  6. -- * Numeric Literal Parsers
  7. , binNumber
  8. , signedBinNumber
  9. , octNumber
  10. , signedOctNumber
  11. , decNumber
  12. , signedDecNumber
  13. , dozNumber
  14. , signedDozNumber
  15. , hexNumber
  16. , signedHexNumber
  17. , signed
  18. ) where
  19. import Data.Char
  20. import Data.Text (Text)
  21. import qualified Data.Text as T
  22. import Text.Parsec
  23. import Text.Parsec.Char (satisfy)
  24. import Text.Parsec.Text (Parser)
  25. -- | Parse an identifier according to the R5RS Scheme standard. This
  26. -- will not normalize case, even though the R5RS standard specifies
  27. -- that all identifiers be normalized to lower case first.
  28. --
  29. -- An R5RS identifier is, broadly speaking, alphabetic or numeric
  30. -- and may include various symbols, but no escapes.
  31. parseR5RSIdent :: Parser Text
  32. parseR5RSIdent =
  33. T.pack <$> ((:) <$> initial <*> many subsequent <|> peculiar)
  34. where initial = letter <|> oneOf "!$%&*/:<=>?^_~"
  35. subsequent = initial <|> digit <|> oneOf "+-.@"
  36. peculiar = string "+" <|> string "-" <|> string "..."
  37. hasCategory :: Char -> [GeneralCategory] -> Bool
  38. hasCategory c cs = generalCategory c `elem` cs
  39. -- | Parse an identifier according to the R6RS Scheme standard. An
  40. -- R6RS identifier may include inline hexadecimal escape sequences
  41. -- so that, for example, @foo@ is equivalent to @f\x6f;o@, and is
  42. -- more liberal than R5RS as to which Unicode characters it may
  43. -- accept.
  44. parseR6RSIdent :: Parser Text
  45. parseR6RSIdent =
  46. T.pack <$> ((:) <$> initial <*> many subsequent <|> peculiar)
  47. where initial = constituent <|> oneOf "!$%&*/:<=>?^_~" <|> inlineHex
  48. constituent = letter
  49. <|> uniClass (\ c -> isLetter c ||
  50. isSymbol c ||
  51. hasCategory c
  52. [ NonSpacingMark
  53. , LetterNumber
  54. , OtherNumber
  55. , DashPunctuation
  56. , ConnectorPunctuation
  57. , OtherPunctuation
  58. , PrivateUse
  59. ])
  60. inlineHex = (chr . fromIntegral) <$> (string "\\x" *> hexNumber <* char ';')
  61. subsequent = initial <|> digit <|> oneOf "+-.@"
  62. <|> uniClass (\ c -> hasCategory c
  63. [ DecimalNumber
  64. , SpacingCombiningMark
  65. , EnclosingMark
  66. ])
  67. peculiar = string "+" <|> string "-" <|> string "..." <|>
  68. ((++) <$> string "->" <*> many subsequent)
  69. uniClass :: (Char -> Bool) -> Parser Char
  70. uniClass sp = satisfy (\ c -> c > '\x7f' && sp c)
  71. -- | Parse an identifier according to the R7RS Scheme standard. An
  72. -- R7RS identifier, in addition to a typical identifier format,
  73. -- can also be a chunk of text surrounded by vertical bars that
  74. -- can contain spaces and other characters. Unlike R6RS, it does
  75. -- not allow escapes to be included in identifiers unless those
  76. -- identifiers are surrounded by vertical bars.
  77. parseR7RSIdent :: Parser Text
  78. parseR7RSIdent = T.pack <$>
  79. ( (:) <$> initial <*> many subsequent
  80. <|> char '|' *> many1 symbolElement <* char '|'
  81. <|> peculiar
  82. )
  83. where initial = letter <|> specInit
  84. specInit = oneOf "!$%&*/:<=>?^_~"
  85. subsequent = initial <|> digit <|> specSubsequent
  86. specSubsequent = expSign <|> oneOf ".@"
  87. expSign = oneOf "+-"
  88. symbolElement = noneOf "\\|"
  89. <|> hexEscape
  90. <|> mnemEscape
  91. <|> ('|' <$ string "\\|")
  92. hexEscape = chr . fromIntegral <$> (string "\\x" *> hexNumber <* char ';')
  93. mnemEscape = '\a' <$ string "\\a"
  94. <|> '\b' <$ string "\\b"
  95. <|> '\t' <$ string "\\t"
  96. <|> '\n' <$ string "\\n"
  97. <|> '\r' <$ string "\\r"
  98. peculiar = (:[]) <$> expSign
  99. <|> cons2 <$> expSign <*> signSub <*> many subsequent
  100. <|> cons3 <$> expSign
  101. <*> char '.'
  102. <*> dotSub
  103. <*> many subsequent
  104. <|> cons2 <$> char '.' <*> dotSub <*> many subsequent
  105. dotSub = signSub <|> char '.'
  106. signSub = initial <|> expSign <|> char '@'
  107. cons2 a b cs = a : b : cs
  108. cons3 a b c ds = a : b : c : ds
  109. -- | A helper function for defining parsers for arbitrary-base integers.
  110. -- The first argument will be the base, and the second will be the
  111. -- parser for the individual digits.
  112. number :: Integer -> Parser Char -> Parser Integer
  113. number base digits = foldl go 0 <$> many1 digits
  114. where go x d = base * x + toInteger (value d)
  115. value c
  116. | c == 'a' || c == 'A' = 0xa
  117. | c == 'b' || c == 'B' = 0xb
  118. | c == 'c' || c == 'C' = 0xc
  119. | c == 'd' || c == 'D' = 0xd
  120. | c == 'e' || c == 'E' = 0xe
  121. | c == 'f' || c == 'F' = 0xf
  122. | c >= '0' && c <= '9' = fromEnum c - fromEnum '0'
  123. | c == '\x218a' = 0xa
  124. | c == '\x218b' = 0xb
  125. | otherwise = error ("Unknown letter in number: " ++ show c)
  126. sign :: Num a => Parser (a -> a)
  127. sign = (pure id <* char '+')
  128. <|> (pure negate <* char '-')
  129. <|> pure id
  130. -- | Given a parser for some kind of numeric literal, this will attempt to
  131. -- parse a leading @+@ or a leading @-@ followed by the numeric literal,
  132. -- and if a @-@ is found, negate that literal.
  133. signed :: Num a => Parser a -> Parser a
  134. signed p = ($) <$> sign <*> p
  135. -- | A parser for non-signed binary numbers
  136. binNumber :: Parser Integer
  137. binNumber = number 2 (char '0' <|> char '1')
  138. -- | A parser for signed binary numbers, with an optional leading @+@ or @-@.
  139. signedBinNumber :: Parser Integer
  140. signedBinNumber = ($) <$> sign <*> binNumber
  141. -- | A parser for non-signed octal numbers
  142. octNumber :: Parser Integer
  143. octNumber = number 8 (oneOf "01234567")
  144. -- | A parser for signed octal numbers, with an optional leading @+@ or @-@.
  145. signedOctNumber :: Parser Integer
  146. signedOctNumber = ($) <$> sign <*> octNumber
  147. -- | A parser for non-signed decimal numbers
  148. decNumber :: Parser Integer
  149. decNumber = number 10 digit
  150. -- | A parser for signed decimal numbers, with an optional leading @+@ or @-@.
  151. signedDecNumber :: Parser Integer
  152. signedDecNumber = ($) <$> sign <*> decNumber
  153. dozDigit = digit <|> oneOf "AaBb\x218a\x218b"
  154. -- | A parser for non-signed duodecimal (dozenal) numbers. This understands both
  155. -- the ASCII characters @'a'@ and @'b'@ and the Unicode characters @'\x218a'@ (↊)
  156. -- and @'\x218b'@ (↋) as digits with the decimal values @10@ and @11@
  157. -- respectively.
  158. dozNumber :: Parser Integer
  159. dozNumber = number 12 dozDigit
  160. -- | A parser for signed duodecimal (dozenal) numbers, with an optional leading @+@ or @-@.
  161. signedDozNumber :: Parser Integer
  162. signedDozNumber = ($) <$> sign <*> dozNumber
  163. -- | A parser for non-signed hexadecimal numbers
  164. hexNumber :: Parser Integer
  165. hexNumber = number 16 hexDigit
  166. -- | A parser for signed hexadecimal numbers, with an optional leading @+@ or @-@.
  167. signedHexNumber :: Parser Integer
  168. signedHexNumber = ($) <$> sign <*> hexNumber
  169. {- $intro
  170. This module contains a selection of parsers for different kinds of
  171. identifiers and literals, from which more elaborate parsers can be
  172. assembled. These can afford the user a quick way of building parsers
  173. for different atom types.
  174. -}