forked from nishant-neo/webbrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlinterp.py
65 lines (58 loc) · 2.13 KB
/
htmlinterp.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
#-------------------------------------------------------------------------------
# Name: interpretHTML
# Purpose:
#
# Author: TRINITI
#
# Created: 17-08-2014
# Copyright: (c) TRINITI 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
import ply.lex as lex
import ply.yacc as yacc
import jstokens
import jsgrammar
import graphics as graphics
import jsinterp
def interpret(trees): # Hello, friend
for tree in trees: # Hello,
# ("word-element","Hello")
nodetype=tree[0] # "word-element"
if nodetype == "word-element":
graphics.word(tree[1])
elif nodetype == "tag-element":
# <b>Strong text</b>
tagname = tree[1] # b
tagargs = tree[2] # []
subtrees = tree[3] # ...Strong Text!...
closetagname = tree[4] # b
if tagname != closetagname:
graphics.warning("(mistmatched " + tagname + " " + closetagname + ")")
else:
graphics.begintag(tagname, tagargs)
interpret(subtrees)
graphics.endtag()
elif nodetype == "javascript-element":
jstext = node[1]
jslexer = lex.lex(module = jstokens)
jsparser = yacc.yacc(module=jsgrammar)
jstree = jsparser.parse(jstext, lexer=jslexer)
result = jsinterp.interpret(jstree)
graphics.word(result)
## if False:
## print jstext
## jslexer.input(jstext)
## while True:
## tok = jslexer.token()
## if not tok: break
## print tok
jsparser = yacc.yacc(module=jsgrammar,tabmodule="parsetabjs")
jsast = jsparser.parse(jstext,lexer=jslexer)
result = jsinterp.interpret(jsast)
graphics.word(result)
## WHAT'S HERE?
# Note that graphics.initialize and finalize will only work surrounding a call
# to interpret
graphics.initialize() # Enables display of output.\
interpret([("word-element","Hello")])
graphics.finalize() # Enables display of output.