-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🖊️ Allow -= and > operators to be used independently when merging gra…
…mmars (#5186) Fixes #5176 Merging grammar rules was only possible if the += operator was present, which means that the -= and > operators could not be used independently of +=. With this change, all operators could be used independently of each other. Because the > operator now does not need the context of the += operator, I had to change it to a symbol which does not appear in the lark language (e.g. ->). Please consider whether there is a better candidate than `>>`, for example `>=`? **How to test** - The introduced merging changes are covered with unit tests - All existing tests should pass
- Loading branch information
1 parent
b23f267
commit 831b28c
Showing
10 changed files
with
156 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// only remove repeat error commands | ||
|
||
command:+= repeat | error_repeat_no_command-=repeat | error_repeat_no_command | error_repeat_no_print | error_repeat_no_times | ||
command: -= error_repeat_no_print | error_repeat_no_times |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import unittest | ||
from hedy import merge_rules_operator | ||
|
||
|
||
class TestGrammarMerging(unittest.TestCase): | ||
|
||
def test_rule_merging_without_operators(self): | ||
prev_definition = ' _PRINT (text)?' | ||
new_definition = ' _PRINT (_print_argument)?' | ||
complete_line = f'print {new_definition}' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'print', complete_line) | ||
|
||
self.assertEqual(complete_line, result) | ||
self.assertEqual([], deletable) | ||
|
||
def test_rule_merging_with_greater_than_sign(self): | ||
prev_definition = ' /([^\\n#ـ])([^\\n#]*)/ -> text //comment' | ||
new_definition = ' /([^\\n#]+)/ -> text' | ||
complete_line = 'text: /([^\\n#]+)/ -> text' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'text', complete_line) | ||
|
||
self.assertEqual(complete_line, result) | ||
self.assertEqual([], deletable) | ||
|
||
def test_rule_merging_add(self): | ||
prev_definition = 'print' | ||
new_definition = '+= assign | sleep' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
self.assertEqual('command: print | assign | sleep', result) | ||
self.assertEqual([], deletable) | ||
|
||
def test_rule_merging_add_and_remove(self): | ||
prev_definition = 'print | play | test ' | ||
new_definition = '+= assign | sleep -= play | print' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
self.assertEqual('command: test | assign | sleep', result) | ||
self.assertEqual(['play', 'print'], deletable) | ||
|
||
def test_rule_merging_add_and_last(self): | ||
prev_definition = 'print | play | test ' | ||
new_definition = '+= assign | sleep >> play | print' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
self.assertEqual('command: test | assign | sleep | play | print', result) | ||
self.assertEqual([], deletable) | ||
|
||
def test_rule_merging_add_remove_and_last(self): | ||
prev_definition = ' print | error | echo | test ' | ||
new_definition = '+= assign | sleep -= echo | print >> test | error' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', new_definition) | ||
|
||
self.assertEqual('command: assign | sleep | test | error', result) | ||
self.assertEqual(['echo', 'print'], deletable) | ||
|
||
def test_rule_merging_remove(self): | ||
prev_definition = 'print | test | assign' | ||
new_definition = '-= assign | print' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
self.assertEqual('command: test', result) | ||
self.assertEqual(['assign', 'print'], deletable) | ||
|
||
def test_rule_merging_remove_non_existent_gives_error(self): | ||
prev_definition = 'print | test | assign' | ||
new_definition = '-= prin' | ||
|
||
with self.assertRaises(Exception): | ||
merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
def test_rule_merging_remove_and_last(self): | ||
prev_definition = 'play | print | test | assign' | ||
new_definition = '-= assign | print >> play' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
self.assertEqual('command: test | play', result) | ||
self.assertEqual(['assign', 'print'], deletable) | ||
|
||
def test_rule_merging_last(self): | ||
prev_definition = 'print | test | assign | play' | ||
new_definition = '>> print | play' | ||
|
||
result, deletable = merge_rules_operator(prev_definition, new_definition, 'command', '') | ||
|
||
self.assertEqual('command: test | assign | print | play', result) | ||
self.assertEqual([], deletable) | ||
|
||
def test_rule_merging_last_non_existent_gives_error(self): | ||
prev_definition = 'print | test | assign' | ||
new_definition = '-= prin' | ||
|
||
with self.assertRaises(Exception): | ||
merge_rules_operator(prev_definition, new_definition, 'command', '') |