diff --git a/harness.py b/harness.py index f4b0264..7c53ea5 100644 --- a/harness.py +++ b/harness.py @@ -112,19 +112,19 @@ class RepairStack(typing.NamedTuple): match action: case parser.Shift(): - rl.debug(f"{stack.state}: SHIFT -> {action.state}") + rl.info(f"{stack.state}: SHIFT -> {action.state}") return stack.push(action.state), False case parser.Accept(): - rl.debug(f"{stack.state}: ACCEPT") + rl.info(f"{stack.state}: ACCEPT") return stack, True # ? case parser.Reduce(): - rl.debug(f"{stack.state}: REDUCE {action.name} {action.count} ") + rl.info(f"{stack.state}: REDUCE {action.name} {action.count} ") new_stack = stack.pop(action.count) - rl.debug(f" -> {new_stack.state}") + rl.info(f" -> {new_stack.state}") new_state = table.gotos[new_stack.state][action.name] - rl.debug(f" goto {new_state}") + rl.info(f" goto {new_state}") stack = new_stack.push(new_state) case parser.Error(): @@ -159,10 +159,6 @@ class Repair: if self.advance >= 3: self.success = True - def __repr__(self): - valstr = f"({self.value})" if self.value is not None else "" - return f"" - def neighbors( self, table: parser.ParseTable, @@ -177,8 +173,8 @@ class Repair: if rl.isEnabledFor(logging.INFO): valstr = f"({self.value})" if self.value is not None else "" - rl.debug(f"{self.repair.value}{valstr} @ {self.cost} input:{input_index}") - rl.debug(f" {','.join(str(s) for s in self.stack.flatten())}") + rl.info(f"{self.repair.value}{valstr} @ {self.cost} input:{input_index}") + rl.info(f" {','.join(str(s) for s in self.stack.flatten())}") state = self.stack.state @@ -192,7 +188,7 @@ class Repair: # necessary, producing a new version of the stack. Count up the # number of successful shifts. for token in table.actions[state].keys(): - rl.debug(f" token: {token}") + rl.info(f" token: {token}") new_stack, success = self.stack.handle_token(table, token) if new_stack is None: # Not clear why this is necessary, but I think state merging @@ -201,7 +197,7 @@ class Repair: continue if token == input[input_index].kind: - rl.debug(f" generate shift {token}") + rl.info(f" generate shift {token}") yield Repair( repair=RepairAction.Shift, parent=self, @@ -210,7 +206,7 @@ class Repair: advance=1, # Move forward by one. ) - rl.debug(f" generate insert {token}") + rl.info(f" generate insert {token}") yield Repair( repair=RepairAction.Insert, value=token, @@ -227,7 +223,7 @@ class Repair: # delete-insert pairs, not insert-delete, because they are # symmetrical and therefore a waste of time and memory.) if self.repair != RepairAction.Insert: - rl.debug(f" generate delete") + rl.info(f" generate delete") yield Repair( repair=RepairAction.Delete, parent=self, @@ -238,7 +234,6 @@ class Repair: def recover(table: parser.ParseTable, input: list[TokenValue], start: int, stack: ParseStack): - rl = recover_log initial = Repair( repair=RepairAction.Base, cost=0, @@ -264,10 +259,6 @@ def recover(table: parser.ParseTable, input: list[TokenValue], start: int, stack repairs.append(repair) repair = repair.parent repairs.reverse() - if rl.isEnabledFor(logging.INFO): - rl.info("Recovered with actions:") - for repair in repairs: - rl.info(" " + repr(repair)) return repairs for neighbor in repair.neighbors(table, input, start): @@ -331,29 +322,21 @@ class Parser: case parser.Reduce(name=name, count=size, transparent=transparent): children: list[TokenValue | Tree] = [] - if size > 0: - for _, c in stack[-size:]: - if c is None: - continue - elif isinstance(c, Tree) and c.name is None: - children.extend(c.children) - else: - children.append(c) - del stack[-size:] - - start = children[0].start - end = children[-1].end - - else: - start = end = current_token.start + for _, c in stack[-size:]: + if c is None: + continue + elif isinstance(c, Tree) and c.name is None: + children.extend(c.children) + else: + children.append(c) value = Tree( name=name if not transparent else None, - start=start, - end=end, + start=children[0].start, + end=children[-1].end, children=tuple(children), ) - + del stack[-size:] goto = self.table.gotos[stack[-1][0]].get(name) assert goto is not None stack.append((goto, value)) @@ -747,10 +730,7 @@ class Harness: # Now that we know how many lines there are we can figure out how # many characters we need for the line number... - if len(lines) > 0: - line_number_chars = int(math.log(len(lines), 10)) + 1 - else: - line_number_chars = 1 + line_number_chars = int(math.log(len(lines), 10)) + 1 # ...which lets us wrap the lines appropriately. wrapper = textwrap.TextWrapper(