This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

142 rader
3.7 KiB

  1. from __future__ import absolute_import
  2. import sys
  3. from unittest import TestCase, main
  4. from lark.tree import Tree
  5. from lark.tools import standalone
  6. try:
  7. from StringIO import StringIO
  8. except ImportError:
  9. from io import StringIO
  10. class TestStandalone(TestCase):
  11. def setUp(self):
  12. pass
  13. def _create_standalone(self, grammar):
  14. code_buf = StringIO()
  15. temp = sys.stdout
  16. sys.stdout = code_buf
  17. standalone.main(StringIO(grammar), 'start')
  18. sys.stdout = temp
  19. code = code_buf.getvalue()
  20. context = {}
  21. exec(code, context)
  22. return context
  23. def test_simple(self):
  24. grammar = """
  25. start: NUMBER WORD
  26. %import common.NUMBER
  27. %import common.WORD
  28. %import common.WS
  29. %ignore WS
  30. """
  31. context = self._create_standalone(grammar)
  32. _Lark = context['Lark_StandAlone']
  33. l = _Lark()
  34. x = l.parse('12 elephants')
  35. self.assertEqual(x.children, ['12', 'elephants'])
  36. x = l.parse('16 candles')
  37. self.assertEqual(x.children, ['16', 'candles'])
  38. self.assertRaises(context['UnexpectedToken'], l.parse, 'twelve monkeys')
  39. self.assertRaises(context['UnexpectedToken'], l.parse, 'twelve')
  40. self.assertRaises(context['UnexpectedCharacters'], l.parse, '$ talks')
  41. def test_contextual(self):
  42. grammar = """
  43. start: a b
  44. a: "A" "B"
  45. b: "AB"
  46. """
  47. context = self._create_standalone(grammar)
  48. _Lark = context['Lark_StandAlone']
  49. l = _Lark()
  50. x = l.parse('ABAB')
  51. class T(context['Transformer']):
  52. def a(self, items):
  53. return 'a'
  54. def b(self, items):
  55. return 'b'
  56. start = list
  57. x = T().transform(x)
  58. self.assertEqual(x, ['a', 'b'])
  59. l2 = _Lark(transformer=T())
  60. x = l2.parse('ABAB')
  61. self.assertEqual(x, ['a', 'b'])
  62. def test_postlex(self):
  63. from lark.indenter import Indenter
  64. class MyIndenter(Indenter):
  65. NL_type = '_NEWLINE'
  66. OPEN_PAREN_types = ['LPAR', 'LSQB', 'LBRACE']
  67. CLOSE_PAREN_types = ['RPAR', 'RSQB', 'RBRACE']
  68. INDENT_type = '_INDENT'
  69. DEDENT_type = '_DEDENT'
  70. tab_len = 8
  71. grammar = r"""
  72. start: "(" ")" _NEWLINE
  73. _NEWLINE: /\n/
  74. """
  75. context = self._create_standalone(grammar)
  76. _Lark = context['Lark_StandAlone']
  77. l = _Lark(postlex=MyIndenter())
  78. x = l.parse('()\n')
  79. self.assertEqual(x, Tree('start', []))
  80. l = _Lark(postlex=MyIndenter())
  81. x = l.parse('(\n)\n')
  82. self.assertEqual(x, Tree('start', []))
  83. def test_transformer(self):
  84. grammar = r"""
  85. start: some_rule "(" SOME_TERMINAL ")"
  86. some_rule: SOME_TERMINAL
  87. SOME_TERMINAL: /[A-Za-z_][A-Za-z0-9_]*/
  88. """
  89. context = self._create_standalone(grammar)
  90. _Lark = context["Lark_StandAlone"]
  91. _Token = context["Token"]
  92. _Tree = context["Tree"]
  93. class MyTransformer(context["Transformer"]):
  94. def SOME_TERMINAL(self, token):
  95. return _Token("SOME_TERMINAL", "token is transformed")
  96. def some_rule(self, children):
  97. return _Tree("rule_is_transformed", [])
  98. parser = _Lark(transformer=MyTransformer())
  99. self.assertEqual(
  100. parser.parse("FOO(BAR)"),
  101. _Tree("start", [
  102. _Tree("rule_is_transformed", []),
  103. _Token("SOME_TERMINAL", "token is transformed")
  104. ])
  105. )
  106. if __name__ == '__main__':
  107. main()