rouge 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,6 +7,9 @@ module Rouge
7
7
  lexer = Lexer.find(lexer) unless lexer.respond_to? :lex
8
8
  raise "unknown lexer #{lexer}" unless lexer
9
9
 
10
+ formatter = Formatter.find(formatter) unless formatter.respond_to? :format
11
+ raise "unknown formatter #{formatter}" unless formatter
12
+
10
13
  formatter.format(lexer.lex(text))
11
14
  end
12
15
  end
@@ -1,6 +1,7 @@
1
1
  module Rouge
2
2
  # A Formatter takes a token stream and formats it for human viewing.
3
3
  class Formatter
4
+ # @private
4
5
  REGISTRY = {}
5
6
 
6
7
  # Specify or get the unique tag for this formatter. This is used
@@ -17,11 +18,17 @@ module Rouge
17
18
  REGISTRY[tag]
18
19
  end
19
20
 
21
+ # Format a token stream. Delegates to {#format}.
22
+ def self.format(tokens, opts={})
23
+ new(opts).format(tokens)
24
+ end
25
+
20
26
  # Format a token stream.
21
27
  def format(tokens)
22
28
  enum_for(:stream, tokens).to_a.join
23
29
  end
24
30
 
31
+ # @deprecated Use {#format} instead.
25
32
  def render(tokens)
26
33
  warn 'Formatter#render is deprecated, use #format instead.'
27
34
  format(tokens)
@@ -68,6 +68,21 @@ module Rouge
68
68
  end
69
69
  end
70
70
 
71
+ # Specify or get the path name containing a small demo for
72
+ # this lexer (can be overriden by {demo}).
73
+ def demo_file(arg=:absent)
74
+ return @demo_file = Pathname.new(arg) unless arg == :absent
75
+
76
+ @demo_file = Pathname.new(__FILE__).dirname.join('demos', tag)
77
+ end
78
+
79
+ # Specify or get a small demo string for this lexer
80
+ def demo(arg=:absent)
81
+ return @demo = arg unless arg == :absent
82
+
83
+ @demo = File.read(demo_file)
84
+ end
85
+
71
86
  # @return a list of all lexers.
72
87
  def all
73
88
  registry.values.uniq
@@ -46,46 +46,85 @@ module Rouge
46
46
  rule /[^\n]+/, 'Error', :pop!
47
47
  end
48
48
 
49
- keywords = %w(
50
- for in while do break return continue switch case default if else
51
- throw try catch finally new delete typeof instanceof void this
52
- ).join('|')
53
-
54
- declarations = %w(var let with function).join('|')
55
-
56
- reserved = %w(
57
- abstract boolean byte char class const debugger double enum export
58
- extends final float goto implements import int interface long
59
- native package private protected public short static super
60
- synchronized throws transient volatile
61
- ).join('|')
62
-
63
- constants = %w(true false null NaN Infinity undefined).join('|')
64
-
65
- builtins = %w(
66
- Array Boolean Date Error Function Math netscape
67
- Number Object Packages RegExp String sun decodeURI
68
- decodeURIComponent encodeURI encodeURIComponent
69
- Error eval isFinite isNaN parseFloat parseInt document this
70
- window
71
- ).join('|')
49
+ def self.keywords
50
+ @keywords ||= Set.new %w(
51
+ for in while do break return continue switch case default
52
+ if else throw try catch finally new delete typeof instanceof
53
+ void this
54
+ )
55
+ end
56
+
57
+ def self.declarations
58
+ @declarations ||= Set.new %w(var let with function)
59
+ end
60
+
61
+ def self.reserved
62
+ @reserved ||= Set.new %w(
63
+ abstract boolean byte char class const debugger double enum
64
+ export extends final float goto implements import int interface
65
+ long native package private protected public short static
66
+ super synchronized throws transient volatile
67
+ )
68
+ end
69
+
70
+ def self.constants
71
+ @constants ||= Set.new %w(true false null NaN Infinity undefined)
72
+ end
73
+
74
+ def self.builtins
75
+ @builtins ||= %w(
76
+ Array Boolean Date Error Function Math netscape
77
+ Number Object Packages RegExp String sun decodeURI
78
+ decodeURIComponent encodeURI encodeURIComponent
79
+ Error eval isFinite isNaN parseFloat parseInt document this
80
+ window
81
+ )
82
+ end
83
+
84
+ id = /[$a-zA-Z_][a-zA-Z0-9_]*/
72
85
 
73
86
  state :root do
74
87
  rule /\A\s*#!.*?\n/m, 'Comment.Preproc'
75
88
  rule %r((?<=\n)(?=\s|/|<!--)), 'Text', :slash_starts_regex
