Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90234d84 authored by David Duarte's avatar David Duarte
Browse files

pdl: Remove '"' prefix and suffix from string

Strings are quotted in '"' in pdl, currently
we keep thoses in the ast.

So

```
custom_field Address : 48 "hci/"
```

ends up in the ast as

```
{
      "kind": "custom_field_declaration",
      "id": "Address",
      "width": 48,
      "function": "\"hci/\""
}
```

To simplify the generator code, remove the quotes to
instead emit

```
{
      "kind": "custom_field_declaration",
      "id": "Address",
      "width": 48,
      "function": "hci/"
}
```

Test: manual
Change-Id: I06fefc06b7f4f740f12dfd68567460015e7b5862
parent 5922eec9
Loading
Loading
Loading
Loading
+32 −3
Original line number Diff line number Diff line
@@ -187,7 +187,7 @@ fn err_missing_rule<T>(expected: Rule) -> Result<T, String> {
    Err(format!("expected rule {:?}, got nothing", expected))
}

fn expect<'i>(iter: &mut NodeIterator<'i>, rule: Rule) -> Result<Node<'i>, String> {
fn expect<'i>(iter: &mut impl Iterator<Item = Node<'i>>, rule: Rule) -> Result<Node<'i>, String> {
    match iter.next() {
        Some(node) if node.as_rule() == rule => Ok(node),
        Some(node) => err_unexpected_rule(rule, node.as_rule()),
@@ -233,8 +233,12 @@ fn parse_identifier_or_integer(
    }
}

fn parse_string(iter: &mut NodeIterator<'_>) -> Result<String, String> {
    expect(iter, Rule::string).map(|n| n.as_string())
fn parse_string<'i>(iter: &mut impl Iterator<Item = Node<'i>>) -> Result<String, String> {
    expect(iter, Rule::string)
        .map(|n| n.as_str())
        .and_then(|s| s.strip_prefix('"').ok_or_else(|| "expected \" prefix".to_owned()))
        .and_then(|s| s.strip_suffix('"').ok_or_else(|| "expected \" suffix".to_owned()))
        .map(|s| s.to_owned())
}

fn parse_size_modifier_opt(iter: &mut NodeIterator<'_>) -> Option<String> {
@@ -533,4 +537,29 @@ mod test {
        assert_eq!(grammar.endianness.value, ast::EndiannessValue::BigEndian);
        assert_ne!(grammar.endianness.loc, ast::SourceRange::default());
    }

    #[test]
    fn test_parse_string_bare() {
        let mut pairs = PDLParser::parse(Rule::string, r#""test""#).unwrap();

        assert_eq!(parse_string(&mut pairs).as_deref(), Ok("test"));
        assert_eq!(pairs.next(), None, "pairs is empty");
    }

    #[test]
    fn test_parse_string_space() {
        let mut pairs = PDLParser::parse(Rule::string, r#""test with space""#).unwrap();

        assert_eq!(parse_string(&mut pairs).as_deref(), Ok("test with space"));
        assert_eq!(pairs.next(), None, "pairs is empty");
    }

    #[test]
    #[should_panic] /* This is not supported */
    fn test_parse_string_escape() {
        let mut pairs = PDLParser::parse(Rule::string, r#""\"test\"""#).unwrap();

        assert_eq!(parse_string(&mut pairs).as_deref(), Ok(r#""test""#));
        assert_eq!(pairs.next(), None, "pairs is empty");
    }
}