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.

79 lines
2.1 KiB

  1. #
  2. # This example demonstrates usage of the included Python grammars
  3. #
  4. import sys
  5. import os, os.path
  6. from io import open
  7. import glob, time
  8. from lark import Lark
  9. from lark.indenter import Indenter
  10. # __path__ = os.path.dirname(__file__)
  11. class PythonIndenter(Indenter):
  12. NL_type = '_NEWLINE'
  13. OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
  14. CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
  15. INDENT_type = '_INDENT'
  16. DEDENT_type = '_DEDENT'
  17. tab_len = 8
  18. kwargs = dict(rel_to=__file__, postlex=PythonIndenter(), start='file_input')
  19. python_parser2 = Lark.open('python2.lark', parser='lalr', **kwargs)
  20. python_parser3 = Lark.open('python3.lark',parser='lalr', **kwargs)
  21. python_parser2_earley = Lark.open('python2.lark', parser='earley', lexer='standard', **kwargs)
  22. try:
  23. xrange
  24. except NameError:
  25. chosen_parser = python_parser3
  26. else:
  27. chosen_parser = python_parser2
  28. def _read(fn, *args):
  29. kwargs = {'encoding': 'iso-8859-1'}
  30. with open(fn, *args, **kwargs) as f:
  31. return f.read()
  32. def _get_lib_path():
  33. if os.name == 'nt':
  34. if 'PyPy' in sys.version:
  35. return os.path.join(sys.prefix, 'lib-python', sys.winver)
  36. else:
  37. return os.path.join(sys.prefix, 'Lib')
  38. else:
  39. return [x for x in sys.path if x.endswith('%s.%s' % sys.version_info[:2])][0]
  40. def test_python_lib():
  41. path = _get_lib_path()
  42. start = time.time()
  43. files = glob.glob(path+'/*.py')
  44. for f in files:
  45. print( f )
  46. chosen_parser.parse(_read(os.path.join(path, f)) + '\n')
  47. end = time.time()
  48. print( "test_python_lib (%d files), time: %s secs"%(len(files), end-start) )
  49. def test_earley_equals_lalr():
  50. path = _get_lib_path()
  51. files = glob.glob(path+'/*.py')
  52. for f in files:
  53. print( f )
  54. tree1 = python_parser2.parse(_read(os.path.join(path, f)) + '\n')
  55. tree2 = python_parser2_earley.parse(_read(os.path.join(path, f)) + '\n')
  56. assert tree1 == tree2
  57. if __name__ == '__main__':
  58. test_python_lib()
  59. # test_earley_equals_lalr()
  60. # python_parser3.parse(_read(sys.argv[1]) + '\n')