forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.rs
More file actions
109 lines (96 loc) · 3.01 KB
/
Copy pathpath.rs
File metadata and controls
109 lines (96 loc) · 3.01 KB
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
use rowan::ast::{support, AstNode};
use super::{ast_node, AstChildren};
use crate::{syntax_node::SyntaxToken, SyntaxKind as SK};
ast_node! {
/// A path.
/// `foo::bar::baz`
pub struct Path,
SK::Path,
IntoIterator<Item=PathSegment>,
}
impl Path {
/// Returns the segments of the path.
pub fn segments(&self) -> AstChildren<PathSegment> {
support::children(self.syntax())
}
}
ast_node! {
/// A path segment.
pub struct PathSegment,
SK::PathSegment
}
impl PathSegment {
pub fn kind(&self) -> Option<PathSegmentKind> {
match self.syntax().first_child_or_token() {
Some(node) => match node.kind() {
SK::IngotKw => Some(PathSegmentKind::Ingot(node.into_token().unwrap())),
SK::SuperKw => Some(PathSegmentKind::Super(node.into_token().unwrap())),
SK::SelfTypeKw => Some(PathSegmentKind::SelfTy(node.into_token().unwrap())),
SK::SelfKw => Some(PathSegmentKind::Self_(node.into_token().unwrap())),
SK::Ident => Some(PathSegmentKind::Ident(node.into_token().unwrap())),
_ => None,
},
_ => None,
}
}
/// Returns the identifier of the segment.
pub fn ident(&self) -> Option<SyntaxToken> {
support::token(self.syntax(), SK::Ident)
}
/// Returns `true` if the segment is a `self` keyword.
pub fn is_self(&self) -> bool {
support::token(self.syntax(), SK::SelfKw).is_some()
}
/// Returns `true` if the segment is a `Self` keyword.
pub fn is_self_ty(&self) -> bool {
support::token(self.syntax(), SK::SelfTypeKw).is_some()
}
}
/// A path segment kind.
pub enum PathSegmentKind {
/// `ingot`
Ingot(SyntaxToken),
/// `super`
Super(SyntaxToken),
/// `Self`
SelfTy(SyntaxToken),
/// `self`
Self_(SyntaxToken),
/// `foo`
Ident(SyntaxToken),
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
lexer::Lexer,
parser::{path::PathScope, Parser},
};
use wasm_bindgen_test::wasm_bindgen_test;
fn parse_path(source: &str) -> Path {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer);
parser.parse(PathScope::default()).unwrap();
Path::cast(parser.finish_to_node().0).unwrap()
}
#[test]
#[wasm_bindgen_test]
fn path_ast() {
let source = r#"self::Foo"#;
let path = parse_path(source);
let mut segments = path.segments();
assert!(segments.next().unwrap().is_self());
assert_eq!(segments.next().unwrap().ident().unwrap().text(), "Foo");
assert!(segments.next().is_none());
}
#[test]
#[wasm_bindgen_test]
fn path_ast2() {
let source = r#"Self::Dep"#;
let path = parse_path(source);
let mut segments = path.segments();
assert!(segments.next().unwrap().is_self_ty());
assert_eq!(segments.next().unwrap().ident().unwrap().text(), "Dep");
assert!(segments.next().is_none());
}
}