def interpret(ast): # AST for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jslexer = lex.lex(module=jstokens) if False: # JavaScript lexer/parser debugging 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)
def interpret(ast): # AST for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jslexer = lex.lex(module=jstokens) if False: # JavaScript lexer/parser debugging 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) htmlast = htmlparser.parse(result,lexer=htmllexer) interpret(htmlast)
def interpret(webpage): for node in webpage: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subpage = node[3] closetagname = node[4] if (tagname != closetagname): graphics.warning("(mistmatched" + tagname + "" + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subpage) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jspage = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jspage) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast) # htmlast = htmlparser.parse(webpage, lexer=htmllexer) # graphics.initialize() # interpret(htmlast) # graphics.finalize()
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jsast) # rather than just call graphics we re-interpret here. # graphics.word(result) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast)
def interpret(ast): print "Entering interpret, ast = " + str(ast) for node in ast: nodetype = node[0] if nodetype == "word-element": #graphics.word(node[1]) print '\nWord Elements:\n' + node[1] elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if (tagname <> closetagname): #graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") print "\nError:\n(mistmatched " + tagname + " " + closetagname + ")" else: #graphics.begintag(tagname,tagargs); interpret(subast) #graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1] jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jsast) # Now interpret result in case it contains HTML tags jshtmlast = htmlparser.parse(result, lexer=htmllexer) interpret(jshtmlast)
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) # rather than just call graphics we re-interpret here. # graphics.word(result) htmlast = htmlparser.parse(result,lexer=htmllexer) interpret(htmlast)
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)
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)
def probe(js_text): js_ast = jsparser.parse(js_text, lexer=jslexer) try: good_result = jsinterp.interpret(js_ast) except JSReturn: good_result = "error!" try: buggy_result = jsinterpbuggy.interpret(js_ast) except JSReturn: buggy_result = "error!" print js_text if good_result == buggy_result: # not interesting print "\tgood = buggy = ", good_result else: # very interesting! print "\tgood = ", good_result print "\tbuggy = ", buggy_result
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) interpret(htmlparser.parse(result,lexer=htmllexer))
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if tagname != closetagname: graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jsast) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast)
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) # result1 = "<b>This whole sentence should be bold, and the concepts in this problem touch on the <a href='http://en.wikipedia.org/wiki/Document_Object_Model'>Document Object Model</a>, which allows web browsers and scripts to <i>manipulate</i> webpages.</b>" # result2 = '<i>10.0</i><b>9.0</b><i>8.0</i><b>7.0</b><i>6.0</i><b>5.0</b><i>4.0</i><b>3.0</b><i>2.0</i><b>1.0</b>' gHtml = htmlparser.parse(result,lexer=htmllexer) interpret(gHtml)