aquavm icon indicating copy to clipboard operation
aquavm copied to clipboard

Support for escaping double quotes in string literal

Open folex opened this issue 3 years ago • 1 comments

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

folex avatar Jul 19 '21 17:07 folex

I think the issue might be here https://github.com/fluencelabs/aquavm/blob/9c88567e59491c1bbfc15e0040e5aa43329e2015/crates/air-lib/air-parser/src/parser/lexer/air_lexer.rs#L87 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?

mikky-j avatar Nov 15 '22 16:11 mikky-j