vb@31: #!/usr/bin/env python3 vb@0: # vim: set fileencoding=utf-8 : vb@0: vb@0: """\ vb@40: YML 2 compiler version 6.2 vb@31: Copyleft (c), 2009-2019, Volker Birk http://fdik.org/yml/ vb@0: vb@0: """ vb@0: vb@0: import sys, os, codecs, locale vb@0: import fileinput, unicodedata vb@0: from optparse import OptionParser vb@0: h@41: from yml2.pyPEG import parse, u vb@0: from yml2 import ymlCStyle, comment, oldSyntax h@41: import yml2.backend as backend vb@0: vb@0: def printInfo(option, opt_str, value, parser): vb@0: sys.stdout.write(__doc__) vb@34: sys.exit(0) vb@0: vb@0: def w(msg): vb@0: if isinstance(msg, BaseException): vb@32: msg = str(msg) + "\n" vb@31: if type(msg) is bytes: vb@0: msg = codecs.encode(msg, sys.stderr.encoding) vb@0: sys.stderr.write(msg) vb@0: vb@0: optParser = OptionParser() vb@0: optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax", vb@0: help="syntax of YML 2 version 1.x (compatibility mode)", default=False) vb@0: optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers", vb@0: help="emit line numbers into the resulting XML for debugging purposes", default=False) vb@0: optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1], vb@0: help="encoding of input files (default to locale)") vb@0: optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH", vb@0: help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files") vb@0: optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm", vb@0: help="does nothing (only there for compatibility reasons)", default=False) vb@0: optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC", vb@0: help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)") vb@0: optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE", vb@0: help="place output in file FILE") vb@0: optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly", vb@0: help="parse only, then output pyAST as text to stdout", default=False) vb@0: optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info") vb@0: (options, args) = optParser.parse_args() vb@0: vb@0: if options.old_syntax: vb@0: oldSyntax() vb@0: vb@0: if options.emitlinenumbers: vb@0: backend.emitlinenumbers = True vb@0: vb@0: backend.encoding = options.encoding vb@0: vb@0: try: vb@0: if options.includePathText: vb@0: backend.includePath = options.includePathText.split(':') vb@0: vb@0: dirs = os.environ.get('YML_PATH', '.').split(':') vb@0: backend.includePath.extend(dirs) vb@0: vb@0: files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding)) vb@0: vb@0: ymlC = ymlCStyle() vb@0: result = parse(ymlC, files, True, comment, packrat=True) vb@0: vb@0: if options.parseonly: vb@7: print(result) vb@0: else: vb@0: result = backend.finish(result) vb@0: if options.normalization != "none": vb@0: result = unicodedata.normalize(options.normalization, result) vb@0: vb@0: if options.outputFile and options.outputFile != "-": vb@31: outfile = open(options.outputFile, "wb") vb@0: outfile.write(codecs.encode(result, options.encoding)) vb@0: outfile.close() vb@0: else: vb@31: sys.stdout.buffer.write(codecs.encode(result, options.encoding)) vb@31: print() vb@0: vb@0: except KeyboardInterrupt: vb@0: w("\n") vb@0: sys.exit(1) vb@7: except KeyError as msg: vb@32: w("not found: " + u(msg) + "\n") vb@0: sys.exit(4) vb@7: except LookupError as msg: vb@32: w("not found: " + u(msg) + "\n") vb@0: sys.exit(4) vb@7: except Exception as msg: vb@0: w(msg) vb@0: sys.exit(5)