lib/rouge/lexers/javascript.rb in rouge-1.8.0 vs lib/rouge/lexers/javascript.rb in rouge-1.9.0

- old
+ new

@@ -82,11 +82,11 @@ 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 + void this yield ) end def self.declarations @declarations ||= Set.new %w(var let with function) @@ -204,27 +204,25 @@ class JSON < RegexLexer desc "JavaScript Object Notation (json.org)" tag 'json' filenames '*.json' - mimetypes 'application/json' + mimetypes 'application/json', 'application/vnd.api+json' # TODO: is this too much of a performance hit? JSON is quite simple, # so I'd think this wouldn't be too bad, but for large documents this # could mean doing two full lexes. def self.analyze_text(text) return 0.8 if text =~ /\A\s*{/m && text.lexes_cleanly?(self) end + string = /"(\\.|[^"])*"/ + state :root do mixin :whitespace - # special case for empty objects - rule /(\{)(\s*)(\})/m do - groups Punctuation, Text::Whitespace, Punctuation - end rule /(?:true|false|null)\b/, Keyword::Constant - rule /{/, Punctuation, :object_key + rule /{/, Punctuation, :object_key_initial rule /\[/, Punctuation, :array rule /-?(?:0|[1-9]\d*)\.\d+(?:e[+-]\d+)?/i, Num::Float rule /-?(?:0|[1-9]\d*)(?:e[+-]\d+)?/i, Num::Integer mixin :has_string end @@ -232,16 +230,27 @@ state :whitespace do rule /\s+/m, Text::Whitespace end state :has_string do - rule /"(\\.|[^"])*"/, Str::Double + rule string, Str::Double end + # in object_key_initial it's allowed to immediately close the object again + state :object_key_initial do + mixin :whitespace + rule string do + token Name::Tag + goto :object_key + end + rule /}/, Punctuation, :pop! + end + + # in object_key at least one more name/value pair is required state :object_key do mixin :whitespace - mixin :has_string + rule string, Name::Tag rule /:/, Punctuation, :object_val rule /}/, Error, :pop! end state :object_val do @@ -254,7 +263,35 @@ rule /\]/, Punctuation, :pop! rule /,/, Punctuation mixin :root end end + + class JSONDOC < JSON + desc "JavaScript Object Notation with extenstions for documentation" + tag 'json-doc' + + prepend :root do + mixin :comments + rule /(\.\.\.)/, Comment::Single + end + + prepend :object_key_initial do + mixin :comments + rule /(\.\.\.)/, Comment::Single + end + + prepend :object_key do + mixin :comments + rule /(\.\.\.)/ do + token Comment::Single + goto :object_key_initial + end + end + + state :comments do + rule %r(//.*?$), Comment::Single + end + end + end end