1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright 2018 The Starlark in Rust Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! String interpolation-related code.

use crate::syntax::errors::SyntaxError;
use crate::values::error::*;
use crate::values::Value;
use codemap::Span;
use codemap_diagnostic::{Diagnostic, Level, SpanLabel, SpanStyle};
use std::convert::TryFrom;
use std::iter;

/// Operator `%` format or evaluation errors
#[derive(Clone, Debug)]
pub enum StringInterpolationError {
    /// `)` is not found when parsing `%(name)` expression.
    UnexpectedEOFClosingParen,
    /// `%` must be followed by specifier.
    UnexpectedEOFPercent,
    /// `%z` where `z` is unknown specifier.
    UnknownSpecifier(char),
    /// Trying to interpolate with %c an integer that is not in the UTF-8 range.
    ValueNotInUTFRange(i64),
    /// Interpolation parameter is too big for the format string.
    TooManyParameters,
    /// Interpolation parameter is too small for the format string.
    NotEnoughParameters,
    /// Value for `%s` is required to be a char
    ValueNotChar,
}

impl SyntaxError for StringInterpolationError {
    fn to_diagnostic(self, file_span: Span) -> Diagnostic {
        let (label, message, code) = match self {
            StringInterpolationError::UnexpectedEOFClosingParen => (
                "Unexpected EOF in format string when looking for closing paren".to_owned(),
                "Could not found ')' when parsing '%(name)f' expression".to_owned(),
                INTERPOLATION_UNEXPECTED_EOF_CLOSING_PAREN,
            ),
            StringInterpolationError::UnexpectedEOFPercent => (
                "End of string while expecting format specifier".to_owned(),
                concat!(
                    "Interpolation string format is incorrect:",
                    " '%' must be followed by an optional name and a specifier ",
                    "('s', 'r', 'd', 'i', 'o', 'x', 'X', 'c') or '%'",
                )
                .to_owned(),
                INTERPOLATION_UNEXPECTED_EOF_PERCENT,
            ),
            StringInterpolationError::UnknownSpecifier(c) => (
                format!("Unknown format string specifier '{}'", c.escape_default()),
                concat!(
                    "Interpolation string format is incorrect:",
                    " '%' must be followed by an optional name and a specifier ",
                    "('s', 'r', 'd', 'i', 'o', 'x', 'X', 'c') or '%'",
                )
                .to_owned(),
                INTERPOLATION_UNKNOWN_SPECIFIER,
            ),
            StringInterpolationError::ValueNotInUTFRange(ref c) => (
                format!("Invalid codepoint 0x{:x}", c),
                format!(
                    concat!(
                        "Value 0x{:x} passed for %c formatter is not a valid",
                        " UTF-8 codepoint"
                    ),
                    c
                ),
                INTERPOLATION_OUT_OF_UTF8_RANGE_ERROR_CODE,
            ),
            StringInterpolationError::TooManyParameters => (
                "Too many arguments for format string".to_owned(),
                "Too many arguments for format string".to_owned(),
                INTERPOLATION_TOO_MANY_PARAMS_ERROR_CODE,
            ),
            StringInterpolationError::NotEnoughParameters => (
                "Not enough arguments for format string".to_owned(),
                "Not enough arguments for format string".to_owned(),
                INTERPOLATION_NOT_ENOUGH_PARAMS_ERROR_CODE,
            ),
            StringInterpolationError::ValueNotChar => (
                "'%c' formatter requires a single-character string".to_owned(),
                "'%c' formatter requires a single-character string".to_owned(),
                INTERPOLATION_VALUE_IS_NOT_CHAR_ERROR_CODE,
            ),
        };
        let sl = SpanLabel {
            span: file_span,
            style: SpanStyle::Primary,
            label: Some(label),
        };
        Diagnostic {
            level: Level::Error,
            message,
            code: Some(code.to_owned()),
            spans: vec![sl],
        }
    }
}

/// Format char
pub(crate) enum ArgFormat {
    // str(x)
    Str,
    // repr(x)
    Repr,
    // signed integer decimal
    Dec,
    // signed octal
    Oct,
    // signed hexadecimal, lowercase
    HexLower,
    // signed hexadecimal, uppercase
    HexUpper,
    // x for string, chr(x) for int
    Char,
    // `%` sign
    Percent,
}

