Class: CSSPool::CSS::Tokenizer

Inherits:
Parser
  • Object
show all
Defined in:
lib/csspool/css/tokenizer.rb

Direct Known Subclasses

SAC::Parser

Defined Under Namespace

Classes: ScanError

Constant Summary

Constants inherited from Parser

Parser::Racc_arg, Parser::Racc_debug_parser, Parser::Racc_token_to_s_table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#_reduce_none

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



15
16
17
# File 'lib/csspool/css/tokenizer.rb', line 15

def filename
  @filename
end

#linenoObject (readonly)

Returns the value of attribute lineno.



14
15
16
# File 'lib/csspool/css/tokenizer.rb', line 14

def lineno
  @lineno
end

#stateObject

Returns the value of attribute state.



16
17
18
# File 'lib/csspool/css/tokenizer.rb', line 16

def state
  @state
end

Instance Method Details

#_next_tokenObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/csspool/css/tokenizer.rb', line 55

def _next_token
  text = @ss.peek(1)
  @lineno  +=  1  if text == "\n"
  token = case @state
  when nil
    case
    when (text = @ss.scan(/url\([\s]*("([^\n\r\f\\"]|\\\n|\r\n|\r|\f|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*"|'([^\n\r\f\\']|\\\n|\r\n|\r|\f|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*')[\s]*\)/i))
       action { [:URI, st(text)] }

    when (text = @ss.scan(/url\([\s]*([!#\$%&*-~]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*[\s]*\)/i))
       action { [:URI, st(text)] }

    when (text = @ss.scan(/U\+[0-9a-fA-F?]{1,6}(-[0-9a-fA-F]{1,6})?/i))
       action {[:UNICODE_RANGE, st(text)] }

    when (text = @ss.scan(/[\s]*\/\*(.|[\s]*)*?\*\/[\s]*/i))
       action { next_token }

    when (text = @ss.scan(/[-@]?([_A-Za-z]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])([_A-Za-z0-9-]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f]|[.])*\(\s*/i))
       action { [:FUNCTION, st(text)] }

    when (text = @ss.scan(/[\s]*@import[\s]*/i))
       action { [:IMPORT_SYM, st(text)] }

    when (text = @ss.scan(/[\s]*@page[\s]*/i))
       action { [:PAGE_SYM, st(text)] }

    when (text = @ss.scan(/[\s]*@charset[\s]*/i))
       action { [:CHARSET_SYM, st(text)] }

    when (text = @ss.scan(/[\s]*@media[\s]*/i))
       action { [:MEDIA_SYM, st(text)] }

    when (text = @ss.scan(/[\s]*!([\s]*|[\s]*\/\*(.|[\s]*)*?\*\/[\s]*)important[\s]*/i))
       action { [:IMPORTANT_SYM, st(text)] }

    when (text = @ss.scan(/[-@]?([_A-Za-z]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])([_A-Za-z0-9-]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*/i))
       action { [:IDENT, st(text)] }

    when (text = @ss.scan(/\#([_A-Za-z0-9-]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])+/i))
       action { [:HASH, st(text)] }

    when (text = @ss.scan(/[\s]*~=[\s]*/i))
       action { [:INCLUDES, st(text)] }

    when (text = @ss.scan(/[\s]*\|=[\s]*/i))
       action { [:DASHMATCH, st(text)] }

    when (text = @ss.scan(/[\s]*\^=[\s]*/i))
       action { [:PREFIXMATCH, st(text)] }

    when (text = @ss.scan(/[\s]*\$=[\s]*/i))
       action { [:SUFFIXMATCH, st(text)] }

    when (text = @ss.scan(/[\s]*\*=[\s]*/i))
       action { [:SUBSTRINGMATCH, st(text)] }

    when (text = @ss.scan(/[\s]*!=[\s]*/i))
       action { [:NOT_EQUAL, st(text)] }

    when (text = @ss.scan(/[\s]*=[\s]*/i))
       action { [:EQUAL, st(text)] }

    when (text = @ss.scan(/[\s]*\)/i))
       action { [:RPAREN, st(text)] }

    when (text = @ss.scan(/[\s]*\([\s]*/i))
       action { [:LPAREN, st(text)] }

    when (text = @ss.scan(/\[[\s]*/i))
       action { [:LSQUARE, st(text)] }

    when (text = @ss.scan(/[\s]*\]/i))
       action { [:RSQUARE, st(text)] }

    when (text = @ss.scan(/[\s]*\+[\s]*/i))
       action { [:PLUS, st(text)] }

    when (text = @ss.scan(/[\s]*\{[\s]*/i))
       action { [:LBRACE, st(text)] }

    when (text = @ss.scan(/[\s]*\}[\s]*/i))
       action { [:RBRACE, st(text)] }

    when (text = @ss.scan(/[\s]*>[\s]*/i))
       action { [:GREATER, st(text)] }

    when (text = @ss.scan(/[\s]*,[\s]*/i))
       action { [:COMMA, st(',')] }

    when (text = @ss.scan(/[\s]*;[\s]*/i))
       action { [:SEMI, st(';')] }

    when (text = @ss.scan(/\*/i))
       action { [:STAR, st(text)] }

    when (text = @ss.scan(/[\s]*~[\s]*/i))
       action { [:TILDE, st(text)] }

    when (text = @ss.scan(/\:not\([\s]*/i))
       action { [:NOT, st(text)]  }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)em[\s]*/i))
       action { [:EMS, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)ex[\s]*/i))
       action { [:EXS, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)(px|cm|mm|in|pt|pc)[\s]*/i))
       action { [:LENGTH, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)(deg|rad|grad)[\s]*/i))
       action { [:ANGLE, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)(ms|s)[\s]*/i))
       action { [:TIME, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)[k]?hz[\s]*/i))
       action { [:FREQ, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)%[\s]*/i))
       action { [:PERCENTAGE, st(text)] }

    when (text = @ss.scan(/[\s]*([0-9]*\.[0-9]+|[0-9]+)[\s]*/i))
       action { [:NUMBER, st(text)] }

    when (text = @ss.scan(/[\s]*\/\/[\s]*/i))
       action { [:DOUBLESLASH, st(text)] }

    when (text = @ss.scan(/[\s]*\/[\s]*/i))
       action { [:SLASH, st('/')] }

    when (text = @ss.scan(/<!--/i))
       action { [:CDO, st(text)] }

    when (text = @ss.scan(/-->/i))
       action { [:CDC, st(text)] }

    when (text = @ss.scan(/[\s]*\-(?![-@]?([_A-Za-z]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])([_A-Za-z0-9-]|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*)[\s]*/i))
       action { [:MINUS, st(text)] }

    when (text = @ss.scan(/[\s]*\+[\s]*/i))
       action { [:PLUS, st(text)] }

    when (text = @ss.scan(/[\s]+/i))
       action { [:S, st(text)] }

    when (text = @ss.scan(/("([^\n\r\f\\"]|\\\n|\r\n|\r|\f|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*"|'([^\n\r\f\\']|\\\n|\r\n|\r|\f|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*')/i))
       action { [:STRING, st(text)] }

    when (text = @ss.scan(/("([^\n\r\f\\"]|\\\n|\r\n|\r|\f|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*|'([^\n\r\f\\']|\\\n|\r\n|\r|\f|[^\0-\177]|\\[0-9A-Fa-f]{1,6}(\r\n|[\s])?|\\[^\n\r\f0-9A-Fa-f])*)/i))
       action { [:INVALID, st(text)] }

    when (text = @ss.scan(/./i))
       action { [st(text), st(text)] }

    else
      text = @ss.string[@ss.pos .. -1]
      raise  ScanError, "can not match: '" + text + "'"
    end  # if

  else
    raise  ScanError, "undefined state: '" + state.to_s + "'"
  end  # case state
  token
