File: coffeescript.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (215 lines) | stat: -rw-r--r-- 5,428 bytes parent folder | download | duplicates (4)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
  module Lexers
    class Coffeescript < RegexLexer
      tag 'coffeescript'
      aliases 'coffee', 'coffee-script'
      filenames '*.coffee', 'Cakefile'
      mimetypes 'text/coffeescript'

      title "CoffeeScript"
      desc 'The Coffeescript programming language (coffeescript.org)'

      def self.detect?(text)
        return true if text.shebang? 'coffee'
      end

      def self.keywords
        @keywords ||= Set.new %w(
          for by while until loop break continue return
          switch when then if else do yield throw try catch finally await
          new delete typeof instanceof super extends this class
          import export debugger
        )
      end

      def self.reserved
        @reserved ||= Set.new %w(
          case function var void with const let enum
          native implements interface package private protected public static
        )
      end

      def self.constants
        @constants ||= Set.new %w(
          true false yes no on off null NaN Infinity undefined
        )
      end

      def self.builtins
        @builtins ||= Set.new %w(
          Array Boolean Date Error Function Math netscape Number Object
          Packages RegExp String sun decodeURI decodeURIComponent
          encodeURI encodeURIComponent eval isFinite isNaN parseFloat
          parseInt document window
        )
      end

      id = /[$a-zA-Z_][a-zA-Z0-9_]*/

      state :comments do
        rule %r/###[^#].*?###/m, Comment::Multiline
        rule %r/#.*$/, Comment::Single
      end

      state :whitespace do
        rule %r/\s+/m, Text
      end

      state :regex_comment do
        rule %r/^#(?!\{).*$/, Comment::Single
        rule %r/(\s+)(#(?!\{).*)$/ do
          groups Text, Comment::Single
        end
      end

      state :multiline_regex_begin do
        rule %r(///) do
          token Str::Regex
          goto :multiline_regex
        end
      end

      state :multiline_regex_end do
        rule %r(///([gimy]+\b|\B)), Str::Regex, :pop!
      end

      state :multiline_regex do
        mixin :multiline_regex_end
        mixin :regex_comment
        mixin :has_interpolation
        mixin :comments
        mixin :whitespace
        mixin :code_escape

        rule %r/\\\D/, Str::Escape
        rule %r/\\\d+/, Name::Variable
        rule %r/./m, Str::Regex
      end

      state :slash_starts_regex do
        mixin :comments
        mixin :whitespace
        mixin :multiline_regex_begin

        rule %r(
          /(\\.|[^\[/\\\n]|\[(\\.|[^\]\\\n])*\])+/ # a regex
          ([gimy]+\b|\B)
        )x, Str::Regex, :pop!

        rule(//) { pop! }
      end

      state :root do
        rule(%r(^(?=\s|/|<!--))) { push :slash_starts_regex }
        mixin :comments
        mixin :whitespace

        rule %r(
          [+][+]|--|~|&&|\band\b|\bor\b|\bis\b|\bisnt\b|\bnot\b|\bin\b|\bof\b|
          [?]|:|=|[|][|]|\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*`%&|^/])=?
        )x, Operator, :slash_starts_regex

        rule %r/[-=]>/, Name::Function

        rule %r/(@)([ \t]*)(#{id})/ do
          groups Name::Variable::Instance, Text, Name::Attribute
          push :slash_starts_regex
        end

        rule %r/([.])([ \t]*)(#{id})/ do
          groups Punctuation, Text, Name::Attribute
          push :slash_starts_regex
        end

        rule %r/#{id}(?=\s*:)/, Name::Attribute, :slash_starts_regex

        rule %r/#{id}/ do |m|
          if self.class.keywords.include? m[0]
            token Keyword
          elsif self.class.constants.include? m[0]
            token Name::Constant
          elsif self.class.builtins.include? m[0]
            token Name::Builtin
          else
            token Name::Other
          end

          push :slash_starts_regex
        end

        rule %r/[{(\[;,]/, Punctuation, :slash_starts_regex
        rule %r/[})\].]/, Punctuation

        rule %r/\d+[.]\d+([eE]\d+)?[fd]?/, Num::Float
        rule %r/0x[0-9a-fA-F]+/, Num::Hex
        rule %r/\d+/, Num::Integer
        rule %r/"""/, Str, :tdqs
        rule %r/'''/, Str, :tsqs
        rule %r/"/, Str, :dqs
        rule %r/'/, Str, :sqs
      end

      state :code_escape do
        rule %r(\\(
          c[A-Z]|
          x[0-9a-fA-F]{2}|
          u[0-9a-fA-F]{4}|
          u\{[0-9a-fA-F]{4}\}
        ))x, Str::Escape
      end

      state :strings do
        # all coffeescript strings are multi-line
        rule %r/[^#\\'"]+/m, Str
        mixin :code_escape
        rule %r/\\./, Str::Escape
        rule %r/#/, Str
      end

      state :double_strings do
        rule %r/'/, Str
        mixin :has_interpolation
        mixin :strings
      end

      state :single_strings do
        rule %r/"/, Str
        mixin :strings
      end

      state :interpolation do
        rule %r/}/, Str::Interpol, :pop!
        mixin :root
      end

      state :has_interpolation do
        rule %r/[#][{]/, Str::Interpol, :interpolation
      end

      state :dqs do
        rule %r/"/, Str, :pop!
        mixin :double_strings
      end

      state :tdqs do
        rule %r/"""/, Str, :pop!
        rule %r/"/, Str
        mixin :double_strings
      end

      state :sqs do
        rule %r/'/, Str, :pop!
        mixin :single_strings
      end

      state :tsqs do
        rule %r/'''/, Str, :pop!
        rule %r/'/, Str
        mixin :single_strings
      end
    end
  end
end