-
Notifications
You must be signed in to change notification settings - Fork 0
/
yml2c
executable file
·102 lines (82 loc) · 3.84 KB
/
yml2c
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
99
100
101
102
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
"""\
YML 2 compiler version 7.6
Copyleft (c), 2009-2023, Volker Birk http://fdik.org/yml/
"""
import sys, os, codecs, locale
import fileinput, unicodedata
from optparse import OptionParser
from yml2.pyPEG import parse, u
from yml2.grammar import ymlCStyle, comment, oldSyntax
import yml2.backend as backend
YML_DEFAULT_PATH = [os.path.dirname(backend.__file__)]
def printInfo(option, opt_str, value, parser):
sys.stdout.write(__doc__)
sys.exit(0)
def w(msg):
if isinstance(msg, BaseException):
msg = str(msg) + "\n"
if type(msg) is bytes:
msg = codecs.encode(msg, sys.stderr.encoding)
sys.stderr.write(msg)
def main():
optParser = OptionParser()
optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers",
help="emit line numbers into the resulting XML for debugging purposes", default=False)
optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getlocale()[1],
help="encoding of input files (default to locale)")
optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH",
help="precede YML_PATH by a colon separated INCLUDE_PATH (semicolon on Windows) to search for include files")
optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm",
help="does nothing (only there for compatibility reasons)", default=False)
optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC",
help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)")
optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE",
help="place output in file FILE")
optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly",
help="parse only, then output pyAST as text to stdout", default=False)
optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info")
(options, args) = optParser.parse_args()
if options.old_syntax:
oldSyntax()
if options.emitlinenumbers:
backend.emitlinenumbers = True
backend.encoding = options.encoding
try:
if options.includePathText:
backend.includePath = options.includePathText.split(';' if os.name == 'nt' else ':')
dirs = os.environ.get('YML_PATH', '.').split(';' if os.name == 'nt' else ':') + YML_DEFAULT_PATH
backend.includePath.extend(dirs)
files = fileinput.input(args, mode="r", openhook=fileinput.hook_encoded(options.encoding))
ymlC = ymlCStyle()
result = parse(ymlC, files, True, comment, packrat=True)
if options.parseonly:
print(result)
else:
result = backend.finish(result)
if options.normalization != "none":
result = unicodedata.normalize(options.normalization, result)
if options.outputFile and options.outputFile != "-":
outfile = open(options.outputFile, "wb")
outfile.write(codecs.encode(result, options.encoding))
outfile.close()
else:
sys.stdout.buffer.write(codecs.encode(result, options.encoding))
print()
except KeyboardInterrupt:
w("\n")
sys.exit(1)
except KeyError as msg:
w("not found: " + u(msg) + "\n")
sys.exit(4)
except LookupError as msg:
w("not found: " + u(msg) + "\n")
sys.exit(4)
except Exception as msg:
w(msg)
sys.exit(5)
if __name__ == "__main__":
main()