Skip to content

Commit

Permalink
Fixed es6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen van der Heijden committed Jul 9, 2018
1 parent b8a4896 commit 1283a3f
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 63 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2018.07.09, Version 1.2.2 (Stable)

* Added export to JavaScript using class (es6) syntax.

* Use jsleri Ref.set() method instead of Object.assign.

2018.07.05, Version 1.2.1 (Stable)

* Format readme on PyPi
Expand Down
2 changes: 1 addition & 1 deletion pyleri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
__author__ = 'Jeroen van der Heijden'
__maintainer__ = 'Jeroen van der Heijden'
__email__ = '[email protected]'
__version__ = '1.2.1'
__version__ = '1.2.2'
4 changes: 2 additions & 2 deletions pyleri/choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def _stop_at_first_match(self, root, tree, rule, s, node):

return is_valid, pos

def _run_export_js(self, js_indent, indent, classes):
return self._export_js_elements(js_indent, indent, classes)
def _run_export_js(self, js_indent, indent, classes, cname):
return self._export_js_elements(js_indent, indent, classes, cname)

def _run_export_py(self, py_indent, indent, classes):
new_indent = indent + 1
Expand Down
15 changes: 8 additions & 7 deletions pyleri/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,26 @@ class NamedElement(Element):

__slots__ = ('name',)

def _export_js(self, js_indent, indent, classes):
def _export_js(self, js_indent, indent, classes, cname):
classes.add(self.__class__.__name__.lstrip('_'))
if hasattr(self, 'name') and indent:
return self.name
return self._run_export_js(js_indent, indent or 1, classes)
if hasattr(self, 'name') and indent > 0:
return '{}.{}'.format(cname, self.name) if cname else self.name
indent = 0 if indent < 0 else 1 if indent == 0 else indent
return self._run_export_js(js_indent, indent, classes, cname)

def _export_js_elements(self, js_indent, indent, classes):
def _export_js_elements(self, js_indent, indent, classes, cname):
new_indent = indent + 1
value = ',\n'.join(['{indent}{elem}'.format(
indent=js_indent * new_indent,
elem=elem._export_js(
js_indent,
new_indent, classes)) for elem in self._elements])
new_indent, classes, cname)) for elem in self._elements])
return '{class_name}(\n{value}\n{indent})'.format(
class_name=self.__class__.__name__.lstrip('_'),
value=value,
indent=js_indent * indent)

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'not_implemented'

def _export_py(self, py_indent, indent, classes):
Expand Down
59 changes: 33 additions & 26 deletions pyleri/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Grammar(metaclass=_OrderedClass):
{arguments}
{indent}{indent}) {{
{language}
{refs}
{indent}window.{name} = Grammar(START, '{re_keywords}');
Expand All @@ -181,23 +182,16 @@ class Grammar(metaclass=_OrderedClass):
import {{ {classes} }} from 'jsleri';
class {name} extends Grammar {
class {name} extends Grammar {{
{language}
{indent}constructor() {
{indent}constructor() {{
{indent}{indent}super({name}.START, '{re_keywords}');
{indent}}}
}}
{refs}
{indent}}
}
(function (
{arguments}
{indent}{indent}) {{
{language}
{indent}window.{name} = Grammar(START, '{re_keywords}');
}})(
{constructors}
);
export default {name};
'''.lstrip()

PY_INDENTATION = ' ' * 4
Expand Down Expand Up @@ -349,52 +343,65 @@ def __init__(self):
def export_js(
self,
js_module_name=JS_MODULE_NAME,
js_template=JS_WINDOW_TEMPLATE,
js_template=JS_ES6_IMPORT_EXPORT_TEMPLATE,
js_indent=JS_INDENTATION):
'''Export the grammar to a JavaScript file which can be
used with the js-lrparsing module.'''
used with the js-lrparsing module.
Two templates are available:
Grammar.JS_WINDOW_TEMPLATE
Grammar.JS_ES6_IMPORT_EXPORT_TEMPLATE (default)
'''

language = []
refs = []
classes = {'Grammar'}
indent = 0
cname = self.__class__.__name__ if 'import ' in js_template else None

for name in self._order:
elem = getattr(self, name, None)
if not isinstance(elem, Element):
continue
if not hasattr(elem, '_export_js'):
continue
language.append('{indent}var {name} = {value};'.format(
language.append('{indent}{var} {name} = {value};'.format(
indent=js_indent,
name=name,
value=elem._export_js(js_indent, indent, classes)))
var='static' if cname else 'var',
value=elem._export_js(js_indent, indent, classes, cname)))

for name, ref in self._refs.items():
language.append(
'{indent}Object.assign({name}, {value});'
refs.append(
'{pre}{name}.set({value});'
.format(
indent=js_indent,
pre='{}.'.format(cname) if cname else js_indent,
name=name,
value=ref._element._export_js(
js_indent,
indent,
classes)))
-1 if cname else indent,
classes,
cname)))

