>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.

95 lines
3.4 KiB

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