aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: f9a6a26b42306d23ffb0af1647cf4f3ecef41cf0 (plain)
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
mod utils;

use js_sys::Array;

use syntax::{
    ast::{self, AstNode},
    NodeOrToken, SourceFile, SyntaxElement, SyntaxError, SyntaxNode,
};
use wasm_bindgen::prelude::*;

use std::{convert::From, str::FromStr};

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub fn put_cst(source: &str) -> String {
    let source_file = SourceFile::parse(source);
    source_file.debug_dump()
}

// wrapper type to pass syntax elements to JS
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub struct SynNode {
    node: SyntaxElement,
}

impl SynNode {
    pub fn new(node: SyntaxElement) -> Self {
        Self { node }
    }
}

#[wasm_bindgen]
impl SynNode {
    pub fn children(&self) -> Vec<JsValue> {
        match &self.node {
            NodeOrToken::Node(n) => n
                .children_with_tokens()
                .map(SynNode::new)
                .map(JsValue::from)
                .collect(),
            NodeOrToken::Token(_) => vec![],
        }
    }

    pub fn is_token(&self) -> bool {
        self.node.as_token().is_some()
    }

    pub fn to_string(&self) -> String {
        format!("{:?}@{:?}", self.node.kind(), self.node.text_range(),)
    }

    pub fn from_str(s: &str) -> Result<JsValue, JsValue> {
        FromStr::from_str(s)
            .map(|p: SynNode| JsValue::from(p))
            .map_err(JsValue::from)
    }
}

impl FromStr for SynNode {
    type Err = Array;
    fn from_str(s: &str) -> Result<Self, Array> {
        let source_file = SourceFile::parse(s);
        if source_file.errors().is_empty() {
            Ok(Self {
                node: NodeOrToken::Node(source_file.ok().unwrap().syntax().clone()),
            })
        } else {
            Err(source_file
                .errors()
                .into_iter()
                .map(SynErr::new)
                .map(JsValue::from)
                .collect())
        }
    }
}

#[wasm_bindgen]
#[derive(Debug, Clone)]
struct SynErr {
    err: SyntaxError,
}

impl SynErr {
    pub fn new(err: &SyntaxError) -> Self {
        Self { err: err.clone() }
    }
}

#[wasm_bindgen]
struct TextRange {
    start: u32,
    end: u32,
}

impl From<(u32, u32)> for TextRange {
    fn from((start, end): (u32, u32)) -> Self {
        TextRange { start, end }
    }
}

#[wasm_bindgen]
impl SynErr {
    pub fn range(&self) -> TextRange {
        let r = self.err.range();
        (r.start().into(), r.end().into()).into()
    }
    pub fn to_string(&self) -> String {
        self.err.to_string()
    }
}