Skip to content

Commit ab3b255

Browse files
authored
Merge pull request #83 from Dinh-Hung-Tu/82
Issue #82 solution
2 parents ac9d56b + e604ff3 commit ab3b255

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

rivescript/parser.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,12 @@ def check_syntax(self, cmd, line):
535535
if parts[0] == "begin" and len(parts) > 1:
536536
return "The 'begin' label takes no additional arguments, should be verbatim '> begin'"
537537
elif parts[0] == "topic":
538-
match = re.match(RE.name_syntax, line)
539-
if match:
538+
search = re.search(RE.name_syntax, line)
539+
if search:
540540
return "Topics should be lowercased and contain only numbers and letters"
541541
elif parts[0] == "object":
542-
match = re.match(RE.name_syntax, line)
543-
if match:
542+
search = re.search(RE.obj_syntax, line) # Upper case is allowed
543+
if search:
544544
return "Objects can only contain numbers and letters"
545545
elif cmd == '+' or cmd == '%' or cmd == '@':
546546
# + Trigger, % Previous, @ Redirect
@@ -585,12 +585,12 @@ def check_syntax(self, cmd, line):
585585

586586
# In UTF-8 mode, most symbols are allowed.
587587
if self.utf8:
588-
match = re.match(RE.utf8_trig, line)
589-
if match:
588+
search = re.search(RE.utf8_trig, line)
589+
if search:
590590
return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode."
591591
else:
592-
match = re.match(RE.trig_syntax, line)
593-
if match:
592+
search = re.search(RE.trig_syntax, line)
593+
if search:
594594
return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > ="
595595
elif cmd == '-' or cmd == '^' or cmd == '/':
596596
# - Trigger, ^ Continue, / Comment

rivescript/regexp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class RE(object):
2424
array = re.compile(r'\@(.+?)\b')
2525
def_syntax = re.compile(r'^.+(?:\s+.+|)\s*=\s*.+?$')
2626
name_syntax = re.compile(r'[^a-z0-9_\-\s]')
27+
obj_syntax = re.compile(r'[^A-Za-z0-9_\-\s]')
2728
utf8_trig = re.compile(r'[A-Z\\.]')
2829
trig_syntax = re.compile(r'[^a-z0-9(\|)\[\]*_#@{}<>=\s]')
2930
cond_syntax = re.compile(r'^.+?\s*(?:==|eq|!=|ne|<>|<|<=|>|>=)\s*.+?=>.+?$')

tests/test_format_message.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,32 @@ def test_format_triggers(self):
2828
2929
""")
3030
self.reply("hi there", "hi there")
31-
self.reply("hi here", "hi here")
31+
self.reply("hi here", "hi here")
32+
33+
def test_invalid_character_raise_exception(self):
34+
self.assertRaises(Exception, self.new, """
35+
+ $hello
36+
- hi
37+
""") # This test passes with `match`, which only check at the beginning
38+
self.assertRaises(Exception, self.new, """
39+
+ hello$
40+
- hi
41+
""") # This test does not pass because the beginning is good, no $
42+
self.assertRaises(Exception, self.new, """
43+
> topic Greetings
44+
+ hello
45+
- hi
46+
<topics
47+
""")
48+
self.assertRaises(Exception, self.new, """
49+
> object hash %perl
50+
my ($rs, $args) = @_;
51+
my $method = shift @{$args};
52+
<object
53+
""") # Test for character violation in object, no %
54+
self.new("""
55+
> object hash Perl
56+
my ($rs, $args) = @_;
57+
my $method = shift @{$args};
58+
<object
59+
""") # No exception raised for uppercase character in object

0 commit comments

Comments
 (0)