lib/rouge/lexers/swift.rb in rouge-1.6.1 vs lib/rouge/lexers/swift.rb in rouge-1.6.2

- old
+ new

@@ -22,20 +22,27 @@ ) end def self.declarations @declarations ||= Set.new %w( - class deinit enum extension func import init let protocol static struct subscript typealias var + class deinit enum extension final func import init internal lazy let optional private protocol public required static struct subscript typealias var dynamic ) end + def self.at_keywords + @at_keywords ||= %w( + autoclosure IBAction IBDesignable IBInspectable IBOutlet noreturn NSCopying NSManaged objc UIApplicationMain + ) + end + def self.types @types ||= Set.new %w( Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64 Int Double Float Bool String Character + AnyObject Any ) end def self.constants @constants ||= Set.new %w( @@ -62,42 +69,76 @@ rule /0_?[0-7]+(?:_[0-7]+)*/, Num::Oct rule /0x[0-9A-Fa-f]+(?:_[0-9A-Fa-f]+)*/, Num::Hex rule /0b[01]+(?:_[01]+)*/, Num::Bin rule %r{[\d]+(?:_\d+)*}, Num::Integer - rule /(?!\b(if|while|for)\b)\b#{id}(?=\s*[(])/, Name::Function + rule /(?!\b(if|while|for|private|internal|@objc)\b)\b#{id}(?=\s*[(])/, Name::Function rule /(#?#{id})(\s*)(:)/ do groups Name::Variable, Text, Punctuation end rule /(let|var)\b(\s*)(#{id})/ do groups Keyword, Text, Name::Variable end + rule /@(#{id})/ do |m| + if m[1] == 'objc' + token Keyword::Declaration + push :objc_setting + elsif self.class.at_keywords.include? m[1] + token Keyword + else + token Error + end + end + rule id do |m| if self.class.keywords.include? m[0] token Keyword elsif self.class.declarations.include? m[0] token Keyword::Declaration + if %w(private internal).include? m[0] + push :access_control_setting + elsif %w(protocol class extension).include? m[0] + push :type_definition + end elsif self.class.types.include? m[0] token Keyword::Type elsif self.class.constants.include? m[0] token Keyword::Constant else token Name end end rule id, Name end + + state :access_control_setting do + rule /\( *(\w+) *\)/ do |m| + if m[1] == 'set' + token Keyword::Declaration + else + token Error + end + end + rule //, Keyword::Declaration, :pop! + end + + state :objc_setting do + rule /(\( *)(\w+)( *\))/ do |m| + token Keyword::Declaration, m[1] + token Name::Class, m[2] + token Keyword::Declaration, m[3] + end + rule //, Keyword::Declaration, :pop! + end state :dq do rule /\\[\\0tnr'"]/, Str::Escape rule /\\[(]/, Str::Escape, :interp - rule /\\x\h{2}/, Str::Escape - rule /\\u\h{4}/, Str::Escape - rule /\\U\h{8}/, Str::Escape + rule /\\u\{\h{1,8}\}/, Str::Escape rule /[^\\"]+/, Str rule /"/, Str, :pop! end state :interp do @@ -110,19 +151,18 @@ rule /[(]/, Punctuation, :push rule /[)]/, Punctuation, :pop! mixin :root end - state :class do + state :type_definition do mixin :whitespace rule id, Name::Class, :pop! end state :namespace do mixin :whitespace rule /(?=[(])/, Text, :pop! rule /(#{id}|[.])+/, Name::Namespace, :pop! end - end end end