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

Support for escaping double quotes in string literal #124

Open
folex opened this issue Jul 19, 2021 · 1 comment
Open

Support for escaping double quotes in string literal #124

folex opened this issue Jul 19, 2021 · 1 comment
Labels
C-enhancement category: an issue proposing an enchacement or a PR with one P-low priority: low

Comments

@folex
Copy link
Member

folex commented Jul 19, 2021

Currently, there's no way to use double quotes inside a string literal. That makes it impossible to create a valid JSON string, for example.

   ┌─ script.air:22:78
   │
22 │                     (call relay (first_service "create_vault_file") ["{\"name\": \"file_share\"}"] config_filename)
   │                                                                              ^ only alphanumeric, '_', and '-' characters are allowed in this position
@mikevoronov mikevoronov added the C-enhancement category: an issue proposing an enchacement or a PR with one label Sep 8, 2021
@mikevoronov mikevoronov added the P-low priority: low label Mar 24, 2022
@mikky-j
Copy link

mikky-j commented Nov 15, 2022

I think the issue might be here


The if statement doesn't check if an escape character (\) is preceeding the double quotes.
I suggest a solution that looks something like this

fn tokenize_string_literal(
        &mut self,
        start_pos: AirPos,
    ) -> Option<Spanned<Token<'input>, AirPos, LexerError>> {
        let mut prev_ch = '';
        for (pos, ch) in &mut self.chars {
            let pos = AirPos::from(pos);
            if ch == '"' && prev_ch != "\\" {
                // + 1 to count an open double quote
                let string_size = pos - start_pos + 1;

                return Some(Ok((
                    start_pos,
                    Token::StringLiteral(&self.input[(start_pos + 1).into()..pos.into()]),
                    start_pos + string_size,
                )));
            }
            prev_ch = ch;
        }

        Some(Err(LexerError::unclosed_quote(
            start_pos..self.input.len().into(),
        )))
    }

It's a really simple solution that works.

Can I make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement category: an issue proposing an enchacement or a PR with one P-low priority: low
Projects
None yet
Development

No branches or pull requests

3 participants