Skip to content

Commit

Permalink
Fix various issues with highlighting in C and C++ lexers (#1069)
Browse files Browse the repository at this point in the history
Before this commit:

1. `void foo();` without a space before ";" would not highlight
   correctly;
2. `else if()` would not highlight correctly in certain contexts;
3. an error in a function definition or declaration had the potential
   to cause incorrect highlighting further down in the file.

The commit also contains minor changes to the Objective-C lexer to fix
regressions caused by dependencies on the broken behaviour of the C
lexer that has been fixed.

This fixes #879 and #1009.
  • Loading branch information
vidarh authored and pyrmont committed May 28, 2019
1 parent c1c5d0b commit 95134f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 42 deletions.
35 changes: 6 additions & 29 deletions lib/rouge/lexers/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -133,47 +133,24 @@ 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]
token Name::Function, m[2]
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
Expand Down
13 changes: 3 additions & 10 deletions lib/rouge/lexers/objective_c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -80,7 +73,7 @@ def self.builtins
rule /\{/, Punctuation, :pop!
rule /;/, Error

mixin :statement
mixin :statements
end

state :message do
Expand Down
11 changes: 8 additions & 3 deletions spec/visual/samples/c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ static int not_a_comment();

#macro


/* Bug #277: C function prototype syntax highlighting broken if space before semicolon */

void foo() ;

void foo() {
Expand All @@ -45,6 +42,14 @@ void foo2(void)
/* nothing */
}

/* Broken declarations should not break subsequent highlighting */

void foo() {
if(x) {
} else if(y) {
puts("foo")
}
}

/* Execute compiled code */

Expand Down

0 comments on commit 95134f8

Please sign in to comment.