##// END OF EJS Templates
Added javascript highlightment support (http://pastie.textmate.org/50774/)...
Jean-Philippe Lang -
r705:27d6da945743
parent child
Show More
@@ -0,0 +1,176
1 # http://pastie.textmate.org/50774/
2 module CodeRay module Scanners
3
4 class JavaScript < Scanner
5
6 register_for :javascript
7
8 RESERVED_WORDS = [
9 'asm', 'break', 'case', 'continue', 'default', 'do', 'else',
10 'for', 'goto', 'if', 'return', 'switch', 'while',
11 # 'struct', 'union', 'enum', 'typedef',
12 # 'static', 'register', 'auto', 'extern',
13 # 'sizeof',
14 'typeof',
15 # 'volatile', 'const', # C89
16 # 'inline', 'restrict', # C99
17 'var', 'function','try','new','in',
18 'instanceof','throw','catch'
19 ]
20
21 PREDEFINED_CONSTANTS = [
22 'void', 'null', 'this',
23 'true', 'false','undefined',
24 ]
25
26 IDENT_KIND = WordList.new(:ident).
27 add(RESERVED_WORDS, :reserved).
28 add(PREDEFINED_CONSTANTS, :pre_constant)
29
30 ESCAPE = / [rbfnrtv\n\\\/'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
31 UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
32
33 def scan_tokens tokens, options
34
35 state = :initial
36 string_type = nil
37 regexp_allowed = true
38
39 until eos?
40
41 kind = :error
42 match = nil
43
44 if state == :initial
45
46 if scan(/ \s+ | \\\n /x)
47 kind = :space
48
49 elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
50 kind = :comment
51 regexp_allowed = false
52
53 elsif match = scan(/ \# \s* if \s* 0 /x)
54 match << scan_until(/ ^\# (?:elif|else|endif) .*? $ | \z /xm) unless eos?
55 kind = :comment
56 regexp_allowed = false
57
58 elsif regexp_allowed and scan(/\//)
59 tokens << [:open, :regexp]
60 state = :regex
61 kind = :delimiter
62
63 elsif scan(/ [-+*\/=<>?:;,!&^|()\[\]{}~%] | \.(?!\d) /x)
64 kind = :operator
65 regexp_allowed=true
66
67 elsif match = scan(/ [$A-Za-z_][A-Za-z_0-9]* /x)
68 kind = IDENT_KIND[match]
69 # if kind == :ident and check(/:(?!:)/)
70 # match << scan(/:/)
71 # kind = :label
72 # end
73 regexp_allowed=false
74
75 elsif match = scan(/["']/)
76 tokens << [:open, :string]
77 string_type = matched
78 state = :string
79 kind = :delimiter
80
81 # elsif scan(/#\s*(\w*)/)
82 # kind = :preprocessor # FIXME multiline preprocs
83 # state = :include_expected if self[1] == 'include'
84 #
85 # elsif scan(/ L?' (?: [^\'\n\\] | \\ #{ESCAPE} )? '? /ox)
86 # kind = :char
87
88 elsif scan(/0[xX][0-9A-Fa-f]+/)
89 kind = :hex
90 regexp_allowed=false
91
92 elsif scan(/(?:0[0-7]+)(?![89.eEfF])/)
93 kind = :oct
94 regexp_allowed=false
95
96 elsif scan(/(?:\d+)(?![.eEfF])/)
97 kind = :integer
98 regexp_allowed=false
99
100 elsif scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
101 kind = :float
102 regexp_allowed=false
103
104 else
105 getch
106 end
107
108 elsif state == :regex
109 if scan(/[^\\\/]+/)
110 kind = :content
111 elsif scan(/\\\/|\\\\/)
112 kind = :content
113 elsif scan(/\//)
114 tokens << [matched, :delimiter]
115 tokens << [:close, :regexp]
116 state = :initial
117 next
118 else
119 getch
120 kind = :content
121 end
122
123 elsif state == :string
124 if scan(/[^\\"']+/)
125 kind = :content
126 elsif scan(/["']/)
127 if string_type==matched
128 tokens << [matched, :delimiter]
129 tokens << [:close, :string]
130 state = :initial
131 string_type=nil
132 next
133 else
134 kind = :content
135 end
136 elsif scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /mox)
137 kind = :char
138 elsif scan(/ \\ | $ /x)
139 kind = :error
140 state = :initial
141 else
142 raise "else case \" reached; %p not handled." % peek(1), tokens
143 end
144
145 # elsif state == :include_expected
146 # if scan(/<[^>\n]+>?|"[^"\n\\]*(?:\\.[^"\n\\]*)*"?/)
147 # kind = :include
148 # state = :initial
149 #
150 # elsif match = scan(/\s+/)
151 # kind = :space
152 # state = :initial if match.index ?\n
153 #
154 # else
155 # getch
156 #
157 # end
158 #
159 else
160 raise 'else-case reached', tokens
161
162 end
163
164 match ||= matched
165 # raise [match, kind], tokens if kind == :error
166
167 tokens << [match, kind]
168
169 end
170 tokens
171
172 end
173
174 end
175
176 end end No newline at end of file
@@ -84,6 +84,7 module FileType
84 'cpp' => :c,
84 'cpp' => :c,
85 'c' => :c,
85 'c' => :c,
86 'h' => :c,
86 'h' => :c,
87 'js' => :javascript,
87 'xml' => :xml,
88 'xml' => :xml,
88 'htm' => :html,
89 'htm' => :html,
89 'html' => :html,
90 'html' => :html,
General Comments 0
You need to be logged in to leave comments. Login now