rouge 3.19.0 → 3.20.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ module Rouge
7
7
  title "HTML"
8
8
  desc "HTML, the markup language of the web"
9
9
  tag 'html'
10
- filenames '*.htm', '*.html', '*.xhtml'
10
+ filenames '*.htm', '*.html', '*.xhtml', '*.cshtml'
11
11
  mimetypes 'text/html', 'application/xhtml+xml'
12
12
 
13
13
  def self.detect?(text)
@@ -225,8 +225,6 @@ module Rouge
225
225
  groups Name::Label, Text, Punctuation
226
226
  end
227
227
 
228
- rule %r/[{}]/, Punctuation
229
-
230
228
  mixin :expr_start
231
229
  end
232
230
 
@@ -6,97 +6,85 @@ module Rouge
6
6
 
7
7
  class JSX < Javascript
8
8
  title 'JSX'
9
- desc 'React JSX (https://facebook.github.io/react/)'
9
+ desc 'An XML-like syntax extension to JavaScript (facebook.github.io/jsx/)'
10
10
  tag 'jsx'
11
11
  aliases 'jsx', 'react'
12
12
  filenames '*.jsx'
13
13
 
14
14
  mimetypes 'text/x-jsx', 'application/x-jsx'
15
15
 
16
- id = Javascript.id_regex
16
+ start { @html = HTML.new(options); push :expr_start }
17
17
 
18
- def start_embed!
19
- @embed ||= JSX.new(options)
20
- @embed.reset!
21
- @embed.push(:expr_start)
22
- push :jsx_embed_root
23
- end
24
-
25
- def tag_token(name)
26
- name[0] =~ /\p{Lower}/ ? Name::Tag : Name::Class
18
+ prepend :expr_start do
19
+ mixin :tag
27
20
  end
28
21
 
29
- start { @html = HTML.new(options) }
30
-
31
- state :jsx_tags do
32
- rule %r/</, Punctuation, :jsx_element
22
+ state :tag do
23
+ rule %r/</ do
24
+ token Punctuation
25
+ push :tag_opening
26
+ push :element
27
+ push :element_name
28
+ end
33
29
  end
34
30
 
35
- state :jsx_internal do
36
- rule %r(</) do
31
+ state :tag_opening do
32
+ rule %r/<\// do
37
33
  token Punctuation
38
- goto :jsx_end_tag
34
+ goto :element
35
+ push :element_name
39
36
  end
