quickbms_lsp/grammar/
parsing.rs1use lsp_types::{Location, Position, Url};
2use tree_sitter::{Language, Parser, Point, Range, Tree};
3
4extern "C" {
5 fn tree_sitter_quickbms() -> Language;
6}
7
8pub fn get_quickbms_language() -> Language {
9 unsafe { tree_sitter_quickbms() }
10}
11
12pub fn parse(text: &str) -> Option<Tree> {
13 let mut parser = Parser::new();
14
15 let language = get_quickbms_language();
16 parser.set_language(language).unwrap();
17
18 parser.parse(text, None)
19}
20
21pub trait PointLike {
22 fn to_point(&self) -> Point;
23 fn to_position(&self) -> Position;
24}
25
26impl PointLike for Point {
27 fn to_point(&self) -> Point {
28 *self
29 }
30
31 fn to_position(&self) -> Position {
32 Position {
33 line: self.row as u32,
34 character: self.column as u32,
35 }
36 }
37}
38
39impl PointLike for Position {
40 fn to_point(&self) -> Point {
41 Point {
42 row: self.line as usize,
43 column: self.character as usize,
44 }
45 }
46
47 fn to_position(&self) -> Position {
48 *self
49 }
50}
51
52pub trait RangeLike {
53 fn to_location(&self, url: &Url) -> Location;
54 fn to_lsp_range(&self) -> lsp_types::Range;
55}
56
57impl RangeLike for Range {
58 fn to_location(&self, url: &Url) -> Location {
59 Location {
60 uri: url.clone(),
61 range: self.to_lsp_range(),
62 }
63 }
64
65 fn to_lsp_range(&self) -> lsp_types::Range {
66 lsp_types::Range::new(self.start_point.to_position(), self.end_point.to_position())
67 }
68}
69
70#[test]
71fn test_parsing() {
72 let tree = parse("set myVar 2");
73
74 assert!(tree.is_some());
75}