Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise custom exceptions on syntax errors #46

Merged
merged 1 commit into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions edn_format/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from .edn_parse import parse as loads
from .edn_parse import add_tag, remove_tag, TaggedElement
from .edn_dump import dump as dumps
from .exceptions import EDNDecodeError
from .immutable_dict import ImmutableDict

__all__ = (
'EDNDecodeError',
'ImmutableDict',
'Keyword',
'Symbol',
Expand Down
5 changes: 3 additions & 2 deletions edn_format/edn_lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import ply.lex

from .exceptions import EDNDecodeError
from .immutable_dict import ImmutableDict


Expand Down Expand Up @@ -204,7 +205,7 @@ def t_FLOAT(t):
if 'e' in t.value or 'E' in t.value:
matches = re.search('[eE]([+-]?\d+)M?$', t.value)
if matches is None:
raise SyntaxError('Invalid float : {}'.format(t.value))
raise EDNDecodeError('Invalid float : {}'.format(t.value))
e_value = int(matches.group(1))
if t.value.endswith('M'):
ctx = decimal.getcontext()
Expand Down Expand Up @@ -251,7 +252,7 @@ def t_SYMBOL(t):


def t_error(t):
raise SyntaxError(
raise EDNDecodeError(
"Illegal character '{c}' with lexpos {p} in the area of ...{a}...".format(
c=t.value[0], p=t.lexpos, a=t.value[0:100]))

Expand Down
7 changes: 4 additions & 3 deletions edn_format/edn_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pyrfc3339

from .edn_lex import tokens, lex
from .exceptions import EDNDecodeError
from .immutable_dict import ImmutableDict


Expand Down Expand Up @@ -93,7 +94,7 @@ def p_map(p):
"""map : MAP_START expressions MAP_OR_SET_END"""
terms = p[2]
if len(terms) % 2 != 0:
raise SyntaxError('Even number of terms required for map')
raise EDNDecodeError('Even number of terms required for map')
# partition terms in pairs
p[0] = ImmutableDict(dict([terms[i:i + 2] for i in range(0, len(terms), 2)]))

Expand Down Expand Up @@ -144,9 +145,9 @@ def p_expression_tagged_element(p):

def p_error(p):
if p is None:
raise SyntaxError('EOF Reached')
raise EDNDecodeError('EOF Reached')
else:
raise SyntaxError(p)
raise EDNDecodeError(p)


def parse(text, input_encoding='utf-8'):
Expand Down
6 changes: 6 additions & 0 deletions edn_format/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: UTF-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals


class EDNDecodeError(ValueError):
pass
7 changes: 6 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import pytz

from edn_format import edn_lex, edn_parse, \
loads, dumps, Keyword, Symbol, TaggedElement, ImmutableDict, add_tag
loads, dumps, Keyword, Symbol, TaggedElement, ImmutableDict, add_tag, \
EDNDecodeError


class ConsoleTest(unittest.TestCase):
Expand Down Expand Up @@ -259,6 +260,10 @@ def test_chars(self):
edn_data = "\\{}".format(ch)
self.assertEqual(ch, loads(edn_data), edn_data)

def test_exceptions(self):
with self.assertRaises(EDNDecodeError):
loads("{")

def test_keyword_keys(self):
unchanged = (
None,
Expand Down