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.

139 lines
3.7 KiB

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