Skip to content

Commit

Permalink
[LANGUAGE] Allows guillemets for quoting (hedyorg#4186)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felienne authored Mar 29, 2023
1 parent 8f47ec8 commit 934f355
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 3 deletions.
2 changes: 1 addition & 1 deletion grammars/level4-Additions.lark
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ error_text_no_print: quoted_text
// anything can be parsed except for a newline, a space and a list separator
textwithoutspaces: /([^\n،,,、 ]+)/ -> text

quoted_text: (/'((?:[^\\']|\\.)*)'/ | /"((?:[^\\"]|\\.)*)"/ | /((?:[^\\]|\\.)*)/ | /((?:[^\\]|\\.)*)/) -> text //text can be between single or double quotes, but quotes may be escaped with \
quoted_text: (/'((?:[^\\']|\\.)*)'/ | /"((?:[^\\"]|\\.)*)"/ | /((?:[^\\]|\\.)*)/ | /((?:[^\\]|\\.)*)/ | /«((?:[^\\»]|\\.)*)»/ ) -> text //text can be between single or double quotes, but quotes may be escaped with \


4 changes: 2 additions & 2 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,8 @@ def is_var_candidate(arg) -> bool:
# static methods
@staticmethod
def is_quoted(s):
opening_quotes = ['‘', "'", '"', "“"]
closing_quotes = ['’', "'", '"', "”"]
opening_quotes = ['‘', "'", '"', "“", "«"]
closing_quotes = ['’', "'", '"', "”", "»"]
return len(s) > 1 and (s[0] in opening_quotes and s[-1] in closing_quotes)

@staticmethod
Expand Down
9 changes: 9 additions & 0 deletions highlighting/rules_automaton.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ def rule_level4():
'regex': "\'[^\']*\'",
'token': 'constant.character',
'unicode': True
}, {
'regex': "«[^»]*»",
'token': 'constant.character',
'unicode': True
}, {
'regex': '\"[^\"]*$',
'token': 'text',
Expand All @@ -365,6 +369,11 @@ def rule_level4():
'token': 'text',
'next': 'start',
'unicode': True
}, {
'regex': "«[^»]*$",
'token': 'text',
'next': 'start',
'unicode': True
}],
"valueSimple": [{
'regex': START_WORD + get_translated_keyword("at") + SPACE + get_translated_keyword("random"),
Expand Down
2 changes: 2 additions & 0 deletions highlighting/rules_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def rule_all(level):
# complete
list_rules.append({'regex': '\"[^\"]*\"', 'token': 'constant.character', 'next': 'start', 'unicode': True})
list_rules.append({'regex': "\'[^\']*\'", 'token': 'constant.character', 'next': 'start', 'unicode': True})
list_rules.append({'regex': "«[^»]*»", 'token': 'constant.character', 'next': 'start', 'unicode': True})

# incomplete
list_rules.append({'regex': '\"[^\"]*$', 'token': 'text', 'next': 'start', 'unicode': True})
list_rules.append({'regex': "\'[^\']*$", 'token': 'text', 'next': 'start', 'unicode': True})
list_rules.append({'regex': "«[^»]*$", 'token': 'text', 'next': 'start', 'unicode': True})

# Rule for blanks marks :
list_rules.append({'regex': '_\\?_', 'token': 'invalid', 'next': 'start', 'unicode': True})
Expand Down
88 changes: 88 additions & 0 deletions tests/test_highlighting/test_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,50 @@ def test_print2(self, level):
"KKKKK SSSSSSSSSSSSSS",
level=level, lang='en')

@parameterized.expand([
("level4"),
("level5"),
("level6"),
("level7"),
("level8"),
("level9"),
("level10"),
("level11"),
("level12"),
("level13"),
("level14"),
("level15"),
("level16"),
("level17"),
])
def test_print3(self, level):
self.assert_highlighted_chr(
'print "hello world!"',
"KKKKK SSSSSSSSSSSSSS",
level=level, lang='en')

@parameterized.expand([
("level4"),
("level5"),
("level6"),
("level7"),
("level8"),
("level9"),
("level10"),
("level11"),
("level12"),
("level13"),
("level14"),
("level15"),
("level16"),
("level17"),
])
def test_print4(self, level):
self.assert_highlighted_chr(
"print «hello world!»",
"KKKKK SSSSSSSSSSSSSS",
level=level, lang='en')

@parameterized.expand([
("level4"),
("level5"),
Expand Down Expand Up @@ -82,6 +126,50 @@ def test_print_incomplete2(self, level):
"KKKKK TTTTTTSTTTTTTSTTTSTTSTTTTTT",
level=level, lang='en')

@parameterized.expand([
("level4"),
("level5"),
("level6"),
("level7"),
("level8"),
("level9"),
("level10"),
("level11"),
("level12"),
("level13"),
("level14"),
("level15"),
("level16"),
("level17"),
])
def test_print_incomplete3(self, level):
self.assert_highlighted_chr(
'print "hello world! var at random',
"KKKKK TTTTTTSTTTTTTSTTTSTTSTTTTTT",
level=level, lang='en')

@parameterized.expand([
("level4"),
("level5"),
("level6"),
("level7"),
("level8"),
("level9"),
("level10"),
("level11"),
("level12"),
("level13"),
("level14"),
("level15"),
("level16"),
("level17"),
])
def test_print_incomplete4(self, level):
self.assert_highlighted_chr(
"print «hello world! var at random",
"KKKKK TTTTTTSTTTTTTSTTTSTTSTTTTTT",
level=level, lang='en')

@parameterized.expand([
("level3"),
("level4"),
Expand Down
9 changes: 9 additions & 0 deletions tests/test_level/test_level_04.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def test_print_single_quoted_text(self):
max_level=11,
expected=expected)

def test_print_french_quoted_text(self):
code = "print «bonjour tous le monde!»"
expected = "print(f'bonjour tous le monde!')"

self.multi_level_tester(
code=code,
max_level=11,
expected=expected)

def test_print_chinese_quoted_text(self):
code = "print “逃离鬼屋!”"
expected = "print(f'逃离鬼屋!')"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_level/test_level_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ def test_print_chinese_quoted_text(self):
max_level=17,
expected=expected)

def test_print_french_quoted_text(self):
code = "print «bonjour tous le monde!»"
expected = "print(f'''bonjour tous le monde!''')"

self.multi_level_tester(
code=code,
max_level=17,
expected=expected)

def test_print_chinese_double_quoted_text(self):
code = "print ‘逃离鬼屋!’"
expected = "print(f'''逃离鬼屋!''')"
Expand Down

0 comments on commit 934f355

Please sign in to comment.