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