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
|
{-|
Module : VeriFuzz.Lexer
Description : Lexer for Verilog.
Copyright : (c) 2019, Yann Herklotz Grave
License : GPL-3
Maintainer : ymherklotz [at] gmail [dot] com
Stability : experimental
Portability : POSIX
Lexer for Verilog.
-}
module VeriFuzz.Lexer
( lexer
, identifier
, reserved
, operator
, reservedOp
, charLiteral
, stringLiteral
, natural
, integer
, float
, naturalOrFloat
, decimal
, hexadecimal
, octal
, symbol
, lexeme
, whiteSpace
, parens
, braces
, angles
, brackets
, squares
, comma
, colon
, dot
, semiSep
, semiSep1
, commaSep
, commaSep1
) where
import Data.Char (digitToInt)
import Text.Parsec
import qualified Text.Parsec.Token as P
type VerilogDef = P.LanguageDef ()
type Lexer = P.TokenParser ()
type Parser = Parsec String ()
verilogDef :: VerilogDef
verilogDef = P.LanguageDef "/*" "*/" "//" False letter (alphaNum <|> char '_')
(oneOf ":!#%&*+./<=>?@\\^|-~") (oneOf ":!#%&*+./<=>?@\\^|-~")
reserved' reservedOp' True
lexer :: Lexer
lexer = P.makeTokenParser verilogDef
identifier :: Parser String
identifier = P.identifier lexer
reserved :: String -> Parser ()
reserved = P.reserved lexer
operator :: Parser String
operator = P.operator lexer
reservedOp :: String -> Parser ()
reservedOp = P.reservedOp lexer
charLiteral :: Parser Char
charLiteral = P.charLiteral lexer
stringLiteral :: Parser String
stringLiteral = P.stringLiteral lexer
natural :: Parser Integer
natural = P.natural lexer
integer :: Parser Integer
integer = P.integer lexer
float :: Parser Double
float = P.float lexer
naturalOrFloat :: Parser (Either Integer Double)
naturalOrFloat = P.naturalOrFloat lexer
decimal :: Parser Integer
decimal = P.decimal lexer
number :: Integer -> Parser Char -> Parser Integer
number base baseDigit = do
digits <- many1 baseDigit
let n = foldl (\x d -> base*x + toInteger (digitToInt d)) 0 digits
seq n (return n)
hexadecimal :: Parser Integer
hexadecimal = number 16 hexDigit
octal :: Parser Integer
octal = number 8 octDigit
symbol :: String -> Parser String
symbol = P.symbol lexer
lexeme :: Parser a -> Parser a
lexeme = P.lexeme lexer
whiteSpace :: Parser ()
whiteSpace = P.whiteSpace lexer
parens :: Parser a -> Parser a
parens = P.parens lexer
braces :: Parser a -> Parser a
braces = P.braces lexer
angles :: Parser a -> Parser a
angles = P.angles lexer
brackets :: Parser a -> Parser a
brackets = P.brackets lexer
squares :: Parser a -> Parser a
squares = P.squares lexer
comma :: Parser String
comma = P.comma lexer
colon :: Parser String
colon = P.colon lexer
dot :: Parser String
dot = P.dot lexer
semiSep :: Parser a -> Parser [a]
semiSep = P.semiSep lexer
semiSep1 :: Parser a -> Parser [a]
semiSep1 = P.semiSep1 lexer
commaSep :: Parser a -> Parser [a]
commaSep = P.commaSep lexer
commaSep1 :: Parser a -> Parser [a]
commaSep1 = P.commaSep1 lexer
reservedOp' :: [String]
reservedOp' = [ "!", "~", "~&", "~|", "+", "-", "*", "/", "%", "==", "!=", "===", "!=="
, "&&", "||", "<", "<=", ">", ">=", "&", "|", "^", "^~", "~^", "**", "<<"
, ">>", "<<<", ">>>"
]
reserved' :: [String]
reserved' = [ "always", "and", "assign", "automatic", "begin", "buf", "bufif0", "bufif1"
, "case", "casex", "casez", "cell", "cmos", "config", "deassign", "default"
, "defparam", "design", "disable", "edge", "else", "end", "endcase", "endconfig"
, "endfunction", "endgenerate", "endmodule", "endprimitive", "endspecify", "endtable"
, "endtask", "event", "for", "force", "forever", "fork", "function", "generate", "genvar"
, "highz0", "highz1", "if", "ifnone", "incdir", "include", "initial", "inout", "input"
, "instance", "integer", "join", "large", "liblist", "library", "localparam", "macromodule"
, "medium", "module", "nand", "negedge", "nmos", "nor", "noshowcancelled", "not", "notif0"
, "notif1", "or", "output", "parameter", "pmos", "posedge", "primitive", "pull0", "pull1"
, "pulldown", "pullup", "pulsestyle_onevent", "pulsestyle_ondetect", "remos", "real"
, "realtime", "reg", "release", "repeat", "rnmos", "rpmos", "rtran", "rtranif0", "rtranif1"
, "scalared", "showcancelled", "signed", "small", "specify", "specparam", "strong0", "strong1"
, "supply0", "supply1", "table", "task", "time", "tran", "tranif0", "tranif1", "tri", "tri0"
, "tri1", "triand", "trior", "trireg", "unsigned", "use", "vectored", "wait", "wand", "weak0"
, "weak1", "while", "wire", "wor", "xnor", "xor"
]
|