From c29092d6b0126c2d8b963d6b6a715d4a37e15d44 Mon Sep 17 00:00:00 2001 From: Massimo Santini Date: Fri, 12 Apr 2024 14:29:48 +0200 Subject: [PATCH] Preparing to release --- CHANGELOG.txt | 6 ++++++ src/liblet/antlr.py | 7 ++++--- src/liblet/automaton.py | 2 +- src/liblet/const.py | 2 +- src/liblet/display.py | 4 ++-- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f43bc0d..22050e4 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,12 @@ Changelog ========= +v1.7.5 (2024-04-12) +------------------- + +- Added the line number as attribute of the ANTLR annotated parse tree. +- Now TopDownInstantaneousDescription.predict does not put the ε on the stack. + v1.7.4 (2024-04-11) ------------------- diff --git a/src/liblet/antlr.py b/src/liblet/antlr.py index 9abc209..65c15f3 100644 --- a/src/liblet/antlr.py +++ b/src/liblet/antlr.py @@ -195,7 +195,8 @@ def tree(self, text, symbol, simple=False, antlr_hook=None): - ``name``: the *rule label* or *token name* (or the rule name if it has no label: or, similarly, the token itself if it has no name), - ``value``: the *token* value (only present for *tokens* named in the *lexer* part of the grammar), - ``rule``: the *rule* name (only present for *rules*, will be different from ``name`` for labelled rules). - - ``src``: the first (and last token, in case of a rule), corredponding to the node. + - ``src``: the first (and last token, in case of a rule), corresponding to the node. + - ``line``: the line number of the (first) token. Note that the values are strings (so if the *value* is a number, it should be converted before usage). @@ -217,7 +218,7 @@ def _rule(ctx): name = ctx.__class__.__name__ name = name[:-7] # remove trailing 'Context' name = name[0].lower() + name[1:] - return {'type': 'rule', 'name': name, 'rule': rule, 'src': ctx.getSourceInterval()} + return {'type': 'rule', 'name': name, 'rule': rule, 'src': ctx.getSourceInterval(), 'line': ctx.start.line} def _token(token): ts = token.symbol @@ -230,7 +231,7 @@ def _token(token): name = '' if name == '': name = self.Parser.literalNames[ts.type][1:-1] - return {'type': 'token', 'name': name, 'value': text, 'src': token.symbol.tokenIndex} + return {'type': 'token', 'name': name, 'value': text, 'src': ts.tokenIndex, 'line': ts.line} class TreeVisitor(ParseTreeVisitor): def visitTerminal(self, t): diff --git a/src/liblet/automaton.py b/src/liblet/automaton.py index 6a5377c..83fb7a1 100644 --- a/src/liblet/automaton.py +++ b/src/liblet/automaton.py @@ -302,7 +302,7 @@ def predict(self, P): c.stack.pop() c.steps += (P,) for X in reversed(P.rhs): - c.stack.push(X) + if X != ε :c.stack.push(X) return c raise ValueError('The top of the stack does not correspond to the production lhs.') diff --git a/src/liblet/const.py b/src/liblet/const.py index ca75edb..e7be3a1 100644 --- a/src/liblet/const.py +++ b/src/liblet/const.py @@ -2,5 +2,5 @@ DIAMOND = '◇' HASH = '♯' GV_FONT_NAME = 'Fira Code' -GV_FONT_SIZE = '8' +GV_FONT_SIZE = '12' # must be a string HTML_FONT_NAME = GV_FONT_NAME diff --git a/src/liblet/display.py b/src/liblet/display.py index 0225fb8..109269a 100644 --- a/src/liblet/display.py +++ b/src/liblet/display.py @@ -112,10 +112,10 @@ def __repr__(self): class GVWrapper: def __init__(self, gv_graph_args=None, node_wrapper=None): gv_graph_args = gv_graph_args or {} - gv_graph_args['node_attr'] = {'fontname': f"'{GV_FONT_NAME}'", 'fontsize': f"'{GV_FONT_SIZE}'"} | ( + gv_graph_args['node_attr'] = {'fontname': GV_FONT_NAME, 'fontsize': GV_FONT_SIZE} | ( gv_graph_args['node_attr'] if 'node_attr' in gv_graph_args else {} ) - gv_graph_args['edge_attr'] = {'fontname': f"'{GV_FONT_NAME}'", 'fontsize': f"'{GV_FONT_SIZE}'"} | ( + gv_graph_args['edge_attr'] = {'fontname': GV_FONT_NAME, 'fontsize': GV_FONT_SIZE} | ( gv_graph_args['edge_attr'] if 'edge_attr' in gv_graph_args else {} ) node_wrapper = node_wrapper or make_node_wrapper()