40
-
37
+ mixin :tag
41
38
  rule %r/{/ do
42
39
  token Str::Interpol
43
- start_embed!
40
+ push :interpol
41
+ push :expr_start
44
42
  end
45
-
46
- rule %r/[^<>{]+/ do
43
+ rule %r/[^<{]+/ do
47
44
  delegate @html
48
45
  end
49
-
50
- mixin :jsx_tags
51
- end
52
-
53
- prepend :expr_start do
54
- mixin :jsx_tags
55
46
  end
56
47
 
57
- state :jsx_tag do
48
+ state :element do
58
49
  mixin :comments_and_whitespace
59
- rule %r/#{id}/ do |m|
60
- token tag_token(m[0])
50
+ rule %r/\/>/ do
51
+ token Punctuation
52
+ pop! 2
61
53
  end
62
-
63
- rule %r/[.]/, Punctuation
64
- end
65
-
66
- state :jsx_end_tag do
67
- mixin :jsx_tag
68
54
  rule %r/>/, Punctuation, :pop!
69
- end
70
-
71
- state :jsx_element do
72
- rule %r/#{id}=/, Name::Attribute, :jsx_attribute
73
- mixin :jsx_tag
74
- rule %r/>/ do token Punctuation; goto :jsx_internal end
75
- rule %r(/>), Punctuation, :pop!
76
- end
77
-
78
- state :jsx_attribute do
79
- rule %r/"(\\[\\"]|[^"])*"/, Str::Double, :pop!
80
- rule %r/'(\\[\\']|[^'])*'/, Str::Single, :pop!
81
55
  rule %r/{/ do
82
56
  token Str::Interpol
83
- pop!
84
- start_embed!
57
+ push :interpol
58
+ push :expr_start
85
59
  end
60
+ rule %r/\w+/, Name::Attribute
61
+ rule %r/=/, Punctuation
62
+ rule %r/(["']).*?(\1)/, Str
86
63
  end
87
64
 
88
- state :jsx_embed_root do
89
- rule %r/[.][.][.]/, Punctuation
65
+ state :element_name do
66
+ rule %r/[A-Z]\w*/, Name::Class
67
+ rule %r/\w+/, Name::Tag
68
+ rule %r/\./, Punctuation
69
+ rule(//) { pop! }
70
+ end
71
+
72
+ state :interpol do
90
73
  rule %r/}/, Str::Interpol, :pop!
91
- mixin :jsx_embed
74
+ rule %r/{/ do
75
+ token Punctuation
76
+ push :interpol_inner
77
+ push :statement
78
+ end
79
+ mixin :root
92
80
  end
93
81
 
94
- state :jsx_embed do
95
- rule %r/{/ do delegate @embed; push :jsx_embed end
96
- rule %r/}/ do delegate @embed; pop! end
97
- rule %r/[^{}]+/ do
98
- delegate @embed
82
+ state :interpol_inner do
83
+ rule %r/}/ do
84
+ token Punctuation
85
+ goto :statement
99
86
  end
87
+ mixin :root
100
88
  end
101
89
  end
102
90
  end
@@ -0,0 +1,310 @@
1
+ # -*- coding: utf-8 -*- #
2
+ # frozen_string_literal: true
3
+
4
+ module Rouge
5
+ module Lexers
6
+ class Livescript < RegexLexer
7
+ tag 'livescript'
8
+ aliases 'ls'
9
+ filenames '*.ls'
10
+ mimetypes 'text/livescript'
11
+
12
+ title 'LiveScript'
13
+ desc 'LiveScript, a language which compiles to JavaScript (livescript.net)'
14
+
15
+ def self.detect?(text)
16
+ return text.shebang? 'lsc'
17
+ end
18
+
19
+ def self.declarations
20
+ @declarations ||= Set.new %w(const let var function class extends implements)
21
+ end
22
+
23
+ def self.keywords
24
+ @keywords ||= Set.new %w(
25
+ loop until for in of while break return continue switch case
26
+ fallthrough default otherwise when then if unless else throw try
27
+ catch finally new delete typeof instanceof super by from to til
28
+ with require do debugger import export yield
29
+ )
30
+ end
31
+
32
+ def self.constants
33
+ @constants ||= Javascript.constants + %w(yes no on off void)
34
+ end
35
+
36
+ def self.builtins
37
+ @builtins ||= Javascript.builtins + %w(this it that arguments)
38
+ end
39
+
40
+ def self.loop_control_keywords
41
+ @loop_control_keywords ||= Set.new %w(break continue)
42
+ end
43
+
44
+ id = /[$a-z_]((-(?=[a-z]))?[a-z0-9_])*/i
45
+ int_number = /\d[\d_]*/
46
+ int = /#{int_number}(e[+-]?#{int_number})?[$\w]*/ # the last class matches units
47
+
48
+ state :root do
49
+ rule(%r(^(?=\s|/))) { push :slash_starts_regex }
50
+ mixin :comments
51
+ mixin :whitespace
52
+
53
+ # list of words
54
+ rule %r/(<\[)(.*?)(\]>)/m do
55
+ groups Punctuation, Str, Punctuation
56
+ end
57
+
58
+ # function declarations
59
+ rule %r/!\s*function\b/, Keyword::Declaration
60
+ rule %r/!?[-~]>|<[-~]!?/, Keyword::Declaration
61
+
62
+ # switch arrow
63
+ rule %r/(=>)/, Keyword
64
+
65
+ # prototype attributes
66
+ rule %r/(::)(#{id})/ do
67
+ groups Punctuation, Name::Attribute
68
+ push :id
69
+ end
70
+ rule %r/(::)(#{int})/ do
71
+ groups Punctuation, Num::Integer
72
+ push :id
73
+ end
74
+
75
+ # instance attributes
76
+ rule %r/(@)(#{id})/ do
77
+ groups Name::Variable::Instance, Name::Attribute
78
+ push :id
79
+ end
80
+ rule %r/([.])(#{id})/ do
81
+ groups Punctuation, Name::Attribute
82
+ push :id
83
+ end
84
+ rule %r/([.])(\d+)/ do
85
+ groups Punctuation, Num::Integer
86
+ push :id
87
+ end
88
+ rule %r/#{id}(?=\s*:[^:=])/, Name::Attribute
89
+
90
+ # operators
91
+ rule %r(
92
+ [+][+]|--|&&|\b(and|x?or|is(nt)?|not)\b(?!-[a-zA-Z]|_)|[|][|]|
93
+ [.]([|&^]|<<|>>>?)[.]|\\(?=\n)|[.:]=|<<<<?|<[|]|[|]>|
94
+ (<<|>>|==?|!=?|[-<>+*%^/~?])=?
95
+ )x, Operator, :slash_starts_regex
96
+
97
+ # arguments shorthand
98
+ rule %r/(&)(#{id})?/ do
99
+ groups Name::Builtin, Name::Attribute
100
+ end
101
+
102
+ # switch case
103
+ rule %r/[|]|\bcase(?=\s)/, Keyword, :switch_underscore
104
+
105
+ rule %r/@/, Name::Variable::Instance
106
+ rule %r/[.]{3}/, Punctuation
107
+ rule %r/:/, Punctuation
108
+
109
+ # keywords
110
+ rule %r/#{id}/ do |m|
111
+ if self.class.loop_control_keywords.include? m[0]
112
+ token Keyword
113
+ push :loop_control
114
+ next
115
+ elsif self.class.keywords.include? m[0]
116
+ token Keyword
117
+ elsif self.class.constants.include? m[0]
118
+ token Name::Constant
119
+ elsif self.class.builtins.include? m[0]
120
+ token Name::Builtin
121
+ elsif self.class.declarations.include? m[0]
122
+ token Keyword::Declaration
123
+ elsif /^[A-Z]/.match(m[0]) && /[^-][a-z]/.match(m[0])
124
+ token Name::Class
125
+ else
126
+ token Name::Variable
127
+ end
128
+ push :id
129
+ end
130
+
131
+ # punctuation and brackets
132
+ rule %r/\](?=[!?.]|#{id})/, Punctuation, :id
133
+ rule %r/[{(\[;,]/, Punctuation, :slash_starts_regex
134
+ rule %r/[})\].]/, Punctuation
135
+
136
+ # literals
137
+ rule %r/#{int_number}[.]#{int}/, Num::Float
138
+ rule %r/0x[0-9A-Fa-f]+/, Num::Hex
139
+ rule %r/#{int}/, Num::Integer
140
+
141
+ # strings
142
+ rule %r/"""/ do
143
+ token Str
144
+ push do
145
+ rule %r/"""/, Str, :pop!
146
+ rule %r/"/, Str
147
+ mixin :double_strings
148
+ end
149
+ end
150
+
151
+ rule %r/'''/ do
152
+ token Str
153
+ push do
154
+ rule %r/'''/, Str, :pop!
155
+ rule %r/'/, Str
156
+ mixin :single_strings
157
+ end
158
+ end
159
+
160
+ rule %r/"/ do
161
+ token Str
162
+ push do
163
+ rule %r/"/, Str, :pop!
164
+ mixin :double_strings
165
+ end
166
+ end
167
+
168
+ rule %r/'/ do
169
+ token Str
170
+ push do
171
+ rule %r/'/, Str, :pop!
172
+ mixin :single_strings
173
+ end
174
+ end
175
+
176
+ # words
177
+ rule %r/\\\S[^\s,;\])}]*/, Str
178
+ end
179
+
180
+ state :code_escape do
181
+ rule %r(\\(
182
+ c[A-Z]|
183
+ x[0-9a-fA-F]{2}|
184
+ u[0-9a-fA-F]{4}|
185
+ u\{[0-9a-fA-F]{4}\}
186
+ ))x, Str::Escape
187
+ end
188
+
189
+ state :interpolated_expression do
190
+ rule %r/}/, Str::Interpol, :pop!
191
+ mixin :root
192
+ end
193
+
194
+ state :interpolation do
195
+ # with curly braces
196
+ rule %r/[#][{]/, Str::Interpol, :interpolated_expression
197
+ # without curly braces
198
+ rule %r/(#)(#{id})/ do |m|
199
+ groups Str::Interpol, (self.class.builtins.include? m[2]) ? Name::Builtin : Name::Variable
200
+ end
201
+ end
202
+
203
+ state :whitespace do
204
+ # white space and loop labels
205
+ rule %r/(\s+?)(?:^([^\S\n]*)(:#{id}))?/m do
206
+ groups Text, Text, Name::Label
207
+ end
208
+ end
209
+
210
+ state :whitespace_single_line do
211
+ rule %r([^\S\n]+), Text
212
+ end
213
+
214
+ state :slash_starts_regex do
215
+ mixin :comments
216
+ mixin :whitespace
217
+ mixin :multiline_regex_begin
218
+
219
+ rule %r(
220
+ /(\\.|[^\[/\\\n]|\[(\\.|[^\]\\\n])*\])+/ # a regex
221
+ ([gimy]+\b|\B)
222
+ )x, Str::Regex, :pop!
223
+
224
+ rule(//) { pop! }
225
+ end
226
+
227
+ state :multiline_regex_begin do
228
+ rule %r(//) do
229
+ token Str::Regex
230
+ goto :multiline_regex
231
+ end
232
+ end
233
+
234
+ state :multiline_regex_end do
235
+ rule %r(//([gimy]+\b|\B)), Str::Regex, :pop!
236
+ end
237
+
238
+ state :multiline_regex do
239
+ mixin :multiline_regex_end
240
+ mixin :regex_comment
241
+ mixin :interpolation
242
+ mixin :code_escape
243
+ rule %r/\\\D/, Str::Escape
244
+ rule %r/\\\d+/, Name::Variable
245
+ rule %r/./m, Str::Regex
246
+ end
247
+
248
+ state :regex_comment do
249
+ rule %r/^#(\s+.*)?$/, Comment::Single
250
+ rule %r/(\s+)(#)(\s+.*)?$/ do
251
+ groups Text, Comment::Single, Comment::Single
252
+ end
253
+ end
254
+
255
+ state :comments do
256
+ rule %r(/\*.*?\*/)m, Comment::Multiline
257
+ rule %r/#.*$/, Comment::Single
258
+ end
259
+
260
+ state :switch_underscore do
261
+ mixin :whitespace_single_line
262
+ rule %r/_(?=\s*=>|\s+then\b)/, Keyword
263
+ rule(//) { pop! }
264
+ end
265
+
266
+ state :loop_control do
267
+ mixin :whitespace_single_line
268
+ rule %r/#{id}(?=[);\n])/, Name::Label
269
+ rule(//) { pop! }
270
+ end
271
+
272
+ state :id do
273
+ rule %r/[!?]|[.](?!=)/, Punctuation
274
+ rule %r/[{]/ do
275
+ # destructuring
276
+ token Punctuation
277
+ push do
278
+ rule %r/[,;]/, Punctuation
279
+ rule %r/#{id}/, Name::Attribute
280
+ rule %r/#{int}/, Num::Integer
281
+ mixin :whitespace
282
+ rule %r/[}]/, Punctuation, :pop!
283
+ end
284
+ end
285
+ rule %r/#{id}/, Name::Attribute
286
+ rule %r/#{int}/, Num::Integer
287
+ rule(//) { goto :slash_starts_regex }
288
+ end
289
+
290
+ state :strings do
291
+ # all strings are multi-line
292
+ rule %r/[^#\\'"]+/m, Str
293
+ mixin :code_escape
294
+ rule %r/\\./, Str::Escape
295
+ rule %r/#/, Str
296
+ end
297
+
298
+ state :double_strings do
299
+ rule %r/'/, Str
300
+ mixin :interpolation
301
+ mixin :strings
302
+ end
303
+
304
+ state :single_strings do
305
+ rule %r/"/, Str
306
+ mixin :strings
307
+ end
308
+ end
309
+ end
310
+ end