File: groovy.rb

package info (click to toggle)
ruby-rugments 1.0.0~beta8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: ruby: 10,293; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 2,974 bytes parent folder | download | duplicates (3)
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
module Rugments
  module Lexers
    class Groovy < RegexLexer
      title 'Groovy'
      desc 'The Groovy programming language (groovy.codehaus.org)'
      tag 'groovy'
      filenames '*.groovy'
      mimetypes 'text/x-groovy'

      ws = %r{(?:\s|//.*?\n|/[*].*?[*]/)+}

      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 enum extends final implements native private
          protected public static strictfp super synchronized throws
          transient volatile
        )
      end

      def self.types
        @types ||= Set.new %w(
          def 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\d.\[\]]*\s+)+?) # return arguments
          (\w[\w\d]*) # method name
          (\s*) (\() # signature start
                }x do |m|
          delegate clone, m[1]
          token Name::Function, m[2]
          token Text, m[3]
          token Operator, m[4]
        end

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

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

        rule /[a-zA-Z_][a-zA-Z0-9_]*:/, Name::Label
        rule /[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 /\d+\.\d+([eE]\d+)?[fd]?/, Num::Float
        rule /0x[0-9a-f]+/, Num::Hex
        rule /[0-9]+L?/, Num::Integer
        rule /\n/, Text
      end

      state :class do
        rule /\s+/, Text
        rule /\w[\w\d]*/, Name::Class, :pop!
      end

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