Skip to content

Commit 0332bba

Browse files
Improve Objective-C vs Mathematica lexer disambiguation (#2103)
* fix: improve Objective-C vs Mathematica disambiguation for .m files fix: improve Objective-C vs Mathematica disambiguation for .m files The previous logic would incorrectly identify Objective-C code as Mathematica when encountering pointer dereference syntax like `if (*foo == 0)`, since it looked similar to Mathematica's comment syntax `(* comment *)`. This change improves the disambiguation by: 1. Only matching Mathematica comments that start at beginning of line 2. Adding an additional Objective-C identifier for braces at end of line * resolve disambiguation conflict, add more test cases
1 parent d43b445 commit 0332bba

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

lib/rouge/guessers/disambiguation.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,24 @@ def match?(filename)
9090
disambiguate '*.m' do
9191
next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/)
9292
next ObjectiveC if contains?('@"')
93+
94+
# Objective-C dereferenced pointers and Mathematica comments are similar.
95+
# Disambiguate for Mathematica by looking for any amount of whitespace (or no whitespace)
96+
# followed by "(*" (e.g. `(* comment *)`).
97+
next Mathematica if matches?(/^\s*\(\*/)
98+
99+
# Disambiguate for objc by looking for a deref'd pointer in a statement (e.g. `if (*foo == 0)`).
100+
# This pattern is less specific than the Mathematica pattern, so its positioned after it.
101+
next ObjectiveC if matches?(/^\s*(if|while|for|switch|do)\s*\([^)]*\*[^)]*\)/)
93102

94-
next Mathematica if contains?('(*')
95103
next Mathematica if contains?(':=')
96104

97105
next Mason if matches?(/<%(def|method|text|doc|args|flags|attr|init|once|shared|perl|cleanup|filter)([^>]*)(>)/)
98106

99107
next Matlab if matches?(/^\s*?%/)
100-
108+
# Matlab cell array creation: data = {
109+
next Matlab if matches?(/^\s*[a-zA-Z]\w*\s*=\s*\{/)
110+
101111
next Mason if matches? %r!(</?%|<&)!
102112
end
103113

spec/lexers/mathematica_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@
1616
assert_guess :mimetype => 'application/vnd.wolfram.mathematica.package'
1717
assert_guess :mimetype => 'application/vnd.wolfram.wl'
1818
end
19+
20+
it 'guesses by source' do
21+
assert_guess :filename => 'foo.m', :source => '(* Mathematica comment *)'
22+
assert_guess :filename => 'foo.m', :source => 'a := b'
23+
end
1924
end
2025
end

spec/lexers/matlab_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
end
3434
eos
3535
end
36+
37+
it 'guesses by source' do
38+
assert_guess :filename => 'foo.m', :source => '% MATLAB comment'
39+
assert_guess :filename => 'foo.m', :source => 'mycell = {'
40+
end
3641
end
3742
end

spec/lexers/objective_c_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
it 'guesses by source' do
2020
assert_guess :filename => 'foo.h', :source => '@"foo"'
2121
assert_guess :filename => 'foo.h', :source => '@implementation Foo'
22+
assert_guess :filename => 'foo.m', :source => 'if (*bar == 0) {'
23+
assert_guess :filename => 'foo.m', :source => 'while (*ptr != NULL)'
24+
assert_guess :filename => 'foo.m', :source => 'if (*(void **)(addr + 8) == target) {'
2225
end
2326
end
2427
end

0 commit comments

Comments
 (0)