From c2e46f1dc6a887e1e9f8266e5dc5fcbbd76adda7 Mon Sep 17 00:00:00 2001 From: Jan Max Meyer Date: Thu, 26 Oct 2023 11:41:29 +0200 Subject: [PATCH] Improved Python target to become more PEP-8 --- CHANGELOG.md | 4 +- targets/python.tlt | 126 +++++++++++++++++++++++---------------------- 2 files changed, 67 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ec103c..5fa6cd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,12 @@ This file is used to document any relevant changes done to UniCC. -## [main] +## [v1.8] Current main branch. +- Improved program module generator to support - and -settings +- Target `python` updated to spaces instead of tabs and further improvements - Target `javascript` updated to more recent JS/ECMAScript6 features ## [v1.7] diff --git a/targets/python.tlt b/targets/python.tlt index 6ed393e..d88e14f 100644 --- a/targets/python.tlt +++ b/targets/python.tlt @@ -5,8 +5,6 @@ - - any any @@value-type-id @@ -33,77 +31,67 @@ pcb.sym = @@sym - @@production-number - , + @@production-number, - ( - ) - , - (@@symbol, @@action, @@index), - + ( + ), + (@@symbol, @@action, @@index), - ( - ) - , - (@@symbol, @@action, @@index), - + ( + ), + (@@symbol, @@action, @@index), - @@machine - , + @@machine, - (@@from, @@to) - , + (@@from, @@to), - @@goto - , + @@goto, - ( - ), - - @@index - , + ( + ), + @@index, - ( - ), - - @@accept - , + ( + ), + @@accept, - ("@@symbol-name", "@@emit", @@type, @@lexem, @@whitespace, @@greedy) - , + ("@@symbol-name", "@@emit", @@type, @@lexem, @@whitespace, @@greedy), - ("@@production", "@@emit", @@length, @@lhs) - , + ("@@production", "@@emit", @@length, @@lhs), - #!/usr/bin/python -#-*- coding: utf-8 -*- - -# Parser module generated by unicc from @@filename. + #!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This parser module was generated by unicc from @@filename. # DO NOT EDIT THIS FILE MANUALLY, IT WILL GO AWAY! +# @@prologue -class @@prefixNode(object): +class @@prefixNode: """ This is an AST node. + + Objects of this class are only created when the parser makes + use of the AST node syntax. """ def __init__(self, emit = None, match = None, children = None): @@ -128,6 +116,7 @@ class @@prefixNode(object): class @@prefixParseException(Exception): """ Exception to be raised on a parse error. + TODO: This might be replaced by SyntaxError builtin. """ def __init__(self, row, col, txt = None): @@ -146,17 +135,25 @@ class @@prefixParseException(Exception): self.expecting = expecting -class @@prefixParserToken(object): +class _@@prefixToken: + """ + Only used internally; + Represents a token on the stack. + """ state = 0 line = 0 column = 0 - node = None - @@value-type-definition -class @@prefixParserControlBlock(object): +class _@@prefixControlBlock: + """ + Only used internally; + Represents the current parser state, and allows to + run multiple parser threads within one @@prefixParser. + """ + def __init__(self, input): # Stack @@ -192,11 +189,13 @@ class @@prefixParserControlBlock(object): self.line = 1 self.column = 1 - @@pcb - -class @@prefixParser(object): +class @@prefixParser: + """ + Implements a LALR-Parser for @@name. + Call @@prefixParser.parse() to start parsing a given string. + """ # Actions _ERROR = 0 @@ -218,23 +217,27 @@ class @@prefixParser(object): @@goto-table ) - _def_prod = (@@default-productions) + _def_prod = ( +@@default-productions + ) # Lexical analysis - _dfa_select = (@@dfa-select) + _dfa_select = ( +@@dfa-select + ) _dfa_index = ( @@dfa-index ) - _dfa_chars = (@@dfa-char) - _dfa_trans = (@@dfa-trans) + _dfa_chars = ( +@@dfa-char + ) + _dfa_trans = ( +@@dfa-trans + ) _dfa_accept = ( @@dfa-accept ) - # Parsing actions -@@scan_actions -@@actions - # Parsing algorithm def _get_act(self, pcb): @@ -394,10 +397,10 @@ class @@prefixParser(object): except NameError: s = input(">") - pcb = @@prefixParserControlBlock(s) + pcb = _@@prefixControlBlock(s) pcb.act = self._SHIFT - pcb.tos = @@prefixParserToken() + pcb.tos = _@@prefixToken() pcb.stack.append(pcb.tos) while True: @@ -438,9 +441,7 @@ class @@prefixParser(object): # Handle AST nodes if self._productions[pcb.idx][1]: #print("%s = %s" % (self._productions[pcb.idx][0], self._productions[pcb.idx][1])) - node = @@prefixNode(self._productions[pcb.idx][1], - children=cnodes) - + node = @@prefixNode(self._productions[pcb.idx][1], children=cnodes) else: node = None @@ -457,7 +458,7 @@ class @@prefixParser(object): self._get_go(pcb) - pcb.tos = @@prefixParserToken() + pcb.tos = _@@prefixToken() pcb.stack.append(pcb.tos) pcb.tos.symbol = self._symbols[pcb.lhs] @@ -490,7 +491,7 @@ class @@prefixParser(object): if pcb.act & self._SHIFT: #print("SHIFT", pcb.sym, self._symbols[pcb.sym]) - pcb.tos = @@prefixParserToken() + pcb.tos = _@@prefixToken() pcb.stack.append(pcb.tos) # Execute scanner actions, if existing. @@ -526,7 +527,8 @@ class @@prefixParser(object): node = None return pcb.ret or node - +@@scan_actions +@@actions @@epilogue if __name__ == "__main__":