Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Objective-C vs Mathematica lexer disambiguation #2103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/rouge/guessers/disambiguation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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*\{/)
EthanArbuckle marked this conversation as resolved.
Show resolved Hide resolved

next Mason if matches? %r!(</?%|<&)!
end

Expand Down
5 changes: 5 additions & 0 deletions spec/lexers/mathematica_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
assert_guess :mimetype => '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
5 changes: 5 additions & 0 deletions spec/lexers/matlab_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions spec/lexers/objective_c_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading