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.

83 lines
2.3 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. def _read(fn, *args):
  23. kwargs = {'encoding': 'iso-8859-1'}
  24. with open(fn, *args, **kwargs) as f:
  25. return f.read()
  26. def _get_lib_path():
  27. if os.name == 'nt':
  28. if 'PyPy' in sys.version:
  29. return os.path.join(sys.prefix, 'lib-python', sys.winver)
  30. else:
  31. return os.path.join(sys.prefix, 'Lib')
  32. else:
  33. return [x for x in sys.path if x.endswith('%s.%s' % sys.version_info[:2])][0]
  34. def test_python_lib():
  35. path = _get_lib_path()
  36. start = time.time()
  37. files = glob.glob(path+'/*.py')
  38. for f in files:
  39. print( f )
  40. try:
  41. # print list(python_parser.lex(_read(os.path.join(path, f)) + '\n'))
  42. try:
  43. xrange
  44. except NameError:
  45. python_parser3.parse(_read(os.path.join(path, f)) + '\n')
  46. else:
  47. python_parser2.parse(_read(os.path.join(path, f)) + '\n')
  48. except:
  49. print ('At %s' % f)
  50. raise
  51. end = time.time()
  52. print( "test_python_lib (%d files), time: %s secs"%(len(files), end-start) )
  53. def test_earley_equals_lalr():
  54. path = _get_lib_path()
  55. files = glob.glob(path+'/*.py')
  56. for f in files:
  57. print( f )
  58. tree1 = python_parser2.parse(_read(os.path.join(path, f)) + '\n')
  59. tree2 = python_parser2_earley.parse(_read(os.path.join(path, f)) + '\n')
  60. assert tree1 == tree2
  61. if __name__ == '__main__':
  62. test_python_lib()
  63. # test_earley_equals_lalr()
  64. # python_parser3.parse(_read(sys.argv[1]) + '\n')