Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (82 sloc) 2.318 kb
eb2afa5 @r3 Added token stream from Author.flux to be used in creating a parser.
r3 authored
1 """Flux Parser
3af8f52 @Havvy Created parser.py. No actual content yet.
authored
2 requirements: Python 3
3
4 Flux is an attempt to create kinder markup language than WikiMedia's
5 WikiCode, and this Lexer is the beginning.
6
7 For more information, visit the project's wiki:
8 http://flux.referata.com/
9 """
10
c3f36fd @r3 Renamed both Parser and Lexer to lower case equivalent. Dropping support...
r3 authored
11 from tokens import (AT, CLOSE_PAREN, COMMA, ESCAPED, EQUALS, NEW_LINE,
12 OPEN_PAREN, PERIOD, TEXT, VARIABLE, OPEN_LINK, CLOSE_LINK)
b4e9cf9 @r3 Minor work on Parser
r3 authored
13 """
14 FLUX
15 LINE
16 TEMPLATE
17 TEXT
18 LINK
19 LIST_ITEM
20 LINK
21 TEXT
22 VARIABLE
23 """
24
25
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
26 class IllegalChildException(Exception):
27 pass
28
29
3e21829 @r3 Inlined Lexer.char_stream and made some progress on Parser. Fixed some m...
r3 authored
30 class ALL():
31 """Temporary class. This will be replaced when I can think of where
32 to best put the damn thing.
33 """
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
34 def __eq__(self, other):
35 return True
36
c3f36fd @r3 Renamed both Parser and Lexer to lower case equivalent. Dropping support...
r3 authored
37 @classmethod
38 def equals(cls, other):
39 return True
3e21829 @r3 Inlined Lexer.char_stream and made some progress on Parser. Fixed some m...
r3 authored
40
41
b4e9cf9 @r3 Minor work on Parser
r3 authored
42 class Node():
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
43 legal_children = ('ALL')
b4e9cf9 @r3 Minor work on Parser
r3 authored
44
45 def __init__(self):
46 self.children = []
47
c3f36fd @r3 Renamed both Parser and Lexer to lower case equivalent. Dropping support...
r3 authored
48 @classmethod
49 def equals(cls, other):
50 return isinstance(other, cls)
51
b4e9cf9 @r3 Minor work on Parser
r3 authored
52 @property
53 @classmethod
54 def name(cls):
55 return cls.__name__
eb2afa5 @r3 Added token stream from Author.flux to be used in creating a parser.
r3 authored
56
b4e9cf9 @r3 Minor work on Parser
r3 authored
57 @classmethod
58 def legal_child(cls, child):
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
59 return child.name in cls.legal_children
b4e9cf9 @r3 Minor work on Parser
r3 authored
60
61 def add_child(self, child):
62 if self.legal_children(child):
63 self.children.append(child)
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
64 else:
65 raise IllegalChildException(
66 "{} is not a legal child of {}".format(child.name, self.name))
b4e9cf9 @r3 Minor work on Parser
r3 authored
67
68
69 class Flux(Node):
70 legal_children = ('Line')
71
72
73 class Line(Node):
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
74 legal_children = ('ALL')
b4e9cf9 @r3 Minor work on Parser
r3 authored
75
76
77 class Template(Node):
78 legal_children = ()
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
79 pattern = ('AT', 'ALL', 'OPEN_PAREN', 'ALL', 'CLOSE_PAREN')
b4e9cf9 @r3 Minor work on Parser
r3 authored
80
aa472dc @r3 Removed type annotations from lexer. Should use them everywhere if we're...
r3 authored
81 def __init__(self, name, *args, **kwargs):
b4e9cf9 @r3 Minor work on Parser
r3 authored
82 super()
83 self.args = {'name': name}
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
84 self.args.update(kwargs)
b4e9cf9 @r3 Minor work on Parser
r3 authored
85
aa472dc @r3 Removed type annotations from lexer. Should use them everywhere if we're...
r3 authored
86 for index, arg in enumerate(args):
87 self.args[str(index)] = arg
88
b4e9cf9 @r3 Minor work on Parser
r3 authored
89
90 class Link(Node):
91 legal_children = ()
4a27580 @r3 Winter cleaning of code that I haven't touched in some time. Removed som...
r3 authored
92 pattern = ('OPEN_LINK', 'ALL', 'CLOSE_LINK')
b4e9cf9 @r3 Minor work on Parser
r3 authored
93
94 def __init__(self, name, dest):
95 super()
96 self.name = name
97 self.destination = dest
98 self.local = dest.startswith('http://')
99
100
101 class Text(Node):
102 legal_children = ()
103 pattern = (TEXT)
104
105 def __init__(self, content):
106 super()
107 self.content = content
108
c3f36fd @r3 Renamed both Parser and Lexer to lower case equivalent. Dropping support...
r3 authored
109
b4e9cf9 @r3 Minor work on Parser
r3 authored
110 if __name__ == '__main__':
111 root = Flux()
112 line = Line()
113
114 root.add_child(line)
Something went wrong with that request. Please try again.