← 输入框|github.com|刷新
Skip to content

Unescape strings at tokenizer level - #1885

Draft
ubaidsk wants to merge 4 commits into
lcompilers:mainfrom
ubaidsk:escape_unescape
Draft

ubaidsk wants to merge 4 commits into
lcompilers:mainfrom
ubaidsk:escape_unescape

Conversation

@ubaidsk

@ubaidsk ubaidsk commented Jun 8, 2023 •

Copy link
Copy Markdown
Collaborator

@ubaidsk
ubaidsk force-pushed the escape_unescape branch 2 times, most recently from 6680c9e to 9f2ceda Compare June 8, 2023 19:34
@ubaidsk
ubaidsk marked this pull request as ready for review June 8, 2023 19:39
@ubaidsk

ubaidsk commented Jun 8, 2023

Copy link
Copy Markdown
Collaborator Author

The step Run mamba-org/provision-with-micromamba@main seems to fail for three jobs at the CI.

@ubaidsk
ubaidsk requested a review from certik June 8, 2023 19:50
@ubaidsk
ubaidsk force-pushed the escape_unescape branch 3 times, most recently from 27b1579 to 4c266ab Compare June 9, 2023 02:11
@ubaidsk
ubaidsk marked this pull request as draft June 9, 2023 02:24
@ubaidsk

ubaidsk commented Jun 9, 2023

