Prototype, extremely bad code implementation of CCHM Cubical Type Theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
6.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. {-# LANGUAGE BangPatterns #-}
  2. module Presyntax.Lexer where
  3. import qualified Data.Text as T
  4. import Data.Text (Text)
  5. import Data.Char
  6. {- HLINT ignore -}
  7. data TokenClass
  8. = Tok_var Text
  9. | Tok_lambda
  10. | Tok_type
  11. | Tok_typeω
  12. | Tok_path
  13. | Tok_Partial
  14. | Tok_PartialP
  15. | Tok_sub
  16. | Tok_comp
  17. | Tok_tr
  18. | Tok_I
  19. | Tok_I0
  20. | Tok_I1
  21. | Tok_Glue
  22. | Tok_glue
  23. | Tok_unglue
  24. | Tok_bool
  25. | Tok_tt
  26. | Tok_ff
  27. | Tok_if
  28. | Tok_oparen
  29. | Tok_cparen
  30. | Tok_osquare
  31. | Tok_csquare
  32. | Tok_colon
  33. | Tok_arrow
  34. | Tok_let
  35. | Tok_equal
  36. | Tok_in
  37. | Tok_and
  38. | Tok_not
  39. | Tok_or
  40. | Tok_fand
  41. | Tok_for
  42. | Tok_assume
  43. | Tok_p1
  44. | Tok_p2
  45. | Tok_comma
  46. | Tok_times
  47. deriving (Eq, Show, Ord)
  48. data Token
  49. = Token { tokLine :: {-# UNPACK #-} !Int
  50. , tokCol :: {-# UNPACK #-} !Int
  51. , tokSOff :: {-# UNPACK #-} !Int
  52. , tokOff :: {-# UNPACK #-} !Int
  53. , tokClass :: !TokenClass
  54. }
  55. deriving (Eq, Show, Ord)
  56. data LexError
  57. = LexError { leChar :: {-# UNPACK #-} !Char
  58. , leLine :: {-# UNPACK #-} !Int
  59. , leCol :: {-# UNPACK #-} !Int
  60. }
  61. | EOFInComment { leLine :: {-# UNPACK #-} !Int
  62. , leCol :: {-# UNPACK #-} !Int
  63. }
  64. deriving (Eq, Show, Ord)
  65. lexString :: String -> Either LexError [Token]
  66. lexString = go 0 0 0 where
  67. go :: Int -> Int -> Int -> String -> Either LexError [Token]
  68. go !off !line !_ ('\n':xs) =
  69. go (off + 1) (line + 1) 0 xs
  70. go !off !line !col (' ':xs) =
  71. go (off + 1) line (col + 1) xs
  72. go !off !line !_ ('-':'-':xs) =
  73. let (a, b) = span (/= '\n') xs
  74. in go (off + length a) line 0 b
  75. go !off !line !col ('{':'-':xs) = skipComment off line col 1 xs
  76. go !off !line !col ('~':cs) =
  77. Token line col off (off + 1) Tok_not `yield` go (off + 1) line (col + 1) cs
  78. go !off !line !col ('(':cs) =
  79. Token line col off (off + 1) Tok_oparen `yield` go (off + 1) line (col + 1) cs
  80. go !off !line !col (')':cs) =
  81. Token line col off (off + 1) Tok_cparen `yield` go (off + 1) line (col + 1) cs
  82. go !off !line !col ('[':cs) =
  83. Token line col off (off + 1) Tok_osquare `yield` go (off + 1) line (col + 1) cs
  84. go !off !line !col (']':cs) =
  85. Token line col off (off + 1) Tok_csquare `yield` go (off + 1) line (col + 1) cs
  86. go !off !line !col (':':cs) =
  87. Token line col off (off + 1) Tok_colon `yield` go (off + 1) line (col + 1) cs
  88. go !off !line !col (',':cs) =
  89. Token line col off (off + 1) Tok_comma `yield` go (off + 1) line (col + 1) cs
  90. go !off !line !col ('=':cs) =
  91. Token line col off (off + 1) Tok_equal `yield` go (off + 1) line (col + 1) cs
  92. go !off !line !col ('\\':'/':cs) =
  93. Token line col off (off + 2) Tok_for `yield` go (off + 2) line (col + 2) cs
  94. go !off !line !col ('\\':cs) =
  95. Token line col off (off + 1) Tok_lambda `yield` go (off + 1) line (col + 1) cs
  96. go !off !line !col ('*':cs) =
  97. Token line col off (off + 1) Tok_times `yield` go (off + 1) line (col + 1) cs
  98. go !off !line !col ('λ':cs) =
  99. Token line col off (off + 1) Tok_lambda `yield` go (off + 1) line (col + 1) cs
  100. go !off !line !col ('→':cs) =
  101. Token line col off (off + 1) Tok_lambda `yield` go (off + 1) line (col + 1) cs
  102. go !off !line !col ('-':'>':cs) =
  103. Token line col off (off + 2) Tok_arrow `yield` go (off + 2) line (col + 2) cs
  104. go !off !line !col ('&':'&':cs) =
  105. Token line col off (off + 2) Tok_and `yield` go (off + 2) line (col + 2) cs
  106. go !off !line !col ('&':cs) =
  107. Token line col off (off + 1) Tok_fand `yield` go (off + 1) line (col + 1) cs
  108. go !off !line !col ('/':'\\':cs) =
  109. Token line col off (off + 1) Tok_fand `yield` go (off + 1) line (col + 1) cs
  110. go !off !line !col ('|':'|':cs) =
  111. Token line col off (off + 2) Tok_or `yield` go (off + 2) line (col + 2) cs
  112. go !off !line !col ('|':cs) =
  113. Token line col off (off + 1) Tok_for `yield` go (off + 1) line (col + 1) cs
  114. go !off !line !col ('.':'1':cs) =
  115. Token line col off (off + 2) Tok_p1 `yield` go (off + 2) line (col + 2) cs
  116. go !off !line !col ('.':'2':cs) =
  117. Token line col off (off + 2) Tok_p2 `yield` go (off + 2) line (col + 2) cs
  118. go !off !line !col (c:cs)
  119. | isAlpha c = goIdent off off line col (T.singleton c) cs
  120. go !_ !line !col (c:_) = Left (LexError c line col)
  121. go _ _ _ [] = pure []
  122. goIdent !soff !off !line !col !acc [] =
  123. pure [Token line col soff off (finishIdent acc)]
  124. goIdent !soff !off !line !col !acc (c:cs)
  125. | isAlphaNum c || c == '\''
  126. = goIdent soff (off + 1) line (col + 1) (T.snoc acc c) cs
  127. | otherwise
  128. = Token line col soff off (finishIdent acc) `yield` go (off + 1) line (col + 1) (c:cs)
  129. skipComment off line col level ('-':'}':cs)
  130. | level == 1 = go (off + 2) line (col + 2) cs
  131. | otherwise = skipComment (off + 2) line (col + 2) (level - 1) cs
  132. skipComment off line col level ('{':'-':cs) =
  133. skipComment (off + 2) line (col + 2) (level + 1) cs
  134. skipComment off line col level ('\n':cs) =
  135. skipComment (off + 1) (line + 1) 0 level cs
  136. skipComment off line col level (c:cs) =
  137. skipComment (off + 1) line (col + 1) level cs
  138. skipComment _ line col _ [] = Left (EOFInComment line col)
  139. yield c = fmap (c:)
  140. finishIdent c
  141. | T.pack "Type" == c = Tok_type
  142. | T.pack "Typeω" == c || T.pack "Pretype" == c = Tok_typeω
  143. | T.pack "Path" == c = Tok_path
  144. | T.pack "Partial" == c = Tok_Partial
  145. | T.pack "PartialP" == c = Tok_PartialP
  146. | T.pack "Sub" == c = Tok_sub
  147. | T.pack "comp" == c = Tok_comp
  148. | T.pack "Glue" == c = Tok_Glue
  149. | T.pack "glue" == c = Tok_glue
  150. | T.pack "unglue" == c = Tok_unglue
  151. | T.pack "Bool" == c = Tok_bool
  152. | T.pack "tt" == c = Tok_tt
  153. | T.pack "ff" == c = Tok_ff
  154. | T.pack "if" == c = Tok_if
  155. | T.pack "tr" == c = Tok_tr
  156. | T.pack "I" == c = Tok_I
  157. | T.pack "i0" == c = Tok_I0
  158. | T.pack "i1" == c = Tok_I1
  159. | T.pack "let" == c = Tok_let
  160. | T.pack "in" == c = Tok_in
  161. | T.pack "assume" == c = Tok_assume
  162. | otherwise = Tok_var c