Skip to content

Commit

Permalink
Improved Python target to become more PEP-8
Browse files Browse the repository at this point in the history
  • Loading branch information
phorward committed Oct 26, 2023
1 parent 98df114 commit c2e46f1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 63 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <true>- and <false>-settings
- Target `python` updated to spaces instead of tabs and further improvements
- Target `javascript` updated to more recent JS/ECMAScript6 features

## [v1.7]
Expand Down
126 changes: 64 additions & 62 deletions targets/python.tlt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
<escape-sequence for="\" do="\\" />
<escape-sequence for="&#x22;" do="\&#x22;" />

<code_localization />

<vstack_def_type>any</vstack_def_type>
<vstack_term_type>any</vstack_term_type>
<vstack_union_att>@@value-type-id</vstack_union_att>
Expand All @@ -33,77 +31,67 @@
<scan_action_set_symbol>pcb.sym = @@sym</scan_action_set_symbol>

<defprod>
<col>@@production-number</col>
<col_sep>, </col_sep>
<col> @@production-number,&#x0A;</col>
</defprod>

<acttab>
<row_start> (</row_start>
<row_end>)</row_end>
<row_sep>,&#x0A;</row_sep>
<col>(@@symbol, @@action, @@index), </col>
<col_sep></col_sep>
<row_start> (&#x0A;</row_start>
<row_end> ),&#x0A;</row_end>
<col> (@@symbol, @@action, @@index),&#x0A;</col>
</acttab>

<gotab>
<row_start> (</row_start>
<row_end>)</row_end>
<row_sep>,&#x0A;</row_sep>
<col>(@@symbol, @@action, @@index), </col>
<col_sep></col_sep>
<row_start> (&#x0A;</row_start>
<row_end> ),&#x0A;</row_end>
<col> (@@symbol, @@action, @@index),&#x0A;</col>
</gotab>

<dfa_select>
<col>@@machine</col>
<col_sep>, </col_sep>
<col> @@machine,&#x0A;</col>
</dfa_select>

<dfa_char>
<col>(@@from, @@to)</col>
<col_sep>, </col_sep>
<col> (@@from, @@to),&#x0A;</col>
</dfa_char>

<dfa_trans>
<col>@@goto</col>
<col_sep>, </col_sep>
<col> @@goto,&#x0A;</col>
</dfa_trans>

<dfa_idx>
<row_start> (</row_start>
<row_end>),</row_end>
<row_sep>&#x0A;</row_sep>
<col>@@index</col>
<col_sep>, </col_sep>
<row_start> (&#x0A;</row_start>
<row_end> ),&#x0A;</row_end>
<col> @@index,&#x0A;</col>
</dfa_idx>

<dfa_accept>
<row_start> (</row_start>
<row_end>),</row_end>
<row_sep>&#x0A;</row_sep>
<col>@@accept</col>
<col_sep>, </col_sep>
<row_start> (&#x0A;</row_start>
<row_end> ),&#x0A;</row_end>
<col> @@accept,&#x0A;</col>
</dfa_accept>

<symbols>
<col> ("@@symbol-name", "@@emit", @@type, @@lexem, @@whitespace, @@greedy)</col>
<col_sep>,&#x0A;</col_sep>
<col> ("@@symbol-name", "@@emit", @@type, @@lexem, @@whitespace, @@greedy),&#x0A;</col>
</symbols>

<productions>
<col> ("@@production", "@@emit", @@length, @@lhs)</col>
<col_sep>,&#x0A;</col_sep>
<col> ("@@production", "@@emit", @@length, @@lhs),&#x0A;</col>
</productions>

<file filename="@@basename.py">#!/usr/bin/python
#-*- coding: utf-8 -*-

# Parser module generated by unicc from @@filename.
<file filename="@@basename.py">#!/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):
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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]
Expand Down Expand Up @@ -490,7 +491,7 @@ class @@prefixParser(object):
if pcb.act &amp; 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.
Expand Down Expand Up @@ -526,7 +527,8 @@ class @@prefixParser(object):
node = None

return pcb.ret or node

@@scan_actions
@@actions
@@epilogue

if __name__ == "__main__":
Expand Down

0 comments on commit c2e46f1

Please sign in to comment.