Skip to content

Commit

Permalink
Paralellize creation of LaTeX symbol table
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed Mar 3, 2020
1 parent ecd5cdd commit 4930c48
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lsp-types = { version = "0.61.0", features = ["proposed"] }
nom = "5.1"
once_cell = "1.3"
petgraph = { version = "0.5", features = ["serde-1"] }
rayon = "1.3"
relative-path = { version = "1.0", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
85 changes: 53 additions & 32 deletions src/syntax/latex/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,49 +63,70 @@ impl SymbolTable {
cwd,
};

let environments = Environment::parse(ctx);
let is_standalone = environments.iter().any(|env| env.is_root(&tree));
let mut environments = None;
let mut includes = None;
let mut citations = None;
let mut command_definitions = None;
let mut glossary_entries = None;
let mut equations = None;
let mut inlines = None;
let mut math_operators = None;
let mut theorem_definitions = None;
let mut sections = None;
let mut labels = None;
let mut label_numberings = None;
let mut captions = None;
let mut items = None;

rayon::scope(|s| {
s.spawn(|_| environments = Some(Environment::parse(ctx)));
s.spawn(|_| includes = Some(Include::parse(ctx)));
s.spawn(|_| citations = Some(Citation::parse(ctx)));
s.spawn(|_| command_definitions = Some(CommandDefinition::parse(ctx)));
s.spawn(|_| glossary_entries = Some(GlossaryEntry::parse(ctx)));
s.spawn(|_| equations = Some(Equation::parse(ctx)));
s.spawn(|_| inlines = Some(Inline::parse(ctx)));
s.spawn(|_| math_operators = Some(MathOperator::parse(ctx)));
s.spawn(|_| theorem_definitions = Some(TheoremDefinition::parse(ctx)));
s.spawn(|_| sections = Some(Section::parse(ctx)));
s.spawn(|_| labels = Some(Label::parse(ctx)));
s.spawn(|_| label_numberings = Some(LabelNumbering::parse(ctx)));
s.spawn(|_| captions = Some(Caption::parse(ctx)));
s.spawn(|_| items = Some(Item::parse(ctx)));
});

let is_standalone = environments
.as_ref()
.unwrap()
.iter()
.any(|env| env.is_root(&tree));

let includes = Include::parse(ctx);
let components = includes
.as_ref()
.unwrap()
.iter()
.flat_map(|include| include.components(&tree))
.collect();

let citations = Citation::parse(ctx);
let command_definitions = CommandDefinition::parse(ctx);
let glossary_entries = GlossaryEntry::parse(ctx);

let equations = Equation::parse(ctx);
let inlines = Inline::parse(ctx);
let math_operators = MathOperator::parse(ctx);
let theorem_definitions = TheoremDefinition::parse(ctx);

let sections = Section::parse(ctx);
let labels = Label::parse(ctx);
let label_numberings = LabelNumbering::parse(ctx);
let captions = Caption::parse(ctx);
let items = Item::parse(ctx);

Self {
tree,
commands,
environments,
environments: environments.unwrap(),
is_standalone,
includes,
includes: includes.unwrap(),
components,
citations,
command_definitions,
glossary_entries,
equations,
inlines,
math_operators,
theorem_definitions,
sections,
labels,
label_numberings,
captions,
items,
citations: citations.unwrap(),
command_definitions: command_definitions.unwrap(),
glossary_entries: glossary_entries.unwrap(),
equations: equations.unwrap(),
inlines: inlines.unwrap(),
math_operators: math_operators.unwrap(),
theorem_definitions: theorem_definitions.unwrap(),
sections: sections.unwrap(),
labels: labels.unwrap(),
label_numberings: label_numberings.unwrap(),
captions: captions.unwrap(),
items: items.unwrap(),
}
}
}
Expand Down
24 changes: 9 additions & 15 deletions src/syntax/latex/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Tree {
pub fn children(&self, parent: NodeIndex) -> impl Iterator<Item = NodeIndex> {
self.graph
.neighbors(parent)
.sorted_by_key(|child| self.graph.node_weight(*child).unwrap().start())
.sorted_by_key(|child| self.graph[*child].start())
}

pub fn walk<V: Visitor>(&self, visitor: &mut V, parent: NodeIndex) {
Expand All @@ -179,7 +179,7 @@ impl Tree {
}

pub fn print(&self, node: NodeIndex) -> String {
let start_position = self.graph.node_weight(node).unwrap().start();
let start_position = self.graph[node].start();
let mut printer = Printer::new(start_position);
printer.visit(self, node);
printer.output
Expand All @@ -193,31 +193,31 @@ impl Tree {
}

pub fn as_group(&self, node: NodeIndex) -> Option<&Group> {
if let Node::Group(group) = self.graph.node_weight(node)? {
if let Node::Group(group) = &self.graph[node] {
Some(group)
} else {
None
}
}

pub fn as_command(&self, node: NodeIndex) -> Option<&Command> {
if let Node::Command(cmd) = self.graph.node_weight(node)? {
if let Node::Command(cmd) = &self.graph[node] {
Some(cmd)
} else {
None
}
}

pub fn as_text(&self, node: NodeIndex) -> Option<&Text> {
if let Node::Text(text) = self.graph.node_weight(node)? {
if let Node::Text(text) = &self.graph[node] {
Some(text)
} else {
None
}
}

pub fn as_math(&self, node: NodeIndex) -> Option<&Math> {
if let Node::Math(math) = self.graph.node_weight(node)? {
if let Node::Math(math) = &self.graph[node] {
Some(math)
} else {
None
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Tree {
let group = self.extract_group(parent, group_kind, index)?;
let mut words = Vec::new();
for child in self.children(group) {
match self.graph.node_weight(child)? {
match &self.graph[child] {
Node::Root(_) | Node::Group(_) | Node::Command(_) | Node::Math(_) => return None,
Node::Text(text) => {
for word in &text.words {
Expand Down Expand Up @@ -313,13 +313,7 @@ impl Finder {

impl Visitor for Finder {
fn visit(&mut self, tree: &Tree, node: NodeIndex) {
if tree
.graph
.node_weight(node)
.unwrap()
.range()
.contains(self.position)
{
if tree.graph[node].range().contains(self.position) {
self.results.push(node);
tree.walk(self, node);
}
Expand Down Expand Up @@ -365,7 +359,7 @@ impl Printer {

impl Visitor for Printer {
fn visit(&mut self, tree: &Tree, node: NodeIndex) {
match tree.graph.node_weight(node).unwrap() {
match &tree.graph[node] {
Node::Root(_) => tree.walk(self, node),
Node::Group(group) => {
self.print_token(&group.left);
Expand Down

0 comments on commit 4930c48

Please sign in to comment.