/
tokens.py
85 lines (50 loc) · 1.39 KB
/
tokens.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
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
"""Flux Tokens
requirements: Python 3
Tokens for use in the Flux lexer
For more information, visit the project's wiki:
http://flux.referata.com/
"""
class Token():
def __init__(self, consumed, start, end):
self.consumed = consumed
self.start = start
self.end = end
@classmethod
def equals(cls, other):
return isinstance(other, cls)
@classmethod
def name(cls):
return cls.__name__
def __eq__(self, other):
for attrib in ("pattern", "consumed", "start", "end"):
if getattr(self, attrib) != getattr(other, attrib):
return False
return True
class TEXT(Token):
pattern = "."
class AT(Token):
pattern = "\@"
class OPEN_PAREN(Token):
pattern = "\("
class CLOSE_PAREN(Token):
pattern = "\)"
class COMMA(Token):
pattern = ","
class ESCAPED(Token):
pattern = "\\\[\S]"
class EQUALS(Token):
pattern = "="
class NEW_LINE(Token):
pattern = "\n"
class VARIABLE(Token):
pattern = "%\w+"
class OPEN_ILINK(Token):
pattern = "\[\["
class CLOSE_ILINK(Token):
pattern = "\]\]"
class OPEN_ELINK(Token):
pattern = "(?<!\[)\[(?!\[)"
class CLOSE_ELINK(Token):
pattern = "(?<!\])\](?!\])"
tokens = (AT, CLOSE_ILINK, CLOSE_PAREN, COMMA, EQUALS, ESCAPED, NEW_LINE,
OPEN_ILINK, OPEN_PAREN, VARIABLE, OPEN_ELINK, CLOSE_ELINK)