Common.hs 9.0 KB

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