76
89
  mixin :comments_and_whitespace
77
90
  rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | ===
78
- | !== | \? | : )x,
91
+ | !== )x,
79
92
  'Operator', :slash_starts_regex
80
93
  rule %r([-<>+*%&|\^/!=]=?), 'Operator', :slash_starts_regex
81
- rule /[{(\[;,]/, 'Punctuation', :slash_starts_regex
82
- rule /[})\].]/, 'Punctuation'
83
- rule /(?:#{keywords})\b/, 'Keyword', :slash_starts_regex
84
- rule /(?:#{declarations})\b/, 'Keyword.Declaration', :slash_starts_regex
85
- rule /(?:#{reserved})\b/, 'Keyword.Reserved'
86
- rule /(?:#{constants})\b/, 'Keyword.Constant'
87
- rule /(?:#{builtins})\b/, 'Name.Builtin'
88
- rule /[$a-zA-Z_][a-zA-Z0-9_]*/, 'Name.Other'
94
+ rule /[(\[;,]/, 'Punctuation', :slash_starts_regex
95
+ rule /[)\].]/, 'Punctuation'
96
+
97
+ rule /[?]/ do
98
+ token 'Punctuation'
99
+ push :ternary
100
+ push :slash_starts_regex
101
+ end
102
+
103
+ rule /[{](?=\s*(#{id}|"[^\n]*?")\s*:)/, 'Punctuation', :object
104
+
105
+ rule /[{]/ do
106
+ token 'Punctuation'
107
+ push :block
108
+ push :slash_starts_regex
109
+ end
110
+
111
+ rule id do |m|
112
+ if self.class.keywords.include? m[0]
113
+ token 'Keyword'
114
+ push :slash_starts_regex
115
+ elsif self.class.declarations.include? m[0]
116
+ token 'Keyword.Declaration'
117
+ push :slash_starts_regex
118
+ elsif self.class.reserved.include? m[0]
119
+ token 'Keyword.Reserved'
120
+ elsif self.class.constants.include? m[0]
121
+ token 'Keyword.Constant'
122
+ elsif self.class.builtins.include? m[0]
123
+ token 'Name.Builtin'
124
+ else
125
+ token 'Name.Other'
126
+ end
127
+ end
89
128
 
90
129
  rule /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, 'Literal.Number.Float'
91
130
  rule /0x[0-9a-fA-F]+/, 'Literal.Number.Hex'
@@ -93,6 +132,33 @@ module Rouge
93
132
  rule /"(\\\\|\\"|[^"])*"/, 'Literal.String.Double'
94
133
  rule /'(\\\\|\\'|[^'])*'/, 'Literal.String.Single'
95
134
  end
135
+
136
+ # braced parts that aren't object literals
137
+ state :block do
138
+ rule /(#{id})(\s*)(:)/ do
139
+ group 'Name.Label'; group 'Text'
140
+ group 'Punctuation'
141
+ end
142
+
143
+ rule /[}]/, 'Punctuation', :pop!
144
+ mixin :root
145
+ end
146
+
147
+ # object literals
148
+ state :object do
149
+ rule /[}]/, 'Punctuation', :pop!
150
+ rule /(#{id})(\s*)(:)/ do
151
+ group 'Name.Attribute'; group 'Text'
152
+ group 'Punctuation'
153
+ end
154
+ mixin :root
155
+ end
156
+
157
+ # ternary expressions, where <id>: is not a label!
158
+ state :ternary do
159
+ rule /:/, 'Punctuation', :pop!
160
+ mixin :root
161
+ end
96
162
  end
97
163
 
98
164
  class JSON < RegexLexer
@@ -37,7 +37,7 @@ module Rouge
37
37
 
38
38
  rule /:/, 'Name.Attribute', :old_style_attr
39
39
 
40
- rule(/(?=.+?:(^a-z|$))/) { push :attribute }
40
+ rule(/(?=.+?:([^a-z]|$))/) { push :attribute }
41
41
 
42
42
  rule(//) { push :selector }
43
43
  end
@@ -76,6 +76,8 @@ module Rouge
76
76
  push :value
77
77
  end
78
78
 
79
+ rule /@extend\b/, 'Keyword', :selector
80
+
79
81
  rule /(@include)(\s+)(#{id})/ do
80
82
  group 'Keyword'; group 'Text'; group 'Name.Decorator'
81
83
  push :value
@@ -96,6 +98,7 @@ module Rouge
96
98
  rule /[$]#{id}/, 'Name.Variable'
97
99
  rule /url[(]/, 'Literal.String.Other', :string_url
98
100
  rule /#{id}(?=\s*[(])/, 'Name.Function'
101
+ rule /%#{id}/, 'Name.Decorator'
99
102
 
100
103
  # named literals
101
104
  rule /(true|false)\b/, 'Name.Pseudo'
@@ -149,6 +152,7 @@ module Rouge
149
152
  rule /:/, 'Name.Decorator', :pseudo_class
150
153
  rule /[.]/, 'Name.Class', :class
151
154
  rule /#/, 'Name.Namespace', :id
155
+ rule /%/, 'Name.Variable', :placeholder
152
156
  rule id, 'Name.Tag'
153
157
  rule /&/, 'Keyword'
154
158
  rule /[~^*!&\[\]()<>\|+=@:;,.\/?-]/, 'Operator'
@@ -192,6 +196,11 @@ module Rouge
192
196
  mixin :selector_piece
193
197
  end
194
198
 
199
+ state :placeholder do
200
+ rule id, 'Name.Variable'
201
+ mixin :selector_piece
202
+ end
203
+
195
204
  state :for do
196
205
  rule /(from|to|through)/, 'Operator.Word'
197
206
  mixin :value
@@ -7,69 +7,76 @@ module Rouge
7
7
  filenames '*.scm', '*.ss', '*.rkt'
8
8
  mimetypes 'text/x-scheme', 'application/x-scheme'
9
9
 
10
- keywords = %w(
11
- lambda define if else cond and or case let let* letrec begin
12
- do delay set! => quote quasiquote unquote unquote-splicing
13
- define-syntax let-syntax letrec-syntax syntax-rules
14
- )
10
+ def self.keywords
11
+ @keywords ||= Set.new %w(
12
+ lambda define if else cond and or case let let* letrec begin
13
+ do delay set! => quote quasiquote unquote unquote-splicing
14
+ define-syntax let-syntax letrec-syntax syntax-rules
15
+ )
16
+ end
15
17
 
16
- builtins = %w(
17
- * + - / < <= = > >= abs acos angle append apply asin
18
- assoc assq assv atan boolean? caaaar caaadr caaar caadar
19
- caaddr caadr caar cadaar cadadr cadar caddar cadddr caddr
20
- cadr call-with-current-continuation call-with-input-file
21
- call-with-output-file call-with-values call/cc car cdaaar cdaadr
22
- cdaar cdadar cdaddr cdadr cdar cddaar cddadr cddar cdddar cddddr
23
- cdddr cddr cdr ceiling char->integer char-alphabetic? char-ci<=?
24
- char-ci<? char-ci=? char-ci>=? char-ci>? char-downcase
25
- char-lower-case? char-numeric? char-ready? char-upcase
26
- char-upper-case? char-whitespace? char<=? char<? char=? char>=?
27
- char>? char? close-input-port close-output-port complex? cons
28
- cos current-input-port current-output-port denominator
29
- display dynamic-wind eof-object? eq? equal? eqv? eval
30
- even? exact->inexact exact? exp expt floor for-each force gcd
31
- imag-part inexact->exact inexact? input-port? integer->char
32
- integer? interaction-environment lcm length list list->string
33
- list->vector list-ref list-tail list? load log magnitude
34
- make-polar make-rectangular make-string make-vector map
35
- max member memq memv min modulo negative? newline not
36
- null-environment null? number->string number? numerator odd?
37
- open-input-file open-output-file output-port? pair? peek-char
38
- port? positive? procedure? quotient rational? rationalize
39
- read read-char real-part real? remainder reverse round
40
- scheme-report-environment set-car! set-cdr! sin sqrt string
41
- string->list string->number string->symbol string-append
42
- string-ci<=? string-ci<? string-ci=? string-ci>=? string-ci>?
43
- string-copy string-fill! string-length string-ref
44
- string-set! string<=? string<? string=? string>=?
45
- string>? string? substring symbol->string symbol?
46
- tan transcript-off transcript-on truncate values vector
47
- vector->list vector-fill! vector-length vector-ref
48
- vector-set! vector? with-input-from-file with-output-to-file
49
- write write-char zero?
50
- )
18
+ def self.builtins
19
+ @builtins ||= Set.new %w(
20
+ * + - / < <= = > >= abs acos angle append apply asin
21
+ assoc assq assv atan boolean? caaaar caaadr caaar caadar
22
+ caaddr caadr caar cadaar cadadr cadar caddar cadddr caddr
23
+ cadr call-with-current-continuation call-with-input-file
24
+ call-with-output-file call-with-values call/cc car cdaaar cdaadr
25
+ cdaar cdadar cdaddr cdadr cdar cddaar cddadr cddar cdddar cddddr
26
+ cdddr cddr cdr ceiling char->integer char-alphabetic? char-ci<=?
27
+ char-ci<? char-ci=? char-ci>=? char-ci>? char-downcase
28
+ char-lower-case? char-numeric? char-ready? char-upcase
29
+ char-upper-case? char-whitespace? char<=? char<? char=? char>=?
30
+ char>? char? close-input-port close-output-port complex? cons
31
+ cos current-input-port current-output-port denominator
32
+ display dynamic-wind eof-object? eq? equal? eqv? eval
33
+ even? exact->inexact exact? exp expt floor for-each force gcd
34
+ imag-part inexact->exact inexact? input-port? integer->char
35
+ integer? interaction-environment lcm length list list->string
36
+ list->vector list-ref list-tail list? load log magnitude
37
+ make-polar make-rectangular make-string make-vector map
38
+ max member memq memv min modulo negative? newline not
39
+ null-environment null? number->string number? numerator odd?
40
+ open-input-file open-output-file output-port? pair? peek-char
41
+ port? positive? procedure? quotient rational? rationalize
42
+ read read-char real-part real? remainder reverse round
43
+ scheme-report-environment set-car! set-cdr! sin sqrt string
44
+ string->list string->number string->symbol string-append
45
+ string-ci<=? string-ci<? string-ci=? string-ci>=? string-ci>?
46
+ string-copy string-fill! string-length string-ref
47
+ string-set! string<=? string<? string=? string>=?
48
+ string>? string? substring symbol->string symbol?
49
+ tan transcript-off transcript-on truncate values vector
50
+ vector->list vector-fill! vector-length vector-ref
51
+ vector-set! vector? with-input-from-file with-output-to-file
52
+ write write-char zero?
53
+ )
54
+ end
51
55
 
52
56
  id = /[a-z0-9!$\%&*+,\/:<=>?@^_~|-]+/i
53
57
 
54
- escape = Regexp.method(:escape)
55
-
56
58
  state :root do
57
59
  # comments
58
60
  rule /;.*$/, 'Comment.Single'
59
61
  rule /\s+/m, 'Text'
60
- rule /-?\d+\.\d+/, 'Number.Float'
62
+ rule /-?\d+\.\d+/, 'Literal.Number.Float'
63
+ rule /-?\d+/, 'Literal.Number.Integer'
64
+
65
+ # Racket infinitites
66
+ rule /[+-]inf[.][f0]/, 'Literal.Number'
67
+
68
+ rule /#b[01]+/, 'Literal.Number.Binary'
69
+ rule /#o[0-7]+/, 'Literal.Number.Oct'
70
+ rule /#d[0-9]+/, 'Literal.Number.Integer'
71
+ rule /#x[0-9a-f]+/i, 'Literal.Number.Hex'
72
+ rule /#[ei][\d.]+/, 'Literal.Number.Other'
61
73
 
62
- # support for uncommon kinds of numbers -
63
- # have to figure out what the characters mean
64
- # rule /(#e|#i|#b|#o|#d|#x)[\d.]+/, 'Number'
65
74
  rule /"(\\\\|\\"|[^"])*"/, 'Literal.String'
66
75
  rule /'#{id}/i, 'Literal.String.Symbol'
67
76
  rule /#\\([()\/'"._!\$%& ?=+-]{1}|[a-z0-9]+)/i,
68
77
  'Literal.String.Char'
69
78
  rule /#t|#f/, 'Name.Constant'
70
79
  rule /(?:'|#|`|,@|,|\.)/, 'Operator'
71
- rule /(?:#{keywords.map(&escape).join('|')})(?=[^\w-])/,
72
- 'Keyword'
73
80
 
74
81
  rule /(['#])(\s*)(\()/m do
75
82
  group 'Literal.String.Symbol'
@@ -84,8 +91,18 @@ module Rouge
84
91
  end
85
92
 
86
93
  state :command do
87
- rule /(?:#{builtins.map(&escape).join('|')})(?=[^\w-])/, 'Name.Builtin', :pop!
88
- rule id, 'Name.Function', :pop!
94
+ rule id, 'Name.Function' do |m|
95
+ if self.class.keywords.include? m[0]
96
+ token 'Keyword'
97
+ elsif self.class.builtins.include? m[0]
98
+ token 'Name.Builtin'
99
+ else
100
+ token 'Name.Function'
101
+ end
102
+
103
+ pop!
104
+ end
105
+
89
106
  rule(//) { pop! }
90
107
  end
91
108
 
@@ -1,5 +1,5 @@
1
1
  module Rouge
2
2
  def self.version
3
- "0.2.3"
3
+ "0.2.4"
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.2.3
4
+ version: 0.2.4
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: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor