-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLexer.x
54 lines (48 loc) · 1.23 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
{
{-# OPTIONS_GHC -w #-}
module Lexer where
-- Author: Aleksander Balicki
-- A lexer
import Prelude hiding (lex)
}
%wrapper "posn"
$digit = 0-9
$alpha = [a-zA-Z]
$quote = "
tokens :-
$digit+ { \p s -> tokenWithPos p (TkInt (read s)) }
IF { \p s -> tokenWithPos p TkIf }
DECISION { \p s -> tokenWithPos p TkDecision }
CASE { \p s -> tokenWithPos p TkCase }
ELSEIF { \p s -> tokenWithPos p TkElseIf }
ARM { \p s -> tokenWithPos p TkArm }
EQUALS { \p s -> tokenWithPos p TkEquals }
AND { \p s -> tokenWithPos p TkAnd }
OR { \p s -> tokenWithPos p TkOr }
VAR { \p s -> tokenWithPos p TkVar }
"_" { \p s -> tokenWithPos p TkWildcard }
"(" { \p s -> tokenWithPos p TkLParen }
")" { \p s -> tokenWithPos p TkRParen }
$quote [^$quote]* $quote { \p s -> tokenWithPos p (TkString (read s)) }
($white)+ ;
{
data BaseToken = TkIf
| TkDecision
| TkCase
| TkElseIf
| TkArm
| TkEquals
| TkAnd
| TkOr
| TkVar
| TkWildcard
| TkLParen
| TkRParen
| TkInt Integer
| TkString String
deriving (Show, Eq)
type Token = ((Int,Int), BaseToken)
tokenWithPos :: AlexPosn -> BaseToken -> Token
tokenWithPos (AlexPn _ line col) t = ((line,col),t)
lex = alexScanTokens
}