-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLex.x.patch
254 lines (231 loc) · 8.28 KB
/
Lex.x.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--- src/Ocaml/Lex.x 2024-02-14 08:29:19
+++ src/Ocaml/Lex.x.bak 2024-02-14 08:29:19
@@ -17,6 +17,8 @@
import Data.Word (Word8)
}
+%wrapper "monadUserState"
+
-- Predefined character classes
$c = [A-Z\192-\221] # [\215] -- capital isolatin1 letter (215 = \times) FIXME
@@ -151,10 +153,18 @@
<0> $l $i*
{ tok (eitherResIdent TV) }
+<0> "(*" { beginComment `andBegin` comment }
+<0> "{" [a-z_]* "|" { beginQuotedString `andBegin` quoted }
+<comment> "(*" { beginComment }
+<comment> "*)" { endComment }
+<comment> (. | \n) ;
+<quoted> "|" [a-z_]* "}" { endQuotedString }
+<quoted> (. | \n) { quotedChar }
+
{
-- | Create a token with position.
-tok :: (String -> Tok) -> (Posn -> String -> Token)
-tok f p = PT p . f
+tok :: (String -> Tok) -> AlexInput -> Int -> Alex Token
+tok f (p, _, _, input) len = return (PT p (f (take len input)))
-- | Token without position.
data Tok
@@ -213,31 +223,34 @@
-- | Token with position.
data Token
- = PT Posn Tok
- | Err Posn
- deriving (Eq, Show, Ord)
+ = PT AlexPosn Tok
+ | Err AlexPosn
+ | EOF
+ deriving (Eq, Show)
-- | Pretty print a position.
-printPosn :: Posn -> String
-printPosn (Pn _ l c) = "line " ++ show l ++ ", column " ++ show c
+printPosn :: AlexPosn -> String
+printPosn (AlexPn _ l c) = "line " ++ show l ++ ", column " ++ show c
-- | Pretty print the position of the first token in the list.
tokenPos :: [Token] -> String
-tokenPos (t:_) = printPosn (tokenPosn t)
-tokenPos [] = "end of file"
+tokenPos (PT p _:_) = printPosn p
+tokenPos (Err p:_) = printPosn p
+tokenPos [] = "end of file"
-- | Get the position of a token.
-tokenPosn :: Token -> Posn
+tokenPosn :: Token -> AlexPosn
tokenPosn (PT p _) = p
tokenPosn (Err p) = p
+tokenPosn EOF = error "no position"
-- | Get line and column of a token.
tokenLineCol :: Token -> (Int, Int)
tokenLineCol = posLineCol . tokenPosn
-- | Get line and column of a position.
-posLineCol :: Posn -> (Int, Int)
-posLineCol (Pn _ l c) = (l,c)
+posLineCol :: AlexPosn -> (Int, Int)
+posLineCol (AlexPn _ l c) = (l,c)
-- | Convert a token into "position token" form.
mkPosToken :: Token -> ((Int, Int), String)
@@ -368,85 +381,110 @@
where
bs = s
--- | Unquote string literal.
-unescapeInitTail :: String -> String
-unescapeInitTail = id . unesc . tail . id
- where
- unesc s = case s of
- '\\':c:cs | elem c ['\"', '\\', '\''] -> c : unesc cs
- '\\':'n':cs -> '\n' : unesc cs
- '\\':'t':cs -> '\t' : unesc cs
- '\\':'r':cs -> '\r' : unesc cs
- '\\':'f':cs -> '\f' : unesc cs
- '"':[] -> []
- c:cs -> c : unesc cs
- _ -> []
-
-------------------------------------------------------------------
-- Alex wrapper code.
-- A modified "posn" wrapper.
-------------------------------------------------------------------
-data Posn = Pn !Int !Int !Int
- deriving (Eq, Show, Ord)
+tokens :: String -> [Token]
+tokens str =
+ case runAlex str go of
+ Left message -> error message
+ Right ts -> ts
+ where
+ go :: Alex [Token]
+ go = do
+ t <- alexMonadScan
+ case t of
+ EOF -> return []
+ Err _ -> return [t]
+ _ -> fmap (t:) go
-alexStartPos :: Posn
-alexStartPos = Pn 0 1 1
+data AlexUserState = AlexUserState
+ { commentNesting :: Int
+ , quotedString :: Maybe String
+ , quotedStringContent :: String
+ }
-alexMove :: Posn -> Char -> Posn
-alexMove (Pn a l c) '\t' = Pn (a+1) l (((c+7) `div` 8)*8+1)
-alexMove (Pn a l c) '\n' = Pn (a+1) (l+1) 1
-alexMove (Pn a l c) _ = Pn (a+1) l (c+1)
+alexInitUserState :: AlexUserState
+alexInitUserState = AlexUserState 0 Nothing ""
-type Byte = Word8
+getCommentNesting :: Alex Int
+getCommentNesting = commentNesting <$> alexGetUserState
-type AlexInput = (Posn, -- current position,
- Char, -- previous char
- [Byte], -- pending bytes on the current char
- String) -- current input string
+setCommentNesting :: Int -> Alex ()
+setCommentNesting nesting = do
+ ust <- alexGetUserState
+ alexSetUserState ust{ commentNesting = nesting }
-tokens :: String -> [Token]
-tokens str = go (alexStartPos, '\n', [], str)
- where
- go :: AlexInput -> [Token]
- go inp@(pos, _, _, str) =
- case alexScan inp 0 of
- AlexEOF -> []
- AlexError (pos, _, _, _) -> [Err pos]
- AlexSkip inp' len -> go inp'
- AlexToken inp' len act -> act pos (take len str) : (go inp')
+getQuotedString :: Alex (Maybe String)
+getQuotedString = quotedString <$> alexGetUserState
-alexGetByte :: AlexInput -> Maybe (Byte,AlexInput)
-alexGetByte (p, c, (b:bs), s) = Just (b, (p, c, bs, s))
-alexGetByte (p, _, [], s) =
- case s of
- [] -> Nothing
- (c:s) ->
- let p' = alexMove p c
- (b:bs) = utf8Encode c
- in p' `seq` Just (b, (p', c, bs, s))
+setQuotedString :: Maybe String -> Alex ()
+setQuotedString maybeQuotedString = do
+ ust <- alexGetUserState
+ alexSetUserState ust{ quotedString = maybeQuotedString }
-alexInputPrevChar :: AlexInput -> Char
-alexInputPrevChar (p, c, bs, s) = c
+getQuotedStringContent :: Alex String
+getQuotedStringContent = quotedStringContent <$> alexGetUserState
--- | Encode a Haskell String to a list of Word8 values, in UTF8 format.
-utf8Encode :: Char -> [Word8]
-utf8Encode = map fromIntegral . go . ord
- where
- go oc
- | oc <= 0x7f = [oc]
+setQuotedStringContent :: String -> Alex ()
+setQuotedStringContent content = do
+ ust <- alexGetUserState
+ alexSetUserState ust{ quotedStringContent = content }
- | oc <= 0x7ff = [ 0xc0 + (oc `Data.Bits.shiftR` 6)
- , 0x80 + oc Data.Bits..&. 0x3f
- ]
+alexEOF :: Alex Token
+alexEOF = return EOF
- | oc <= 0xffff = [ 0xe0 + (oc `Data.Bits.shiftR` 12)
- , 0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f)
- , 0x80 + oc Data.Bits..&. 0x3f
- ]
- | otherwise = [ 0xf0 + (oc `Data.Bits.shiftR` 18)
- , 0x80 + ((oc `Data.Bits.shiftR` 12) Data.Bits..&. 0x3f)
- , 0x80 + ((oc `Data.Bits.shiftR` 6) Data.Bits..&. 0x3f)
- , 0x80 + oc Data.Bits..&. 0x3f
- ]
+customAlexError :: String -> Alex Token
+customAlexError message = do
+ (p, _, _, _) <- alexGetInput
+ return (Err p)
+
+beginComment :: AlexInput -> Int -> Alex Token
+beginComment input len = do
+ nesting <- getCommentNesting
+ setCommentNesting (nesting + 1)
+ skip input len
+
+endComment :: AlexInput -> Int -> Alex Token
+endComment input len = do
+ nesting <- getCommentNesting
+ setCommentNesting (nesting - 1)
+ if nesting == 1
+ then begin 0 input len
+ else skip input len
+
+beginQuotedString :: AlexInput -> Int -> Alex Token
+beginQuotedString (_, _, _, input) len = do
+ maybeQuotedString <- getQuotedString
+ case maybeQuotedString of
+ Just _ -> customAlexError "Begin of quoted string but the most recent quoted string is not yet closed"
+ Nothing -> do
+ setQuotedString (Just (take (len - 2) (tail input)))
+ skip input len
+
+endQuotedString :: AlexInput -> Int -> Alex Token
+endQuotedString (pos, _, _, input) len = do
+ maybeQuotedString <- getQuotedString
+ let quotation = take (len - 2) (tail input)
+ case maybeQuotedString of
+ Nothing -> customAlexError ("Closing quoted string " ++ quotation ++ " without opening")
+ Just q -> if q == quotation
+ then do
+ setQuotedString Nothing
+ quotedStringContent <- getQuotedStringContent
+ setQuotedStringContent ""
+ alexSetStartCode 0
+ return (PT pos (T_STRING quotedStringContent))
+ else do
+ content <- getQuotedStringContent
+ setQuotedStringContent (content ++ take len input)
+ skip input len
+
+quotedChar :: AlexInput -> Int -> Alex Token
+quotedChar (_, _, _, input) len = do
+ content <- getQuotedStringContent
+ setQuotedStringContent (content ++ take len input)
+ skip input len
}