lib/rouge/lexers/javascript.rb in rouge-0.0.2 vs lib/rouge/lexers/javascript.rb in rouge-0.0.3
- old
+ new
@@ -1,36 +1,41 @@
module Rouge
module Lexers
- JavascriptLexer = RegexLexer.create do
- option :debug, true
-
- name 'javascript'
+ class JavascriptLexer < RegexLexer
+ tag 'javascript'
aliases 'js'
+ extensions 'js'
- lexer :comments_and_whitespace do
+ state :comments_and_whitespace do
rule /\s+/, 'Text'
rule /<!--/, 'Comment' # really...?
rule %r(//.*?\n), 'Comment.Single'
rule %r(/\*.*?\*/), 'Comment.Multiline'
end
- lexer :slash_starts_regex do
+ state :slash_starts_regex do
mixin :comments_and_whitespace
+
rule %r(
- /(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/ # a nonempty regex
- (?:[gim]+\b|\B) # regex flags
+ / # opening slash
+ ( \\. # escape sequences
+ | [^/\\\n] # regular characters
+ | \[ (\\. | [^\]\\\n])* \] # character classes
+ )+
+ / # closing slash
+ (?:[gim]+\b|\B) # flags
)x, 'Literal.String.Regex'
# if it's not matched by the above r.e., it's not
# a valid expression, so we use :bad_regex to eat until the
# end of the line.
rule %r(/), 'Literal.String.Regex', :bad_regex
rule //, 'Text', :pop!
+ end
- lexer :bad_regex do
- rule /[^\n]+/, 'Error', :pop!
- end
+ state :bad_regex do
+ rule /[^\n]+/, 'Error', :pop!
end
keywords = %w(
for in while do break return continue switch case default if else
throw try catch finally new delete typeof instanceof void this
@@ -53,14 +58,15 @@
decodeURIComponent encodeURI encodeURIComponent
Error eval isFinite isNaN parseFloat parseInt document this
window
).join('|')
- lexer :root do
+ state :root do
rule %r(^(?=\s|/|<!--)), 'Text', :slash_starts_regex
mixin :comments_and_whitespace
- rule %r(\+\+|--|~|&&|\?|\|\||\\(?=\n)|<<|>>>?|===|!==),
+ rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | ===
+ | !== | \? | : )x,
'Operator', :slash_starts_regex
rule %r([-<>+*%&|\^/!=]=?), 'Operator', :slash_starts_regex
rule /[{(\[;,]/, 'Punctuation', :slash_starts_regex
rule /[})\].]/, 'Punctuation'
rule /(?:#{keywords})\b/, 'Keyword', :slash_starts_regex
@@ -68,16 +74,14 @@
rule /(?:#{reserved})\b/, 'Keyword.Reserved'
rule /(?:#{constants})\b/, 'Keyword.Constant'
rule /(?:#{builtins})\b/, 'Name.Builtin'
rule /[$a-zA-Z_][a-zA-Z0-9_]*/, 'Name.Other'
- rule /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, 'Number.Float'
- rule /0x[0-9a-fA-F]+/, 'Number.Hex'
- rule /[0-9]+/, 'Number.Integer'
+ rule /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, 'Literal.Number.Float'
+ rule /0x[0-9a-fA-F]+/, 'Literal.Number.Hex'
+ rule /[0-9]+/, 'Literal.Number.Integer'
rule /"(\\\\|\\"|[^"])*"/, 'Literal.String.Double'
rule /'(\\\\|\\'|[^'])*'/, 'Literal.String.Single'
end
-
- mixin :root
end
end
end