end

#actionObject



24
25
26
# File 'lib/csspool/css/tokenizer.rb', line 24

def action
  yield
end

#load_file(filename) ⇒ Object



34
35
36
37
38
39
# File 'lib/csspool/css/tokenizer.rb', line 34

def load_file( filename )
  @filename = filename
  open(filename, "r") do |f|
    scan_setup(f.read)
  end
end

#next_tokenObject



47
48
49
50
51
52
53
# File 'lib/csspool/css/tokenizer.rb', line 47

def next_token
  return if @ss.eos?
  
  # skips empty actions
  until token = _next_token or @ss.eos?; end
  token
end

#scan_file(filename) ⇒ Object



41
42
43
44
# File 'lib/csspool/css/tokenizer.rb', line 41

def scan_file( filename )
  load_file(filename)
  do_parse
end

#scan_setup(str) ⇒ Object



18
19
20
21
22
# File 'lib/csspool/css/tokenizer.rb', line 18

def scan_setup(str)
  @ss = StringScanner.new(str)
  @lineno =  1
  @state  = nil
end

#scan_str(str) ⇒ Object Also known as: scan



28
29
30
31
# File 'lib/csspool/css/tokenizer.rb', line 28

def scan_str(str)
  scan_setup(str)
  do_parse
end

#st(o) ⇒ Object

def _next_token



222
223
224
225
# File 'lib/csspool/css/tokenizer.rb', line 222

def st o
  @st ||= Hash.new { |h,k| h[k] = k }
  @st[o]
end