if 'Rule' in classes:
classes.remove('Rule')

return js_template.format(
name=self.__class__.__name__,
indent=js_indent,
js_module=js_module_name,
datetime=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
language='\n'.join(language),
refs='\n{}'.format('\n'.join(refs)),
arguments=',\n'.join(map(lambda s:
js_indent * 3 + s, classes)),
re_keywords=self.RE_KEYWORDS.pattern.replace('\\', '\\\\'),
classes=', '.join(classes),
constructors=',\n'.join(
map(lambda s: js_indent + s,
['.'.join(
[
'window',
js_module_name, n]) for n in classes])))
['.'.join([
'window',
js_module_name, n]) for n in classes])))

def export_py(
self,
Expand Down
2 changes: 1 addition & 1 deletion pyleri/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _get_node_result(self, root, tree, rule, s, node):

return is_valid, node.end or node.start

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Keyword(\'{}\'{})'.format(
self._keyword,
', true' if self._ign_case else '')
Expand Down
6 changes: 3 additions & 3 deletions pyleri/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def _get_node_result(self, root, tree, rule, s, node):

return is_valid, pos

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'List({}, {}, {}, {}, {})'.format(
self._element._export_js(js_indent, indent, classes),
self._delimiter._export_js(js_indent, indent, classes),
self._element._export_js(js_indent, indent, classes, cname),
self._delimiter._export_js(js_indent, indent, classes, cname),
self._min,
self._max or 'undefined',
'true' if self._opt else 'false')
Expand Down
4 changes: 2 additions & 2 deletions pyleri/optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def _get_node_result(self, root, tree, rule, _s, node):

return True, node.end or node.start

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Optional({})'.format(
self._element._export_js(js_indent, indent, classes))
self._element._export_js(js_indent, indent, classes, cname))

def _run_export_py(self, py_indent, indent, classes):
return 'Optional({})'.format(
Expand Down
4 changes: 2 additions & 2 deletions pyleri/prio.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def _get_node_result(self, root, tree, rule, s, node):

return rule._tested[node.start]

def _run_export_js(self, js_indent, indent, classes):
return self._export_js_elements(js_indent, indent, classes)
def _run_export_js(self, js_indent, indent, classes, cname):
return self._export_js_elements(js_indent, indent, classes, cname)

def _run_export_py(self, py_indent, indent, classes):
return self._export_py_elements(py_indent, indent, classes)
Expand Down
2 changes: 1 addition & 1 deletion pyleri/ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def element(self, element):
self._element = self._validate_element(element)
self._get_node_result = self._element._get_node_result

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Ref({})'.format(self._element.__class__.__name__)

def _run_export_py(self, py_indent, indent, classes):
Expand Down
2 changes: 1 addition & 1 deletion pyleri/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _get_node_result(self, root, tree, rule, s, node):

return is_valid, node.end or node.start

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Regex(\'{}\'{})'.format(
self._compiled.pattern.replace('\\', '\\\\').replace('\'', '\\\''),
', true' if self._compiled.flags & re.IGNORECASE else '')
Expand Down
4 changes: 2 additions & 2 deletions pyleri/repeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def _get_node_result(self, root, tree, rule, s, node):

return is_valid, pos

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Repeat({}, {}, {})'.format(
self._element._export_js(js_indent, indent, classes),
self._element._export_js(js_indent, indent, classes, cname),
self._min,
self._max or 'undefined')

Expand Down
4 changes: 2 additions & 2 deletions pyleri/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def _get_node_result(self, root, tree, rule, _s, node):
root._append_tree(tree, node, pos)
return is_valid, pos

def _run_export_js(self, js_indent, indent, classes):
return self._element._export_js(js_indent, indent, classes)
def _run_export_js(self, js_indent, indent, classes, cname):
return self._element._export_js(js_indent, indent, classes, cname)

def _run_export_py(self, py_indent, indent, classes):
return self._element._export_py(py_indent, indent, classes)
Expand Down
4 changes: 2 additions & 2 deletions pyleri/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def _get_node_result(self, root, tree, rule, _s, node):

return is_valid, pos

def _run_export_js(self, js_indent, indent, classes):
return self._export_js_elements(js_indent, indent, classes)
def _run_export_js(self, js_indent, indent, classes, cname):
return self._export_js_elements(js_indent, indent, classes, cname)

def _run_export_py(self, py_indent, indent, classes):
return self._export_py_elements(py_indent, indent, classes)
Expand Down
2 changes: 1 addition & 1 deletion pyleri/this.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _get_node_result(self, root, tree, rule, _s, node):

return rule._tested[node.start]

def _export_js(self, js_indent, indent, classes):
def _export_js(self, js_indent, indent, classes, cname):
classes.add('THIS')
return 'THIS'

Expand Down
2 changes: 1 addition & 1 deletion pyleri/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _get_node_result(self, root, tree, rule, s, node):

return is_valid, node.end or node.start

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Token(\'{}\')'.format(self._token)

def _run_export_py(self, py_indent, indent, classes):
Expand Down
2 changes: 1 addition & 1 deletion pyleri/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _get_node_result(self, root, tree, rule, s, node):

return False, node.start

def _run_export_js(self, js_indent, indent, classes):
def _run_export_js(self, js_indent, indent, classes, cname):
return 'Tokens(\'{}\')'.format(' '.join(self._tokens))

def _run_export_py(self, py_indent, indent, classes):
Expand Down
7 changes: 2 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
Upload to PyPI, Thx to: http://peterdowns.com/posts/first-time-with-pypi.html
python3 setup.py sdist
twine upload --repository pypitest dist/pyleri-1.2.1c.tar.gz
twine upload --repository pypitest dist/pyleri-x.x.x.tar.gz
python3 setup.py register -r pypitest
python3 setup.py sdist upload -r pypitest
twine upload --repository pypi dist/pyleri-x.x.x.tar.gz
python3 setup.py register -r pypi
python3 setup.py sdist upload -r pypi
"""
from setuptools import setup
from pyleri import __version__ as version
Expand Down
7 changes: 4 additions & 3 deletions test/test_pyleri.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import gc
import sys
sys.path.append('..')
sys.path.insert(0, '..')

from pyleri import (
Keyword,
Expand Down Expand Up @@ -195,5 +195,6 @@ def tearDown(self):
# print(my_grammar.parse('hi "Iris"').is_valid) # => True
# print(my_grammar.parse('bye "Iris"').is_valid) # => False

# # my_grammar = JsonGrammar()
# print(my_grammar.export_go())
# my_grammar = JsonGrammar()
# print(my_grammar.export_js(js_template=Grammar.JS_WINDOW_TEMPLATE))
# print(my_grammar.export_js())

0 comments on commit 1283a3f

Please sign in to comment.