yggdrasil_wasi/syntax_node/
mod.rs

1use crate::exports::peg::core::cst::SnytaxFlags;
2use rctree::Node;
3use std::{
4    fmt::{Debug, Formatter},
5    ops::Range,
6    rc::Rc,
7};
8use yggdrasil_rt::{TokenPair, YggdrasilRule};
9
10pub struct NativeLanguage {
11    pub name: &'static str,
12    pub glob: &'static [&'static str],
13}
14
15pub struct NativeSyntaxRule {
16    pub language: NativeLanguage,
17    pub flags: SnytaxFlags,
18    pub tag: &'static str,
19    pub name: &'static str,
20    pub styles: &'static [&'static str],
21}
22
23pub struct NativeSyntaxData {
24    pub rule: &'static str,
25    pub text: Rc<str>,
26    pub span: Range<usize>,
27}
28
29impl Debug for NativeSyntaxData {
30    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31        f.debug_struct("SyntaxNode")
32            // .field("language", &self.language)
33            .field("rule", &self.rule)
34            .field("text", &&self.text[self.span.clone()])
35            .finish()
36    }
37}
38
39impl NativeSyntaxData {
40    pub fn new<R: YggdrasilRule>(input: Rc<str>, pair: TokenPair<R>) -> Node<NativeSyntaxData> {
41        let rule = pair.get_rule();
42        let parent = Node::new(NativeSyntaxData {
43            // language: "json5",
44            rule: rule.get_name(),
45            text: input.clone(),
46            span: pair.get_span().get_range(),
47        });
48        for child in pair.into_inner() {
49            parent.append(NativeSyntaxData::new(input.clone(), child))
50        }
51        parent
52    }
53}