Project

Profile

Help

Revision 457ab841

Added by Vincent Le Goff over 4 years ago

Now handle semi-colons in SharpScript

View differences:

src/sharp/engine.py
111 111
                argument = "compile(" + argument + ", 'SharpScript', 'exec')"
112 112
            elif argument.startswith("{"):
113 113
                argument = repr(argument[1:-1])
114
                argument = self.replace_semicolons(argument)
114 115
            elif argument[0] in "-+":
115 116
                kwargs[argument[1:]] = True if argument[0] == "+" else False
116 117
                continue
117 118
            else:
118 119
                argument = repr(argument)
120
                argument = self.replace_semicolons(argument)
119 121

  
120 122
            arguments.append(argument)
121 123

  
......
214 216
            i += 1
215 217

  
216 218
        return None
219

  
220
    @staticmethod
221
    def replace_semicolons(text):
222
        """Replace all not-escaped semi-colons."""
223
        i = 0
224
        while i < len(text):
225
            remaining = text[i:]
226
            if remaining.startswith(";;"):
227
                i += 2
228
                continue
229
            elif remaining.startswith(";"):
230
                text = text[:i] + "\n" + text[i + 1:]
231
            i += 1
232

  
233
        return text.replace(";;", ";")
src/tests/sharp/test_syntax.py
101 101
        self.assertEqual(statements, [
102 102
            "compile('print 1\nprint 3', 'SharpScript', 'exec')",
103 103
        ])
104

  
105
    def test_semicolons(self):
106
        """Test the semi-colons."""
107
        # A test with simple text
108
        statements = self.engine.feed("#macro F1 north;south;;east")
109
        self.assertEqual(statements, ["macro('F1', 'north\nsouth;east')"])
110

  
111
        # A test with SharpScript
112
        statements = self.engine.feed("#trigger ok {#play new.wav;#stop}")
113
        self.assertEqual(statements, [
114
                "trigger('ok', '#play new.wav\n#stop')",
115
        ])

Also available in: Unified diff