This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
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.

66 lines
2.2 KiB

  1. import sys
  2. from argparse import ArgumentParser, FileType
  3. try:
  4. from textwrap import indent
  5. except ImportError:
  6. def indent(text, prefix):
  7. return ''.join(prefix + line for line in text.splitlines(True))
  8. from logging import DEBUG, INFO, WARN, ERROR
  9. import warnings
  10. from lark import Lark, logger
  11. lalr_argparser = ArgumentParser(add_help=False, epilog='Look at the Lark documentation for more info on the options')
  12. flags = [
  13. ('d', 'debug'),
  14. 'keep_all_tokens',
  15. 'regex',
  16. 'propagate_positions',
  17. 'maybe_placeholders',
  18. 'use_bytes'
  19. ]
  20. options = ['start', 'lexer']
  21. lalr_argparser.add_argument('-v', '--verbose', action='count', default=0, help="Increase Logger output level, up to three times")
  22. lalr_argparser.add_argument('-s', '--start', action='append', default=[])
  23. lalr_argparser.add_argument('-l', '--lexer', default='contextual', choices=('standard', 'contextual'))
  24. k = {'encoding': 'utf-8'} if sys.version_info > (3, 4) else {}
  25. lalr_argparser.add_argument('-o', '--out', type=FileType('w', **k), default=sys.stdout, help='the output file (default=stdout)')
  26. lalr_argparser.add_argument('grammar_file', type=FileType('r', **k), help='A valid .lark file')
  27. for f in flags:
  28. if isinstance(f, tuple):
  29. options.append(f[1])
  30. lalr_argparser.add_argument('-' + f[0], '--' + f[1], action='store_true')
  31. else:
  32. options.append(f)
  33. lalr_argparser.add_argument('--' + f, action='store_true')
  34. def build_lalr(namespace):
  35. logger.setLevel((ERROR, WARN, INFO, DEBUG)[min(namespace.verbose, 3)])
  36. if len(namespace.start) == 0:
  37. namespace.start.append('start')
  38. kwargs = {n: getattr(namespace, n) for n in options}
  39. return Lark(namespace.grammar_file, parser='lalr', **kwargs), namespace.out
  40. def showwarning_as_comment(message, category, filename, lineno, file=None, line=None):
  41. # Based on warnings._showwarnmsg_impl
  42. text = warnings.formatwarning(message, category, filename, lineno, line)
  43. text = indent(text, '# ')
  44. if file is None:
  45. file = sys.stderr
  46. if file is None:
  47. return
  48. try:
  49. file.write(text)
  50. except OSError:
  51. pass
  52. def make_warnings_comments():
  53. warnings.showwarning = showwarning_as_comment