lib/rouge/lexers/rust.rb in rouge-0.2.10 vs lib/rouge/lexers/rust.rb in rouge-0.2.11
- old
+ new
@@ -31,10 +31,20 @@
Right Send Shl Shr size_t Some ssize_t str Sub Success time_t
u16 u32 u64 u8 uint uintptr_t
)
end
+ def macro_closed?
+ @macro_delims.values.all?(&:zero?)
+ end
+
+ start {
+ @macro_delims = { ']' => 0, ')' => 0, '}' => 0 }
+ }
+
+ delim_map = { '[' => ']', '(' => ')', '{' => '}' }
+
id = /[a-z_]\w*/i
hex = /[0-9a-f]/i
escapes = %r(
\\ ([nrt'\\] | x#{hex}{2} | u#{hex}{4} | U#{hex}{8})
)x
@@ -72,18 +82,19 @@
rule %r([=-]>), 'Keyword'
rule %r(<->), 'Keyword'
rule /[()\[\]{}|,:;]/, 'Punctuation'
rule /[*!@~&+%^<>=-]/, 'Operator'
- rule /[.]?#{id}(?=\s*[(])/m, 'Name.Function'
- rule /[.]#{id}/, 'Name.Property'
+ rule /([.]\s*)?#{id}(?=\s*[(])/m, 'Name.Function'
+ rule /[.]\s*#{id}/, 'Name.Property'
rule /(#{id})(::)/m do
group 'Name.Namespace'; group 'Punctuation'
end
# macros
- rule /#{id}!/, 'Name.Decorator'
+ rule /\bmacro_rules!/, 'Name.Decorator', :macro_rules
+ rule /#{id}!/, 'Name.Decorator', :macro
rule /#{id}/ do |m|
name = m[0]
if self.class.builtins.include? name
token 'Name.Builtin'
@@ -91,14 +102,43 @@
token 'Name'
end
end
end
+ state :macro do
+ mixin :has_literals
+
+ rule /[\[{(]/ do |m|
+ @macro_delims[delim_map[m[0]]] += 1
+ debug { " macro_delims: #{@macro_delims.inspect}" }
+ token 'Punctuation'
+ end
+
+ rule /[\]})]/ do |m|
+ @macro_delims[m[0]] -= 1
+ debug { " macro_delims: #{@macro_delims.inspect}" }
+ pop! if macro_closed?
+ token 'Punctuation'
+ end
+
+ # same as the rule in root, but don't push another macro state
+ rule /#{id}!/, 'Name.Decorator'
+ mixin :root
+
+ # No syntax errors in macros
+ rule /./, 'Text'
+ end
+
+ state :macro_rules do
+ rule /[$]#{id}(:#{id})?/, 'Name.Variable'
+ rule /[$]/, 'Name.Variable'
+
+ mixin :macro
+ end
+
state :has_literals do
# constants
rule /\b(?:true|false|nil)\b/, 'Keyword.Constant'
- # ()
- rule /[(]\s*[)]/, 'Keyword.Constant'
# characters
rule %r(
' (?: #{escapes} | [^\\] ) '
)x, 'Literal.String.Char'