Skip to content

Commit

Permalink
support backrefs in included contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
keith-hall committed Apr 14, 2020
1 parent d52e9f1 commit 3f66649
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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;
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

0 comments on commit 3f66649

Please sign in to comment.