lib/rouge/lexers/rust.rb in rouge-3.6.0 vs lib/rouge/lexers/rust.rb in rouge-3.7.0
- old
+ new
@@ -20,11 +20,11 @@
return true if text.shebang? 'rustc'
end
def self.keywords
@keywords ||= %w(
- as assert break const copy do drop else enum extern fail false
+ as assert async await break const copy do drop else enum extern fail false
fn for if impl let log loop match mod move mut priv pub pure
ref return self static struct true trait type unsafe use where
while box
)
end
@@ -47,10 +47,11 @@
@macro_delims.values.all?(&:zero?)
end
start {
@macro_delims = { ']' => 0, ')' => 0, '}' => 0 }
+ push :bol
}
delim_map = { '[' => ']', '(' => ')', '{' => '}' }
id = /[a-z_]\w*/i
@@ -58,24 +59,25 @@
escapes = %r(
\\ ([nrt'"\\0] | x#{hex}{2} | u#{hex}{4} | U#{hex}{8})
)x
size = /8|16|32|64/
- state :start_line do
+ # Although not officially part of Rust, the rustdoc tool allows code in
+ # comments to begin with `#`. Code like this will be evaluated but not
+ # included in the HTML output produced by rustdoc. So that code intended
+ # for these comments can be higlighted with Rouge, the Rust lexer needs
+ # to check if the beginning of the line begins with a `# `.
+ state :bol do
mixin :whitespace
- rule %r/\s+/, Text
- rule %r/#\[/ do
- token Name::Decorator; push :attribute
- end
+ rule %r/#\s[^\n]*/, Comment::Special
rule(//) { pop! }
- rule %r/#\s[^\n]*/, Comment::Preproc
end
state :attribute do
mixin :whitespace
mixin :has_literals
- rule %r/[(,)=]/, Name::Decorator
+ rule %r/[(,)=:]/, Name::Decorator
rule %r/\]/, Name::Decorator, :pop!
rule id, Name::Decorator
end
state :whitespace do
@@ -83,11 +85,12 @@
rule %r(//[^\n]*), Comment
rule %r(/[*].*?[*]/)m, Comment::Multiline
end
state :root do
- rule %r/\n/, Text, :start_line
+ rule %r/\n/, Text, :bol
mixin :whitespace
+ rule %r/#!?\[/, Name::Decorator, :attribute
rule %r/\b(?:#{Rust.keywords.join('|')})\b/, Keyword
mixin :has_literals
rule %r([=-]>), Keyword
rule %r(<->), Keyword