Skip to content

Commit

Permalink
Merge pull request aichaos#83 from Dinh-Hung-Tu/82
Browse files Browse the repository at this point in the history
Issue aichaos#82 solution
  • Loading branch information
kirsle authored Feb 24, 2017
2 parents ac9d56b + e604ff3 commit ab3b255
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
16 changes: 8 additions & 8 deletions rivescript/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,12 @@ def check_syntax(self, cmd, line):
if parts[0] == "begin" and len(parts) > 1:
return "The 'begin' label takes no additional arguments, should be verbatim '> begin'"
elif parts[0] == "topic":
match = re.match(RE.name_syntax, line)
if match:
search = re.search(RE.name_syntax, line)
if search:
return "Topics should be lowercased and contain only numbers and letters"
elif parts[0] == "object":
match = re.match(RE.name_syntax, line)
if match:
search = re.search(RE.obj_syntax, line) # Upper case is allowed
if search:
return "Objects can only contain numbers and letters"
elif cmd == '+' or cmd == '%' or cmd == '@':
# + Trigger, % Previous, @ Redirect
Expand Down Expand Up @@ -585,12 +585,12 @@ def check_syntax(self, cmd, line):

# In UTF-8 mode, most symbols are allowed.
if self.utf8:
match = re.match(RE.utf8_trig, line)
if match:
search = re.search(RE.utf8_trig, line)
if search:
return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode."
else:
match = re.match(RE.trig_syntax, line)
if match:
search = re.search(RE.trig_syntax, line)
if search:
return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > ="
elif cmd == '-' or cmd == '^' or cmd == '/':
# - Trigger, ^ Continue, / Comment
Expand Down
1 change: 1 addition & 0 deletions rivescript/regexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RE(object):
array = re.compile(r'\@(.+?)\b')
def_syntax = re.compile(r'^.+(?:\s+.+|)\s*=\s*.+?$')
name_syntax = re.compile(r'[^a-z0-9_\-\s]')
obj_syntax = re.compile(r'[^A-Za-z0-9_\-\s]')
utf8_trig = re.compile(r'[A-Z\\.]')
trig_syntax = re.compile(r'[^a-z0-9(\|)\[\]*_#@{}<>=\s]')
cond_syntax = re.compile(r'^.+?\s*(?:==|eq|!=|ne|<>|<|<=|>|>=)\s*.+?=>.+?$')
Expand Down
30 changes: 29 additions & 1 deletion tests/test_format_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,32 @@ def test_format_triggers(self):
""")
self.reply("hi there", "hi there")
self.reply("hi here", "hi here")
self.reply("hi here", "hi here")

def test_invalid_character_raise_exception(self):
self.assertRaises(Exception, self.new, """
+ $hello
- hi
""") # This test passes with `match`, which only check at the beginning
self.assertRaises(Exception, self.new, """
+ hello$
- hi
""") # This test does not pass because the beginning is good, no $
self.assertRaises(Exception, self.new, """
> topic Greetings
+ hello
- hi
<topics
""")
self.assertRaises(Exception, self.new, """
> object hash %perl
my ($rs, $args) = @_;
my $method = shift @{$args};
<object
""") # Test for character violation in object, no %
self.new("""
> object hash Perl
my ($rs, $args) = @_;
my $method = shift @{$args};
<object
""") # No exception raised for uppercase character in object

0 comments on commit ab3b255

Please sign in to comment.