-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHplLexer.py
40 lines (28 loc) · 1.22 KB
/
HplLexer.py
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
import re
from hwc.Lexer import Lexer
from hwc.TokenKinds import *
class HplLexer(Lexer):
def __init__(self):
whiteSpacePattern = r"(?:\s+|;[^\n]*)+"
operatorPattern = "[()]"
terminate = r"(?=[\s;()]|$)"
intPattern = "-?\d+" + terminate
hexPattern = "-?0[xX][0-9a-fA-F]+" + terminate
symbolPattern = r"[0-9a-zA-Z~!@#$%^&*\-_=+\|:,<.>/?]+" + terminate
charPattern = r"'(?:\\[strn0'\\]|[^\r\n'\\])'" + terminate
stringPattern = r'"(?:\\[strn0"\\]|[^\r\n"\\])*"' + terminate
errorPattern = ".?"
uncompiledPatterns = [
(WHITESPACE, whiteSpacePattern),
(OPERATOR, operatorPattern),
(INT_LITERAL, intPattern),
(HEX_LITERAL, hexPattern),
(SYMBOL, symbolPattern),
(CHAR_LITERAL, charPattern),
(STRING_LITERAL, stringPattern),
(ERROR, errorPattern) ]
compiledPatterns = [(kind, re.compile(pattern))
for (kind, pattern) in uncompiledPatterns]
self.__patterns = compiledPatterns
def patterns(self):
return self.__patterns