Copy link
Copy Markdown
Collaborator Author
(lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py                     
r"""
Text
123\n\t
"""
(lp) ubaid@ubaids-MacBook-Pro lpython % python -m tokenize examples/expr2.py      
0,0-0,0:            ENCODING       'utf-8'        
1,0-4,3:            STRING         'r"""\nText\n123\\n\\t\n"""'
4,3-4,4:            NEWLINE        '\n'           
5,0-5,0:            ENDMARKER      ''             
(lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py --show-ast        
(Module
    [(Expr
        (ConstantStr
            "\nText\n123\n\t\n"
            ()
        )
    )]
    []
)

In the above example, we have a raw string. For raw strings, escape sequences are treated as regular characters. We see that the current output of lpython is not as expected. Previously, the unescaping of string was done at the parser level, where we did not unescape strings which have PREFIX_STRING (see semantics.h) of r.

@certik

certik commented Jun 9, 2023

Copy link
Copy Markdown
Contributor

Good point. I see two ways forward:

  • unescape at the parser level
  • add a special token for raw strings of the type r"xxx", and then handle unescaping correctly. Modify the parser accordingly.

@ubaidsk

ubaidsk commented Jun 12, 2023

Copy link
Copy Markdown
Collaborator Author

add a special token for raw strings of the type r"xxx", and then handle unescaping correctly. Modify the parser accordingly.

It seems that adding token for strings is possible, but is leading to many new tokens being added (raw_strings, bytes, raw_bytes, formatted strings, raw_formatted strings, etc.)

raw_str1 = ("r" | "R") (string1 | string2);
raw_str2 = ("r" | "R") (string3 | string4);
bytes1 = ("b" | "B") (string1 | string2);
bytes2 = ("b" | "B") (string3 | string4);
raw_bytes1 = ("rb" | "rB" | "Rb" | "RB" | "br" | "bR" | "Br" | "BR") (string1 | string2);
raw_bytes2 = ("rb" | "rB" | "Rb" | "RB" | "br" | "bR" | "Br" | "BR") (string3 | string4);
...

For the moment it seems it is better to proceed with unescaping at the parser level.

@certik

certik commented Jun 12, 2023

Copy link
Copy Markdown
Contributor

I think if you use 'rb' then it is case insensitive in re2c. But it's fine.

@certik

certik commented Jun 12, 2023

Copy link
Copy Markdown
Contributor

Looks good to me. @Shaikh-Ubaid let me know when this is ready for review.

@ubaidsk

ubaidsk commented Jun 12, 2023 •

Copy link
Copy Markdown
Collaborator Author

For the moment it seems it is better to proceed with unescaping at the parser level.

There is another concern here. For LFortran when unescaping strings, we need to know the quote used to create the string (' or "). I think this information is not available with the parser. So, it seems it needs unescaping at the tokenizer level.

(Expr
(ConstantBytes
"b'\n\n\\n'"
"b'\n\\n\\\\n'"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the example above, the given string is

b'''
\n\\n'''

With respect to this example, it seems that all strings (including raw strings, bytes, raw bytes, etc.) need unescaping at the tokenizer level. And then we need to escape them if they are raw strings/ bytes/ raw bytes, etc (I think at the parser level).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do raw strings have to be escaped?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right, Sir. I will look into it.

(JoinedStr
[(ConstantStr
"\\n"
"\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bug.

Comment thread src/libasr/asdl_cpp.py
self.emit("}", 2)
else:
self.emit('s.append("\\"" + get_escaped_str(x.m_%s) + "\\"");' % field.name, 2)
self.emit('s.append("\\"" + str_escape_c(x.m_%s) + "\\"");' % field.name, 2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this change.

|| token == yytokentype::TK_RAW_STRING
|| token == yytokentype::TK_BYTES
|| token == yytokentype::TK_RAW_BYTES) {
t = t + " " + "\"" + str_escape_c(yystype.string.str()) + "\"";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep this change (the str_escape_c).

(data (;1;) (i32.const 12) " ")
(data (;2;) (i32.const 16) "\18\00\00\00\01\00\00\00")
(data (;3;) (i32.const 24) "\0a ")
(data (;3;) (i32.const 24) "\n ")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this change.

@ubaidsk

ubaidsk commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator Author

See #1902 (comment).

ubaidsk added 3 commits June 14, 2023 04:53
Support raw strings token

Support bytes and raw bytes

Add support for unicode, fmt, raw_fmt strings
@ubaidsk ubaidsk changed the title Support Escaping/Unescaping strings in tokenizer Unescape strings at tokenizer level Jun 13, 2023
@ubaidsk
ubaidsk marked this pull request as ready for review June 13, 2023 23:26
@ubaidsk

ubaidsk commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator Author

Good point. I see two ways forward:

  • unescape at the parser level
  • add a special token for raw strings of the type r"xxx", and then handle unescaping correctly. Modify the parser accordingly.

This PR currently unescapes at tokenizer level. (LFortran also currently unescapes at tokenizer level since it needs to know the quote used to create the string literal).

The concern in the current approach of unescaping at the tokenizer level in LPython is that we are needing to create several/many new tokens like raw strings, unicode strings, formatted strings, raw formatted strings, bytes, raw bytes (and these also need support at the parser level).

@ubaidsk

ubaidsk commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator Author

In the current approach, we unescape all strings, bytes unless it is marked raw in which case we do not unescape (and therefore the escape sequences present (if any) in the raw string/bytes would be treated as character literals).

@ubaidsk

ubaidsk commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator Author

Ready.

@ubaidsk
ubaidsk requested a review from certik June 13, 2023 23:40
(ConstantStr
"Text"
()
"u"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change fixes a bug?

@ubaidsk ubaidsk Jun 14, 2023 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the prefixes "u" and "U" were meaningful for Python 2. In Python 3, all string literals are Unicode by default. Thus, it seems the prefixes "u" and "U" do not add much/any value in Python 3 (Reference https://chat.openai.com/share/1270fc0a-fb93-4638-822d-3d1619488027).

With respect to the above, I think it might not be a bug fix. Previously, we were passing the kind only for "u" (prefix). Currently, we also pass the kind value for "U" (capital U) (prefix).

@certik

certik commented Jun 14, 2023

Copy link
Copy Markdown
Contributor

Thanks for this. I think you might have found and fixed another bug (#1885 (comment)).

That bug aside, it does seem this approach is more complicated than doing it in the parser. Let's keep this PR open for a while and think about it.

@czgdp1807 let us know your opinion on this approach as well.

@czgdp1807 czgdp1807 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the status of this? Do we need this?

@ubaidsk

ubaidsk commented Jul 20, 2023

Copy link
Copy Markdown
Collaborator Author

What's the status of this? Do we need this?

We need your opinion on it. Please, let us know what we should do.

We currently handle unescaping at the parser level for LPython (and tokenizer level for LFortran). This PR supports unescaping at tokenizer level for LPython.

Any approach works for me. Please feel free to close/merge it as required.

@czgdp1807

Copy link
Copy Markdown
Collaborator

If there is no significant benefit of bringing un-escaping to the tokenizer level in LPython, then I wouldn't work on it for the time being. There are higher priority things to be done (like bug fixing, performance improvements to perform better than LPython's competitors), so let's work on those.

@czgdp1807
czgdp1807 marked this pull request as draft August 28, 2023 04:58
@ubaidsk ubaidsk mentioned this pull request Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants