-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.x
183 lines (164 loc) · 4.91 KB
/
Lexer.x
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
{
{-# OPTIONS -w #-}
-- This file is part of FairCheck
--
-- FairCheck is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published
-- by the Free Software Foundation, either version 3 of the License,
-- or (at your option) any later version.
--
-- FairCheck is distributed in the hope that it will be useful, but
-- WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with FairCheck. If not, see <http://www.gnu.org/licenses/>.
--
-- Copyright 2021 Luca Padovani
module Lexer
( Token(..)
, AlexPosn(..)
, TokenClass(..)
, Alex(..)
, runAlex'
, alexMonadScan'
, alexError'
) where
import Prelude hiding (lex)
import Control.Monad (liftM)
}
%wrapper "monadUserState"
$digit = 0-9
$extra = [₀-₉⁰-⁹⁺⁻₊₋]
$alpha = [A-Za-z]
@lower = [a-z]
@upper = [A-Z]
@next = $alpha | $digit | $extra | \_ | \'
@lid = @lower @next*
@cid = @upper @next*
@int = $digit+
@float = (@int \. @int) | (@int \. @int)
@string = \"[^\"]*\"
tokens :-
$white+ ;
"//".* ;
"." { lex' TokenDot }
"," { lex' TokenComma }
":" { lex' TokenColon }
";" { lex' TokenSemiColon }
"(" { lex' TokenLParen }
")" { lex' TokenRParen }
"{" { lex' TokenLBrace }
"}" { lex' TokenRBrace }
"[" { lex' TokenLBrack }
"]" { lex' TokenRBrack }
"⌈" { lex' TokenLCeil }
"⌉" { lex' TokenRCeil }
"⟨" { lex' TokenLAngle }
"⟩" { lex' TokenRAngle }
"=" { lex' TokenEQ }
"+" { lex' TokenPlus }
"⊕" { lex' TokenOPlus }
"?" { lex' TokenQMark }
"!" { lex' TokenEMark }
@lid { lex lookupLID }
@cid { lex TokenCID }
@int { lex TokenINT }
{
-- To improve error messages, We keep the path of the file we are
-- lexing in our own state.
data AlexUserState = AlexUserState { filePath :: FilePath }
alexInitUserState :: AlexUserState
alexInitUserState = AlexUserState "<unknown>"
getFilePath :: Alex FilePath
getFilePath = liftM filePath alexGetUserState
setFilePath :: FilePath -> Alex ()
setFilePath = alexSetUserState . AlexUserState
keywords :: [(String, TokenClass)]
keywords = [("type", TokenType),
("rec", TokenRec),
("done", TokenDone),
("end", TokenEnd),
("in", TokenIn),
("new", TokenNew),
("close", TokenClose),
("wait", TokenWait)]
lookupLID :: String -> TokenClass
lookupLID s = case lookup s keywords of
Nothing -> TokenLID s
Just tok -> tok
-- The token type, consisting of the source code position and a token class.
data Token = Token AlexPosn TokenClass
deriving (Show)
data TokenClass
= TokenType
| TokenRec
| TokenDone
| TokenEnd
| TokenNew
| TokenIn
| TokenClose
| TokenWait
| TokenINT String
| TokenLID String
| TokenCID String
| TokenEQ
| TokenPlus
| TokenOPlus
| TokenMinus
| TokenTimes
| TokenDot
| TokenComma
| TokenColon
| TokenSemiColon
| TokenLParen
| TokenRParen
| TokenLBrace
| TokenRBrace
| TokenLBrack
| TokenRBrack
| TokenLCeil
| TokenRCeil
| TokenLAngle
| TokenRAngle
| TokenQMark
| TokenEMark
| TokenEOF
deriving (Show)
alexEOF :: Alex Token
alexEOF = do
(p,_,_,_) <- alexGetInput
return $ Token p TokenEOF
-- Unfortunately, we have to extract the matching bit of string
-- ourselves...
lex :: (String -> TokenClass) -> AlexAction Token
lex f = \(p,_,_,s) i -> return $ Token p (f (take i s))
-- For constructing tokens that do not depend on the input
lex' :: TokenClass -> AlexAction Token
lex' = lex . const
-- We rewrite alexMonadScan' to delegate to alexError' when lexing fails
-- (the default implementation just returns an error message).
alexMonadScan' :: Alex Token
alexMonadScan' = do
inp <- alexGetInput
sc <- alexGetStartCode
case alexScan inp sc of
AlexEOF -> alexEOF
AlexError (p, _, _, s) ->
alexError' p ("lexical error at character '" ++ take 1 s ++ "'")
AlexSkip inp' len -> do
alexSetInput inp'
alexMonadScan'
AlexToken inp' len action -> do
alexSetInput inp'
action (ignorePendingBytes inp) len
-- Signal an error, including a commonly accepted source code position.
alexError' :: AlexPosn -> String -> Alex a
alexError' (AlexPn _ l c) msg = do
fp <- getFilePath
alexError (fp ++ ":" ++ show l ++ ":" ++ show c ++ ": " ++ msg)
-- A variant of runAlex, keeping track of the path of the file we are lexing.
runAlex' :: Alex a -> FilePath -> String -> Either String a
runAlex' a fp input = runAlex input (setFilePath fp >> a)
}