rouge 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ @interface Person : NSObject {
2
+ @public
3
+ NSString *name;
4
+ @private
5
+ int age;
6
+ }
7
+
8
+ @property(copy) NSString *name;
9
+ @property(readonly) int age;
10
+
11
+ -(id)initWithAge:(int)age;
12
+ @end
@@ -205,7 +205,7 @@ module Rouge
205
205
 
206
206
  best_result = threshold
207
207
  best_match = nil
208
- registry.values.each do |lexer|
208
+ lexers.each do |lexer|
209
209
  result = lexer.analyze_text(source) || 0
210
210
  return lexer if result == 1
211
211
 
@@ -22,6 +22,19 @@ module Rouge
22
22
  def self.keywords_type
23
23
  @keywords_type ||= Set.new %w(
24
24
  int long float short double char unsigned signed void
25
+
26
+ jmp_buf FILE DIR div_t ldiv_t mbstate_t sig_atomic_t fpos_t
27
+ clock_t time_t va_list size_t ssize_t off_t wchar_t ptrdiff_t
28
+ wctrans_t wint_t wctype_t
29
+
30
+ _Bool _Complex int8_t int16_t int32_t int64_t
31
+ uint8_t uint16_t uint32_t uint64_t int_least8_t
32
+ int_least16_t int_least32_t int_least64_t
33
+ uint_least8_t uint_least16_t uint_least32_t
34
+ uint_least64_t int_fast8_t int_fast16_t int_fast32_t
35
+ int_fast64_t uint_fast8_t uint_fast16_t uint_fast32_t
36
+ uint_fast64_t intptr_t uintptr_t intmax_t
37
+ uintmax_t
25
38
  )
26
39
  end
27
40
 
@@ -34,20 +47,31 @@ module Rouge
34
47
  )
35
48
  end
36
49
 
50
+ # high priority for filename matches
51
+ def self.analyze_text(*)
52
+ 0.3
53
+ end
54
+
55
+ def self.builtins
56
+ @builtins ||= []
57
+ end
58
+
37
59
  start { push :bol }
38
60
 
39
- state :bol do
61
+ state :expr_bol do
40
62
  mixin :inline_whitespace
41
63
 
42
- rule /#if\s0/, Comment::Preproc, :if_0
43
-
64
+ rule /#if\s0/, Comment, :if_0
44
65
  rule /#/, Comment::Preproc, :macro
45
66
 
