summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.5.2/lib/rouge/lexers/groovy.rb
blob: 4c1045870fd5c12e98616d86b5b3eaaa0b208768 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    class Groovy < RegexLexer
      title "Groovy"
      desc 'The Groovy programming language (http://www.groovy-lang.org/)'
      tag 'groovy'
      aliases 'nextflow', 'nf'
      filenames '*.groovy', 'Jenkinsfile', '*.Jenkinsfile', '*.nf'
      mimetypes 'text/x-groovy'

      def self.detect?(text)
        return true if text.shebang?(/groovy/)
        return true if text.shebang?(/nextflow/)
      end

      def self.keywords
        @keywords ||= Set.new %w(
          assert break case catch continue default do else finally for
          if goto instanceof new return switch this throw try while in as
        )
      end

      def self.declarations
        @declarations ||= Set.new %w(
          abstract const extends final implements native private
          protected public static strictfp super synchronized throws
          transient volatile
        )
      end

      def self.types
        @types ||= Set.new %w(
          def var boolean byte char double float int long short void
        )
      end

      def self.constants
        @constants ||= Set.new %w(true false null)
      end

      state :root do
        rule %r(^
          (\s*(?:\w[\w.\[\]]*\s+)+?) # return arguments
          (\w\w*) # method name
          (\s*) (\() # signature start
        )x do |m|
          delegate self.clone, m[1]
          token Name::Function, m[2]
          token Text, m[3]
          token Operator, m[4]
        end

        # whitespace
        rule %r/[^\S\n]+/, Text
        rule %r(//.*?$), Comment::Single
        rule %r(/[*].*?[*]/)m, Comment::Multiline
        rule %r/@\w[\w.]*/, Name::Decorator
        rule %r/(class|interface|trait|enum|record)\b/,  Keyword::Declaration, :class
        rule %r/package\b/, Keyword::Namespace, :import
        rule %r/import\b/, Keyword::Namespace, :import

        # TODO: highlight backslash escapes
        rule %r/""".*?"""/m, Str::Double
        rule %r/'''.*?'''/m, Str::Single

        rule %r/"(\\.|\\\n|.)*?"/, Str::Double
        rule %r/'(\\.|\\\n|.)*?'/, Str::Single
        rule %r(\$/(\$.|.)*?/\$)m, Str
        rule %r(/(\\.|\\\n|.)*?/), Str
        rule %r/'\\.'|'[^\\]'|'\\u[0-9a-f]{4}'/, Str::Char
        rule %r/(\.)([a-zA-Z_][a-zA-Z0-9_]*)/ do
          groups Operator, Name::Attribute
        end

        rule %r/[a-zA-Z_][a-zA-Z0-9_]*:/, Name::Label
        rule %r/[a-zA-Z_\$][a-zA-Z0-9_]*/ do |m|
          if self.class.keywords.include? m[0]
            token Keyword
          elsif self.class.declarations.include? m[0]
            token Keyword::Declaration
          elsif self.class.types.include? m[0]
            token Keyword::Type
          elsif self.class.constants.include? m[0]
            token Keyword::Constant
          else
            token Name
          end
        end

        rule %r([~^*!%&\[\](){}<>\|+=:;,./?-]), Operator

        # numbers
        rule %r/\d+\.\d+([eE]\d+)?[fd]?/, Num::Float
        rule %r/0x[0-9a-f]+/, Num::Hex
        rule %r/[0-9]+L?/, Num::Integer
        rule %r/\n/, Text
      end

      state :class do
        rule %r/\s+/, Text
        rule %r/\w\w*/, Name::Class, :pop!
      end

      state :import do
        rule %r/\s+/, Text
        rule %r/[\w.]+[*]?/, Name::Namespace, :pop!
      end
    end
  end
end