Browse Source

Added tests for standalone generator

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.6
Erez Shinan 6 years ago
parent
commit
eba4a604cb
4 changed files with 60 additions and 7 deletions
  1. +9
    -6
      lark/tools/standalone.py
  2. +1
    -0
      tests/__main__.py
  3. +50
    -0
      tests/test_tools.py
  4. +0
    -1
      tests/test_trees.py

+ 9
- 6
lark/tools/standalone.py View File

@@ -167,9 +167,8 @@ class TreeBuilderAtoms:
print('}')
print('parse_tree_builder = ParseTreeBuilder(RULES.values(), Tree)')

def main(fn, start):
with codecs.open(fn, encoding='utf8') as f:
lark_inst = Lark(f, parser="lalr", start=start)
def main(fobj, start):
lark_inst = Lark(fobj, parser="lalr", start=start)

lexer_atoms = LexerAtoms(lark_inst.parser.lexer)
parser_atoms = ParserAtoms(lark_inst.parser.parser)
@@ -178,9 +177,12 @@ def main(fn, start):
print('# The file was automatically generated by Lark v%s' % lark.__version__)

for pyfile in EXTRACT_STANDALONE_FILES:
print (extract_sections(open(os.path.join(__larkdir__, pyfile)))['standalone'])
with open(os.path.join(__larkdir__, pyfile)) as f:
print (extract_sections(f)['standalone'])

with open(os.path.join(__larkdir__, 'grammar.py')) as grammar_py:
print(grammar_py.read())

print(open(os.path.join(__larkdir__, 'grammar.py')).read())
print('Shift = 0')
print('Reduce = 1')
lexer_atoms.print_python()
@@ -200,4 +202,5 @@ if __name__ == '__main__':
else:
assert False, sys.argv

main(fn, start)
with codecs.open(fn, encoding='utf8') as f:
main(f, start)

+ 1
- 0
tests/__main__.py View File

@@ -4,6 +4,7 @@ import unittest
import logging

from .test_trees import TestTrees
from .test_tools import TestStandalone

try:
from .test_nearley.test_nearley import TestNearley


+ 50
- 0
tests/test_tools.py View File

@@ -0,0 +1,50 @@
from __future__ import absolute_import

import sys
import unittest
from unittest import TestCase

from lark.tree import Tree

from lark.tools import standalone

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

class TestStandalone(TestCase):
def setUp(self):
pass

def test_simple(self):
grammar = """
start: NUMBER WORD

%import common.NUMBER
%import common.WORD
%import common.WS
%ignore WS

"""

code_buf = StringIO()
temp = sys.stdout
sys.stdout = code_buf
standalone.main(StringIO(grammar), 'start')
sys.stdout = temp
code = code_buf.getvalue()

context = {}
exec(code, context)
_Lark = context['Lark_StandAlone']

l = _Lark()
x = l.parse('12 elephants')
self.assertEqual(x.children, ['12', 'elephants'])


if __name__ == '__main__':
unittest.main()



+ 0
- 1
tests/test_trees.py View File

@@ -2,7 +2,6 @@ from __future__ import absolute_import

import unittest
from unittest import TestCase
import logging
import copy
import pickle



Loading…
Cancel
Save