picomeson 0.1.3

A small parser for meson.build files
Documentation
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#![allow(dead_code)]

use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;

use hashbrown::HashMap;

use crate::parser::{BinaryOperator, ParseError, Parser, Statement, Value};

#[derive(Debug, Clone, PartialEq)]
pub enum MachineValue {
    String(String),
    Integer(i64),
    Boolean(bool),
    Array(Vec<MachineValue>),
}

impl MachineValue {
    /// Convert from parser Value to MachineValue
    fn from_value(value: Value) -> Result<Self, ParseError> {
        match value {
            Value::String(s) => Ok(MachineValue::String(s)),
            Value::Integer(i) => Ok(MachineValue::Integer(i)),
            Value::Boolean(b) => Ok(MachineValue::Boolean(b)),
            Value::Array(arr) => {
                let mut result = Vec::new();
                for item in arr {
                    result.push(MachineValue::from_value(item)?);
                }
                Ok(MachineValue::Array(result))
            }
            _ => Err(ParseError::UnexpectedToken),
        }
    }

    /// Convert MachineValue back to Value for evaluation
    fn to_value(&self) -> Value {
        match self {
            MachineValue::String(s) => Value::String(s.clone()),
            MachineValue::Integer(i) => Value::Integer(*i),
            MachineValue::Boolean(b) => Value::Boolean(*b),
            MachineValue::Array(arr) => Value::Array(arr.iter().map(|v| v.to_value()).collect()),
        }
    }
}

impl MachineValue {
    pub fn as_string(&self) -> Option<&str> {
        if let MachineValue::String(s) = self {
            Some(s)
        } else {
            None
        }
    }

