Skip to content

Commit 1f597e8

Browse files
authored
Merge pull request #103 from Dinh-Hung-Tu/check_syntax
Check syntax
2 parents 858dd6d + 99480ef commit 1f597e8

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

rivescript/parser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,17 @@ def check_syntax(self, cmd, line):
574574
angle += 1
575575
elif char == '>':
576576
angle -= 1
577+
elif char == '|':
578+
if parens == 0 and square == 0: # Pipe outside the alternative and option
579+
return "Pipe | must be within parenthesis brackets or square brackets"
580+
581+
if (angle != 0) and (char in {"(", ")", "[", "]", "{", "}"}):
582+
return "Angle bracket must be closed before closing or opening other type of brackets"
583+
584+
total = parens + square + curly # At each character, not more than 1 bracket opens, except <>
585+
for special_char_count in [parens, square, curly, angle, total]:
586+
if special_char_count not in (0, 1):
587+
return "Unbalanced brackets"
577588

578589
# Any mismatches?
579590
if parens != 0:

tests/test_format.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,29 @@ def test_format_triggers(self):
3333
def test_check_syntax(self):
3434
mismatch_brackets = ["a (b", "a [b", "a {b", "a <b", "a b)", "a b]", "a b}", "a b>"]
3535
empty_pipes = ["[a|b| ]", "[a|b|]", "[a| |c]", "[a||c]", "[ |b|c]", "[|b|c]"]
36+
advanced_brackets = [") a (", "] b [", "> c <", "} d {", "a (b [c) d]", "a (b [c|d] e)"]
37+
angle_brackets = ["(a <b) c>", "<a (b > c)", "[a <b ] c>", "< a [b > c]", "{ a < b } c >", "< a {b > c }"]
38+
pipe_outside = ["a|b", "a|", "|b", "(a|b) | (c|d)", "(a|b)|(c|d)"]
3639

37-
for failing_trigger in mismatch_brackets+empty_pipes:
40+
for failing_trigger in mismatch_brackets + empty_pipes + advanced_brackets + pipe_outside + angle_brackets:
3841
self.assertRaises(Exception, self.new, """
3942
+ {}
4043
- hi
4144
""".format(failing_trigger))
4245

46+
self.new("""
47+
! version = 2.0
48+
49+
// Bot variables
50+
! var name = Tutorial
51+
! var nickname = tut
52+
53+
+ [<bot name>|<nickname>] *
54+
- You called?
55+
""")
56+
self.reply("Tutorial", "You called?")
57+
self.reply("tut", "You called?")
58+
4359
def test_invalid_character_raise_exception(self):
4460
self.assertRaises(Exception, self.new, """
4561
+ $hello

0 commit comments

Comments
 (0)