forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
126 lines (113 loc) · 3.96 KB
/
Copy pathlib.rs
File metadata and controls
126 lines (113 loc) · 3.96 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
pub mod ast;
pub mod lexer;
pub mod parser;
pub mod syntax_kind;
pub mod syntax_node;
pub use parser::RecoveryMode;
pub use rowan::TextSize;
use smallvec::SmallVec;
pub use syntax_kind::SyntaxKind;
pub use syntax_node::{FeLang, GreenNode, NodeOrToken, SyntaxNode, SyntaxToken, TextRange};
use parser::RootScope;
pub fn parse_source_file(text: &str, recovery_mode: RecoveryMode) -> (GreenNode, Vec<ParseError>) {
let lexer = lexer::Lexer::new(text);
let mut parser = parser::Parser::new(lexer, recovery_mode);
let checkpoint = parser.enter(RootScope::default(), None);
let _ = parser.parse(parser::ItemListScope::default());
parser.leave(checkpoint);
let (node, errs) = parser.finish();
(node, errs)
}
/// An parse error which is accumulated in the [`parser::Parser`] while parsing.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ParseError {
Expected(SmallVec<SyntaxKind, 2>, ExpectedKind, TextSize),
Unexpected(String, TextRange),
Msg(String, TextRange),
}
impl ParseError {
pub(crate) fn expected(
tokens: &[SyntaxKind],
kind: Option<ExpectedKind>,
pos: TextSize,
) -> Self {
let mut expected_tokens = SmallVec::<SyntaxKind, 2>::new();
expected_tokens.extend_from_slice(tokens);
ParseError::Expected(
expected_tokens,
kind.unwrap_or(ExpectedKind::Unspecified),
pos,
)
}
pub fn msg(&self) -> String {
match self {
ParseError::Expected(_, exp, _) => match exp {
ExpectedKind::Body(kind) => format!("{} requires a body", kind.describe()),
ExpectedKind::Name(kind) => format!("expected name for {}", kind.describe()),
ExpectedKind::ClosingBracket { bracket, parent } => format!(
"missing closing {} for {}",
bracket.describe(),
parent.describe()
),
ExpectedKind::Separator { separator, element } => {
format!(
"expected {} separator after {}",
separator.describe(),
element.describe()
)
}
ExpectedKind::TypeSpecifier(kind) => {
format!("missing type bound for {}", kind.describe())
}
ExpectedKind::Syntax(kind) => format!("expected {}", kind.describe()),
ExpectedKind::Unspecified => self.label(),
},
ParseError::Unexpected(m, _) => m.clone(),
ParseError::Msg(m, _) => m.clone(),
}
}
pub fn label(&self) -> String {
match self {
ParseError::Expected(tokens, _, _) => {
if tokens.len() == 1 {
return format!("expected {}", tokens[0].describe());
}
let mut s = "expected ".to_string();
let mut delim = "";
for (i, t) in tokens.iter().enumerate() {
s.push_str(delim);
s.push_str(t.describe());
delim = if i + 2 == tokens.len() { " or " } else { ", " };
}
s
}
ParseError::Unexpected(_, _) => "unexpected".into(),
ParseError::Msg(msg, _) => msg.clone(),
}
}
pub fn range(&self) -> TextRange {
match self {
ParseError::Expected(_, _, pos) => TextRange::empty(*pos),
ParseError::Unexpected(_, r) | ParseError::Msg(_, r) => *r,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExpectedKind {
Body(SyntaxKind),
Name(SyntaxKind),
ClosingBracket {
bracket: SyntaxKind,
parent: SyntaxKind,
},
TypeSpecifier(SyntaxKind),
Separator {
separator: SyntaxKind,
element: SyntaxKind,
},
Syntax(SyntaxKind),
Unspecified,
// TODO:
// - newline after attribute in attrlistscope
//
}