class HtmlConditionalComment::Lexer
Converts string into array of tokens. Token is an array, first element is symbol representing the token, second element is string value.
Constants
- AND
- CLOSE
Closing statement with additional comments after “endif”
- CLOSE_PAREN
- ENDIF_STATEMENT
- FALSE
- FEATURE
- GREATER_THAN
- GREATER_THAN_EQUAL
- IF_STATEMENT
- LESS_THAN
- LESS_THAN_EQUAL
- NOT
- OPEN
Opening statement plus positive look ahead to avoid conflicts with other comments, could also have additional comments before “if”
- OPEN_PAREN
- OR
- TOKENS
- TRUE
- VERSION_VECTOR
- WHITE_SPACE
Public Class Methods
new(html_or_comment)
click to toggle source
# File lib/html-conditional-comment/lexer.rb, line 59 def initialize(html_or_comment) @scanner = StringScanner.new(html_or_comment) end
Public Instance Methods
tokenize()
click to toggle source
# File lib/html-conditional-comment/lexer.rb, line 63 def tokenize() tokens = [] open = false #Run until nothing left in string until @scanner.eos?() #Split between if the conditional comment has been opened or not #State will help handle all the other HTML we don't care about if open @scanner.skip(WHITE_SPACE) if token = @scanner.scan(CLOSE) open = false tokens << [:close, token] else #Go through token specs and scan and stop on first one token = TOKENS.inject(nil) do |previous, spec| t = @scanner.scan(spec[1]) unless t.nil?() break [spec[0], t] end end if token tokens << token else raise TokenError.new(@scanner.rest()) end end #Closed (not opened) conditional comment else #Scan till we find an open token, if not done and use the rest if match = @scanner.scan_until(OPEN) open = true #TODO Gross way to get up till scan has succeeded match = match.slice(0..-(@scanner.matched.size() + 1)) tokens << [:html, match] unless match.empty?() tokens << [:open, @scanner.matched] else tokens << [:html, @scanner.rest()] if @scanner.rest?() break end end end @scanner.reset() tokens end