diff --git a/lib/rouge/guessers/disambiguation.rb b/lib/rouge/guessers/disambiguation.rb index 6f2cfd5d91..202eb0e4e4 100644 --- a/lib/rouge/guessers/disambiguation.rb +++ b/lib/rouge/guessers/disambiguation.rb @@ -90,14 +90,24 @@ def match?(filename) disambiguate '*.m' do next ObjectiveC if matches?(/@(end|implementation|protocol|property)\b/) next ObjectiveC if contains?('@"') + + # Objective-C dereferenced pointers and Mathematica comments are similar. + # Disambiguate for Mathematica by looking for any amount of whitespace (or no whitespace) + # followed by "(*" (e.g. `(* comment *)`). + next Mathematica if matches?(/^\s*\(\*/) + + # Disambiguate for objc by looking for a deref'd pointer in a statement (e.g. `if (*foo == 0)`). + # This pattern is less specific than the Mathematica pattern, so its positioned after it. + next ObjectiveC if matches?(/^\s*(if|while|for|switch|do)\s*\([^)]*\*[^)]*\)/) - next Mathematica if contains?('(*') next Mathematica if contains?(':=') next Mason if matches?(/<%(def|method|text|doc|args|flags|attr|init|once|shared|perl|cleanup|filter)([^>]*)(>)/) next Matlab if matches?(/^\s*?%/) - + # Matlab cell array creation: data = { + next Matlab if matches?(/^\s*[a-zA-Z]\w*\s*=\s*\{/) + next Mason if matches? %r!( 'application/vnd.wolfram.mathematica.package' assert_guess :mimetype => 'application/vnd.wolfram.wl' end + + it 'guesses by source' do + assert_guess :filename => 'foo.m', :source => '(* Mathematica comment *)' + assert_guess :filename => 'foo.m', :source => 'a := b' + end end end diff --git a/spec/lexers/matlab_spec.rb b/spec/lexers/matlab_spec.rb index 71d3be18f2..8095c271eb 100644 --- a/spec/lexers/matlab_spec.rb +++ b/spec/lexers/matlab_spec.rb @@ -33,5 +33,10 @@ end eos end + + it 'guesses by source' do + assert_guess :filename => 'foo.m', :source => '% MATLAB comment' + assert_guess :filename => 'foo.m', :source => 'mycell = {' + end end end diff --git a/spec/lexers/objective_c_spec.rb b/spec/lexers/objective_c_spec.rb index 5f56f090e7..cdb1d57033 100644 --- a/spec/lexers/objective_c_spec.rb +++ b/spec/lexers/objective_c_spec.rb @@ -19,6 +19,9 @@ it 'guesses by source' do assert_guess :filename => 'foo.h', :source => '@"foo"' assert_guess :filename => 'foo.h', :source => '@implementation Foo' + assert_guess :filename => 'foo.m', :source => 'if (*bar == 0) {' + assert_guess :filename => 'foo.m', :source => 'while (*ptr != NULL)' + assert_guess :filename => 'foo.m', :source => 'if (*(void **)(addr + 8) == target) {' end end end