blob: 9910390fe7543e4443f4bd46f4fe58e228f6c340 (
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
|
mod ast;
mod finder;
mod lexer;
mod parser;
use self::lexer::BibtexLexer;
use self::parser::BibtexParser;
pub use self::ast::*;
pub use self::finder::*;
use lsp_types::Position;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct BibtexSyntaxTree {
pub root: BibtexRoot,
}
impl BibtexSyntaxTree {
pub fn entries(&self) -> Vec<&BibtexEntry> {
let mut entries: Vec<&BibtexEntry> = Vec::new();
for declaration in &self.root.children {
if let BibtexDeclaration::Entry(entry) = declaration {
entries.push(&entry);
}
}
entries
}
pub fn strings(&self) -> Vec<&BibtexString> {
let mut strings: Vec<&BibtexString> = Vec::new();
for declaration in &self.root.children {
if let BibtexDeclaration::String(string) = declaration {
strings.push(&string);
}
}
strings
}
pub fn find(&self, position: Position) -> Vec<BibtexNode> {
let mut finder = BibtexFinder::new(position);
finder.visit_root(&self.root);
finder.results
}
pub fn find_entry(&self, key: &str) -> Option<&BibtexEntry> {
self.entries()
.into_iter()
.find(|entry| entry.key.as_ref().map(BibtexToken::text) == Some(key))
}
pub fn resolve_crossref(&self, entry: &BibtexEntry) -> Option<&BibtexEntry> {
if let Some(field) = entry.find_field("crossref") {
if let Some(BibtexContent::BracedContent(content)) = &field.content {
if let Some(BibtexContent::Word(name)) = content.children.get(0) {
return self.find_entry(name.token.text());
}
}
}
None
}
}
impl From<BibtexRoot> for BibtexSyntaxTree {
fn from(root: BibtexRoot) -> Self {
BibtexSyntaxTree { root }
}
}
impl From<&str> for BibtexSyntaxTree {
fn from(text: &str) -> Self {
let lexer = BibtexLexer::new(text);
let mut parser = BibtexParser::new(lexer);
parser.root().into()
}
}
|