Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rust: Include starting apostrophe in lifetime / loop label token (#739)
  • Loading branch information
CosmicHorrorDev committed Jan 29, 2023
1 parent 633f8e0 commit 0ea6503
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lexers/embedded/rust.xml
Expand Up @@ -23,9 +23,6 @@
</rule>
</state>
<state name="lifetime">
<rule pattern="(static|_)">
<token type="NameBuiltin"/>
</rule>
<rule pattern="[a-zA-Z_]+\w*">
<token type="NameAttribute"/>
</rule>
Expand Down Expand Up @@ -281,8 +278,11 @@
<token type="LiteralString"/>
</bygroups>
</rule>
<rule pattern="&#39;(static|_)">
<token type="NameBuiltin"/>
</rule>
<rule pattern="&#39;">
<token type="Operator"/>
<token type="NameAttribute"/>
<push state="lifetime"/>
</rule>
<rule pattern="\.\.=?">
Expand Down Expand Up @@ -315,8 +315,11 @@
<rule pattern="&amp;">
<token type="KeywordPseudo"/>
</rule>
<rule pattern="&#39;(static|_)">
<token type="NameBuiltin"/>
</rule>
<rule pattern="&#39;">
<token type="Operator"/>
<token type="NameAttribute"/>
<push state="lifetime"/>
</rule>
<rule pattern="(DoubleEndedIterator|ExactSizeIterator|IntoIterator|PartialOrd|PartialEq|ToString|Iterator|ToOwned|Default|Result|String|FnOnce|Extend|Option|FnMut|Unpin|Sized|AsRef|AsMut|Clone|None|From|Into|Sync|drop|Send|Drop|Copy|Some|Ord|Err|Box|Vec|Eq|Ok|Fn)\b">
Expand Down
16 changes: 16 additions & 0 deletions lexers/testdata/rust/lifetimes_and_loop_labels.actual
@@ -0,0 +1,16 @@
struct Bar<'b>(&'b str);

struct Foo<'b, 'a: 'b>(&'a Bar<'b>);

fn foo<'some_name>(_: Bar<'_>, _: impl Iterator<Item = Bar<'static>> + 'static) {
'im_a_loop_label: loop {
break 'im_a_loop_label;
}

// None of these should have lifetimes
let _ = 'a';
let _ = b'a';
let _ = "
'not_a_lifetime_or_label
";
}
117 changes: 117 additions & 0 deletions lexers/testdata/rust/lifetimes_and_loop_labels.expected
@@ -0,0 +1,117 @@
[
{"type":"Keyword","value":"struct"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Bar"},
{"type":"Operator","value":"\u003c"},
{"type":"NameAttribute","value":"'b"},
{"type":"Operator","value":"\u003e"},
{"type":"Punctuation","value":"("},
{"type":"Operator","value":"\u0026"},
{"type":"NameAttribute","value":"'b"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordType","value":"str"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Keyword","value":"struct"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Foo"},
{"type":"Operator","value":"\u003c"},
{"type":"NameAttribute","value":"'b"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"NameAttribute","value":"'a"},
{"type":"Text","value":": "},
{"type":"NameAttribute","value":"'b"},
{"type":"Operator","value":"\u003e"},
{"type":"Punctuation","value":"("},
{"type":"Operator","value":"\u0026"},
{"type":"NameAttribute","value":"'a"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"Bar"},
{"type":"Operator","value":"\u003c"},
{"type":"NameAttribute","value":"'b"},
{"type":"Operator","value":"\u003e"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Keyword","value":"fn"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"foo"},
{"type":"Operator","value":"\u003c"},
{"type":"NameAttribute","value":"'some_name"},
{"type":"Operator","value":"\u003e"},
{"type":"Punctuation","value":"("},
{"type":"Name","value":"_"},
{"type":"Text","value":": "},
{"type":"NameClass","value":"Bar"},
{"type":"Operator","value":"\u003c"},
{"type":"NameBuiltin","value":"'_"},
{"type":"Operator","value":"\u003e"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"_"},
{"type":"Text","value":": "},
{"type":"NameClass","value":"impl"},
{"type":"TextWhitespace","value":" "},
{"type":"NameBuiltin","value":"Iterator"},
{"type":"Operator","value":"\u003c"},
{"type":"Name","value":"Item"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"Bar"},
{"type":"Operator","value":"\u003c"},
{"type":"NameBuiltin","value":"'static"},
{"type":"Operator","value":"\u003e\u003e"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"+"},
{"type":"TextWhitespace","value":" "},
{"type":"NameBuiltin","value":"'static"},
{"type":"Punctuation","value":")"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameAttribute","value":"'im_a_loop_label"},
{"type":"Text","value":": "},
{"type":"NameClass","value":"loop"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"break"},
{"type":"TextWhitespace","value":" "},
{"type":"NameLabel","value":"'im_a_loop_label"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"CommentSingle","value":"// None of these should have lifetimes\n"},
{"type":"TextWhitespace","value":" "},
{"type":"KeywordDeclaration","value":"let"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"_"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralStringChar","value":"'a'"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"let"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"_"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Error","value":"b'a'"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"let"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"_"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralString","value":"\"\n 'not_a_lifetime_or_label\n \""},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n"}
]
File renamed without changes.
File renamed without changes.

0 comments on commit 0ea6503

Please sign in to comment.