diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5547a06 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tests/test_nearley/nearley"] + path = tests/test_nearley/nearley + url = https://github.com/kasbah/nearley diff --git a/lark/tools/nearley.py b/lark/tools/nearley.py index 735ee6c..1e55b70 100644 --- a/lark/tools/nearley.py +++ b/lark/tools/nearley.py @@ -169,51 +169,6 @@ def create_code_for_nearley_grammar(g, start, builtin_path, folder_path): return ''.join(emit_code) -def test(): - css_example_grammar = """ -# http://www.w3.org/TR/css3-color/#colorunits - - @builtin "whitespace.ne" - @builtin "number.ne" - @builtin "postprocessors.ne" - - csscolor -> "#" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit {% - function(d) { - return { - "r": parseInt(d[1]+d[2], 16), - "g": parseInt(d[3]+d[4], 16), - "b": parseInt(d[5]+d[6], 16), - } - } - %} - | "#" hexdigit hexdigit hexdigit {% - function(d) { - return { - "r": parseInt(d[1]+d[1], 16), - "g": parseInt(d[2]+d[2], 16), - "b": parseInt(d[3]+d[3], 16), - } - } - %} - | "rgb" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")" {% $({"r": 4, "g": 8, "b": 12}) %} - | "hsl" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")" {% $({"h": 4, "s": 8, "l": 12}) %} - | "rgba" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")" {% $({"r": 4, "g": 8, "b": 12, "a": 16}) %} - | "hsla" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")" {% $({"h": 4, "s": 8, "l": 12, "a": 16}) %} - - hexdigit -> [a-fA-F0-9] - colnum -> unsigned_int {% id %} | percentage {% - function(d) {return Math.floor(d[0]*255); } - %} - """ - code = create_code_for_nearley_grammar(css_example_grammar, 'csscolor', '/home/erez/nearley/builtin', './') - d = {} - exec (code, d) - parse = d['parse'] - - print(parse('#a199ff')) - print(parse('rgb(255, 70%, 3)')) - - def main(): if len(sys.argv) < 3: print("Reads Nearley grammar (with js functions) outputs an equivalent lark parser.") @@ -228,4 +183,3 @@ def main(): if __name__ == '__main__': main() - # test() diff --git a/tests/__main__.py b/tests/__main__.py index df1c65e..c9fe2ad 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -5,6 +5,8 @@ import logging from .test_trees import TestTrees +from .test_nearley.test_nearley import TestNearley + # from .test_selectors import TestSelectors # from .test_grammars import TestPythonG, TestConfigG diff --git a/tests/test_nearley/__init__.py b/tests/test_nearley/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_nearley/nearley b/tests/test_nearley/nearley new file mode 160000 index 0000000..0abb6e5 --- /dev/null +++ b/tests/test_nearley/nearley @@ -0,0 +1 @@ +Subproject commit 0abb6e5feef2f4ebf80d5a0891c1ad709011b1c7 diff --git a/tests/test_nearley/test_nearley.py b/tests/test_nearley/test_nearley.py new file mode 100644 index 0000000..dc8ca08 --- /dev/null +++ b/tests/test_nearley/test_nearley.py @@ -0,0 +1,70 @@ +from __future__ import absolute_import + +import unittest +import logging +import os +import sys + +logging.basicConfig(level=logging.INFO) + +from lark.tools.nearley import create_code_for_nearley_grammar + +NEARLEY_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), 'nearley')) +BUILTIN_PATH = os.path.join(NEARLEY_PATH, 'builtin') + +class TestNearley(unittest.TestCase): + def test_css(self): + css_example_grammar = """ + # http://www.w3.org/TR/css3-color/#colorunits + + @builtin "whitespace.ne" + @builtin "number.ne" + @builtin "postprocessors.ne" + + csscolor -> "#" hexdigit hexdigit hexdigit hexdigit hexdigit hexdigit {% + function(d) { + return { + "r": parseInt(d[1]+d[2], 16), + "g": parseInt(d[3]+d[4], 16), + "b": parseInt(d[5]+d[6], 16), + } + } + %} + | "#" hexdigit hexdigit hexdigit {% + function(d) { + return { + "r": parseInt(d[1]+d[1], 16), + "g": parseInt(d[2]+d[2], 16), + "b": parseInt(d[3]+d[3], 16), + } + } + %} + | "rgb" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")" {% $({"r": 4, "g": 8, "b": 12}) %} + | "hsl" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ ")" {% $({"h": 4, "s": 8, "l": 12}) %} + | "rgba" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")" {% $({"r": 4, "g": 8, "b": 12, "a": 16}) %} + | "hsla" _ "(" _ colnum _ "," _ colnum _ "," _ colnum _ "," _ decimal _ ")" {% $({"h": 4, "s": 8, "l": 12, "a": 16}) %} + + hexdigit -> [a-fA-F0-9] + colnum -> unsigned_int {% id %} | percentage {% + function(d) {return Math.floor(d[0]*255); } + %} + """ + + code = create_code_for_nearley_grammar(css_example_grammar, 'csscolor', BUILTIN_PATH, './') + d = {} + exec (code, d) + parse = d['parse'] + + c = parse('#a199ff') + assert c['r'] == 161 + assert c['g'] == 153 + assert c['b'] == 255 + + c = parse('rgb(255, 70%, 3)') + assert c['r'] == 255 + assert c['g'] == 178 + assert c['b'] == 3 + + +if __name__ == '__main__': + unittest.main()