This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

57 行
1.4 KiB

  1. from __future__ import absolute_import
  2. import sys
  3. from unittest import TestCase, main
  4. from lark import Lark, Token
  5. from lark.load_grammar import GrammarLoader, GrammarError
  6. class TestGrammar(TestCase):
  7. def setUp(self):
  8. pass
  9. def test_errors(self):
  10. for msg, examples in GrammarLoader.ERRORS:
  11. for example in examples:
  12. try:
  13. p = Lark(example)
  14. except GrammarError as e:
  15. assert msg in str(e)
  16. else:
  17. assert False, "example did not raise an error"
  18. def test_override_rule(self):
  19. # Overrides the 'sep' template in existing grammar to add an optional terminating delimiter
  20. # Thus extending it beyond its original capacity
  21. p = Lark("""
  22. %import .test_templates_import (start, sep)
  23. %override sep{item, delim}: item (delim item)* delim?
  24. %ignore " "
  25. """, source_path=__file__)
  26. a = p.parse('[1, 2, 3]')
  27. b = p.parse('[1, 2, 3, ]')
  28. assert a == b
  29. def test_override_terminal(self):
  30. p = Lark("""
  31. %import .grammars.ab (startab, A, B)
  32. %override A: "C"
  33. %override B: "D"
  34. """, start='startab', source_path=__file__)
  35. a = p.parse('CD')
  36. self.assertEqual(a.children[0].children, [Token('A', 'C'), Token('B', 'D')])
  37. if __name__ == '__main__':
  38. main()