This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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