zephyr_build/devicetree/
parse.rs

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
//! DTS Parser
//!
//! Parse a limited subset of the devicetree source file that is output by the device tree compiler.
//! This is used to parse the `zephyr.dts` file generated as a part of a Zephyr build.

use std::{cell::RefCell, collections::BTreeMap, rc::Rc};

use pest::{
    iterators::{Pair, Pairs},
    Parser,
};
use pest_derive::Parser;

use crate::devicetree::Phandle;

use super::{ordmap::OrdMap, DeviceTree, Node, Property, Value, Word};

#[derive(Parser)]
#[grammar = "devicetree/dts.pest"]
pub struct Dts;

pub fn parse(text: &str, ords: &OrdMap) -> DeviceTree {
    let pairs = Dts::parse(Rule::file, text).expect("Parsing zephyr.dts");

    let b = TreeBuilder::new(ords);
    b.walk(pairs)
}

struct TreeBuilder<'a> {
    ords: &'a OrdMap,
    /// All labels.
    labels: BTreeMap<String, Rc<Node>>,
}

impl<'a> TreeBuilder<'a> {
    fn new(ords: &'a OrdMap) -> TreeBuilder<'a> {
        TreeBuilder {
            ords,
            labels: BTreeMap::new(),
        }
    }

    fn walk(mut self, pairs: Pairs<'_, Rule>) -> DeviceTree {
        // There is a single node at the top.
        let node = pairs.into_iter().next().unwrap();
        assert_eq!(node.as_rule(), Rule::node);

        DeviceTree {
            root: self.walk_node(node, "", &[]),
            labels: self.labels,
        }
    }

    // This is a single node in the DTS.  The name should match one of the ordmap entries.
    // The root node doesn't get a nodename.
    fn walk_node(&mut self, node: Pair<'_, Rule>, path: &str, route: &[String]) -> Rc<Node> {
        /*
        let ord = self.ords.0.get(name)
            .expect("Unexpected node path");
        println!("Root: {:?} {}", name, ord);
        */

        let mut name = LazyName::new(path, route.to_owned(), self.ords);
        let mut labels = Vec::new();
        let mut properties = Vec::new();
        let mut children = Vec::new();

        for pair in node.into_inner() {
            match pair.as_rule() {
                Rule::nodename => {
                    let text = pair.as_str();
                    name.set(text.to_string());
                }
                Rule::label => {
                    labels.push(pair.as_str().to_string());
                }
                Rule::property => {
                    properties.push(decode_property(pair));
                }
                Rule::node => {
                    let child_path = name.path_ref();
                    children.push(self.walk_node(pair, child_path, name.route_ref()));
                }
                r => panic!("node: {:?}", r),
            }
        }

        // Make a clone of the labels, as we need them cloned anyway.
        let labels2 = labels.clone();

        // Build this node.
        // println!("Node: {:?}", name.path_ref());
        let mut result = name.into_node();
        result.labels = labels;
        result.properties = properties;
        result.children = children;
        let node = Rc::new(result);

        // Insert all of the labels.
        for lab in labels2 {
            self.labels.insert(lab, node.clone());
        }
        node
    }
}

/// Decode a property node in the parse tree.
fn decode_property(node: Pair<'_, Rule>) -> Property {
    let mut name = None;
    let mut value = Vec::new();
    for pair in node.into_inner() {
        match pair.as_rule() {
            Rule::nodename => {
                name = Some(pair.as_str().to_string());
            }
            Rule::words => {
                value.push(Value::Words(decode_words(pair)));
            }
            Rule::phandle => {
                // TODO: Decode these.
                // println!("phandle: {:?}", pair.as_str());
                value.push(Value::Phandle(Phandle::new(pair.as_str()[1..].to_string())));
            }
            Rule::string => {
                // No escapes at this point.
                let text = pair.as_str();
                // Remove the quotes.
                let text = &text[1..text.len() - 1];
                value.push(Value::String(text.to_string()));
            }
            Rule::bytes => {
                value.push(Value::Bytes(decode_bytes(pair)));
            }
            r => panic!("rule: {:?}", r),
        }
    }
    Property {
        name: name.unwrap(),
        value,
    }
}

fn decode_words(node: Pair<'_, Rule>) -> Vec<Word> {
    let mut value = Vec::new();
    for pair in node.into_inner() {
        match pair.as_rule() {
            Rule::hex_number => {
                let text = pair.as_str();
                let num = u32::from_str_radix(&text[2..], 16).unwrap();
                value.push(Word::Number(num));
            }
            Rule::decimal_number => {
                let text = pair.as_str();
                let num = text.parse::<u32>().unwrap();
                value.push(Word::Number(num));
            }
            Rule::phandle => {
                // println!("phandle: {:?}", pair.as_str());
                let text = pair.as_str();
                value.push(Word::Phandle(Phandle::new(text[1..].to_string())));
            }
            _ => unreachable!(),
        }
    }
    value
}

fn decode_bytes(node: Pair<'_, Rule>) -> Vec<u8> {
    let mut value = Vec::new();
    for pair in node.into_inner() {
        match pair.as_rule() {
            Rule::plain_hex_number => {
                let text = pair.as_str();
                let num = u8::from_str_radix(text, 16).unwrap();
                value.push(num)
            }
            _ => unreachable!(),
        }
    }
    value
}

// Lazily track the path and node name.  The parse tree has the nodename for a given node come after
// entering the node, but before child nodes are seen.
struct LazyName<'a, 'b> {
    // The parent path leading up to this node.  Will be the empty string for the root node.
    path: &'a str,
    route: Vec<String>,
    ords: &'b OrdMap,
    // Our information, once we have it.
    info: Option<Info>,
}

struct Info {
    name: String,
    // Our path, the parent path combined with our name.
    path: String,
    ord: usize,
}

impl<'a, 'b> LazyName<'a, 'b> {
    fn new(path: &'a str, route: Vec<String>, ords: &'b OrdMap) -> LazyName<'a, 'b> {
        if path.is_empty() {
            let ord = ords.0["/"];
            LazyName {
                path,
                route,
                ords,
                info: Some(Info {
                    name: "/".to_string(),
                    path: "/".to_string(),
                    ord,
                }),
            }
        } else {
            LazyName {
                path,
                route,
                ords,
                info: None,
            }
        }
    }

    /// Indicate that we now know our name.
    fn set(&mut self, name: String) {
        if self.info.is_some() {
            panic!("Grammar error, node has multiple names");
        }

        self.route.push(name.clone());

        let mut path = self.path.to_string();
        if path.len() > 1 {
            path.push('/');
        }
        path.push_str(&name);
        // println!("node: {:?}", path);
        let ord = self.ords.0[&path];
        self.info = Some(Info { name, path, ord });
    }

    fn path_ref(&self) -> &str {
        &self.info.as_ref().unwrap().path
    }

    fn route_ref(&self) -> &[String] {
        &self.route
    }

    fn ord(&self) -> usize {
        self.info.as_ref().unwrap().ord
    }

    // Turn this into a template for a node, with the properties, labels and children as empty.
    fn into_node(self) -> Node {
        let info = self.info.unwrap();

        Node {
            name: info.name,
            path: info.path,
            route: self.route,
            ord: info.ord,
            labels: Vec::new(),
            properties: Vec::new(),
            children: Vec::new(),
            parent: RefCell::new(None),
        }
    }
}