    pub fn coerce_string(&self) -> String {
        match self {
            MachineValue::String(s) => s.clone(),
            MachineValue::Integer(i) => i.to_string(),
            MachineValue::Boolean(b) => b.to_string(),
            MachineValue::Array(arr) => {
                let strs: Vec<String> = arr.iter().map(|v| v.coerce_string()).collect();
                strs.join(",")
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct MachineFile {
    pub sections: HashMap<String, HashMap<String, MachineValue>>,
}

impl MachineFile {
    fn new() -> Self {
        Self::default()
    }

    pub fn parse(content: &str) -> Result<MachineFile, ParseError> {
        let mut parser = MachineFileParser::new(content);
        let machine_file = parser.parse()?;
        Ok(machine_file)
    }

    /// Get a value from a section
    pub fn get(&self, section: &str, key: &str) -> Option<&MachineValue> {
        self.sections.get(section)?.get(key)
    }

    /// Set a value in a section
    fn set(&mut self, section: &str, key: &str, value: MachineValue) {
        self.sections
            .entry(section.to_string())
            .or_default()
            .insert(key.to_string(), value);
    }

    pub fn section(&self, section: &str) -> Option<&HashMap<String, MachineValue>> {
        self.sections.get(section)
    }
}

struct MachineFileParser {
    lines: Vec<String>,
    pos: usize,
    sections: HashMap<String, HashMap<String, (usize, Value)>>,
}

impl MachineFileParser {
    fn new(content: &str) -> Self {
        let lines = content
            .lines()
            .map(|line| line.trim().to_string())
            .collect();

        Self {
            lines,
            pos: 0,
            sections: HashMap::new(),
        }
    }

    fn set(&mut self, section: &str, key: &str, value: Value) {
        let section = self.sections.entry(section.to_string()).or_default();

        let n = section.len();
        if let Some(entry) = section.get_mut(key) {
            // Key already exists, overwrite it
            entry.1 = value;
        } else {
            section.insert(key.to_string(), (n, value));
        }
    }

    fn parse(&mut self) -> Result<MachineFile, ParseError> {
        self.pos = 0;

        let mut current_section = String::new();

        while self.pos < self.lines.len() {
            let line = &self.lines[self.pos].clone();
            self.pos += 1;

            // Skip empty lines
            if line.is_empty() {
                continue;
            }

            // Check for section header
            if line.starts_with('[') && line.ends_with(']') {
                current_section = line[1..line.len() - 1].to_string();
                continue;
            }

            // Parse key-value assignment
            if let Some(equals_pos) = line.find('=') {
                let key = line[..equals_pos].trim().to_string();
                let value_str = line[equals_pos + 1..].trim();

                // Parse the value expression with available variables
                let value = self.parse_value(value_str)?;

                // Add to section variables for future references
                self.set(&current_section, &key, value);
            }
        }

        let mut machine_file = MachineFile::new();

        let mut sections = core::mem::take(&mut self.sections);

        // Evaluate all values with context of their section variables
        if let Some(entries) = sections.remove("constants") {
            self.evaluate_section(&mut machine_file, "constants", entries)?;
        }

        for (section, entries) in sections.into_iter() {
            self.evaluate_section(&mut machine_file, &section, entries)?;
        }

        Ok(machine_file)
    }

    fn evaluate_section(
        &self,
        machine_file: &mut MachineFile,
        section: &str,
        entries: HashMap<String, (usize, Value)>,
    ) -> Result<(), ParseError> {
        let mut entries = entries.into_iter().collect::<Vec<_>>();
        entries.sort_by_key(|(_, (idx, _))| *idx);
        for (k, (_, v)) in entries {
            let evaluated = machine_file.evaluate_value(section, &v)?;
            let mv = MachineValue::from_value(evaluated)?;
            machine_file.set(section, &k, mv);
        }
        Ok(())
    }

    fn parse_value(&mut self, first_line: &str) -> Result<Value, ParseError> {
        let mut array_content = String::from(first_line);

        // Continue reading lines until we find the closing bracket
        while self.pos < self.lines.len() {
            // Use the main parser to decide when the statement is complete
            if Parser::new(&array_content).parse().is_ok() {
                break;
            }

            let line = self.lines[self.pos].as_str();
            self.pos += 1;

            array_content.push('\n');
            array_content.push_str(line);
        }

        // Parse the complete value
        let mut parser = Parser::new(&array_content);
        let mut statements = parser.parse()?;

        if statements.len() != 1 {
            return Err(ParseError::UnexpectedToken);
        }

        if let Statement::Expression(expr) = statements.swap_remove(0) {
            Ok(expr)
        } else {
            Err(ParseError::UnexpectedToken)
        }
    }
}

impl MachineFile {
    fn evaluate_value(&self, section: &str, value: &Value) -> Result<Value, ParseError> {
        match value {
            Value::String(s) => Ok(Value::String(s.clone())),
            Value::Integer(i) => Ok(Value::Integer(*i)),
            Value::Boolean(b) => Ok(Value::Boolean(*b)),
            Value::Array(arr) => {
                let mut result = Vec::new();
                for item in arr {
                    result.push(self.evaluate_value(section, item)?);
                }
                Ok(Value::Array(result))
            }
            Value::Identifier(name) => {
                // Look up variable in constants first, then section variables
                if let Some(val) = self.get("constants", name) {
                    Ok(val.to_value())
                } else if let Some(val) = self.get(section, name) {
                    Ok(val.to_value())
                } else {
                    Err(ParseError::UnexpectedToken)
                }
            }
            Value::BinaryOp(left, op, right) => {
                let left_val = self.evaluate_value(section, left)?;
                let right_val = self.evaluate_value(section, right)?;

                match op {
                    BinaryOperator::Add => {
                        // String/array concatenation
                        match (&left_val, &right_val) {
                            (Value::String(a), Value::String(b)) => {
                                let mut result = a.clone();
                                result.push_str(b);
                                Ok(Value::String(result))
                            }
                            (Value::Array(a), Value::Array(b)) => {
                                let mut result = a.clone();
                                result.extend(b.clone());
                                Ok(Value::Array(result))
                            }
                            (Value::Array(a), Value::String(b)) => {
                                let mut result = a.clone();
                                result.push(Value::String(b.clone()));
                                Ok(Value::Array(result))
                            }
                            (Value::String(a), Value::Array(b)) => {
                                let mut result = vec![Value::String(a.clone())];
                                result.extend(b.iter().cloned());
                                Ok(Value::Array(result))
                            }
                            _ => Err(ParseError::UnexpectedToken),
                        }
                    }
                    BinaryOperator::Div => {
                        // Path joining
                        match (&left_val, &right_val) {
                            (Value::String(a), Value::String(b)) => {
                                // TODO: use the Os::join_paths method here
                                let mut result = a.clone();
                                if !result.ends_with('/') && !result.ends_with('\\') {
                                    result.push('/');
                                }
                                // Remove leading slash from right side if present
                                let b = if b.starts_with('/') || b.starts_with('\\') {
                                    &b[1..]
                                } else {
                                    b
                                };
                                result.push_str(b);
                                Ok(Value::String(result))
                            }
                            _ => Err(ParseError::UnexpectedToken),
                        }
                    }
                    _ => Err(ParseError::UnexpectedToken),
                }
            }
            _ => Err(ParseError::UnexpectedToken),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // ... existing tests ...

    #[test]
    fn test_constants_section() {
        let content = r##"
[constants]
toolchain_prefix = '/usr/bin/'
gcc_name = 'gcc'
base_url = 'https://example.com'
url = base_url / '#some_hash'
array = [
    'item1',
    'item2',
    'item3'
] + 'item4'

[binaries]
c = toolchain_prefix / gcc_name # some comment here
cpp = toolchain_prefix / 'g++'
"##;
        let machine_file = MachineFile::parse(content).unwrap();

        assert_eq!(
            machine_file.get("constants", "array"),
            Some(&MachineValue::Array(vec![
                MachineValue::String("item1".to_string()),
                MachineValue::String("item2".to_string()),
                MachineValue::String("item3".to_string()),
                MachineValue::String("item4".to_string()),
            ]))
        );
        assert_eq!(
            machine_file.get("constants", "url"),
            Some(&MachineValue::String(
                "https://example.com/#some_hash".to_string()
            ))
        );
        assert_eq!(
            machine_file.get("binaries", "c"),
            Some(&MachineValue::String("/usr/bin/gcc".to_string()))
        );
        assert_eq!(
            machine_file.get("binaries", "cpp"),
            Some(&MachineValue::String("/usr/bin/g++".to_string()))
        );
    }

    #[test]
    fn test_string_concatenation() {
        let content = r#"
[constants]
prefix = '-D'
foo = 'FOO'

[properties]
c_args = [prefix + foo + '=1', '-DBAR=2']
"#;
        let machine_file = MachineFile::parse(content).unwrap();

        if let Some(MachineValue::Array(arr)) = machine_file.get("properties", "c_args") {
            assert_eq!(arr[0], MachineValue::String("-DFOO=1".to_string()));
        } else {
            panic!("Expected array value");
        }
    }

    #[test]
    fn test_array_concatenation() {
        let content = r#"
[constants]
base_args = ['-O2', '-g']

[properties]
c_args = base_args + ['-DFOO=1']
"#;
        let machine_file = MachineFile::parse(content).unwrap();

        if let Some(MachineValue::Array(arr)) = machine_file.get("properties", "c_args") {
            assert_eq!(arr.len(), 3);
            assert_eq!(arr[0], MachineValue::String("-O2".to_string()));
            assert_eq!(arr[1], MachineValue::String("-g".to_string()));
            assert_eq!(arr[2], MachineValue::String("-DFOO=1".to_string()));
        } else {
            panic!("Expected array value");
        }
    }

    #[test]
    fn test_section_local_variables() {
        let content = r#"
[binaries]
prefix = '/usr/bin/'
c = prefix / 'gcc'
cpp = prefix / 'g++'
"#;
        let machine_file = MachineFile::parse(content).unwrap();

        assert_eq!(
            machine_file.get("binaries", "c"),
            Some(&MachineValue::String("/usr/bin/gcc".to_string()))
        );
    }

    #[test]
    fn test_section_composition() {
        let content = r#"
[constants]
a = 'Foo'
b = a + 'World'

[constants]
a = 'Hello'
"#;
        let machine_file = MachineFile::parse(content).unwrap();

        assert_eq!(
            machine_file.get("constants", "b"),
            Some(&MachineValue::String("HelloWorld".to_string()))
        );
    }

    #[test]
    fn test_section_composition_err() {
        let content = r#"
[constants]
b = a + 'World'

[constants]
a = 'Hello'
"#;
        MachineFile::parse(content).unwrap_err();
    }

    #[test]
    fn test_section_composition_ok() {
        let content = r#"
[constants]
a = 'Hello'

[constants]
b = a + 'World'
"#;
        let machine_file = MachineFile::parse(content).unwrap();

        assert_eq!(
            machine_file.get("constants", "b"),
            Some(&MachineValue::String("HelloWorld".to_string()))
        );
    }
}