Commit 772704b2 authored by eckhart's avatar eckhart
Browse files

- parser.py: better parser history representation

parent f45d1378
...@@ -55,7 +55,9 @@ Vegard Øye: General Parser Combinators in Racket, 2012, ...@@ -55,7 +55,9 @@ Vegard Øye: General Parser Combinators in Racket, 2012,
https://epsil.github.io/gll/ https://epsil.github.io/gll/
""" """
import collections
import copy import copy
import html
import os import os
from functools import partial from functools import partial
...@@ -143,6 +145,7 @@ class HistoryRecord: ...@@ -143,6 +145,7 @@ class HistoryRecord:
MATCH = "MATCH" MATCH = "MATCH"
ERROR = "ERROR" ERROR = "ERROR"
FAIL = "FAIL" FAIL = "FAIL"
Snapshot = collections.namedtuple('Snapshot', ['line', 'column', 'stack', 'status', 'text'])
def __init__(self, call_stack: List['Parser'], node: Node, text: StringView) -> None: def __init__(self, call_stack: List['Parser'], node: Node, text: StringView) -> None:
# copy call stack, dropping uninformative Forward-Parsers # copy call stack, dropping uninformative Forward-Parsers
...@@ -157,13 +160,19 @@ class HistoryRecord: ...@@ -157,13 +160,19 @@ class HistoryRecord:
self.line_col = line_col(lbreaks, len(document) - len(text)) self.line_col = line_col(lbreaks, len(document) - len(text))
def __str__(self): def __str__(self):
return '%4i, %2i: %s; %s; "%s"' % self.as_tuple()
def as_tuple(self) -> Snapshot:
if self.node: if self.node:
excerpt = self.text[:len(self.node)] excerpt = self.text[:len(self.node)]
else: else:
excerpt = str(self.text[:25]) + '...' excerpt = str(self.text[:25]) + '...'
excerpt = excerpt.replace('\n', '\\n') excerpt = excerpt.replace('\n', '\\n')
return '%5i, %5i, %s, %s, "%s"' % \ return self.Snapshot(self.line_col[0], self.line_col[1], self.stack, self.status, excerpt)
(self.line_col[0], self.line_col[1], self.stack, self.status, excerpt)
def as_html_table_row(self) -> str:
escaped = tuple(html.escape(str(item)) for item in self.as_tuple())
return "<td>%i</td><td>%i</td> <td>%s</td> <td>%s</td> <td>%s</td>" % escaped
def err_msg(self) -> str: def err_msg(self) -> str:
return self.ERROR + ": " + "; ".join( return self.ERROR + ": " + "; ".join(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment