Skip to content

Commit

Permalink
PEP8 compliance and simple test of PegJsVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
obenjiro committed Jun 25, 2016
1 parent 13a46a6 commit 75fbd86
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 105 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js,py}]
charset = utf-8

# 4 space indentation
[*.py]
indent_style = space
indent_size = 4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,6 @@ ENV/

# Rope project settings
.ropeproject

# IDE
.idea/
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Python version of SimplePEG
--------
---------------------------
.. image:: https://travis-ci.org/SimplePEG/Python.svg?branch=master
:target: https://travis-ci.org/SimplePEG/Python
:target: https://travis-ci.org/SimplePEG/Python
.. image:: https://coveralls.io/repos/github/SimplePEG/Python/badge.svg?branch=master
:target: https://coveralls.io/github/SimplePEG/Python?branch=master
:target: https://coveralls.io/github/SimplePEG/Python?branch=master

To use, simply do::

Expand All @@ -18,4 +18,4 @@ or::
>>> from simplepeg import SPEG
>>> parser = SPEG()
>>> ast = parser.parse('GRAMMAR test b -> "a";', 'a')
>>> print ast.to_json()
>>> print ast.to_json()
46 changes: 25 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
from setuptools import setup


def readme():
with open('README.rst') as f:
return f.read()

with open('LICENSE') as f:
license = f.read()

def license_text():
with open('LICENSE') as f:
return f.read()


setup(name='simplepeg',
version='1.0.2',
description='Python version of SimplePEG',
long_description=readme(),
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Topic :: Text Processing :: Linguistic',
],
url='https://github.com/SimplePEG/Python',
author='Oleksii Okhrymenko',
author_email='[email protected]',
keywords='peg parser grammar',
license=license,
test_suite='nose.collector',
tests_require=['nose'],
packages=['simplepeg'],
include_package_data=True,
zip_safe=False)
version='1.0.3',
description='Python version of SimplePEG',
long_description=readme(),
classifiers=[
'Development Status :: 5 - Production/Stable',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Topic :: Text Processing :: Linguistic',
],
url='https://github.com/SimplePEG/Python',
author='Oleksii Okhrymenko',
author_email='[email protected]',
keywords='peg parser grammar',
license=license_text(),
test_suite='nose.collector',
tests_require=['nose'],
packages=['simplepeg'],
include_package_data=True,
zip_safe=False)
2 changes: 1 addition & 1 deletion simplepeg/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .speg import SPEG
from .speg import SPEG
56 changes: 37 additions & 19 deletions simplepeg/rd_parser.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
"""Recursince decend parser"""
"""Recursive decent parser"""
# pylint: disable=too-few-public-methods

import json
import re


class State(object):
"""Current parser state"""
text = ""
position = 0
rules = []
lastExpectations = []
last_expectations = []

def __init__(self, **kwargs):
self.__dict__.update(kwargs)

def to_json(self):
"""returns json string"""
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=2)


class Node(object):
"""Node of AST"""
match = ""
children = None
action = None

def __init__(self, **kwargs):
self.__dict__.update(kwargs)

Expand All @@ -32,26 +36,28 @@ def to_json(self):

class Expectation(object):
"""Expectation object"""

def __init__(self, **kwargs):
self.__dict__.update(kwargs)

def to_json(self):
"""returns json string"""
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=False, indent=2)

def getLastError(state):
if len(state.lastExpectations) < 1:

def get_last_error(state):
if len(state.last_expectations) < 1:
return False
lines = state.text.split('\n')
last_exp_position = max([exp.position for exp in state.lastExpectations])
last_exp_position = max([exp.position for exp in state.last_expectations])
last_position = 0
line_of_error = ''
error_line_number = None
position_of_error = 0
i = 0
while i < len(lines):
line_lenght = len(lines[i]) + 1
if last_exp_position >= last_position and last_exp_position < last_position + line_lenght:
if last_position <= last_exp_position < last_position + line_lenght:
line_of_error = lines[i]
position_of_error = last_exp_position - last_position
error_line_number = i + 1
Expand All @@ -64,15 +70,16 @@ def getLastError(state):
if last_exp_position < len(state.text):
unexpected_char = state.text[last_exp_position]
unexpected = 'Unexpected "' + unexpected_char + '"'
expected_rules = [exp.rule for exp in state.lastExpectations]
expected_rules = [exp.rule for exp in state.last_expectations]
expected = ' expected (' + ' or '.join(expected_rules) + ')'
pointer = ('-'*(position_of_error + 2 + error_ln_length)) + '^'
extra = line_of_error + '\n' + pointer
return unexpected + expected + '\n' + str_error_ln + ': ' + extra


def string(rule):
def _(state):
state.lastExpectations = []
state.last_expectations = []
if state.text[state.position:state.position+len(rule)] == rule:
start_position = state.position
state.position += len(rule)
Expand All @@ -83,17 +90,18 @@ def _(state):
end_position=state.position
)
else:
state.lastExpectations = [Expectation(
state.last_expectations = [Expectation(
type='string',
rule=rule,
position=state.position
)]
return False
return _


def regex_char(rule):
def _(state):
state.lastExpectations = []
state.last_expectations = []
match = re.match(rule, state.text[state.position:])
if match and match.start() == 0:
start_position = state.position
Expand All @@ -105,14 +113,15 @@ def _(state):
end_position=state.position
)
else:
state.lastExpectations = [Expectation(
state.last_expectations = [Expectation(
type='regex_char',
rule=rule,
position=state.position
)]
return False
return _


def sequence(parsers):
def _(state):
asts = []
Expand All @@ -135,6 +144,7 @@ def _(state):
)
return _


def ordered_choice(parsers):
def _(state):
expectations = []
Expand All @@ -154,12 +164,13 @@ def _(state):
else:
state.text = initial_text
state.position = initial_position
expectations = expectations + state.lastExpectations
expectations = expectations + state.last_expectations
i += 1
state.lastExpectations = expectations
state.last_expectations = expectations
return False
return _


def zero_or_more(parser):
def _(state):
asts = []
Expand All @@ -172,7 +183,7 @@ def _(state):
asts.append(ast)
else:
state.position = state_position
state.lastExpectations = []
state.last_expectations = []
match = ''.join([(ast.match if ast.match is not None else '') for ast in asts])
return Node(
type='zero_or_more',
Expand All @@ -183,6 +194,7 @@ def _(state):
)
return _


def one_or_more(parser):
def _(state):
asts = []
Expand All @@ -196,7 +208,7 @@ def _(state):
else:
state.position = state_position
if len(asts) > 0:
state.lastExpectations = []
state.last_expectations = []
match = ''.join([(ast.match if ast.match is not None else '') for ast in asts])
return Node(
type='one_or_more',
Expand All @@ -209,6 +221,7 @@ def _(state):
return False
return _


def optional(parser):
def _(state):
start_position = state.position
Expand All @@ -227,6 +240,7 @@ def _(state):
)
return _


def and_predicate(parser):
def _(state):
current_text = state.text
Expand All @@ -246,6 +260,7 @@ def _(state):
return False
return _


def not_predicate(parser):
def _(state):
current_text = state.text
Expand All @@ -254,14 +269,14 @@ def _(state):
if ast:
state.text = current_text
state.position = current_position
state.lastExpectations = [Expectation(
state.last_expectations = [Expectation(
type='not_predicate',
children=[ast],
position=state.position
)]
return False
else:
state.lastExpectations = []
state.last_expectations = []
return Node(
type='not_predicate',
match=None,
Expand All @@ -271,6 +286,7 @@ def _(state):
)
return _


def end_of_file():
def _(state):
if len(state.text) == state.position:
Expand All @@ -282,16 +298,17 @@ def _(state):
end_position=state.position
)
else:
state.lastExpectations = [Expectation(
state.last_expectations = [Expectation(
type='end_of_file',
rule='EOF',
position=state.position
)]
return False
return _


def rec(func):
"""Allows you to do recurrcive currying"""
"""Allows you to do recursive currying"""
def _(*args, **kwargs):
return func()(*args, **kwargs)
return _
Expand All @@ -305,6 +322,7 @@ def _(*args, **kwargs):
return ast
return _


def call_rule_by_name(name):
def _(state):
rule = next((x for x in state.rules if x.name == name), None)
Expand Down
Loading

0 comments on commit 75fbd86

Please sign in to comment.