forked from aixp/pycoco
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__main__.py
More file actions
98 lines (69 loc) · 3.49 KB
/
Copy path__main__.py
File metadata and controls
98 lines (69 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
"""Coco.py -- the Compiler Driver"""
__copyright__ = """
Compiler Generator Coco/R,
Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
extended by M. Loeberbauer & A. Woess, Univ. of Linz
ported from Java to Python by Ronald Longo
improved and refactored by KOLANICH
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
As an exception, it is allowed to write an extension of Coco/R that is used as a plugin in non-free software.
If not otherwise stated, any source code generated by Coco/R (other than Coco/R itself) does not fall under the GNU General Public License.
""" # pylint: disable=duplicate-code
import sys
from pathlib import Path
import plumbum.cli
from .CLI import CocoArgs
from .controller import Controller
from .setupInfo import MetaData
from .utils import astIntoText
ROOT_DIR = Path(__file__).absolute().parent
class CocoCli(CocoArgs):
if "version" in MetaData and "author" in MetaData and "author_email" in MetaData:
DESCRIPTION = "Coco/R v%s for Python - Translated by %s (%s)\n" % (MetaData["version"], MetaData["author"], MetaData["author_email"])
else:
DESCRIPTION = "Coco/R v??? for Python - Translated by ??? (??)\nWE CANNOT RETRIEVE THE METADATA correctly, SOMETHING GONE WRONG, FIX IT: " + repr(MetaData)
outputDir = plumbum.cli.SwitchAttr(("-O", "outputDir"), plumbum.cli.ExistingDirectory, default=False, help="Output files to that directory.")
useAnnotatedAssignments = plumbum.cli.Flag(("useAnnotatedAssignments",), default=False, help="Use annotated assignments. This will make your code incompatible to python < 3.5. It is not recommended to enable it. When disabled, type comments - a backward-compatible mechanism is used.")
def main(self, ATGName: plumbum.cli.ExistingFile):
ATGName = Path(ATGName)
dirName = ATGName.parent
fileName = ATGName.name
if not self.outputDir:
self.outputDir = dirName
self.outputDir = Path(self.outputDir)
# Initialize the Scanner
try:
strVal = ATGName.read_text(encoding="utf-8")
except IOError:
raise RuntimeError('-- Compiler Error: Cannot open file "%s"' % ATGName)
c = Controller()
for key in c.table.ddt.__slots__:
try:
v = getattr(self, key)
except AttributeError:
continue
setattr(c.table.ddt, key, v)
c.parse(strVal)
res = c.pipeline(useAnnotatedAssignments=self.useAnnotatedAssignments)
sys.stdout.write(c.errors.Summarize(c.scanner.buffer))
if res.parser:
sys.stdout.write("parser")
try:
(self.outputDir / "Parser.py").write_text(astIntoText(res.parser))
except BaseException as ex:
print(ex)
raise RuntimeError("-- Compiler Error: Cannot generate parser file.")
if res.scanner:
sys.stdout.write(" + scanner")
try:
(self.outputDir / "Scanner.py").write_text(astIntoText(res.scanner))
except BaseException as ex:
print(ex)
raise RuntimeError("-- Compiler Error: Cannot generate scanner file.")
sys.stdout.write(" generated\n")
return 0
if __name__ == "__main__":
CocoCli.run()