author | Hartmut Goebel <h.goebel@crazy-compilers.com> |
Tue, 17 Mar 2020 11:05:51 +0100 | |
changeset 45 | 4ac3bb01f0bc |
parent 43 | fb35b9db9ca1 |
child 50 | 963c1b542610 |
permissions | -rwxr-xr-x |
31 | 1 |
#!/usr/bin/env python3 |
0 | 2 |
# vim: set fileencoding=utf-8 : |
3 |
||
4 |
"""\ |
|
40 | 5 |
YML 2 compiler version 6.2 |
31 | 6 |
Copyleft (c), 2009-2019, Volker Birk http://fdik.org/yml/ |
0 | 7 |
|
8 |
""" |
|
9 |
||
10 |
import sys, os, codecs, locale |
|
11 |
import fileinput, unicodedata |
|
12 |
from optparse import OptionParser |
|
13 |
||
41
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
40
diff
changeset
|
14 |
from yml2.pyPEG import parse, u |
0 | 15 |
from yml2 import ymlCStyle, comment, oldSyntax |
41
98a53c3282c3
Convert yml2 into a Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
40
diff
changeset
|
16 |
import yml2.backend as backend |
0 | 17 |
|
43
fb35b9db9ca1
Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
41
diff
changeset
|
18 |
YML_DEFAULT_PATH = [os.path.dirname(backend.__file__)] |
fb35b9db9ca1
Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
41
diff
changeset
|
19 |
|
0 | 20 |
def printInfo(option, opt_str, value, parser): |
21 |
sys.stdout.write(__doc__) |
|
34 | 22 |
sys.exit(0) |
0 | 23 |
|
24 |
def w(msg): |
|
25 |
if isinstance(msg, BaseException): |
|
32 | 26 |
msg = str(msg) + "\n" |
31 | 27 |
if type(msg) is bytes: |
0 | 28 |
msg = codecs.encode(msg, sys.stderr.encoding) |
29 |
sys.stderr.write(msg) |
|
30 |
||
31 |
optParser = OptionParser() |
|
32 |
optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", |
|
33 |
help="syntax of YML 2 version 1.x (compatibility mode)", default=False) |
|
34 |
optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", |
|
35 |
help="emit line numbers into the resulting XML for debugging purposes", default=False) |
|
36 |
optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], |
|
37 |
help="encoding of input files (default to locale)") |
|
38 |
optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", |
|
39 |
help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") |
|
40 |
optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", |
|
41 |
help="does nothing (only there for compatibility reasons)", default=False) |
|
42 |
optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", |
|
43 |
help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") |
|
44 |
optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", |
|
45 |
help="place output in file FILE") |
|
46 |
optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", |
|
47 |
help="parse only, then output pyAST as text to stdout", default=False) |
|
48 |
optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info") |
|
49 |
(options, args) = optParser.parse_args() |
|
50 |
||
51 |
if options.old_syntax: |
|
52 |
oldSyntax() |
|
53 |
||
54 |
if options.emitlinenumbers: |
|
55 |
backend.emitlinenumbers = True |
|
56 |
||
57 |
backend.encoding = options.encoding |
|
58 |
||
59 |
try: |
|
60 |
if options.includePathText: |
|
61 |
backend.includePath = options.includePathText.split(':') |
|
62 |
||
43
fb35b9db9ca1
Move data files into the Python package.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents:
41
diff
changeset
|
63 |
dirs = os.environ.get('YML_PATH', '.').split(':') + YML_DEFAULT_PATH |
0 | 64 |
backend.includePath.extend(dirs) |
65 |
||
66 |
files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) |
|
67 |
||
68 |
ymlC = ymlCStyle() |
|
69 |
result = parse(ymlC, files, True, comment, packrat=True) |
|
70 |
||
71 |
if options.parseonly: |
|
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
72 |
print(result) |
0 | 73 |
else: |
74 |
result = backend.finish(result) |
|
75 |
if options.normalization != "none": |
|
76 |
result = unicodedata.normalize(options.normalization, result) |
|
77 |
||
78 |
if options.outputFile and options.outputFile != "-": |
|
31 | 79 |
outfile = open(options.outputFile, "wb") |
0 | 80 |
outfile.write(codecs.encode(result, options.encoding)) |
81 |
outfile.close() |
|
82 |
else: |
|
31 | 83 |
sys.stdout.buffer.write(codecs.encode(result, options.encoding)) |
84 |
print() |
|
0 | 85 |
|
86 |
except KeyboardInterrupt: |
|
87 |
w("\n") |
|
88 |
sys.exit(1) |
|
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
89 |
except KeyError as msg: |
32 | 90 |
w("not found: " + u(msg) + "\n") |
0 | 91 |
sys.exit(4) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
92 |
except LookupError as msg: |
32 | 93 |
w("not found: " + u(msg) + "\n") |
0 | 94 |
sys.exit(4) |
7
f81a4471bc28
beginning with Python 3 compat
Volker Birk <vb@pep.foundation>
parents:
4
diff
changeset
|
95 |
except Exception as msg: |
0 | 96 |
w(msg) |
97 |
sys.exit(5) |