>b's YML 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.8 KiB

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