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
use crate::error::UnexpectedTokenError;
use crate::error::{ExpectToken, InvalidArrayKeyError, ParseError, ResultExt, SpannedError};
use crate::lexer::Token;
use crate::string::{unescape_double, unescape_single, UnescapeError};
use crate::{Key, Value};
use logos::{Lexer, Logos};
use std::collections::HashMap;

/// Parse a php literal
///
/// ## Example
///
/// ```rust
/// use php_literal_parser::{parse, Value, Key};
/// # use std::fmt::Debug;
/// # use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let map = parse(r#"["foo" => true, "nested" => ['foo' => false]]"#)?;
///
/// assert_eq!(map["foo"], true);
/// assert_eq!(map["nested"]["foo"], false);
/// # Ok(())
/// # }
/// ```
///
pub fn parse(source: &str) -> Result<Value, SpannedError<ParseError>> {
    let mut lexer: Lexer<Token> = Token::lexer(source);
    parse_lexer(source, &mut lexer)
}

pub fn parse_lexer<'source>(
    source: &'source str,
    lexer: &mut Lexer<Token>,
) -> Result<Value, SpannedError<'source, ParseError>> {
    let token = lexer
        .next()
        .expect_token(&[
            Token::Bool,
            Token::Integer,
            Token::Float,
            Token::LiteralString,
            Token::Null,
            Token::Array,
            Token::SquareOpen,
        ])
        .with_span(lexer.span(), source)?;
    let value = match token {
        Token::Bool => Value::Bool(lexer.slice().parse().with_span(lexer.span(), source)?),
        Token::Integer => Value::Int(lexer.slice().parse().with_span(lexer.span(), source)?),
        Token::Float => Value::Float(lexer.slice().parse().with_span(lexer.span(), source)?),
        Token::LiteralString => {
            Value::String(parse_string(lexer.slice()).with_span(lexer.span(), source)?)
        }
        Token::Null => Value::Null,
        Token::Array => Value::Array(parse_array(source, lexer, ArraySyntax::Long)?),
        Token::SquareOpen => Value::Array(parse_array(source, lexer, ArraySyntax::Short)?),
        _ => unreachable!(),
    };

    Ok(value)
}

fn parse_string(literal: &str) -> Result<String, UnescapeError> {
    let single_quote = literal.bytes().next().unwrap() == b'\'';
    let inner = &literal[1..(literal.len()) - 1];

    if single_quote {
        unescape_single(inner)
    } else {
        unescape_double(inner)
    }
}

#[derive(Default)]
struct ArrayBuilder {
    next_int_key: i64,
    data: HashMap<Key, Value>,
}

impl ArrayBuilder {
    fn push_value(&mut self, value: Value) {
        let key = Key::Int(self.next_int_key);
        self.next_int_key += 1;
        self.data.insert(key, value);
    }

    fn push_key_value(&mut self, key: Key, value: Value) {
        if let Key::Int(int) = &key {
            self.next_int_key = int + 1;
        }
        self.data.insert(key, value);
    }
}

#[derive(Eq, PartialEq)]
enum ArraySyntax {
    Short,
    Long,
}

impl ArraySyntax {
    fn close_bracket(&self) -> Token {
        match self {
            ArraySyntax::Long => Token::BracketClose,
            ArraySyntax::Short => Token::SquareClose,
        }
    }
}

