summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.5.2/lib/rouge/lexers/hack.rb
blob: 1e8f8317b52fa201543d15b4aef00c42918fd066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

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.detect?(text)
        return true if /<\?hh/ =~ text
        return true if text.shebang?('hhvm')
        return true if /async function [a-zA-Z]/ =~ text
        return true if /\): Awaitable</ =~ text

        return false
      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 :root do
        rule %r/<\?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