-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser2.py
97 lines (86 loc) · 3.7 KB
/
parser2.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
# This file exports the parse_tokens function, and the ParseException.
# The nodes are imported from evaluate.py
from lexer import lex
from evaluate import *
TEXT = "text"
OPEN_BRACE = "{{"
CLOSE_BRACE = "}}"
OPEN_PERCENT = "{%"
CLOSE_PERCENT = "%}"
IF = "if"
END_IF = "end if"
FOR = "for"
IN = "in"
END_FOR = "end for"
ITERABLE = "iterable"
INCLUDE = "include"
FILE = "file"
class ParseException(Exception):
pass
'''
parse = [
(TEXT, "<p>"), (OPEN_BRACE, "{{"), (TEXT, "name"), (CLOSE_BRACE, "}}"), (TEXT, "</p>"), (OPEN_PERCENT, "{%"), (IF, "if"),
(TEXT, "1==user.friends"), (CLOSE_PERCENT, "%}"), (TEXT, "<br/>"), (OPEN_PERCENT, "{%"), (END_IF, "end if"), (CLOSE_PERCENT, "%}"),
(TEXT, "<p>"),(OPEN_BRACE, "{{"), (TEXT, "name"), (CLOSE_BRACE, "}}"), (TEXT, "</p>"), (OPEN_PERCENT, "{%"), (FOR, "for"), (TEXT, "i"), (IN, "in"),
(ITERABLE, "iterable"), (CLOSE_PERCENT, "%}"), (TEXT, "<p>"), (OPEN_BRACE, "{{"), (TEXT, "name"), (CLOSE_BRACE, "}}"), (TEXT, "</p>"), (OPEN_PERCENT, "{%"),
(END_FOR, "end for"), (CLOSE_PERCENT, "%}")]
'''
parse = [(OPEN_PERCENT, "{%"), (INCLUDE, "include"), (FILE, "pages/header.html"), (CLOSE_PERCENT, "%}")]
def if_handler(tokens, pos):
predicate = tokens[pos + 2][1]
if tokens[pos + 3][0] != CLOSE_PERCENT:
raise ParseException("Mwahaha you failure, closed squigglies were not detected. Fool.")
groupnode, new_pos = block(tokens, pos + 4)
return IfNode(predicate, groupnode), new_pos
def for_handler(tokens, pos):
if tokens[pos + 3][0] != IN:
raise ParseException("What were you thinking‽ Can you write python? No "in" found in for loop.")
if tokens[pos + 5][0] != CLOSE_PERCENT:
raise ParseException("Mwahaha you failure, closed squigglies were not detected. Fool.")
variable = tokens[pos + 2][1]
iterable = tokens[pos + 4][1]
groupnode, new_pos = block(tokens, pos + 6)
return ForNode(variable, iterable, groupnode), new_pos
def inc_handler(tokens, pos):
with open(tokens[pos+2][1], 'r') as f:
tokens = lex(f.read())
new_node, _ = block(tokens, 0)
return new_node, pos+4
def python(tokens, pos):
node = PyNode(tokens[pos + 1][1])
if tokens[pos + 2][0] != CLOSE_BRACE:
raise ParseException("Mwahaha you failure, closed squigglies were not detected. Fool.")
return node, pos + 3
def block(tokens, pos):
nodes = []
while pos < len(tokens):
if tokens[pos][0] == TEXT:
nodes.append(TextNode(tokens[pos][1]))
pos += 1
elif tokens[pos][0] == OPEN_BRACE:
node, new_pos = python(tokens, pos)
nodes.append(node)
pos = new_pos
elif tokens[pos][0] == OPEN_PERCENT:
if tokens[pos+1][0] == IF:
new_node, new_pos = if_handler(tokens, pos)
pos = new_pos
nodes.append(new_node)
elif tokens[pos+1][0] == FOR:
new_node, new_pos = for_handler(tokens, pos)
pos = new_pos
nodes.append(new_node)
elif tokens[pos+1][0] == INCLUDE:
node, new_pos = inc_handler(tokens, pos)
nodes.append(node)
pos = new_pos
elif tokens[pos+1][0] == END_IF or tokens[pos+1][0] == END_FOR:
pos += 3
return GroupNode(nodes), pos
else:
raise ParseException("Ah Rip. You suck at writting this language :( This is not valid :O")
return GroupNode(nodes), pos
def parse_tokens(tokens):
return block(tokens, 0)[0]
if __name__ == '__main__':
print(parse_tokens(parse).returnText({'title': 'blahblah'}))