aboutsummaryrefslogtreecommitdiff
path: root/src/journal_lib/parse/parsewrapper.py
diff options
context:
space:
mode:
authorjakobst1n <jakob.stendahl@outlook.com>2023-10-16 20:54:46 +0200
committerjakobst1n <jakob.stendahl@outlook.com>2023-10-16 20:54:46 +0200
commit3bb38fcbbc9703ba22429441604d66f577fc6010 (patch)
treeec90232fb8b6502d1efc6a6c6a748f15bb3fcc0e /src/journal_lib/parse/parsewrapper.py
downloadjournal-lib-3bb38fcbbc9703ba22429441604d66f577fc6010.tar.gz
journal-lib-3bb38fcbbc9703ba22429441604d66f577fc6010.zip
Initial commit
Diffstat (limited to 'src/journal_lib/parse/parsewrapper.py')
-rw-r--r--src/journal_lib/parse/parsewrapper.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/journal_lib/parse/parsewrapper.py b/src/journal_lib/parse/parsewrapper.py
new file mode 100644
index 0000000..c75b32c
--- /dev/null
+++ b/src/journal_lib/parse/parsewrapper.py
@@ -0,0 +1,29 @@
+from .ply import yacc
+
+class ParseWrapper(object):
+
+ def __init__(self, tokens = None, debug: bool = False):
+ if tokens is not None:
+ self.tokens = tokens
+ self.debug = debug
+ self.build(debug=debug)
+
+ def build(self, **kwargs):
+ self.parser = yacc.yacc(module=self, **kwargs)
+
+ def parse(self, *args, **kwargs):
+ return self.parser.parse(*args, **kwargs)
+
+ def _hl_token(self, p):
+ try:
+ linestart = p.lexer.lexdata.rfind("\n", 0, p.lexpos) + 1
+ lineend = p.lexer.lexdata.find("\n", p.lexpos)
+ markpos = p.lexpos - linestart
+ marklen = len(str(p.value))
+ lineno = p.lexer.lexdata[0:linestart+1].count("\n")
+ print(f"Syntax error at '{p.value}' on line {lineno}, position {markpos}")
+ print(f" {p.lexer.lexdata[linestart:lineend]}")
+ print(f" {' ' * markpos}{'^' * marklen}")
+ except Exception as e:
+ print(f"An error occured when showing the position of token {p}\n{e}")
+