46
- rule /#{id}:(?!:)/, Name::Label
47
-
48
67
  rule(//) { pop! }
49
68
  end
50
69
 
70
+ state :bol do
71
+ rule /#{id}:(?!:)/, Name::Label
72
+ mixin :expr_bol
73
+ end
74
+
51
75
  state :inline_whitespace do
52
76
  rule /[ \t\r]+/, Text
53
77
  rule /\\\n/, Text # line continuation
@@ -55,14 +79,18 @@ module Rouge
55
79
  end
56
80
 
57
81
  state :whitespace do
58
- rule /\n+/, Text, :bol
82
+ rule /\n+/m, Text, :bol
59
83
  rule %r(//(\\.|.)*?\n), Comment::Single, :bol
60
84
  mixin :inline_whitespace
61
85
  end
62
86
 
63
- state :statements do
64
- rule /\s+/m, Text
87
+ state :expr_whitespace do
88
+ rule /\n+/m, Text, :expr_bol
89
+ mixin :whitespace
90
+ end
65
91
 
92
+ state :statements do
93
+ mixin :whitespace
66
94
  rule /L?"/, Str, :string
67
95
  rule %r(L?'(\\.|\\[0-7]{1,3}|\\x[a-f0-9]{1,2}|[^\\'\n])')i, Str::Char
68
96
  rule %r((\d+\.\d*|\.\d+|\d+)[e][+-]?\d+[lu]*)i, Num::Float
@@ -83,6 +111,8 @@ module Rouge
83
111
  token Keyword::Type
84
112
  elsif self.class.reserved.include? name
85
113
  token Keyword::Reserved
114
+ elsif self.class.builtins.include? name
115
+ token Name::Builtin
86
116
  else
87
117
  token Name
88
118
  end
@@ -95,7 +125,7 @@ module Rouge
95
125
  end
96
126
 
97
127
  state :root do
98
- mixin :whitespace
128
+ mixin :expr_whitespace
99
129
 
100
130
  # functions
101
131
  rule %r(
@@ -105,10 +135,10 @@ module Rouge
105
135
  (#{ws})({) # open brace
106
136
  )mx do |m|
107
137
  # TODO: do this better.
108
- delegate C, m[1]
138
+ recurse m[1]
109
139
  token Name::Function, m[2]
110
- delegate C, m[3]
111
- delegate C, m[4]
140
+ recurse m[3]
141
+ recurse m[4]
112
142
  token Punctuation, m[5]
113
143
  push :function
114
144
  end
@@ -121,10 +151,10 @@ module Rouge
121
151
  (#{ws})(;) # semicolon
122
152
  )mx do |m|
123
153
  # TODO: do this better.
124
- delegate C, m[1]
154
+ recurse m[1]
125
155
  token Name::Function
126
- delegate C, m[3]
127
- delegate C, m[4]
156
+ recurse m[3]
157
+ recurse m[4]
128
158
  token Punctuation
129
159
  push :statement
130
160
  end
@@ -134,7 +164,7 @@ module Rouge
134
164
 
135
165
  state :statement do
136
166
  rule /;/, Punctuation, :pop!
137
- mixin :whitespace
167
+ mixin :expr_whitespace
138
168
  mixin :statements
139
169
  rule /[{}]/, Punctuation
140
170
  end
@@ -156,18 +186,19 @@ module Rouge
156
186
  end
157
187
 
158
188
  state :macro do
159
- rule %r([^/\n]+), Comment::Preproc
160
- rule %r(/[*].*?[*]/)m, Comment::Multiline
161
- rule %r(//.*$), Comment::Single
162
- rule %r(/), Comment::Preproc
163
- rule /(?<=\\)\n/, Comment::Preproc
189
+ # NB: pop! goes back to :bol
164
190
  rule /\n/, Comment::Preproc, :pop!
191
+ rule %r([^/\n\\]+), Comment::Preproc
192
+ rule /\\./m, Comment::Preproc
193
+ mixin :inline_whitespace
194
+ rule %r(/), Comment::Preproc
165
195
  end
166
196
 
167
197
  state :if_0 do
168
- rule /^\s*#if.*?(?<!\\)\n/, Comment, :if_0
169
- rule /^\s*#el(?:se|if).*\n/, Comment::Preproc, :pop!
170
- rule /^\s*#endif.*?(?<!\\)\n/, Comment, :pop!
198
+ # NB: no \b here, to cover #ifdef and #ifndef
199
+ rule /^\s*#if/, Comment, :if_0
200
+ rule /^\s*#\s*el(?:se|if)/, Comment, :pop!
201
+ rule /^\s*#\s*endif\b.*?(?<!\\)\n/m, Comment, :pop!
171
202
  rule /.*?\n/, Comment
172
203
  end
173
204
  end
@@ -1,6 +1,8 @@
1
+ require Pathname.new(__FILE__).dirname.join('c.rb')
2
+
1
3
  module Rouge
2
4
  module Lexers
3
- class Cpp < RegexLexer
5
+ class Cpp < C
4
6
  desc "The C++ programming language"
5
7
 
6
8
  tag 'cpp'
@@ -13,110 +15,30 @@ module Rouge
13
15
  mimetypes 'text/x-c++hdr', 'text/x-c++src'
14
16
 
15
17
  def self.keywords
16
- @keywords ||= Set.new %w(
17
- asm auto break case catch const const_cast continue
18
- default delete do dynamic_cast else enum explicit export
19
- extern for friend goto if mutable namespace new operator
20
- private protected public register reinterpret_cast return
21
- restrict sizeof static static_cast struct switch template
22
- this throw throws try typedef typeid typename union using
23
- volatile virtual while
24
- )
25
- end
26
-
27
- def self.keywords_type
28
- @keywords_type ||= Set.new %w(
29
- bool int long float short double char unsigned signed void wchar_t
30
- )
18
+ @keywords ||= super + Set.new(%w(
19
+ asm catch const_cast delete dynamic_cast explicit export
20
+ friend mutable namespace new operator private protected public
21
+ reinterpret_cast restrict static_cast template this throw
22
+ throws typeid typename using virtual
23
+ ))
31
24
  end
32
25
 
33
26
  def self.reserved
34
- @reserved ||= Set.new %w(
35
- __asm __int8 __based __except __int16 __stdcall __cdecl
36
- __fastcall __int32 __declspec __finally __int64 __try
37
- __leave __wchar_t __w64 __virtual_inheritance __uuidof
38
- __unaligned __super __single_inheritance __raise __noop
39
- __multiple_inheritance __m128i __m128d __m128 __m64 __interface
40
- __identifier __forceinline __event __assume
41
- inline _inline __inline
42
- naked _naked __naked
43
- thread _thread __thread
44
- )
27
+ @reserved ||= super + Set.new(%w(
28
+ __virtual_inheritance __uuidof __super __single_inheritance
29
+ __multiple_inheritance __interface __event
30
+ ))
45
31
  end
46
32
 
47
- # optional comments or whitespace
48
- ws = %r((?:\s|//.*?\n|/[*].*?[*]/)+)
49
33
  id = /[a-zA-Z_][a-zA-Z0-9]*/
50
34
 
51
- start { push :bol }
52
-
53
- state :bol do
54
- mixin :inline_whitespace
55
- rule /#if\s+0/ do
56
- token Comment::Preproc
57
- goto :if_0
58
- end
59
-
60
- rule /#/ do
61
- token Comment::Preproc
62
- goto :macro
63
- end
64
-
65
- rule(//) { pop! }
66
- end
67
-
68
- state :inline_whitespace do
69
- rule /[ \t\r]+/, Text
70
- rule /\\\n/, Text
71
- rule %r(/(\\\n)?[*].*?[*](\\\n)?/)m, Comment::Multiline
72
- end
73
-
74
- state :whitespace do
75
- mixin :inline_whitespace
76
- rule %r(/(\\\n)?/(\n|(.|\n)*?[^\\]\n)), Comment::Single, :bol
77
- rule /\n/, Text, :bol
78
- end
79
-
80
- state :multiline_comment do
81
- rule %r([*](\\\n)?/), Comment::Multiline, :pop!
82
- rule %r([*]), Comment::Multiline
83
- rule %r([^*]+), Comment::Multiline
84
- end
85
-
86
- state :root do
87
- mixin :whitespace
88
-
89
- rule /L?"/, Str, :string
90
- rule %r(L?'(\\.|\\[0-7]{1,3}|\\x[a-f0-9]{1,2}|[^\\'\n])')i, Str::Char
91
- rule %r((\d+\.\d*|\.\d+|\d+)[e][+-]?\d+[lu]*)i, Num::Float
92
- rule /0x[0-9a-f]+[lu]*/i, Num::Hex
93
- rule /0[0-7]+[lu]*/i, Num::Oct
94
- rule /\d+[lu]*/i, Num::Integer
95
- rule %r(\*/), Error
96
- rule %r([~!%^&*+=\|?:<>/-]), Operator
97
- rule /[()\[\],.;{}]/, Punctuation
98
-
99
- rule /class\b/, Keyword, :classname
100
-
35
+ prepend :root do
101
36
  # Offload C++ extensions, http://offload.codeplay.com/
102
37
  rule /(?:__offload|__blockingoffload|__outer)\b/, Keyword::Pseudo
38
+ end
103
39
 
104
- rule /(true|false)\b/, Keyword::Constant
105
- rule /NULL\b/, Name::Builtin
106
- rule /#{id}:(?!:)/, Name::Label
107
- rule id do |m|
108
- name = m[0]
109
-
110
- if self.class.keywords.include? name
111
- token Keyword
112
- elsif self.class.keywords_type.include? name
113
- token Keyword::Type
114
- elsif self.class.reserved.include? name
115
- token Keyword::Reserved
116
- else
117
- token Name
118
- end
119
- end
40
+ prepend :statements do
41
+ rule /class\b/, Keyword, :classname
120
42
  end
121
43
 
122
44
  state :classname do
@@ -126,33 +48,6 @@ module Rouge
126
48
  rule /\s*(?=>)/m, Text, :pop!
127
49
  mixin :whitespace
128
50
  end
129
-
130
- state :string do
131
- rule /"/, Str, :pop!
132
- rule /\\([\\abfnrtv"']|x[a-fA-F0-9]{2,4}|[0-7]{1,3})/, Str::Escape
133
- rule /[^\\"\n]+/, Str
134
- rule /\\\n/, Str
135
- rule /\\/, Str # stray backslash
136
- end
137
-
138
- state :macro do
139
- rule %r([^/\n]+), Comment::Preproc
140
- rule %r(/[*].*?[*]/)m, Comment::Multiline
141
- rule %r(//.*$), Comment::Single
142
- rule %r(/), Comment::Preproc
143
- rule /(?<=\\)\n/, Comment::Preproc
144
- rule /\n/ do
145
- token Comment::Preproc
146
- goto :bol
147
- end
148
- end
149
-
150
- state :if_0 do
151
- rule /^\s*#if.*?(?<!\\)\n/, Comment::Preproc, :if_0
152
- rule /^\s*#el(?:se|if).*\n/, Comment::Preproc, :pop!
153
- rule /^\s*#endif.*?(?<!\\)\n/, Comment::Preproc, :pop!
154
- rule /.*?\n/, Comment
155
- end
156
51
  end
157
52
  end
158
53
  end
@@ -0,0 +1,186 @@
1
+ require Pathname.new(__FILE__).dirname.join('c.rb')
2
+
3
+ module Rouge
4
+ module Lexers
5
+ class ObjectiveC < C
6
+ desc 'objective_c'
7
+ tag 'objective_c'
8
+ aliases 'objc'
9
+ filenames '*.m', '*.h'
10
+
11
+ mimetypes 'text/x-objective_c', 'application/x-objective_c'
12
+
13
+ def self.at_keywords
14
+ @at_keywords ||= %w(
15
+ selector private protected public encode synchronized try
16
+ throw catch finally end property synthesize dynamic optional
17
+ interface implementation
18
+ )
19
+ end
20
+
21
+ def self.at_builtins
22
+ @at_builtins ||= %w(true false YES NO)
23
+ end
24
+
25
+ def self.builtins
26
+ @builtins ||= %w(YES NO nil)
27
+ end
28
+
29
+ def self.analyze_text(text)
30
+ return 1 if text =~ /@(end|implementation|protocol)\b/
31
+
32
+ id = /[a-z$_][a-z0-9$_]*/i
33
+ return 0.4 if text =~ %r(
34
+ \[ \s* #{id} \s+
35
+ (?:
36
+ #{id} \s* \]
37
+ | #{id}? :
38
+ )
39
+ )x
40
+ return 0.4 if text.include? '@"'
41
+ end
42
+
43
+ id = /[a-z$_][a-z0-9$_]*/i
44
+
45
+ prepend :statements do
46
+ rule /@"/, Str, :string
47
+ rule /@'(\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|\\.|[^\\'\n]')/
48
+ Str::Char
49
+ rule /@(\d+[.]\d*|[.]\d+|\d+)e[+-]?\d+l?/i,
50
+ Num::Float
51
+ rule /@(\d+[.]\d*|[.]\d+|\d+f)f?/i, Num::Float
52
+ rule /@0x\h+[lL]?/, Num::Hex
53
+ rule /@0[0-7]+l?/i, Num::Oct
54
+ rule /@\d+l?/, Num::Integer
55
+ rule /\bin\b/, Keyword
56
+
57
+ rule /@(?:interface|implementation)\b/ do
58
+ token Keyword
59
+ goto :classname
60
+ end
61
+
62
+ rule /@(?:class|protocol)\b/ do
63
+ token Keyword
64
+ goto :forward_classname
65
+ end
66
+
67
+ rule /@([[:alnum:]]+)/ do |m|
68
+ if self.class.at_keywords.include? m[1]
69
+ token Keyword
70
+ elsif self.class.at_builtins.include? m[1]
71
+ token Name::Builtin
72
+ else
73
+ token Error
74
+ end
75
+ end
76
+
77
+ rule /[?]/, Punctuation, :ternary
78
+ rule /\[/, Punctuation, :message
79
+ end
80
+
81
+ state :ternary do
82
+ rule /:/, Punctuation, :pop!
83
+ mixin :statements
84
+ end
85
+
86
+ state :message_shared do
87
+ rule /\]/, Punctuation, :pop!
88
+ rule /;/, Error
89
+
90
+ mixin :statement
91
+ end
92
+
93
+ state :message do
94
+ rule /(#{id})(\s*)(:)/ do
95
+ groups(Name::Function, Text, Punctuation)
96
+ goto :message_with_args
97
+ end
98
+
99
+ rule /(#{id})(\s*)(\])/ do
100
+ groups(Name::Function, Text, Punctuation)
101
+ pop!
102
+ end
103
+
104
+ mixin :message_shared
105
+ end
106
+
107
+ state :message_with_args do
108
+ rule /(#{id})(\s*)(:)/ do
109
+ groups(Name::Function, Text, Punctuation)
110
+ end
111
+
112
+ mixin :message_shared
113
+ end
114
+
115
+ state :classname do
116
+ mixin :whitespace
117
+
118
+ rule /(#{id})(\s*)(:)(\s*)(#{id})/ do
119
+ groups(Name::Class, Text,
120
+ Punctuation, Text,
121
+ Name::Class)
122
+ pop!
123
+ end
124
+
125
+ rule /(#{id})(\s*)([(])(\s*)(#{id})(\s*)([)])/ do
126
+ groups(Name::Class, Text,
127
+ Punctuation, Text,
128
+ Name::Label, Text,
129
+ Punctuation)
130
+ end
131
+
132
+ rule id, Name::Class, :pop!
133
+ end
134
+
135
+ state :forward_classname do
136
+ rule /(#{id})(\s*)(,)(\s*)/ do
137
+ groups(Name::Class, Text, Punctuation, Text)
138
+ push
139
+ end
140
+
141
+ rule /(#{id})(\s*)(;?)/ do
142
+ groups(Name::Class, Text, Punctuation)
143
+ pop!
144
+ end
145
+ end
146
+
147
+ prepend :root do
148
+ rule %r(
149
+ ([-+])(\s*)
150
+ ([(].*?[)])?(\s*)
151
+ (?=#{id}:?)
152
+ )ix do |m|
153
+ token Keyword, m[1]; token Text, m[2]
154
+ recurse m[3]; token Text, m[4]
155
+ push :method_definition
156
+ end
157
+ end
158
+
159
+ state :method_definition do
160
+ rule /,/, Punctuation
161
+ rule /[.][.][.]/, Punctuation
162
+ rule /([(].*?[)])(#{id})/ do |m|
163
+ recurse m[1]; token Name::Variable, m[2]
164
+ end
165
+
166
+ rule /(#{id})(\s*)(:)/m do
167
+ groups(Name::Function, Text, Punctuation)
168
+ end
169
+
170
+ rule /;/, Punctuation, :pop!
171
+
172
+ rule /{/ do
173
+ token Punctuation
174
+ goto :function
175
+ end
176
+
177
+ mixin :inline_whitespace
178
+ rule %r(//.*?\n), Comment::Single
179
+ rule /\s+/m, Text
180
+
181
+ rule(//) { pop! }
182
+ end
183
+ end
184
+ end
185
+ end
186
+
@@ -2,7 +2,7 @@ require 'rouge/lexers/sass/common'
2
2
 
3
3
  module Rouge
4
4
  module Lexers
5
- class Sass < RegexLexer
5
+ class Sass < SassCommon
6
6
  include Indentation
7
7
 
8
8
  desc 'The Sass stylesheet language language (sass-lang.com)'
@@ -66,8 +66,6 @@ module Rouge
66
66
  state :end_section do
67
67
  rule(/\n/) { token Text; reset_stack }
68
68
  end
69
-
70
- include SassCommon
71
69
  end
72
70
  end
73
71
  end
@@ -1,180 +1,171 @@
1
1
  module Rouge
2
2
  module Lexers
3
3
  # shared states with SCSS
4
- # TODO: make this less nasty to do
5
- module SassCommon
6
- include Token::Tokens
4
+ class SassCommon < RegexLexer
5
+ id = /[\w-]+/
7
6
 
8
- def self.included(klass)
9
- klass.class_eval(&INCLUDED_PROC)
10
- end
11
-
12
- INCLUDED_PROC = proc do
13
- id = /[\w-]+/
14
-
15
- state :content_common do
16
- rule /@for\b/, Keyword, :for
17
- rule /@(debug|warn|if|while)/, Keyword, :value
18
- rule /(@mixin)(\s+)(#{id})/ do
19
- group Keyword; group Text; group Name::Function
20
- push :value
21
- end
7
+ state :content_common do
8
+ rule /@for\b/, Keyword, :for
9
+ rule /@(debug|warn|if|while)/, Keyword, :value
10
+ rule /(@mixin)(\s+)(#{id})/ do
11
+ group Keyword; group Text; group Name::Function
12
+ push :value
13
+ end
22
14
 
23
- rule /@extend\b/, Keyword, :selector
15
+ rule /@extend\b/, Keyword, :selector
24
16
 
25
- rule /(@include)(\s+)(#{id})/ do
26
- group Keyword; group Text; group Name::Decorator
27
- push :value
28
- end
17
+ rule /(@include)(\s+)(#{id})/ do
18
+ group Keyword; group Text; group Name::Decorator
19
+ push :value
20
+ end
29
21
 
30
- rule /@#{id}/, Keyword, :selector
22
+ rule /@#{id}/, Keyword, :selector
31
23
 
32
- # $variable: assignment
33
- rule /([$]#{id})([ \t]*)(:)/ do
34
- group Name::Variable; group Text; group Punctuation
35
- push :value
36
- end
24
+ # $variable: assignment
25
+ rule /([$]#{id})([ \t]*)(:)/ do
26
+ group Name::Variable; group Text; group Punctuation
27
+ push :value
37
28
  end
29
+ end
38
30
 
39
- state :value do
40
- mixin :end_section
41
- rule /[ \t]+/, Text
42
- rule /[$]#{id}/, Name::Variable
43
- rule /url[(]/, Str::Other, :string_url
44
- rule /#{id}(?=\s*[(])/, Name::Function
45
- rule /%#{id}/, Name::Decorator
46
-
47
- # named literals
48
- rule /(true|false)\b/, Name::Builtin::Pseudo
49
- rule /(and|or|not)\b/, Operator::Word
50
-
51
- # colors and numbers
52
- rule /#[a-z0-9]{1,6}/i, Num::Hex
53
- rule /-?\d+(%|[a-z]+)?/, Num
54
- rule /-?\d*\.\d+(%|[a-z]+)?/, Num::Integer
55
-
56
- mixin :has_strings
57
- mixin :has_interp
58
-
59
- rule /[~^*!&%<>\|+=@:,.\/?-]+/, Operator
60
- rule /[\[\]()]+/, Punctuation
61
- rule %r(/[*]), Comment::Multiline, :inline_comment
62
- rule %r(//[^\n]*), Comment::Single
63
-
64
- # identifiers
65
- rule(id) do |m|
66
- if CSS.builtins.include? m[0]
67
- token Name::Builtin
68
- elsif CSS.constants.include? m[0]
69
- token Name::Constant
70
- else
71
- token Name
72
- end
31
+ state :value do
32
+ mixin :end_section
33
+ rule /[ \t]+/, Text
34
+ rule /[$]#{id}/, Name::Variable
35
+ rule /url[(]/, Str::Other, :string_url
36
+ rule /#{id}(?=\s*[(])/, Name::Function
37
+ rule /%#{id}/, Name::Decorator
38
+
39
+ # named literals
40
+ rule /(true|false)\b/, Name::Builtin::Pseudo
41
+ rule /(and|or|not)\b/, Operator::Word
42
+
43
+ # colors and numbers
44
+ rule /#[a-z0-9]{1,6}/i, Num::Hex
45
+ rule /-?\d+(%|[a-z]+)?/, Num
46
+ rule /-?\d*\.\d+(%|[a-z]+)?/, Num::Integer
47
+
48
+ mixin :has_strings
49
+ mixin :has_interp
50
+
51
+ rule /[~^*!&%<>\|+=@:,.\/?-]+/, Operator
52
+ rule /[\[\]()]+/, Punctuation
53
+ rule %r(/[*]), Comment::Multiline, :inline_comment
54
+ rule %r(//[^\n]*), Comment::Single
55
+
56
+ # identifiers
57
+ rule(id) do |m|
58
+ if CSS.builtins.include? m[0]
59
+ token Name::Builtin
60
+ elsif CSS.constants.include? m[0]
61
+ token Name::Constant
62
+ else
63
+ token Name
73
64
  end
74
65
  end
66
+ end
75
67
 
76
- state :has_interp do
77
- rule /[#][{]/, Str::Interpol, :interpolation
78
- end
68
+ state :has_interp do
69
+ rule /[#][{]/, Str::Interpol, :interpolation
70
+ end
79
71
 
80
- state :has_strings do
81
- rule /"/, Str::Double, :dq
82
- rule /'/, Str::Single, :sq
83
- end
72
+ state :has_strings do
73
+ rule /"/, Str::Double, :dq
74
+ rule /'/, Str::Single, :sq
75
+ end
84
76
 
85
- state :interpolation do
86
- rule /}/, Str::Interpol, :pop!
87
- mixin :value
88
- end
77
+ state :interpolation do
78
+ rule /}/, Str::Interpol, :pop!
79
+ mixin :value
80
+ end
89
81
 
90
- state :selector do
91
- mixin :end_section
92
-
93
- mixin :has_strings
94
- mixin :has_interp
95
- rule /[ \t]+/, Text
96
- rule /:/, Name::Decorator, :pseudo_class
97
- rule /[.]/, Name::Class, :class
98
- rule /#/, Name::Namespace, :id
99
- rule /%/, Name::Variable, :placeholder
100
- rule id, Name::Tag
101
- rule /&/, Keyword
102
- rule /[~^*!&\[\]()<>\|+=@:;,.\/?-]/, Operator
103
- end
82
+ state :selector do
83
+ mixin :end_section
84
+
85
+ mixin :has_strings
86
+ mixin :has_interp
87
+ rule /[ \t]+/, Text
88
+ rule /:/, Name::Decorator, :pseudo_class
89
+ rule /[.]/, Name::Class, :class
90
+ rule /#/, Name::Namespace, :id
91
+ rule /%/, Name::Variable, :placeholder
92
+ rule id, Name::Tag
93
+ rule /&/, Keyword
94
+ rule /[~^*!&\[\]()<>\|+=@:;,.\/?-]/, Operator
95
+ end
104
96
 
105
- state :dq do
106
- rule /"/, Str::Double, :pop!
107
- mixin :has_interp
108
- rule /(\\.|#(?![{])|[^\n"#])+/, Str::Double
109
- end
97
+ state :dq do
98
+ rule /"/, Str::Double, :pop!
99
+ mixin :has_interp
100
+ rule /(\\.|#(?![{])|[^\n"#])+/, Str::Double
101
+ end
110
102
 
111
- state :sq do
112
- rule /'/, Str::Single, :pop!
113
- mixin :has_interp
114
- rule /(\\.|#(?![{])|[^\n'#])+/, Str::Single
115
- end
103
+ state :sq do
104
+ rule /'/, Str::Single, :pop!
105
+ mixin :has_interp
106
+ rule /(\\.|#(?![{])|[^\n'#])+/, Str::Single
107
+ end
116
108
 
117
- state :string_url do
118
- rule /[)]/, Str::Other, :pop!
119
- rule /(\\.|#(?![{])|[^\n)#])+/, Str::Other
120
- mixin :has_interp
121
- end
109
+ state :string_url do
110
+ rule /[)]/, Str::Other, :pop!
111
+ rule /(\\.|#(?![{])|[^\n)#])+/, Str::Other
112
+ mixin :has_interp
113
+ end
122
114
 
123
- state :selector_piece do
124
- mixin :has_interp
125
- rule(//) { pop! }
126
- end
115
+ state :selector_piece do
116
+ mixin :has_interp
117
+ rule(//) { pop! }
118
+ end
127
119
 
128
- state :pseudo_class do
129
- rule id, Name::Decorator
130
- mixin :selector_piece
131
- end
120
+ state :pseudo_class do
121
+ rule id, Name::Decorator
122
+ mixin :selector_piece
123
+ end
132
124
 
133
- state :class do
134
- rule id, Name::Class
135
- mixin :selector_piece
136
- end
125
+ state :class do
126
+ rule id, Name::Class
127
+ mixin :selector_piece
128
+ end
137
129
 
138
- state :id do
139
- rule id, Name::Namespace
140
- mixin :selector_piece
141
- end
130
+ state :id do
131
+ rule id, Name::Namespace
132
+ mixin :selector_piece
133
+ end
142
134
 
143
- state :placeholder do
144
- rule id, Name::Variable
145
- mixin :selector_piece
146
- end
135
+ state :placeholder do
136
+ rule id, Name::Variable
137
+ mixin :selector_piece
138
+ end
147
139
 
148
- state :for do
149
- rule /(from|to|through)/, Operator::Word
150
- mixin :value
151
- end
140
+ state :for do
141
+ rule /(from|to|through)/, Operator::Word
142
+ mixin :value
143
+ end
152
144
 
153
- state :attr_common do
154
- mixin :has_interp
155
- rule id do |m|
156
- if CSS.attributes.include? m[0]
157
- token Name::Label
158
- else
159
- token Name::Attribute
160
- end
145
+ state :attr_common do
146
+ mixin :has_interp
147
+ rule id do |m|
148
+ if CSS.attributes.include? m[0]
149
+ token Name::Label
150
+ else
151
+ token Name::Attribute
161
152
  end
162
153
  end
154
+ end
163
155
 
164
- state :attribute do
165
- mixin :attr_common
156
+ state :attribute do
157
+ mixin :attr_common
166
158
 
167
- rule /([ \t]*)(:)/ do
168
- group Text; group Punctuation
169
- push :value
170
- end
159
+ rule /([ \t]*)(:)/ do
160
+ group Text; group Punctuation
161
+ push :value
171
162
  end
163
+ end
172
164
 
173
- state :inline_comment do
174
- rule /(\\#|#(?=[^\n{])|\*(?=[^\n\/])|[^\n#*])+/, Comment::Multiline
175
- mixin :has_interp
176
- rule %r([*]/), Comment::Multiline, :pop!
177
- end
165
+ state :inline_comment do
166
+ rule /(\\#|#(?=[^\n{])|\*(?=[^\n\/])|[^\n#*])+/, Comment::Multiline
167
+ mixin :has_interp
168
+ rule %r([*]/), Comment::Multiline, :pop!
178
169
  end
179
170
  end
180
171
  end
@@ -2,7 +2,7 @@ require 'rouge/lexers/sass/common'
2
2
 
3
3
  module Rouge
4
4
  module Lexers
5
- class Scss < RegexLexer
5
+ class Scss < SassCommon
6
6
  desc "SCSS stylesheets (sass-lang.com)"
7
7
  tag 'scss'
8
8
  filenames '*.scss'
@@ -26,8 +26,6 @@ module Rouge
26
26
  rule /\n/, Text
27
27
  rule(/[;{}]/) { token Punctuation; reset_stack }
28
28
  end
29
-
30
- include SassCommon
31
29
  end
32
30
  end
33
31
  end
@@ -58,12 +58,12 @@ module Rouge
58
58
  @next_indent += match.size
59
59
  end
60
60
 
61
- def set_indent(opts={})
61
+ def set_indent(match, opts={})
62
62
  if indent < @next_indent
63
63
  @indent_stack << @next_indent
64
64
  end
65
65
 
66
- @next_indent += @last_match[0].size unless opts[:implicit]
66
+ @next_indent += match.size unless opts[:implicit]
67
67
  end
68
68
 
69
69
  plain_scalar_start = /[^ \t\n\r\f\v?:,\[\]{}#&*!\|>'"%@`]/
@@ -119,7 +119,7 @@ module Rouge
119
119
  end
120
120
 
121
121
  # block collection indicators
122
- rule(/[?:-](?=[ ]|$)/) { token Punctuation::Indicator; set_indent }
122
+ rule(/[?:-](?=[ ]|$)/) { |m| token Punctuation::Indicator; set_indent m[0] }
123
123
 
124
124
  # the beginning of a block line
125
125
  rule(/[ ]*/) { |m| token Text; continue_indent(m[0]); pop! }
@@ -163,9 +163,9 @@ module Rouge
163
163
 
164
164
  state :block_nodes do
165
165
  # implicit key
166
- rule /:(?=\s|$)/ do
166
+ rule /:(?=\s|$)/ do |m|
167
167
  token Punctuation::Indicator
168
- set_indent :implicit => true
168
+ set_indent m[0], :implicit => true
169
169
  end
170
170
 
171
171
  # literal and folded scalars
@@ -35,35 +35,50 @@ module Rouge
35
35
  #
36
36
  # @see RegexLexer.state
37
37
  class State
38
- attr_reader :name
39
- def initialize(name, &defn)
38
+ attr_reader :name, :rules
39
+ def initialize(name, rules)
40
40
  @name = name
41
- @defn = defn
41
+ @rules = rules
42
42
  end
43
43
 
44
- def rules
45
- @rules ||= []
44
+ def inspect
45
+ "#<#{self.class.name} #{@name.inspect}>"
46
46
  end
47
+ end
47
48
 
48
- def load!(lexer_class)
49
- return self if @loaded
50
- @loaded = true
51
- StateDSL.new(rules).instance_eval(&@defn)
49
+ class StateDSL
50
+ attr_reader :rules
51
+ def initialize(name, &defn)
52
+ @name = name
53
+ @defn = defn
54
+ @rules = []
55
+ end
52
56
 
53
- rules.map! do |rule|
57
+ def to_state(lexer_class)
58
+ load!
59
+ rules = @rules.map do |rule|
54
60
  rule.is_a?(String) ? lexer_class.get_state(rule) : rule
55
61
  end
62
+ State.new(@name, rules)
63
+ end
56
64
 
57
- self
65
+ def prepended(&defn)
66
+ parent_defn = @defn
67
+ StateDSL.new(@name) do
68
+ instance_eval(&defn)
69
+ instance_eval(&parent_defn)
70
+ end
58
71
  end
59
- end
60
72
 
61
- class StateDSL
62
- attr_reader :rules
63
- def initialize(rules)
64
- @rules = rules
73
+ def appended(&defn)
74
+ parent_defn = @defn
75
+ StateDSL.new(@name) do
76
+ instance_eval(&parent_defn)
77
+ instance_eval(&defn)
78
+ end
65
79
  end
66
80
 
81
+ protected
67
82
  # Define a new rule for this state.
68
83
  #
69
84
  # @overload rule(re, token, next_state=nil)
@@ -99,8 +114,15 @@ module Rouge
99
114
  # Mix in the rules from another state into this state. The rules
100
115
  # from the mixed-in state will be tried in order before moving on
101
116
  # to the rest of the rules in this state.
102
- def mixin(lexer_name)
103
- rules << lexer_name.to_s
117
+ def mixin(state)
118
+ rules << state.to_s
119
+ end
120
+
121
+ private
122
+ def load!
123
+ return if @loaded
124
+ @loaded = true
125
+ instance_eval(&@defn)
104
126
  end
105
127
  end
106
128
 
@@ -110,6 +132,16 @@ module Rouge
110
132
  @states ||= {}
111
133
  end
112
134
 
135
+ def self.state_definitions
136
+ @state_definitions ||= InheritableHash.new(superclass.state_definitions)
137
+ end
138
+ @state_definitions = {}
139
+
140
+ def self.replace_state(name, new_defn)
141
+ states[name] = nil
142
+ state_definitions[name] = new_defn
143
+ end
144
+
113
145
  # The routines to run at the beginning of a fresh lex.
114
146
  # @see start
115
147
  def self.start_procs
@@ -129,16 +161,31 @@ module Rouge
129
161
  # The block will be evaluated in the context of a {StateDSL}.
130
162
  def self.state(name, &b)
131
163
  name = name.to_s
132
- states[name] = State.new(name, &b)
164
+ state_definitions[name] = StateDSL.new(name, &b)
165
+ end
166
+
167
+ def self.prepend(name, &b)
168
+ name = name.to_s
169
+ dsl = state_definitions[name] or raise "no such state #{name.inspect}"
170
+ replace_state(name, dsl.prepended(&b))
171
+ end
172
+
173
+ def self.append(state, &b)
174
+ name = name.to_s
175
+ dsl = state_definitions[name] or raise "no such state #{name.inspect}"
176
+ replace_state(name, dsl.appended(&b))
133
177
  end
134
178
 
135
179
  # @private
136
180
  def self.get_state(name)
137
181
  return name if name.is_a? State
138
182
 
139
- state = states[name.to_s]
140
- raise "unknown state: #{name}" unless state
141
- state.load!(self)
183
+ name = name.to_s
184
+
185
+ states[name] ||= begin
186
+ defn = state_definitions[name] or raise "unknown state: #{name.inspect}"
187
+ defn.to_state(self)
188
+ end
142
189
  end
143
190
 
144
191
  # @private
@@ -306,10 +353,14 @@ module Rouge
306
353
 
307
354
  lexer.lex(text, :continue => true) do |tok, val|
308
355
  debug { " delegated token: #{tok.inspect}, #{val.inspect}" }
309
- token(tok, val)
356
+ yield_token(tok, val)
310
357
  end
311
358
  end
312
359
 
360
+ def recurse(text=nil)
361
+ delegate(self.class, text)
362
+ end
363
+
313
364
  # Push a state onto the stack. If no state name is given and you've
314
365
  # passed a block, a state will be dynamically created using the
315
366
  # {StateDSL}.
@@ -1,5 +1,5 @@
1
1
  module Rouge
2
2
  def self.version
3
- "0.5.1"
3
+ "0.5.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rouge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-15 00:00:00.000000000 Z
12
+ date: 2013-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  type: :runtime
@@ -93,6 +93,7 @@ files:
93
93
  - lib/rouge/lexers/erlang.rb
94
94
  - lib/rouge/lexers/yaml.rb
95
95
  - lib/rouge/lexers/make.rb
96
+ - lib/rouge/lexers/objective_c.rb
96
97
  - lib/rouge/lexers/lua.rb
97
98
  - lib/rouge/lexers/viml.rb
98
99
  - lib/rouge/lexers/java.rb
@@ -168,6 +169,7 @@ files:
168
169
  - lib/rouge/demos/erb
169
170
  - lib/rouge/demos/cpp
170
171
  - lib/rouge/demos/coffeescript
172
+ - lib/rouge/demos/objective_c
171
173
  - lib/rouge/demos/sql
172
174
  - lib/rouge/demos/puppet
173
175
  - lib/rouge/demos/ini