From 79d1efe33c53ffc82e71841a72b7fb82e174f79b Mon Sep 17 00:00:00 2001 From: Vidar Hokstad Date: Fri, 18 Jan 2019 04:39:18 +0000 Subject: [PATCH] Fixes #1009, fixes #879 issues w/C and C++ highlighting 1. void foo(); without space before ";" does not highlight correctly. 2. else if() in certain contexts highlights incorrectly. 3. any error in a function definition or declaration has the potential to cause incorrect highlighting further down in the file. Contains minor changes to Objc-C lexer to fix dependencies on the C lexer that were broken by the C lexer fix. --- lib/rouge/lexers/c.rb | 35 ++++++--------------------------- lib/rouge/lexers/objective_c.rb | 13 +++--------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/lib/rouge/lexers/c.rb b/lib/rouge/lexers/c.rb index 89ac83125b..2d2fc94289 100644 --- a/lib/rouge/lexers/c.rb +++ b/lib/rouge/lexers/c.rb @@ -106,7 +106,7 @@ def self.builtins rule /\d+[lu]*/i, Num::Integer rule %r(\*/), Error rule %r([~!%^&*+=\|?:<>/-]), Operator - rule /[()\[\],.]/, Punctuation + rule /[()\[\],.;]/, Punctuation rule /\bcase\b/, Keyword, :case rule /(?:true|false|NULL)\b/, Name::Builtin rule id do |m| @@ -133,13 +133,11 @@ def self.builtins state :root do mixin :expr_whitespace - - # functions rule %r( ([\w*\s]+?[\s*]) # return arguments (#{id}) # function name (\s*\([^;]*?\)) # signature - (#{ws})({) # open brace + (#{ws}?)({|;) # open brace or semicolon )mx do |m| # TODO: do this better. recurse m[1] @@ -147,33 +145,12 @@ def self.builtins recurse m[3] recurse m[4] token Punctuation, m[5] - push :function - end - - # function declarations - rule %r( - ([\w*\s]+?[\s*]) # return arguments - (#{id}) # function name - (\s*\([^;]*?\)) # signature - (#{ws})(;) # semicolon - )mx do |m| - # TODO: do this better. - recurse m[1] - token Name::Function, m[2] - recurse m[3] - recurse m[4] - token Punctuation, m[5] - push :statement + if m[5] == ?{ + push :function + end end - - rule(//) { push :statement } - end - - state :statement do - rule /;/, Punctuation, :pop! - mixin :expr_whitespace + rule /\{/, Punctuation, :function mixin :statements - rule /[{}]/, Punctuation end state :function do diff --git a/lib/rouge/lexers/objective_c.rb b/lib/rouge/lexers/objective_c.rb index 63f23351d2..4eb82b2a8b 100644 --- a/lib/rouge/lexers/objective_c.rb +++ b/lib/rouge/lexers/objective_c.rb @@ -44,15 +44,8 @@ def self.builtins rule /@\d+l?/, Num::Integer rule /\bin\b/, Keyword - rule /@(?:interface|implementation)\b/ do - token Keyword - goto :classname - end - - rule /@(?:class|protocol)\b/ do - token Keyword - goto :forward_classname - end + rule /@(?:interface|implementation)\b/, Keyword, :classname + rule /@(?:class|protocol)\b/, Keyword, :forward_classname rule /@([[:alnum:]]+)/ do |m| if self.class.at_keywords.include? m[1] @@ -80,7 +73,7 @@ def self.builtins rule /\{/, Punctuation, :pop! rule /;/, Error - mixin :statement + mixin :statements end state :message do