def test_comments(self): '''tests that comments are ignored by the lexer''' string = '// This is a comment with key words. All of this will get ignored: if else done' toks = lex(string) self.assertEquals(len(toks), 0) string = '''/* this is a big comment block this will not fail if else done return const let int float char function */''' oks = lex(string) self.assertEquals(len(toks), 0) pass
def test_reserved(self): '''tests that reserved keywords work''' self.reserved("if") self.reserved("else") self.reserved("done") self.reserved("return") self.reserved("const") self.reserved("let") self.reserved("int", "INTTYPE") self.reserved("float", "FLOATTYPE") self.reserved("char", "CHARTYPE") self.reserved("function", "FUNCTIONTYPE") st = "if else done return const let int float char function" tokenTypes = [ "IF", "ELSE", "DONE", "RETURN", "CONST", "LET", "INTTYPE", "FLOATTYPE", "CHARTYPE", "FUNCTIONTYPE" ] st_list = st.split() tokens = lex(st) self.assertEquals(len(tokens), 10) for ndx, i in enumerate(tokens): self.assertEquals(i.type, tokenTypes[ndx]) self.assertEquals(i.value, st_list[ndx]) pass pass
def reserved(self, word, typ=None): typ = typ if typ is not None else word.upper() toks = lex(word) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, typ) self.assertEquals(toks[0].value, word) pass
def test_non_math_operator(): try: rpn(infix(lex("3 4 op"))) except ValueError: pass else: raise ValueError("Expected exception, but it didn't raise")
def test_no_operator(): try: rpn(infix(lex("3 5"))) except ValueError: pass else: raise ValueError("Expected exception, bit it didnt raise")
def assertEqual(self, first: str, second: List[Tuple[str, str]], msg: Any = ...) -> None: super().assertEqual(list(lex(first)), [Token(name, value) for name, value in second], msg)
def js_innerHTML(self, handle, s): doc = parse(lex("<html><body>" + s + "</body></html>")) new_nodes = doc.children[0].children elt = self.handle_to_node[handle] elt.children = new_nodes for child in elt.children: child.parent = elt print("Layout called from js_innerHTML") self.reflow(layout_for_node(self.document, elt))
def main(): if args.quiet == False: quit = False while quit == False: user_input = input(">>>") user_input = user_input.strip() if user_input == "quit": quit = True elif len(user_input.strip()) == 0: continue else: answer = lex(user_input) answer = infix(answer) answer = rpn(answer) print(answer) else: user_input = input() print(rpn(infix(lex(user_input))))
def test_float(self): '''tests that lexer matches flloating points!''' floats = '.33' toks = lex(floats) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "FLOAT") self.assertEquals(toks[0].value, floats) floats = '0.33' toks = lex(floats) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "FLOAT") self.assertEquals(toks[0].value, floats) floats = '-0.33' toks = lex(floats) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "FLOAT") self.assertEquals(toks[0].value, floats) pass
def test_prog(): prog = ''' var y = 10; var x = 20; print(x + y); ''' tokens = lex(prog) # print(list(tokens)) tree = parse(tokens) print(type(tree)) tree.eval({})
def test_name(self): '''tests that names are discovered''' name = "this" toks = lex(name) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "NAME") self.assertEquals(toks[0].value, name) name = "yoseph" toks = lex(name) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "NAME") self.assertEquals(toks[0].value, name) name = '12tiemyshoe' def throwsException(): toks = lex(name) pass self.assertRaises(LexerError, throwsException) pass
def load(self, url, body=None): self.address_bar = url self.url = url self.history.append(url) self.timer.start("Downloading") req_headers = {"Cookie": self.cookie_string()} headers, body = request(url, headers=req_headers, payload=body) if "set-cookie" in headers: kv, *params = headers["set-cookie"].split(";") key, value = kv.split("=", 1) self.cookies[key] = value print(f"Received Cookie key={key}, value={value}") origin = url_origin(self.history[-1]) self.cookies.setdefault(origin, {})[key] = value self.timer.start("Parsing HTML") self.nodes = parse(lex(body)) self.timer.start("Parsing CSS") with open("browser/src/browser.css") as f: browser_style = f.read() rules = CSSParser(browser_style).parse() for link in find_links(self.nodes, []): headers, body = request(relative_url(link, url), headers=req_headers) rules.extend(CSSParser(body).parse()) # tree_to_string(self.nodes) rules.sort(key=lambda selector_body: selector_body[0].priority(), reverse=True) self.rules = rules self.timer.start("Running JS") self.setup_js() for script in find_scripts(self.nodes, []): header, body = request(relative_url(script, self.history[-1]), headers=req_headers) try: # print("Script returned: ", self.js_environment.evaljs(body)) self.js_environment.evaljs(body) except dukpy.JSRuntimeError as e: print("Script", script, "crashed", e) print("Layout called from load") self.layout(self.nodes)
def test_int(self): '''Tests that the lexer matches to int types''' integer = '1431' toks = lex(integer) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "INT") self.assertEquals(toks[0].value, integer) integer = '11' toks = lex(integer) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "INT") self.assertEquals(toks[0].value, integer) integer = '11.34' toks = lex(integer) self.assertEquals(len(toks), 1) self.assertNotEquals(toks[0].type, "INT") integer = '011' toks = lex(integer) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "OCT") self.assertEquals(toks[0].value, integer) integer = '0x11AeFF' toks = lex(integer) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "HEX") self.assertEquals(toks[0].value, integer) integer = '-32111' toks = lex(integer) self.assertEquals(len(toks), 1) self.assertEquals(toks[0].type, "INT") self.assertEquals(toks[0].value, integer) integer = '11 123 55312 4391 2434' toks = lex(integer) self.assertEquals(len(toks), 5) for ndx, i in enumerate(toks): self.assertEquals(i.value, integer.split()[ndx]) self.assertEquals(i.type, "INT") pass pass
def test_complex_mult(): assert rpn(infix(lex("5 * ( 1 + 2) * 1"))) == (5 * (1 + 2) * 1)
def test_multiple_levels(): assert rpn(infix(lex("(4 + 2) / 3 * 5"))) == (4 + 2) / 3 * 5
def test_negative_decimal(): assert rpn(infix(lex("-5.0")))
def test_negative(): assert rpn(infix(lex("-5"))) == -5
def test_decimal(): assert rpn(infix(lex("5.0"))) == 5.0
def test_negative_number(): assert rpn(infix(lex("-5 + 8"))) == -5 + 8
def test_left_to_right(): assert rpn(infix(lex("10 * 4 / 8"))) == (10 * 4 / 8)
def test_basic(): assert rpn(infix(lex("1 + 1"))) == (1 + 1)
def test_negative_and_float_division(): assert rpn(infix(lex("(-5 + 3.0) / 2"))) == (-5 + 3.0) / 2
def test_float_number(): assert rpn(infix(lex("(7 - 3) / 7.0"))) == (7 - 3) / 7.0
def test_fraction(): assert rpn(infix(lex("(1 + 5) / 7"))) == (1 + 5) / 7
def test_float_answer(): assert rpn(infix(lex("(5 + 8) / 7"))) == (5 + 8) / 7
def test_multi_level(): assert rpn(infix(lex("(3 + 6) * 7"))) == (3 + 6) * 7
def test_complex_division(): assert rpn(infix(lex("5 * ( 1 + 2) / 1"))) == (5 * (1 + 2) / 1)
def test_num(): assert rpn(infix(lex("1"))) == 1
def test_negative_division(): assert rpn(infix(lex("(-5 + 3) / 2"))) == (-5 + 3) / 2
def test_negative_answer(): assert rpn(infix(lex("5 - 8"))) == 5 - 8