Move to upstream macro_rules! model

This commit is contained in:
Jonas Schievink 2020-12-15 15:37:37 +01:00
parent 39aae835fd
commit c1cb595382
36 changed files with 325 additions and 275 deletions

View file

@ -150,13 +150,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
};
Some(node)
},
ast::MacroCall(it) => {
match it.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) {
Some(path_segment) if path_segment.text() == "macro_rules"
=> decl(it),
_ => None,
}
},
ast::MacroRules(it) => decl(it),
_ => None,
}
}
@ -380,7 +374,7 @@ fn very_obsolete() {}
label: "mc",
navigation_range: 284..286,
node_range: 271..303,
kind: MACRO_CALL,
kind: MACRO_RULES,
detail: None,
deprecated: false,
},
@ -389,7 +383,7 @@ fn very_obsolete() {}
label: "mcexp",
navigation_range: 334..339,
node_range: 305..356,
kind: MACRO_CALL,
kind: MACRO_RULES,
detail: None,
deprecated: false,
},
@ -398,7 +392,7 @@ fn very_obsolete() {}
label: "mcexp",
navigation_range: 387..392,
node_range: 358..409,
kind: MACRO_CALL,
kind: MACRO_RULES,
detail: None,
deprecated: false,
},

View file

@ -770,7 +770,7 @@ fn foo() {
}
"#,
expect![[r#"
m1 MACRO_CALL FileId(0) 0..46 29..31 Other
m1 MACRO_RULES FileId(0) 0..46 29..31 Other
FileId(0) 63..65 StructLiteral
FileId(0) 73..75 StructLiteral

View file

@ -74,6 +74,7 @@ pub(crate) fn highlight(
let mut stack = HighlightedRangeStack::new();
let mut current_macro_call: Option<ast::MacroCall> = None;
let mut current_macro_rules: Option<ast::MacroRules> = None;
let mut format_string_highlighter = FormatStringHighlighter::default();
let mut macro_rules_highlighter = MacroRulesHighlighter::default();
let mut inside_attribute = false;
@ -106,28 +107,26 @@ pub(crate) fn highlight(
binding_hash: None,
});
}
if let Some(name) = mc.is_macro_rules() {
macro_rules_highlighter.init();
if let Some((highlight, binding_hash)) = highlight_element(
&sema,
&mut bindings_shadow_count,
syntactic_name_ref_highlighting,
name.syntax().clone().into(),
) {
stack.add(HighlightedRange {
range: name.syntax().text_range(),
highlight,
binding_hash,
});
}
}
current_macro_call = Some(mc.clone());
continue;
}
WalkEvent::Leave(Some(mc)) => {
assert!(current_macro_call == Some(mc));
assert_eq!(current_macro_call, Some(mc));
current_macro_call = None;
format_string_highlighter = FormatStringHighlighter::default();
}
_ => (),
}
match event.clone().map(|it| it.into_node().and_then(ast::MacroRules::cast)) {
WalkEvent::Enter(Some(mac)) => {
macro_rules_highlighter.init();
current_macro_rules = Some(mac);
continue;
}
WalkEvent::Leave(Some(mac)) => {
assert_eq!(current_macro_rules, Some(mac));
current_macro_rules = None;
macro_rules_highlighter = MacroRulesHighlighter::default();
}
_ => (),
@ -163,6 +162,12 @@ pub(crate) fn highlight(
let range = element.text_range();
if current_macro_rules.is_some() {
if let Some(tok) = element.as_token() {
macro_rules_highlighter.advance(tok);
}
}
let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT {
// Inside a macro -- expand it first
let token = match element.clone().into_token() {
@ -173,9 +178,6 @@ pub(crate) fn highlight(
let parent = token.parent();
format_string_highlighter.check_for_format_string(&parent);
if let Some(tok) = element.as_token() {
macro_rules_highlighter.advance(tok);
}
// We only care Name and Name_ref
match (token.kind(), parent.kind()) {
@ -386,10 +388,14 @@ impl HighlightedRangeStack {
let mut res = self.stack.pop().unwrap();
res.sort_by_key(|range| range.range.start());
// Check that ranges are sorted and disjoint
assert!(res
.iter()
.zip(res.iter().skip(1))
.all(|(left, right)| left.range.end() <= right.range.start()));
for (left, right) in res.iter().zip(res.iter().skip(1)) {
assert!(
left.range.end() <= right.range.start(),
"left: {:#?}, right: {:#?}",
left,
right
);
}
res
}
}

View file

@ -95,7 +95,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="comment documentation">/// ```</span>
<span class="comment documentation">/// </span><span class="macro injected">noop!</span><span class="punctuation injected">(</span><span class="numeric_literal injected">1</span><span class="punctuation injected">)</span><span class="punctuation injected">;</span><span class="punctuation injected">
</span><span class="comment documentation">/// ```</span>
<span class="macro">macro_rules!</span> <span class="macro declaration">noop</span> <span class="punctuation">{</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">noop</span> <span class="punctuation">{</span>
<span class="punctuation">(</span><span class="punctuation">$</span>expr<span class="punctuation">:</span>expr<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">{</span>
<span class="punctuation">$</span>expr
<span class="punctuation">}</span>

View file

@ -35,13 +35,13 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
</style>
<pre><code><span class="macro">macro_rules!</span> <span class="macro declaration">println</span> <span class="punctuation">{</span>
<pre><code><span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">println</span> <span class="punctuation">{</span>
<span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>arg<span class="punctuation">:</span>tt<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">(</span><span class="punctuation">{</span>
<span class="punctuation">$</span>crate<span class="punctuation">:</span><span class="punctuation">:</span>io<span class="punctuation">:</span><span class="punctuation">:</span>_print<span class="punctuation">(</span><span class="punctuation">$</span>crate<span class="punctuation">:</span><span class="punctuation">:</span>format_args_nl<span class="punctuation">!</span><span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>arg<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span><span class="punctuation">)</span><span class="punctuation">;</span>
<span class="punctuation">}</span><span class="punctuation">)</span>
<span class="punctuation">}</span>
#[rustc_builtin_macro]
<span class="macro">macro_rules!</span> <span class="macro declaration">format_args_nl</span> <span class="punctuation">{</span>
<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="function attribute">rustc_builtin_macro</span><span class="attribute attribute">]</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">format_args_nl</span> <span class="punctuation">{</span>
<span class="punctuation">(</span><span class="punctuation">$</span>fmt<span class="punctuation">:</span>expr<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">{</span><span class="punctuation">{</span> <span class="comment">/* compiler built-in */</span> <span class="punctuation">}</span><span class="punctuation">}</span><span class="punctuation">;</span>
<span class="punctuation">(</span><span class="punctuation">$</span>fmt<span class="punctuation">:</span>expr<span class="punctuation">,</span> <span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>args<span class="punctuation">:</span>tt<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">{</span><span class="punctuation">{</span> <span class="comment">/* compiler built-in */</span> <span class="punctuation">}</span><span class="punctuation">}</span><span class="punctuation">;</span>
<span class="punctuation">}</span>

View file

@ -125,7 +125,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">let</span> <span class="variable declaration">bar</span> <span class="operator">=</span> <span class="function">foobar</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
<span class="punctuation">}</span>
<span class="macro">macro_rules!</span> <span class="macro declaration">def_fn</span> <span class="punctuation">{</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">def_fn</span> <span class="punctuation">{</span>
<span class="punctuation">(</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>tt<span class="punctuation">:</span>tt<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">{</span><span class="punctuation">$</span><span class="punctuation">(</span><span class="punctuation">$</span>tt<span class="punctuation">)</span><span class="punctuation">*</span><span class="punctuation">}</span>
<span class="punctuation">}</span>
@ -135,13 +135,13 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="macro">macro_rules!</span> <span class="macro declaration">noop</span> <span class="punctuation">{</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">noop</span> <span class="punctuation">{</span>
<span class="punctuation">(</span><span class="punctuation">$</span>expr<span class="punctuation">:</span>expr<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">{</span>
<span class="punctuation">$</span>expr
<span class="punctuation">}</span>
<span class="punctuation">}</span>
<span class="macro">macro_rules!</span> <span class="macro declaration">keyword_frag</span> <span class="punctuation">{</span>
<span class="keyword">macro_rules</span><span class="punctuation">!</span> <span class="macro declaration">keyword_frag</span> <span class="punctuation">{</span>
<span class="punctuation">(</span><span class="punctuation">$</span>type<span class="punctuation">:</span>ty<span class="punctuation">)</span> <span class="operator">=</span><span class="punctuation">&gt;</span> <span class="punctuation">(</span><span class="punctuation">$</span>type<span class="punctuation">)</span>
<span class="punctuation">}</span>