class Gammo::Tokenizer::ScriptScanner

Attributes

buffer[R]
raw_tag[R]
scanner[R]

Public Class Methods

new(scanner, raw_tag:, debug: false) click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 12
def initialize(scanner, raw_tag:, debug: false)
  @scanner = scanner
  @buffer  = ''
  @raw_tag = raw_tag
  @debug   = debug
end

Public Instance Methods

scan() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 19
def scan
  scan_script_data
  buffer
end

Private Instance Methods

consume() { |byte| ... } click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 192
def consume
  return unless byte = scanner.get_byte
  buffer << byte
  yield byte
end
revert_byte() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 198
def revert_byte
  @buffer = buffer.slice(0, buffer.length - 1)
  scanner.unscan
end
scan_raw_end_tag?() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 203
def scan_raw_end_tag?
  raw_tag.each_char do |ch|
    return false unless byte = scanner.get_byte
    if byte.downcase != ch
      scanner.unscan
      return false
    end
    buffer << byte
  end
  case byte = scanner.get_byte
  when ?>, ?\s, ?/
    desired = 3 + raw_tag.length
    scanner.pos -= desired
    @buffer = buffer.slice(0, buffer.length - desired + 1)
    return true
  when nil
    return false
  else
    buffer << byte
  end
  scanner.unscan
  @buffer = buffer.slice(0, buffer.length - 1)
  false
end
scan_script_data() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 34
def scan_script_data
 with_extendable_stack do
    consume do |byte|
      byte == ?< ? scan_script_data_less_than_sign : scan_script_data
    end
 end
end
scan_script_data_double_escape_end() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 180
def scan_script_data_double_escape_end
  if scan_raw_end_tag?
    end_tag = "</#{raw_tag}>"
    # Last matched char needs to be concatenated.
    buffer << scanner.string.slice(scanner.pos, end_tag.length)
    scanner.pos += end_tag.length
    return scan_script_data_escaped
  end
  return if scanner.eos?
  scan_script_data_double_escaped
end
scan_script_data_double_escape_start() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 119
def scan_script_data_double_escape_start
  revert_byte
  'script'.each_char.with_index do |ch, index|
    ch = scanner.get_byte
    buffer << ch
    return if scanner.eos?
    unless ch.downcase == 'script'[index]
      revert_byte
      return scan_script_data_escaped
    end
  end
  byte = scanner.get_byte
  buffer << byte
  return if scanner.eos?
  case byte
  when ?\s, ?/, ?> then return scan_script_data_double_escaped
  else
    revert_byte
    scan_script_data_escaped
  end
end
scan_script_data_double_escaped() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 141
def scan_script_data_double_escaped
  consume do |byte|
    case byte
    when ?- then return scan_script_data_double_escaped_dash
    when ?< then return scan_script_data_double_escaped_less_than_sign
    else return scan_script_data_double_escaped
    end
  end
end
scan_script_data_double_escaped_dash() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 151
def scan_script_data_double_escaped_dash
  consume do |byte|
    case byte
    when ?- then return scan_script_data_double_escaped_dash_dash
    when ?< then return scan_script_data_double_escaped_less_than_sign
    else return scan_script_data_double_escaped
    end
  end
end
scan_script_data_double_escaped_dash_dash() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 161
def scan_script_data_double_escaped_dash_dash
  consume do |byte|
    case byte
    when ?- then return scan_script_data_double_escaped_dash_dash
    when ?< then return scan_script_data_double_escaped_less_than_sign
    when ?> then return scan_script_data
    else return scan_script_data_double_escaped
    end
  end
end
scan_script_data_double_escaped_less_than_sign() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 172
def scan_script_data_double_escaped_less_than_sign
  consume do |byte|
    return scan_script_data_double_escape_end if byte == ?/
    revert_byte
    scan_script_data_double_escaped
  end
end
scan_script_data_end_tag_open() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 53
def scan_script_data_end_tag_open
  return if scan_raw_end_tag? || scanner.eos?
  scan_script_data
end
scan_script_data_escape_start() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 58
def scan_script_data_escape_start
  consume do |byte|
    return scan_script_data_escape_start_dash if byte == ?-
    revert_byte
    scan_script_data
  end
end
scan_script_data_escape_start_dash() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 66
def scan_script_data_escape_start_dash
  consume do |byte|
    return scan_script_data_escaped_dash_dash if byte == ?-
    revert_byte
    scan_script_data
  end
end
scan_script_data_escaped() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 74
def scan_script_data_escaped
  consume do |byte|
    case byte
    when ?- then return scan_script_data_escaped_dash
    when ?< then return scan_script_data_escaped_less_than_sign
    else return scan_script_data_escaped
    end
  end
end
scan_script_data_escaped_dash() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 84
def scan_script_data_escaped_dash
  consume do |byte|
    case byte
    when ?- then return scan_script_data_escaped_dash_dash
    when ?< then return scan_script_data_escaped_less_than_sign
    else return scan_script_data_escaped
    end
  end
end
scan_script_data_escaped_dash_dash() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 94
def scan_script_data_escaped_dash_dash
  consume do |byte|
    case byte
    when ?- then return scan_script_data_escaped_dash_dash
    when ?< then return scan_script_data_escaped_less_than_sign
    when ?> then return scan_script_data
    else return scan_script_data_escaped
    end
  end
end
scan_script_data_escaped_end_tag_open() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 114
def scan_script_data_escaped_end_tag_open
  return if scan_raw_end_tag? || scanner.eos?
  scan_script_data_escaped
end
scan_script_data_escaped_less_than_sign() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 105
def scan_script_data_escaped_less_than_sign
  consume do |byte|
    return scan_script_data_escaped_end_tag_open if byte == ?/
    return scan_script_data_double_escape_start if byte =~ /[a-zA-Z]/
    revert_byte
    scan_script_data
  end
end
scan_script_data_less_than_sign() click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 42
def scan_script_data_less_than_sign
  consume do |byte|
    case byte
    when ?/ then return scan_script_data_end_tag_open
    when ?! then return scan_script_data_escape_start
    end
    revert_byte
    scan_script_data
  end
end
with_extendable_stack() { || ... } click to toggle source
# File lib/gammo/tokenizer/script_scanner.rb, line 26
def with_extendable_stack
  begin
    yield
  rescue SystemStackError
    Fiber.new { yield }.resume
  end
end