/
nodes.py
114 lines (82 loc) · 2.26 KB
/
nodes.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
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
"""Flux Parser
requirements: Python 3
Flux is an attempt to create kinder markup language than WikiMedia's
WikiCode, and this Lexer is the beginning.
For more information, visit the project's wiki:
http://flux.referata.com/
"""
from tokens import (AT, CLOSE_PAREN, COMMA, ESCAPED, EQUALS, NEW_LINE,
OPEN_PAREN, PERIOD, TEXT, VARIABLE, OPEN_LINK, CLOSE_LINK)
"""
FLUX
LINE
TEMPLATE
TEXT
LINK
LIST_ITEM
LINK
TEXT
VARIABLE
"""
class IllegalChildException(Exception):
pass
class ALL():
"""Temporary class. This will be replaced when I can think of where
to best put the damn thing.
"""
def __eq__(self, other):
return True
@classmethod
def equals(cls, other):
return True
class Node():
legal_children = ('ALL')
def __init__(self):
self.children = []
@classmethod
def equals(cls, other):
return isinstance(other, cls)
@property
@classmethod
def name(cls):
return cls.__name__
@classmethod
def legal_child(cls, child):
return child.name in cls.legal_children
def add_child(self, child):
if self.legal_children(child):
self.children.append(child)
else:
raise IllegalChildException(
"{} is not a legal child of {}".format(child.name, self.name))
class Flux(Node):
legal_children = ('Line')
class Line(Node):
legal_children = ('ALL')
class Template(Node):
legal_children = ()
pattern = ('AT', 'ALL', 'OPEN_PAREN', 'ALL', 'CLOSE_PAREN')
def __init__(self, name, *args, **kwargs):
super()
self.args = {'name': name}
self.args.update(kwargs)
for index, arg in enumerate(args):
self.args[str(index)] = arg
class Link(Node):
legal_children = ()
pattern = ('OPEN_LINK', 'ALL', 'CLOSE_LINK')
def __init__(self, name, dest):
super()
self.name = name
self.destination = dest
self.local = dest.startswith('http://')
class Text(Node):
legal_children = ()
pattern = (TEXT)
def __init__(self, content):
super()
self.content = content
if __name__ == '__main__':
root = Flux()
line = Line()
root.add_child(line)