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

Impossible character escapes in strings #290

Closed
ISSOtm opened this issue Jun 12, 2018 · 10 comments
Closed

Impossible character escapes in strings #290

ISSOtm opened this issue Jun 12, 2018 · 10 comments
Labels
docs This affects the documentation (web-specific issues go to rgbds-www) enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM

Comments

@ISSOtm
Copy link
Member

ISSOtm commented Jun 12, 2018

I have a simple macro:

dstr: MACRO
    REPT _NARG
        db \1
        shift
    ENDR
    db 0
ENDM

And I pass it this string:

    dstr "Line 1\nLine 2"

And I get this error:

ERROR: engine.asm(7) -> engine/title.asm(1357) -> dstr(3) -> dstr(1):
    Unterminated string

I then try to pass this string instead:

    dstr "Line1\\\nLine2"

And I get this error:

ERROR: engine.asm(7) -> engine/title.asm(1357) -> dstr(3) -> dstr(1):
    Illegal character escape '
'

I would expect either of these variants to work, since they're right there in the manual!

@ISSOtm
Copy link
Member Author

ISSOtm commented Jun 12, 2018

Well, I figured out that the correct answer was \\n. Which both isn't intuitive and not really pointed to in the manual. Plus the Unterminated string error doesn't give much insight as to what to do - I only knew what to do because I had encountered it using commas.

@AntonioND
Copy link
Member

Well, it makes sense. The \\ is converted to \ after the first pass. If you do the same to the n, \n will be converted to a newline.

Where is the problem in the manual?

I agree that the error messages aren't always helpful, but it's quite hard to get the parser to say useful things in some cases...

@ISSOtm
Copy link
Member Author

ISSOtm commented Jun 12, 2018

Well, the manual states that \, is an escape sequence, so when I got an Unterminated string error after using a comma, I thought of escaping it. But I expected \n to work as well, which it didn't.

In retrospect, \\n does make sense, but it's not obvious to have the idea -- it took me a day to have that idea.

@AntonioND
Copy link
Member

Well, I think I know what's happening.

dstr: MACRO
    REPT _NARG
        db \1
        shift
    ENDR
    db 0
ENDM

dstr "Line1\\\nLine2"

The parser sees, after the expansion:

    REPT _NARG
        db "Line1\
Line2"
        shift
    ENDR
    db 0

As far as I remember, strings can't be split into multiple lines. So yeah, there is an unterminated string there.

@ISSOtm
Copy link
Member Author

ISSOtm commented Jun 12, 2018

That's also my guess, although the parser actually attempts to escape the line feed, instead of calling it an unterminated string. But that's just nitpicking.

@AntonioND
Copy link
Member

However, even though this is not a bug, maybe the documentation can be updated. The documentation has an example, actually:

Line continuations work as usual inside macros or lists of arguments of macros.
Strings, however, are a bit trickier. The following example shows how to use
strings as arguments for a macro:

    PrintMacro : MACRO 
        PRINTT \1 
    ENDM 
 
        PrintMacro STRCAT(\"Hello\"\,  \ 
                          \" world\\n\")

Maybe it's just in the wrong place.

@ISSOtm
Copy link
Member Author

ISSOtm commented Jun 13, 2018

I think it is. It also feels weird to have to escape everything, because there are no quotes wrapping the argument. Intuitively, it feels like you shouldn't have to escape those...

@AntonioND
Copy link
Member

Well, that's a macro argument, and macro arguments are a tricky thing...

The problem is that the parser isn't great and there are many hacks everywhere. This is a problem that isn't going to go away.

The only thing I can do is to change the docs, but I don't know where to put that, so I'm open for suggestions.

@AntonioND
Copy link
Member

RE: They are tricky things: #63

@AntonioND AntonioND added enhancement Typically new features; lesser priority than bugs docs This affects the documentation (web-specific issues go to rgbds-www) rgbasm This affects RGBASM labels Jul 1, 2018
@AntonioND
Copy link
Member

I'm going to close this. If you have a suggestion on how to change the docs feel free to propose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This affects the documentation (web-specific issues go to rgbds-www) enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM
Projects
None yet
Development

No branches or pull requests

2 participants