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.

110 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import unittest
  4. import logging
  5. import os
  6. import codecs
  7. from lark import logger
  8. from lark.tools.nearley import create_code_for_nearley_grammar, main as nearley_tool_main
  9. logger.setLevel(logging.INFO)
  10. TEST_PATH = os.path.abspath(os.path.dirname(__file__))
  11. NEARLEY_PATH = os.path.join(TEST_PATH, 'nearley')
  12. BUILTIN_PATH = os.path.join(NEARLEY_PATH, 'builtin')
  13. if not os.path.exists(NEARLEY_PATH):
  14. logger.warn("Nearley not installed. Skipping Nearley tests!")
  15. raise ImportError("Skipping Nearley tests!")
  16. import js2py # Ensures that js2py exists, to avoid failing tests
  17. class TestNearley(unittest.TestCase):
  18. def test_css(self):
  19. fn = os.path.join(NEARLEY_PATH, 'examples/csscolor.ne')
  20. with open(fn) as f:
  21. grammar = f.read()
  22. code = create_code_for_nearley_grammar(grammar, 'csscolor', BUILTIN_PATH, os.path.dirname(fn))
  23. d = {}
  24. exec (code, d)
  25. parse = d['parse']
  26. c = parse('#a199ff')
  27. assert c['r'] == 161
  28. assert c['g'] == 153
  29. assert c['b'] == 255
  30. c = parse('rgb(255, 70%, 3)')
  31. assert c['r'] == 255
  32. assert c['g'] == 178
  33. assert c['b'] == 3
  34. def test_include(self):
  35. fn = os.path.join(NEARLEY_PATH, 'test/grammars/folder-test.ne')
  36. with open(fn) as f:
  37. grammar = f.read()
  38. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, os.path.dirname(fn))
  39. d = {}
  40. exec (code, d)
  41. parse = d['parse']
  42. parse('a')
  43. parse('b')
  44. def test_multi_include(self):
  45. fn = os.path.join(NEARLEY_PATH, 'test/grammars/multi-include-test.ne')
  46. with open(fn) as f:
  47. grammar = f.read()
  48. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, os.path.dirname(fn))
  49. d = {}
  50. exec (code, d)
  51. parse = d['parse']
  52. parse('a')
  53. parse('b')
  54. parse('c')
  55. def test_utf8(self):
  56. grammar = u'main -> "±a"'
  57. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
  58. d = {}
  59. exec (code, d)
  60. parse = d['parse']
  61. parse(u'±a')
  62. def test_backslash(self):
  63. grammar = r'main -> "\""'
  64. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
  65. d = {}
  66. exec (code, d)
  67. parse = d['parse']
  68. parse(u'"')
  69. def test_null(self):
  70. grammar = r'main -> "a" | null'
  71. code = create_code_for_nearley_grammar(grammar, 'main', BUILTIN_PATH, './')
  72. d = {}
  73. exec (code, d)
  74. parse = d['parse']
  75. parse('a')
  76. parse('')
  77. def test_utf8_2(self):
  78. fn = os.path.join(TEST_PATH, 'grammars/unicode.ne')
  79. nearley_tool_main(fn, 'x', NEARLEY_PATH)
  80. def test_include_utf8(self):
  81. fn = os.path.join(TEST_PATH, 'grammars/include_unicode.ne')
  82. nearley_tool_main(fn, 'main', NEARLEY_PATH)
  83. if __name__ == '__main__':
  84. unittest.main()