class Jkf::Parser::Base

Base of Parser

Public Instance Methods

parse(input) click to toggle source

start parse

@param [String] input

@return [Hash] JKF

# File lib/jkf/parser/base.rb, line 9
def parse(input)
  @input = input.clone

  @current_pos = 0
  @reported_pos = 0
  @cached_pos = 0
  @cached_pos_details = { line: 1, column: 1, seenCR: false }
  @max_fail_pos = 0
  @max_fail_expected = []
  @silent_fails = 0

  @result = parse_root

  if success? && @current_pos == @input.size
    return @result
  else
    fail(type: "end", description: "end of input") if failed? && @current_pos < input.size
    raise ParseError
  end
end

Protected Instance Methods

fail(expected) click to toggle source

record failure

# File lib/jkf/parser/base.rb, line 107
def fail(expected)
  return if @current_pos < @max_fail_pos

  if @current_pos > @max_fail_pos
    @max_fail_pos = @current_pos
    @max_fail_expected = []
  end

  @max_fail_expected << expected
end
failed?() click to toggle source
# File lib/jkf/parser/base.rb, line 36
def failed?; !success?; end
match_digit() click to toggle source

match digit

# File lib/jkf/parser/base.rb, line 81
def match_digit
  match_regexp(/^\d/)
end
match_digits() click to toggle source

match digits

# File lib/jkf/parser/base.rb, line 86
def match_digits
  stack = []
  matched = match_digit
  while matched != :failed
    stack << matched
    matched = match_digit
  end
  stack
end
match_digits!() click to toggle source

match digit one ore more

# File lib/jkf/parser/base.rb, line 97
def match_digits!
  matched = match_digits
  if matched.empty?
    :failed
  else
    matched
  end
end
match_regexp(reg) click to toggle source

match regexp

# File lib/jkf/parser/base.rb, line 39
def match_regexp(reg)
  ret = nil
  if matched = reg.match(@input[@current_pos])
    ret = matched.to_s
    @current_pos += ret.size
  else
    ret = :failed
    fail(type: "class", value: reg.inspect, description: reg.inspect) if @silent_fails == 0
  end
  ret
end
match_space() click to toggle source

match space

# File lib/jkf/parser/base.rb, line 65
def match_space
  match_str(" ")
end
match_spaces() click to toggle source

match space one or more

# File lib/jkf/parser/base.rb, line 70
def match_spaces
  stack = []
  matched = match_space
  while matched != :failed
    stack << matched
    matched = match_space
  end
  stack
end
match_str(str) click to toggle source

match string

# File lib/jkf/parser/base.rb, line 52
def match_str(str)
  ret = nil
  if @input[@current_pos, str.size] == str
    ret = str
    @current_pos += str.size
  else
    ret = :failed
    fail(type: "literal", value: str, description: "\"#{str}\"") if @slient_fails == 0
  end
  ret
end
success?() click to toggle source
# File lib/jkf/parser/base.rb, line 32
def success?
  @result != :failed
end