fn parse_array<'source>(
    source: &'source str,
    lexer: &mut Lexer<Token>,
    syntax: ArraySyntax,
) -> Result<HashMap<Key, Value>, SpannedError<'source, ParseError>> {
    let mut builder = ArrayBuilder::default();

    if syntax == ArraySyntax::Long {
        lexer
            .next()
            .expect_token(&[Token::BracketOpen])
            .with_span(lexer.span(), source)?;
    }

    loop {
        let key_or_value = match parse_lexer(source, lexer) {
            Ok(value) => value,
            Err(err) => {
                // trailing comma or empty array
                match err.error() {
                    ParseError::UnexpectedToken(UnexpectedTokenError {
                        found: Some(token),
                        ..
                    }) if token == &syntax.close_bracket() => break,
                    _ => return Err(err),
                }
            }
        };
        let key_or_value_span = lexer.span();
        let next = lexer
            .next()
            .expect_token(&[syntax.close_bracket(), Token::Comma, Token::Arrow])
            .with_span(lexer.span(), source)?;

        match next {
            Token::BracketClose => {
                builder.push_value(key_or_value);
                break;
            }
            Token::SquareClose => {
                builder.push_value(key_or_value);
                break;
            }
            Token::Comma => {
                builder.push_value(key_or_value);
            }
            Token::Arrow => {
                let value = parse_lexer(source, lexer)?;
                let key = match key_or_value {
                    Value::Int(int) => Key::Int(int),
                    Value::Float(float) => Key::Int(float as i64),
                    Value::String(str) => Key::String(str),
                    value => {
                        let err = ParseError::InvalidArrayKey(InvalidArrayKeyError(value));
                        let span_err = SpannedError::new(err, key_or_value_span, source);
                        return Err(span_err);
                    }
                };
                builder.push_key_value(key, value);

                match lexer
                    .next()
                    .expect_token(&[syntax.close_bracket(), Token::Comma])
                    .with_span(lexer.span(), source)?
                {
                    Token::BracketClose => {
                        break;
                    }
                    Token::SquareClose => {
                        break;
                    }
                    Token::Comma => {}
                    _ => unreachable!(),
                }
            }
            _ => {
                unreachable!();
            }
        }
    }

    Ok(builder.data)
}

#[test]
fn test_parse() {
    use maplit::hashmap;

    assert_eq!(Value::Bool(true), parse("true").unwrap());
    assert_eq!(Value::Bool(false), parse("false").unwrap());
    assert_eq!(Value::Int(12), parse("12").unwrap());
    assert_eq!(Value::Int(-1), parse("-1").unwrap());
    assert_eq!(Value::Float(1.12), parse("1.12").unwrap());
    assert_eq!(
        Value::String("test".to_string()),
        parse(r#""test""#).unwrap()
    );
    assert_eq!(Value::Array(hashmap! {}), parse(r#"array()"#).unwrap());
    assert_eq!(
        Value::Array(hashmap! {
            Key::Int(0) => Value::Int(3),
            Key::Int(1) => Value::Int(4),
            Key::Int(2) => Value::Int(5),
        }),
        parse(r#"array(3,4,5)"#).unwrap()
    );
    assert_eq!(
        Value::Array(hashmap! {
            Key::Int(0) => Value::Int(3),
            Key::Int(1) => Value::Int(4),
            Key::Int(2) => Value::Int(5),
        }),
        parse(r#"array(3,4,5,)"#).unwrap()
    );
    assert_eq!(
        Value::Array(hashmap! {
            Key::Int(1) => Value::Int(3),
            Key::Int(3) => Value::Int(4),
            Key::Int(5) => Value::Int(5),
        }),
        parse(r#"array(1=>3,3=>4,5=>5)"#).unwrap()
    );
    assert_eq!(
        Value::Array(hashmap! {
            Key::Int(1) => Value::Int(3),
            Key::Int(2) => Value::Int(4),
            Key::Int(3) => Value::Int(5),
        }),
        parse(r#"array(1=>3,4,5)"#).unwrap()
    );
    assert_eq!(
        Value::Array(hashmap! {
            Key::Int(1) => Value::Int(3),
            Key::String("foo".into()) => Value::Int(4),
            Key::Int(2) => Value::Int(5),
        }),
        parse(r#"array(1=>3,"foo" => 4,5)"#).unwrap()
    );
    assert_eq!(
        Value::Array(hashmap! {
            Key::String("foo".into()) => Value::Bool(true),
            Key::String("nested".into()) => Value::Array(hashmap! {
                Key::String("foo".into()) => Value::Bool(false),
            }),
        }),
        parse(r#"array("foo" => true, "nested" => array ('foo' => false))"#).unwrap()
    );
    assert_eq!(
        Value::Array(hashmap! {
            Key::String("foo".into()) => Value::Bool(true),
            Key::String("nested".into()) => Value::Array(hashmap! {
                Key::String("foo".into()) => Value::Null,
            }),
        }),
        parse(r#"["foo" => true, "nested" => ['foo' => null]]"#).unwrap()
    );
}