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 1 commit
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
21 changes: 21 additions & 0 deletions src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,27 @@ 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);
}

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
26 changes: 26 additions & 0 deletions src/parsing/syntax_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,32 @@ impl SyntaxSetBuilder {
}
Self::link_context(&mut context, syntax, &syntaxes);
}

for context_id in syntax.contexts.values() {
let index = context_id.index();
let context = &all_contexts[index];
if !context.uses_backrefs {
let mut uses_backrefs = false;
keith-hall marked this conversation as resolved.
Show resolved Hide resolved
for pattern in &context.patterns {
match *pattern {
Pattern::Include(ref context_ref) => {
match context_ref {
ContextReference::Direct(ref id) => {
if (&all_contexts[id.index()]).uses_backrefs {
uses_backrefs = true;
break;
}
}, _ => {},
}
}, _ => {},
}
}
if uses_backrefs {
let mut context = &mut all_contexts[index];
context.uses_backrefs = true;
}
}
}
}

#[cfg(feature = "metadata")]
Expand Down