Skip to content

Commit

Permalink
Disambiguate free pascal and puppet (#1845)
Browse files Browse the repository at this point in the history
Both free pascal and puppet are sharing the same file extension, that is
`.pp`. This change adds `.pp` as a possible extension for pascal lexer
and disambiguates them using keywords.
  • Loading branch information
tancnle committed Jul 7, 2022
1 parent b8e146f commit 3da7ac7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/rouge/guessers/disambiguation.rb
Expand Up @@ -131,6 +131,13 @@ def match?(filename)
next TeX if matches?(/\A\s*(?:\\|%)/)
next Apex
end

disambiguate '*.pp' do
next Pascal if matches?(/\b(function|begin|var)\b/)
next Pascal if matches?(/\b(end(;|\.))/)

Puppet
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rouge/lexers/pascal.rb
Expand Up @@ -7,7 +7,7 @@ class Pascal < RegexLexer
tag 'pascal'
title "Pascal"
desc 'a procedural programming language commonly used as a teaching language.'
filenames '*.pas', '*.lpr'
filenames '*.pas', '*.lpr', '*.pp'

mimetypes 'text/x-pascal'

Expand Down
36 changes: 36 additions & 0 deletions spec/guesser_spec.rb
Expand Up @@ -76,4 +76,40 @@
assert_guess(Rouge::Lexers::Ruby, :source => '# v' + 'im: syntax=ruby')
end
end

describe 'disambiguation guessing' do
describe 'guesses *.pp filename' do
it 'guesses pascal' do
assert_guess(
Rouge::Lexers::Pascal,
filename: 'foo.pp',
source: <<~SOURCE
function sum(a, b: integer): integer;
var tempSum: integer
begin
tempSum := a + b;
sum := tempSum;
end;
SOURCE
)
end

it 'guesses puppet' do
assert_guess(
Rouge::Lexers::Puppet,
filename: 'foo.pp',
source: <<~SOURCE
class foo::bar (
Array[String] = foo::bar::baz,
) {
$foo = [
'bar',
'baz',
]
}
SOURCE
)
end
end
end
end
5 changes: 4 additions & 1 deletion spec/lexers/pascal_spec.rb
Expand Up @@ -9,7 +9,10 @@

it 'guesses by filename' do
assert_guess :filename => 'foo.pas'
assert_guess :filename => 'foo.lpr'
assert_guess :filename => 'foo.lpr'

# *.pp needs source hints because it's also used by Puppet
assert_guess :filename => 'foo.pp', :source => 'begin end.'
end

it 'guesses by mimetype' do
Expand Down
3 changes: 2 additions & 1 deletion spec/lexers/puppet_spec.rb
Expand Up @@ -8,7 +8,8 @@
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.pp'
# *.pp needs source hints because it's also used by Pascal
assert_guess :filename => 'foo.pp', :source => 'class privileges { }'
end

it 'guesses by source' do
Expand Down

0 comments on commit 3da7ac7

Please sign in to comment.