Skip to content

Commit

Permalink
Raise custom exceptions on syntax errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bfontaine committed Aug 4, 2018
1 parent aab186e commit def0785
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
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 @@ -203,7 +204,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'):
t.value = decimal.Decimal(t.value[:-1]) * pow(1, e_value)
Expand Down Expand Up @@ -249,7 +250,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 @@ -251,6 +252,10 @@ def __str__(self):
step3 = dumps(step2)
self.assertEqual(step1, step3)

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

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

0 comments on commit def0785

Please sign in to comment.