Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪲 Broken boolean translation #5574

Merged
merged 11 commits into from
May 29, 2024
29 changes: 19 additions & 10 deletions hedy_translation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import namedtuple
from lark import Token, Visitor
from lark import Token, Transformer, v_args
from lark.exceptions import VisitError
import hedy
import operator
Expand Down Expand Up @@ -97,7 +97,7 @@ def translate_keywords(input_string, from_lang="en", to_lang="nl", level=1):
program_root = parser.parse(processed_input + "\n").children[0]

translator = Translator(processed_input)
translator.visit(program_root)
translator.transform(program_root)
ordered_rules = reversed(sorted(translator.rules, key=operator.attrgetter("line", "start")))

# checks whether any error production nodes are present in the parse tree
Expand Down Expand Up @@ -161,7 +161,7 @@ def find_command_keywords(
program_root = parser.parse(input_string).children[0]

translator = Translator(input_string)
translator.visit(program_root)
translator.transform(program_root)

return {
k: find_keyword_in_rules(
Expand Down Expand Up @@ -189,12 +189,15 @@ def get_original_keyword(keyword_dict, keyword, line):
return keyword


class Translator(Visitor):
"""The visitor finds tokens that must be translated and stores information about their exact position
@v_args(tree=True)
class Translator(Transformer):
"""The translator finds tokens that must be translated and stores information about their exact position
in the user input string and original value. The information is later used to replace the token in
the original user input with the translated token value."""
the original user input with the translated token value. Please note that it is a transformer
instead of a visitor because we need tokens to be visited too."""

def __init__(self, input_string):
super().__init__()
self.input_string = input_string
self.rules = []

Expand Down Expand Up @@ -277,6 +280,16 @@ def red(self, tree):
def clear(self, tree):
self.add_rule_for_grammar_rule("clear", tree)

def TRUE(self, token):
name = 'True' if token and token[0].isupper() else 'true'
rule = Rule(name, token.line, token.column - 1, token.end_column - 2, token)
self.rules.append(rule)

def FALSE(self, token):
name = 'False' if token and token[0].isupper() else 'false'
rule = Rule(name, token.line, token.column - 1, token.end_column - 2, token)
self.rules.append(rule)

def assign_list(self, tree):
self.add_rule_for_grammar_token("_IS", "is", tree)
commas = self.get_keyword_tokens("_COMMA", tree)
Expand Down Expand Up @@ -355,10 +368,6 @@ def for_loop(self, tree):
self.add_rule_for_grammar_token("_RANGE", "range", tree)
self.add_rule_for_grammar_token("_TO", "to", tree)

def boolean(self, tree):
self.add_rule('TRUE', "true", tree)
self.add_rule('FALSE', "false", tree)

def while_loop(self, tree):
self.add_rule_for_grammar_token("_WHILE", "while", tree)

Expand Down
1 change: 0 additions & 1 deletion tests/test_level/test_level_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def test_color_basic(self):

self.single_level_tester(
code=code,
translate=False,
expected=expected
)

Expand Down
Loading