lib/rouge/lexers/javascript.rb in rouge-0.2.3 vs lib/rouge/lexers/javascript.rb in rouge-0.2.4
- old
+ new
@@ -44,55 +44,121 @@
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
- ).join('|')
+ def self.keywords
+ @keywords ||= Set.new %w(
+ for in while do break return continue switch case default
+ if else throw try catch finally new delete typeof instanceof
+ void this
+ )
+ end
- declarations = %w(var let with function).join('|')
+ def self.declarations
+ @declarations ||= Set.new %w(var let with function)
+ end
- reserved = %w(
- abstract boolean byte char class const debugger double enum export
- extends final float goto implements import int interface long
- native package private protected public short static super
- synchronized throws transient volatile
- ).join('|')
+ def self.reserved
+ @reserved ||= Set.new %w(
+ abstract boolean byte char class const debugger double enum
+ export extends final float goto implements import int interface
+ long native package private protected public short static
+ super synchronized throws transient volatile
+ )
+ end
- constants = %w(true false null NaN Infinity undefined).join('|')
+ def self.constants
+ @constants ||= Set.new %w(true false null NaN Infinity undefined)
+ end
- builtins = %w(
- Array Boolean Date Error Function Math netscape
- Number Object Packages RegExp String sun decodeURI
- decodeURIComponent encodeURI encodeURIComponent
- Error eval isFinite isNaN parseFloat parseInt document this
- window
- ).join('|')
+ def self.builtins
+ @builtins ||= %w(
+ Array Boolean Date Error Function Math netscape
+ Number Object Packages RegExp String sun decodeURI
+ decodeURIComponent encodeURI encodeURIComponent
+ Error eval isFinite isNaN parseFloat parseInt document this
+ window
+ )
+ end
+ id = /[$a-zA-Z_][a-zA-Z0-9_]*/
+
state :root do
rule /\A\s*#!.*?\n/m, 'Comment.Preproc'
rule %r((?<=\n)(?=\s|/|<!--)), 'Text', :slash_starts_regex
mixin :comments_and_whitespace
rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | ===
- | !== | \? | : )x,
+ | !== )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
- rule /(?:#{declarations})\b/, 'Keyword.Declaration', :slash_starts_regex
- 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 /[(\[;,]/, 'Punctuation', :slash_starts_regex
+ rule /[)\].]/, 'Punctuation'
+ rule /[?]/ do
+ token 'Punctuation'
+ push :ternary
+ push :slash_starts_regex
+ end
+
+ rule /[{](?=\s*(#{id}|"[^\n]*?")\s*:)/, 'Punctuation', :object
+
+ rule /[{]/ do
+ token 'Punctuation'
+ push :block
+ push :slash_starts_regex
+ end
+
+ rule id do |m|
+ if self.class.keywords.include? m[0]
+ token 'Keyword'
+ push :slash_starts_regex
+ elsif self.class.declarations.include? m[0]
+ token 'Keyword.Declaration'
+ push :slash_starts_regex
+ elsif self.class.reserved.include? m[0]
+ token 'Keyword.Reserved'
+ elsif self.class.constants.include? m[0]
+ token 'Keyword.Constant'
+ elsif self.class.builtins.include? m[0]
+ token 'Name.Builtin'
+ else
+ token 'Name.Other'
+ end
+ end
+
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
+
+ # braced parts that aren't object literals
+ state :block do
+ rule /(#{id})(\s*)(:)/ do
+ group 'Name.Label'; group 'Text'
+ group 'Punctuation'
+ end
+
+ rule /[}]/, 'Punctuation', :pop!
+ mixin :root
+ end
+
+ # object literals
+ state :object do
+ rule /[}]/, 'Punctuation', :pop!
+ rule /(#{id})(\s*)(:)/ do
+ group 'Name.Attribute'; group 'Text'
+ group 'Punctuation'
+ end
+ mixin :root
+ end
+
+ # ternary expressions, where <id>: is not a label!
+ state :ternary do
+ rule /:/, 'Punctuation', :pop!
+ mixin :root
end
end
class JSON < RegexLexer
desc "JavaScript Object Notation (json.org)"