Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support backrefs in included contexts #288

Merged
merged 5 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,50 @@ contexts:
expect_scope_stacks("\u{1F600}x", &["<source.test>, <test.good>"], syntax);
}

#[test]
fn can_include_backrefs() {
let syntax = SyntaxDefinition::load_from_str(r#"
name: Backref Include Test
scope: source.backrefinc
contexts:
main:
- match: (a)
scope: a
push: context1
context1:
- include: context2
context2:
- match: \1
scope: b
pop: true
"#, true, None).unwrap();

expect_scope_stacks_with_syntax(&"aa", &["<a>", "<b>"], syntax);
}

#[test]
fn can_include_nested_backrefs() {
let syntax = SyntaxDefinition::load_from_str(r#"
name: Backref Include Test
scope: source.backrefinc
contexts:
main:
- match: (a)
scope: a
push: context1
context1:
- include: context3
context3:
- include: context2
context2:
- match: \1
scope: b
pop: true
"#, true, None).unwrap();

expect_scope_stacks_with_syntax(&"aa", &["<a>", "<b>"], syntax);
}

fn expect_scope_stacks(line_without_newline: &str, expect: &[&str], syntax: &str) {
println!("Parsing with newlines");
let line_with_newline = format!("{}\n", line_without_newline);
Expand Down
33 changes: 33 additions & 0 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ impl SyntaxSetBuilder {
syntaxes.push(syntax);
}

let mut found_more_backref_includes = true;
for syntax in &syntaxes {
let mut no_prototype = HashSet::new();
let prototype = syntax.contexts.get("prototype");
Expand All @@ -494,6 +495,38 @@ impl SyntaxSetBuilder {
}
}
Self::link_context(&mut context, syntax, &syntaxes);

if context.uses_backrefs {
found_more_backref_includes = true;
}
}
}

// We need to recursively mark contexts that include contexts which
// use backreferences as using backreferences. In theory we could use
// a more efficient method here like doing a toposort or constructing
// a representation with reversed edges and then tracing in the
// opposite direction, but I benchmarked this and it adds <2% to link
// time on the default syntax set, and linking doesn't even happen
// when loading from a binary dump.
while found_more_backref_includes {
found_more_backref_includes = false;
// find any contexts which include a context which uses backrefs
// and mark those as using backrefs - to support nested includes
for context_index in 0..all_contexts.len() {
let context = &all_contexts[context_index];
if !context.uses_backrefs && context.patterns.iter().any(|pattern| {
match pattern {
Pattern::Include(ContextReference::Direct(id))
if all_contexts[id.index()].uses_backrefs => true,
_ => false,
}
}) {
let mut context = &mut all_contexts[context_index];
context.uses_backrefs = true;
// look for contexts including this context
found_more_backref_includes = true;
}
}
}

Expand Down