yml2c
author Volker Birk <vb@pep-project.org>
Thu, 08 Apr 2021 21:11:30 +0200
changeset 77 5b23278ed81f
parent 72 e52ee17bca47
permissions -rwxr-xr-x
updating documentation
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
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
"""\
40
Volker Birk <vb@pep-project.org>
parents: 35
diff changeset
     5
YML 2 compiler version 6.2
50
963c1b542610 Update copyright year.
Hartmut Goebel <h.goebel@crazy-compilers.com>
parents: 43
diff changeset
     6
Copyleft (c), 2009-2020, 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
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
72
e52ee17bca47 grammar in own file
Volker Birk <vb@pep-project.org>
parents: 68
diff changeset
    15
from yml2.grammar 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
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    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
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    20
def printInfo(option, opt_str, value, parser):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    21
    sys.stdout.write(__doc__)
34
d9eaf22f13ea exit on version string
Volker Birk <vb@pep-project.org>
parents: 32
diff changeset
    22
    sys.exit(0)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    23
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    24
def w(msg):
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    25
    if isinstance(msg, BaseException):
32
Volker Birk <vb@pep-project.org>
parents: 31
diff changeset
    26
        msg = str(msg) + "\n"
31
d3dddb80d1f5 adapting to Python 3
Volker Birk <vb@pep-project.org>
parents: 29
diff changeset
    27
    if type(msg) is bytes:
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    28
        msg = codecs.encode(msg, sys.stderr.encoding)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    29
    sys.stderr.write(msg)
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    30
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    31
def main():
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    32
    optParser = OptionParser()
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    33
    optParser.add_option("-C", "--old-syntax", action="store_true", dest="old_syntax",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    34
            help="syntax of YML 2 version 1.x (compatibility mode)", default=False)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    35
    optParser.add_option("-D", "--emit-linenumbers", action="store_true", dest="emitlinenumbers",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    36
            help="emit line numbers into the resulting XML for debugging purposes", default=False)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    37
    optParser.add_option("-E", "--encoding", dest="encoding", metavar="ENCODING", default=locale.getdefaultlocale()[1],
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    38
            help="encoding of input files (default to locale)")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    39
    optParser.add_option("-I", "--include", dest="includePathText", metavar="INCLUDE_PATH",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    40
            help="precede YML_PATH by a colon separated INCLUDE_PATH to search for include files")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    41
    optParser.add_option("-m", "--omit-empty-parm-tags", action="store_true", dest="omitemptyparm",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    42
            help="does nothing (only there for compatibility reasons)", default=False)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    43
    optParser.add_option("-n", "--normalization", dest="normalization", metavar="NORMALIZATION", default="NFC",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    44
            help="Unicode normalization (none, NFD, NFKD, NFC, NFKC, FCD, default is NFC)")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    45
    optParser.add_option("-o", "--output", dest="outputFile", metavar="FILE",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    46
            help="place output in file FILE")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    47
    optParser.add_option("-p", "--parse-only", action="store_true", dest="parseonly",
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    48
            help="parse only, then output pyAST as text to stdout", default=False)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    49
    optParser.add_option("-V", "--version", action="callback", callback=printInfo, help="show version info")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    50
    (options, args) = optParser.parse_args()
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    51
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    52
    if options.old_syntax:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    53
        oldSyntax()
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    54
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    55
    if options.emitlinenumbers:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    56
        backend.emitlinenumbers = True
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    57
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    58
    backend.encoding = options.encoding
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    59
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    60
    try:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    61
        if options.includePathText:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    62
            backend.includePath = options.includePathText.split(':')
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    63
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    64
        dirs = os.environ.get('YML_PATH', '.').split(':') + YML_DEFAULT_PATH
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    65
        backend.includePath.extend(dirs)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    66
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    67
        files = fileinput.input(args, mode="rU", openhook=fileinput.hook_encoded(options.encoding))
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    68
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    69
        ymlC = ymlCStyle()
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    70
        result = parse(ymlC, files, True, comment, packrat=True)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    71
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    72
        if options.parseonly:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    73
            print(result)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    74
        else:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    75
            result = backend.finish(result)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    76
            if options.normalization != "none":
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    77
                result = unicodedata.normalize(options.normalization, result)
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    78
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    79
            if options.outputFile and options.outputFile != "-":
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    80
                outfile = open(options.outputFile, "wb")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    81
                outfile.write(codecs.encode(result, options.encoding))
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    82
                outfile.close()
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    83
            else:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    84
                sys.stdout.buffer.write(codecs.encode(result, options.encoding))
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    85
                print()
0
76005e62091d initial commit
Volker Birk <vb@pep-project.org>
parents:
diff changeset
    86
64
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    87
    except KeyboardInterrupt:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    88
        w("\n")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    89
        sys.exit(1)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    90
    except KeyError as msg:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    91
        w("not found: " + u(msg) + "\n")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    92
        sys.exit(4)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    93
    except LookupError as msg:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    94
        w("not found: " + u(msg) + "\n")
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    95
        sys.exit(4)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    96
    except Exception as msg:
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    97
        w(msg)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    98
        sys.exit(5)
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
    99
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
   100
4e4782144f4f make main(), easier merge
Claudio Luck <claudio.luck@pep.foundation>
parents: 50
diff changeset
   101
if __name__ == "__main__":
68
26120af11afd do not need this
Volker Birk <vb@pep-project.org>
parents: 64
diff changeset
   102
    main()