impl ArgFormat {
    fn format_arg(&self, out: &mut String, arg: Value) -> Result<(), ValueError> {
        use std::fmt::Write;

        match self {
            ArgFormat::Str => write!(out, "{}", arg.to_str()).unwrap(),
            ArgFormat::Repr => write!(out, "{}", arg.to_repr()).unwrap(),
            ArgFormat::Dec => write!(out, "{}", arg.to_int()?).unwrap(),
            ArgFormat::Oct => {
                let v = arg.to_int()?;
                write!(
                    out,
                    "{}{:o}",
                    if v < 0 { "-" } else { "" },
                    v.wrapping_abs() as u64
                )
                .unwrap();
            }
            ArgFormat::HexLower => {
                let v = arg.to_int()?;
                write!(
                    out,
                    "{}{:x}",
                    if v < 0 { "-" } else { "" },
                    v.wrapping_abs() as u64
                )
                .unwrap();
            }
            ArgFormat::HexUpper => {
                let v = arg.to_int()?;
                write!(
                    out,
                    "{}{:X}",
                    if v < 0 { "-" } else { "" },
                    v.wrapping_abs() as u64
                )
                .unwrap();
            }
            ArgFormat::Char => match arg.get_type() {
                "string" => {
                    if arg.length()? != 1 {
                        return Err(StringInterpolationError::ValueNotChar.into());
                    } else {
                        write!(out, "{}", arg.to_str()).unwrap();
                    }
                }
                _ => {
                    let i = arg.to_int()?;
                    let codepoint = match u32::try_from(i) {
                        Ok(codepoint) => codepoint,
                        Err(_) => {
                            return Err(StringInterpolationError::ValueNotInUTFRange(i).into())
                        }
                    };
                    match std::char::from_u32(codepoint) {
                        Some(c) => write!(out, "{}", c).unwrap(),
                        None => {
                            return Err(StringInterpolationError::ValueNotInUTFRange(i64::from(
                                codepoint,
                            ))
                            .into())
                        }
                    }
                }
            },
            ArgFormat::Percent => {
                write!(out, "%").unwrap();
            }
        }
        Ok(())
    }
}

// %(name)s or %s
enum NamedOrPositional {
    Named(String),
    Positional,
}

/// Parsed format string
pub(crate) struct ArgsFormat {
    /// String before first parameter
    init: String,
    /// Number of positional arguments
    positional_count: usize,
    /// Number of named arguments
    named_count: usize,
    /// Arguments followed by uninterpreted strings
    parameters: Vec<(NamedOrPositional, ArgFormat, String)>,
}

impl ArgsFormat {
    fn append_literal(&mut self, c: char) {
        if let Some(p) = self.parameters.last_mut() {
            p.2.push(c);
        } else {
            self.init.push(c)
        }
    }

    pub fn parse(format: &str) -> Result<ArgsFormat, ValueError> {
        let mut result = ArgsFormat {
            init: String::new(),
            positional_count: 0,
            named_count: 0,
            parameters: Vec::new(),
        };
        let mut chars = format.chars();
        while let Some(c) = chars.next() {
            if c != '%' {
                result.append_literal(c);
            } else {
                let next = chars
                    .next()
                    .ok_or(StringInterpolationError::UnexpectedEOFPercent)?;
                let (named_or_positional, format_char) = if next == '(' {
                    let mut name = String::new();
                    loop {
                        match chars.next() {
                            None => {
                                return Err(
                                    StringInterpolationError::UnexpectedEOFClosingParen.into()
                                )
                            }
                            Some(')') => {
                                break;
                            }
                            Some(c) => name.push(c),
                        }
                    }
                    (
                        NamedOrPositional::Named(name),
                        chars
                            .next()
                            .ok_or(StringInterpolationError::UnexpectedEOFPercent)?,
                    )
                } else {
                    (NamedOrPositional::Positional, next)
                };
                let format = match format_char {
                    's' => ArgFormat::Str,
                    'r' => ArgFormat::Repr,
                    'd' | 'i' => ArgFormat::Dec,
                    'o' => ArgFormat::Oct,
                    'x' => ArgFormat::HexLower,
                    'X' => ArgFormat::HexUpper,
                    'c' => ArgFormat::Char,
                    '%' => match named_or_positional {
                        NamedOrPositional::Positional => {
                            result.append_literal('%');
                            continue;
                        }
                        NamedOrPositional::Named(_) => {
                            // In both Python and Starlark Go implementations
                            // `%(n)%` consumes named argument, but
                            // `%%` does not consume positional argument.
                            // So `Percent` variant is added only when `ArgFormat` is `Named`.
                            ArgFormat::Percent
                        }
                    },
                    c => return Err(StringInterpolationError::UnknownSpecifier(c).into()),
                };
                match named_or_positional {
                    NamedOrPositional::Positional => {
                        result.positional_count += 1;
                    }
                    NamedOrPositional::Named(..) => {
                        result.named_count += 1;
                    }
                }
                result
                    .parameters
                    .push((named_or_positional, format, String::new()));
            }
        }
        Ok(result)
    }

