def run_script_text(dbn_script, **options): options = options or {} VERBOSE = options.get('verbose', False) dump_javascript = options.get('javascript', False) tokenizer = DBNTokenizer() parser = DBNParser() tokens = tokenizer.tokenize(dbn_script) if VERBOSE: for token in tokens: print token dbn_ast = parser.parse(tokens) if dump_javascript: print dbn_ast.to_js(varname='ast') if VERBOSE: dbn_ast.pprint() state = DBNInterpreterState() state = dbn_ast.apply(state) return state
class DBNTextInput(Tkinter.Text): def __init__(self, root, initial_script=''): Tkinter.Text.__init__(self, root, height=30, width=80, borderwidth=0, selectborderwidth=0, highlightthickness=0) self.WIDTH=80 self.bind_events() self.insert(1.0, initial_script) self.highlight_active = False self.highlighted_lines = [] self.tokenizer = DBNTokenizer() def bind_events(self): self.bind("<Tab>", self.insert_tab) def insert_tab(self, event): # insert 4 spaces self.insert(Tkinter.INSERT, " " * 4) return "break" def get_mouse_position(self): pointer_x, pointer_y = self.winfo_pointerxy() root_x, root_y = self.winfo_rootx(), self.winfo_rooty() cx, cy = pointer_x - root_x, pointer_y - root_y return cx, cy def get_contents(self): return self.get(1.0, Tkinter.END) def get_line(self, tkinter_index): """ given an X, Y, gets the line text, or None """ line, column = tkinter_index.split('.') end_of_line = self.index("%s.end" % line).split('.')[1] if int(column) < int(end_of_line): start = "%s.0" % (line) end = "%s.end" % (line) return self.get(start, end) else: return None def get_ghost_key(self, x, y): """ given an x, y, returns the ghost key there, or None """ tkinter_index = self.index("@%d,%d" % (x, y)) str_line_no = tkinter_index.split('.')[0] line = self.get_line(tkinter_index) if line is not None: tokens = self.tokenizer.tokenize(line) args = parser.parse_ghost_line(tokens) if args is None: return None # we have to manually get bounding box for args bounding_boxes = [] for index, arg in enumerate(args): _, str_start_char = arg.start_location().split('.') _, str_end_char = arg.end_location().split('.') arg_start = "%s.%s" % (str_line_no, str_start_char) arg_end = "%s.%s" % (str_line_no, str_end_char) start_bbox = self.bbox(arg_start) start_x, start_y, width, height = start_bbox end_bbox = self.bbox(arg_end) end_x, end_y, _, _ = end_bbox arg_bbox = (start_x, start_y, (end_x - start_x), height) bounding_boxes.append(arg_bbox) int_arg_index = None for index, arg_bbox in enumerate(bounding_boxes): start_x, start_y, width, height = arg_bbox if start_x <= x < start_x + width: int_arg_index = index break if int_arg_index is not None: return "l%sa%d" % (str_line_no, int_arg_index) else: return None else: return None def highlight_line(self, n): start_index = "%d.0" % n line_end_index = "%d.end" % n mark_name = "%d.prehighlight_end" % n self.mark_set(mark_name, line_end_index) self.mark_gravity(mark_name, Tkinter.LEFT) end_char = int(self.index(line_end_index).split('.')[1]) leftover = self.WIDTH - end_char self.insert(mark_name, " "*leftover) self.tag_add('highlighted', start_index, line_end_index) self.highlighted_lines.append(n) self.tag_config('highlighted', background="pink") def clear_line_highlights(self): self.tag_delete('highlighted') for n in self.highlighted_lines: old_mark = "%d.prehighlight_end" % n line_end_index = "%d.end" % n self.delete(old_mark, line_end_index) self.highlighted_lines = []
def tokenizer_tester(string): tokenizer = DBNTokenizer() return [token.type for token in tokenizer.tokenize(string)]
class DBNTextInput(Tkinter.Text): def __init__(self, root, initial_script=''): Tkinter.Text.__init__(self, root, height=30, width=80, borderwidth=0, selectborderwidth=0, highlightthickness=0) self.WIDTH = 80 self.bind_events() self.insert(1.0, initial_script) self.highlight_active = False self.highlighted_lines = [] self.tokenizer = DBNTokenizer() def bind_events(self): self.bind("<Tab>", self.insert_tab) def insert_tab(self, event): # insert 4 spaces self.insert(Tkinter.INSERT, " " * 4) return "break" def get_mouse_position(self): pointer_x, pointer_y = self.winfo_pointerxy() root_x, root_y = self.winfo_rootx(), self.winfo_rooty() cx, cy = pointer_x - root_x, pointer_y - root_y return cx, cy def get_contents(self): return self.get(1.0, Tkinter.END) def get_line(self, tkinter_index): """ given an X, Y, gets the line text, or None """ line, column = tkinter_index.split('.') end_of_line = self.index("%s.end" % line).split('.')[1] if int(column) < int(end_of_line): start = "%s.0" % (line) end = "%s.end" % (line) return self.get(start, end) else: return None def get_ghost_key(self, x, y): """ given an x, y, returns the ghost key there, or None """ tkinter_index = self.index("@%d,%d" % (x, y)) str_line_no = tkinter_index.split('.')[0] line = self.get_line(tkinter_index) if line is not None: tokens = self.tokenizer.tokenize(line) args = parser.parse_ghost_line(tokens) if args is None: return None # we have to manually get bounding box for args bounding_boxes = [] for index, arg in enumerate(args): _, str_start_char = arg.start_location().split('.') _, str_end_char = arg.end_location().split('.') arg_start = "%s.%s" % (str_line_no, str_start_char) arg_end = "%s.%s" % (str_line_no, str_end_char) start_bbox = self.bbox(arg_start) start_x, start_y, width, height = start_bbox end_bbox = self.bbox(arg_end) end_x, end_y, _, _ = end_bbox arg_bbox = (start_x, start_y, (end_x - start_x), height) bounding_boxes.append(arg_bbox) int_arg_index = None for index, arg_bbox in enumerate(bounding_boxes): start_x, start_y, width, height = arg_bbox if start_x <= x < start_x + width: int_arg_index = index break if int_arg_index is not None: return "l%sa%d" % (str_line_no, int_arg_index) else: return None else: return None def highlight_line(self, n): start_index = "%d.0" % n line_end_index = "%d.end" % n mark_name = "%d.prehighlight_end" % n self.mark_set(mark_name, line_end_index) self.mark_gravity(mark_name, Tkinter.LEFT) end_char = int(self.index(line_end_index).split('.')[1]) leftover = self.WIDTH - end_char self.insert(mark_name, " " * leftover) self.tag_add('highlighted', start_index, line_end_index) self.highlighted_lines.append(n) self.tag_config('highlighted', background="pink") def clear_line_highlights(self): self.tag_delete('highlighted') for n in self.highlighted_lines: old_mark = "%d.prehighlight_end" % n line_end_index = "%d.end" % n self.delete(old_mark, line_end_index) self.highlighted_lines = []
repeat_test = """ Repeat X 1 100 { Repeat A 0 X { Set [X A] ((100 * A) / X) } } """ arithmetic_test_string = """ Set A (5 + 4 * 9) """ tokenizer = DBNTokenizer() parser = DBNParser() tokens = tokenizer.tokenize(repeat_test) for token in tokens: print token dbn_ast = parser.parse(tokens) dbn_ast.pprint() state = DBNInterpreterState() first_state = state state = dbn_ast.apply(state) output.draw_window(state.image._image)