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.

37 lines
1.3 KiB

  1. import sys
  2. from argparse import ArgumentParser, FileType
  3. from lark import Lark
  4. base_argparser = ArgumentParser(add_help=False, epilog='Look at the Lark documentation for more info on the options')
  5. flags = [
  6. ('d', 'debug'),
  7. 'keep_all_tokens',
  8. 'regex',
  9. 'propagate_positions',
  10. 'maybe_placeholders',
  11. 'use_bytes'
  12. ]
  13. options = ['start', 'lexer']
  14. base_argparser.add_argument('-s', '--start', action='append', default=[])
  15. base_argparser.add_argument('-l', '--lexer', default='contextual', choices=('standard', 'contextual'))
  16. k = {'encoding':'utf-8'} if sys.version_info > (3, 4) else {}
  17. base_argparser.add_argument('-o', '--out', type=FileType('w', **k), default=sys.stdout, help='the output file (default=stdout)')
  18. base_argparser.add_argument('grammar_file', type=FileType('r', **k), help='A valid .lark file')
  19. for f in flags:
  20. if isinstance(f, tuple):
  21. options.append(f[1])
  22. base_argparser.add_argument('-' + f[0], '--' + f[1], action='store_true')
  23. else:
  24. options.append(f)
  25. base_argparser.add_argument('--' + f, action='store_true')
  26. def build_lalr(namespace):
  27. if len(namespace.start) == 0:
  28. namespace.start.append('start')
  29. kwargs = {n: getattr(namespace, n) for n in options}
  30. return Lark(namespace.grammar_file, parser='lalr', **kwargs), namespace.out