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.

65 regels
1.7 KiB

  1. from __future__ import absolute_import
  2. import unittest
  3. import logging
  4. import os
  5. logging.basicConfig(level=logging.INFO)
  6. from lark.tools.nearley import create_code_for_nearley_grammar
  7. NEARLEY_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'nearley'))
  8. BUILTIN_PATH = os.path.join(NEARLEY_PATH, 'builtin')
  9. class TestNearley(unittest.TestCase):
  10. def test_css(self):
  11. fn = os.path.join(NEARLEY_PATH, 'examples/csscolor.ne')
  12. with open(fn) as f:
  13. grammar = f.read()
  14. code = create_code_for_nearley_grammar(grammar, 'csscolor', BUILTIN_PATH, os.path.dirname(fn))
  15. d = {}
  16. exec (code, d)
  17. parse = d['parse']
  18. c = parse('#a199ff')
  19. assert c['r'] == 161
  20. assert c['g'] == 153
  21. assert c['b'] == 255
  22. c = parse('rgb(255, 70%, 3)')
  23. assert c['r'] == 255
  24. assert c['g'] == 178
  25. assert c['b'] == 3
  26. def test_include(self):
  27. fn = os.path.join(NEARLEY_PATH, 'test/grammars/folder-test.ne')
  28. with open(fn) as f:
  29. grammar = f.read()
  30. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, os.path.dirname(fn))
  31. d = {}
  32. exec (code, d)
  33. parse = d['parse']
  34. parse('a')
  35. parse('b')
  36. def test_multi_include(self):
  37. fn = os.path.join(NEARLEY_PATH, 'test/grammars/multi-include-test.ne')
  38. with open(fn) as f:
  39. grammar = f.read()
  40. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, os.path.dirname(fn))
  41. d = {}
  42. exec (code, d)
  43. parse = d['parse']
  44. parse('a')
  45. parse('b')
  46. parse('c')
  47. if __name__ == '__main__':
  48. unittest.main()