Skip to content

Commit dd4bc01

Browse files
authored
Merge pull request #87 from carpie/master
Allow a custom set of characters in a token Signed-off-by: Philippe Ombredanne <[email protected]>
2 parents b13a5a1 + af32227 commit dd4bc01

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

boolean/boolean.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ class BooleanAlgebra(object):
114114
"""
115115

116116
def __init__(self, TRUE_class=None, FALSE_class=None, Symbol_class=None,
117-
NOT_class=None, AND_class=None, OR_class=None):
117+
NOT_class=None, AND_class=None, OR_class=None,
118+
allowed_in_token=('.', ':', '_')):
118119
"""
119120
The types for TRUE, FALSE, NOT, AND, OR and Symbol define the boolean
120121
algebra elements, operations and Symbol variable. They default to the
@@ -158,6 +159,9 @@ def __init__(self, TRUE_class=None, FALSE_class=None, Symbol_class=None,
158159
for name, value in tf_nao.items():
159160
setattr(obj, name, value)
160161

162+
# Set the set of characters allowed in tokens
163+
self.allowed_in_token = allowed_in_token
164+
161165
def definition(self):
162166
"""
163167
Return a tuple of this algebra defined elements and types as:
@@ -461,7 +465,7 @@ def tokenize(self, expr):
461465
position += 1
462466
while position < length:
463467
char = expr[position]
464-
if char.isalnum() or char in ('.', ':', '_'):
468+
if char.isalnum() or char in self.allowed_in_token:
465469
position += 1
466470
tok += char
467471
else:

boolean/test_boolean.py

+11
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,17 @@ def build_symbol(current_dotted):
273273
)
274274
self.assertEqual(expected, expr)
275275

276+
def test_allowing_additional_characters_in_tokens(self):
277+
algebra = BooleanAlgebra(allowed_in_token=('.', '_', '-', '+'))
278+
test_expr = 'l-a AND b+c'
279+
280+
expr = algebra.parse(test_expr)
281+
expected = algebra.AND(
282+
algebra.Symbol('l-a'),
283+
algebra.Symbol('b+c')
284+
)
285+
self.assertEqual(expected, expr)
286+
276287
def test_parse_raise_ParseError1(self):
277288
algebra = BooleanAlgebra()
278289
expr = 'l-a AND none'

0 commit comments

Comments
 (0)