    pub fn format(self, other: Value) -> Result<String, ValueError> {
        let mut r = self.init;
        let other_iter;
        let mut arg_iter: Box<dyn Iterator<Item = Value>> = if self.positional_count > 1 {
            other_iter = Some(other.iter()?);
            other_iter.as_ref().unwrap().iter()
        } else if self.positional_count == 1 {
            Box::new(iter::once(other.clone()))
        } else if self.named_count != 0 {
            Box::new(iter::empty())
        } else {
            // If both positional count is zero and named count is zero
            // we should check that iterable has zero elements.
            other_iter = Some(other.iter()?);
            other_iter.as_ref().unwrap().iter()
        };
        for (named_or_positional, format, tail) in self.parameters {
            let arg = match named_or_positional {
                NamedOrPositional::Positional => match arg_iter.next() {
                    Some(a) => a,
                    None => return Err(StringInterpolationError::NotEnoughParameters.into()),
                },
                NamedOrPositional::Named(name) => other.at(Value::new(name))?,
            };
            format.format_arg(&mut r, arg)?;
            r.push_str(&tail);
        }

        if arg_iter.next().is_some() {
            return Err(StringInterpolationError::TooManyParameters.into());
        }

        Ok(r)
    }
}

#[cfg(test)]
mod test {
    use crate::values::Value;
    use std::collections::HashMap;
    use std::convert::TryFrom;

    #[test]
    fn test_string_interpolation() {
        // "Hello %s, your score is %d" % ("Bob", 75) == "Hello Bob, your score is 75"
        assert_eq!(
            Value::from("Hello %s, your score is %d")
                .percent(Value::from(("Bob", 75)))
                .unwrap(),
            Value::from("Hello Bob, your score is 75")
        );

        // "%d %o %x %c" % (65, 65, 65, 65) == "65 101 41 A"
        assert_eq!(
            Value::from("%d %o %x %c")
                .percent(Value::from((65, 65, 65, 65)))
                .unwrap(),
            Value::from("65 101 41 A")
        );

        // "%(greeting)s, %(audience)s" % {"greeting": "Hello", "audience": "world"} ==
        //      "Hello, world"
        let mut d = Value::try_from(HashMap::<String, Value>::new()).unwrap();
        d.set_at(Value::from("greeting"), Value::from("Hello"))
            .unwrap();
        d.set_at(Value::from("audience"), Value::from("world"))
            .unwrap();
        assert_eq!(
            Value::from("%(greeting)s, %(audience)s")
                .percent(d)
                .unwrap(),
            Value::from("Hello, world")
        );

        // Both Python and Starlark Go behave this way:
        // "%s%(a)%" % {"a": 1} == "{\"a\": 1}%"
        // "%s%(a)s" % {"a": 1} == "{\"a\": 1}1"
        let mut d = Value::try_from(HashMap::<String, Value>::new()).unwrap();
        d.set_at(Value::from("a"), Value::from(1)).unwrap();
        assert_eq!(
            Value::from("%s%(a)%").percent(d.clone()).unwrap(),
            Value::from("{\"a\": 1}%")
        );
        assert_eq!(
            Value::from("%s%(a)s").percent(d.clone()).unwrap(),
            Value::from("{\"a\": 1}1")
        );
    }
}