forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See hacklang.org Changes to PHP and C++ (and corresponding tests) to make sure that ambiguities are covered. Will add XHP support in a separate pull request. refs rouge-ruby#392
- Loading branch information
1 parent
6275798
commit db7cf94
Showing
7 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
module Rouge | ||
module Lexers | ||
load_lexer 'php.rb' | ||
|
||
class Hack < PHP | ||
title 'Hack' | ||
desc 'The Hack programming language (hacklang.org)' | ||
tag 'hack' | ||
aliases 'hack', 'hh' | ||
filenames '*.php', '*.hh' | ||
|
||
def self.analyze_text(text) | ||
return 1 if /<\?hh/ =~ text | ||
return 0.5 if text.shebang?('hhvm') | ||
return 0.2 if /async function/ =~ text | ||
return 0.2 if /\): Awaitable</ =~ text | ||
return 0.2 if /(vec|dict|keyset)\[/ =~ text | ||
return 0.2 if /(Map|Set|Vec)\s*{/ =~ text | ||
# Never succeed on a filename-only match: | ||
# - PHP wins for .php | ||
# - C++ wins for .hh | ||
-1 | ||
end | ||
|
||
def self.keywords | ||
@hh_keywords ||= super.merge Set.new %w( | ||
type newtype enum | ||
as super | ||
async await Awaitable | ||
vec dict keyset | ||
void int string bool float double | ||
arraykey num Stringish | ||
) | ||
end | ||
|
||
prepend :template do | ||
rule /<\?hh(\s*\/\/\s*(strict|decl|partial))?$/, Comment::Preproc, :php | ||
end | ||
|
||
prepend :php do | ||
rule %r((/\*\s*)(HH_(?:IGNORE_ERROR|FIXME)\[\d+\])([^*]*)(\*/)) do | ||
groups Comment::Preproc, Comment::Preproc, Comment::Multiline, Comment::Preproc | ||
end | ||
|
||
rule %r(// UNSAFE(?:_EXPR|_BLOCK)?), Comment::Preproc | ||
rule %r(/\*\s*UNSAFE_EXPR\s*\*/), Comment::Preproc | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
describe Rouge::Lexers::Hack do | ||
let(:subject) { Rouge::Lexers::Hack.new } | ||
|
||
describe 'guessing' do | ||
include Support::Guessing | ||
|
||
|
||
it 'Guesses .php and .hh files that contain Hack code' do | ||
assert_guess :filename => 'foo.php', :source => '<?hh // strict' | ||
assert_guess :filename => 'foo.hh', :source => '<?hh // strict' | ||
end | ||
|
||
it 'Does not guess .php or .hh files that contain non-hack code' do | ||
deny_guess :filename => 'foo.php', :source => '<? foo();' | ||
deny_guess :filename => 'foo.hh', :source => '#include <foo.h>' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
describe Rouge::Lexers::PHP do | ||
let(:subject) { Rouge::Lexers::PHP.new } | ||
|
||
describe 'guessing' do | ||
include Support::Guessing | ||
|
||
it 'Guesses files containing <?php' do | ||
assert_guess :source => '<?php foo();' | ||
end | ||
|
||
it 'Guesses PHP files that do not contain Hack code' do | ||
assert_guess :filename => 'foo.php', :source => '<? foo();' | ||
end | ||
|
||
it 'Guesses .php files containing <?, but not hack code' do | ||
deny_guess :filenaame => 'foo.php', :source => '<?hh // strict' | ||
end | ||
|
||
it "Does not guess files containing <?hh" do | ||
deny_guess :source => '<?hh foo();' | ||
deny_guess :source => '<?hh // strict' | ||
deny_guess :filename => '.php', :source => '<?hh foo();' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?hh // strict | ||
|
||
// Unlike PHP, there's no top-level content (e.g. HTML) before `<?hh`, and `?>` is unsupported. | ||
|
||
namespace Foo\Bar; | ||
|
||
async function main(Vector<string> $argv): Awaitable<void> { | ||
var_dump($argv->map($x ==> 'Arg: '.$x."\n")); | ||
} | ||
|
||
// vec: new by-value container | ||
// Vector: old by-ref (object semantics) container | ||
async function wrap(vec<string> $argv): Awaitable<void> { | ||
await main(new Vector($argv)); | ||
} | ||
|
||
function foo(int $x): string { | ||
// UNSAFE | ||
return $x; | ||
} | ||
|
||
function bar(int $x): string { | ||
return /* UNSAFE_EXPR */ $x; | ||
} | ||
|
||
/* HH_FIXME[1002] pseudomain in strict file */ | ||
HH\Asio\join(main()); |