jamesjuett / lobster

Interactive Program Visualization Tools
8 stars 3 forks source link

Escape sequences in string literals #285

Open jamesjuett opened 3 years ago

jamesjuett commented 3 years ago

This doesn't seem to print correctly:

int main() {
  cout << "1\n0\n1\n" << endl;
}
iteemhe commented 2 years ago

This issue is caused by StringLiteralASTNode in createFromAST in StringLiteralExpression. The string literal returned from AST is unescaped. For example "1\n2\n3" is unescaped to "1\\n2\\n3\\n".

Also, there is an issue in util.escapeString, this function will only replace the first occurrence with the corresponding sequence. The correct method is to use replaceAll() method, but it requires ES 2021. So I use a nested loop to fix this.

export function escapeString(text: string) {
    for (var i = 0; i < escapes.length; ++i) {
        // text = text.replace(escapes[i], escaped[i]);

        //! requires ES 2021
        // text = text.replaceAll(escapes[i], escaped[i]);

        while (text.includes(escapes[i])) {
            text = text.replace(escapes[i], escaped[i]);
        }
    }
    return text;
}

And for createFromAST method in StringLiteralExpression, we only need to call escapeString for ast.value.

    public static createFromAST(
        ast: StringLiteralASTNode,
        context: ExpressionContext
    ) {
        return new StringLiteralExpression(
            context,
            ast,
            escapeString(ast.value)
        );
    }

This issue can be addressed by above fix. But it will cause a side effect, the string on the right-hand side window is also replaced.

Screen Shot 2022-02-21 at 10 01 29

iteemhe commented 2 years ago

I think a better solution is to call escapeString on all strings printed in console, but I haven't fully understood the structure of lobster. Maybe you can find a better way to fix this without causing side effect.

Frankly, I personally think this side effect is very useful in some way.

jamesjuett commented 2 years ago

I'm not able to reproduce the case where the "1 2 3" shows up on different lines in the code simulation. Unless, is that what happens with the proposed addition?

iteemhe commented 2 years ago

I'm not able to reproduce the case where the "1 2 3" shows up on different lines in the code simulation. Unless, is that what happens with the proposed addition?

Yes, that happens with proposed addition.

jamesjuett commented 2 years ago

For newlines, I'm not sure whether I like the way it shows up as a multiline or not. But for other things like \0 it would be important to show the escaped version (rather than actually putting a null character to the html). Probably the best fix would be to escape when printed to the console.