class STSBlockParseProcOpts

Public Class Methods

new(elems, idx) click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 3
def initialize(elems, idx)
  @size = elems.size
  @elems = elems
  @idx = idx
  @result_hash = Hash.new()
end

Public Instance Methods

arg_opt() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 76
def arg_opt()
  opt_key = ident()
  if (!peek.nil?) && peek.type == :sign && peek.e1 == "="
    next_token()  # At =
    if peek.type == :sign && peek.e1 == "[" # RHS of = is array
      next_token() # Now at [
      opt_value = array()
    elsif peek.type == :ident # RHS of = is ident, meaning just ident or function
      next_token()
      if (! peek.nil?) && peek.type == :sign && peek.e1 == "("
        opt_value = func()
      else
        opt_value = ident() # According to BNF, this should be parimary(). However, ident() is more direct and makes sense here.
      end
    elsif [:string, :num].include? peek.type
       next_token()
       opt_value = primary()
    else
      p current_token()
      raise "the token should be :ident or primaries such as :ident, :num and :string after = . Current token: " + current_token().type.to_s
    end
  else
    opt_value = true
  end
  @result_hash[opt_key.to_s] = opt_value
end
arg_opts() click to toggle source

arg_opts : arg_opt

| arg_opts arg_opt

arg_opt : IDENT = primary

| IDENT = array
| IDENT = func 
| IDENT

parimary : STRING

| NUM
| IDENT

array : [ elems ]

func : IDENT ( elems )

elems : primary

| elems , primary
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 68
def arg_opts()
  arg_opt()
  if has_next?
    next_token()
    arg_opts()
  end
end
array() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 103
def array()
  raise "array should start with [ " if ! (current_token.type == :sign && current_token.e1 == "[")

  next_token()
  ary = Array.new()
  elems( ary )

  raise "array should end with ] " if ! (current_token.type == :sign && current_token.e1 == "]")

  return ary
end
current_token() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 35
def current_token()
  @elems[@idx]
end
elems( ary ) click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 115
def elems( ary )
  ary.push primary()
  next_token
  if current_token.type == :sign && current_token.e1 == ","
    next_token
    elems( ary )
  else
    return ary
  end
end
func() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 139
def func()
  func_name = ident()
  parenthesis = next_token()
  raise "func arg should start with ( " if ! (parenthesis.type == :sign && parenthesis.e1 == "(")

  next_token()
  func_args = elems( Array.new() )
  raise "func should end with ) " if ! (current_token.type == :sign && current_token.e1 == ")")

  func_hash = {"type" => :func , "fname" => func_name.to_s , "fargs" => func_args}
  return func_hash
end
has_next?() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 10
def has_next?()
  if @idx + 1 < @size
    return true
  else
    return false
  end
end
ident() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 152
def ident()
  raise "the current token should be ident" if current_token.type != :ident 
  return type_adjust( current_token.e1, :ident)
end
next_token() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 18
def next_token()
  if has_next?
    @idx = @idx + 1
    @elems[@idx]
  else
    nil
  end
end
num() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 162
def num()
  raise "the current token should be num" if current_token.type != :num
  return type_adjust( current_token.e1, :num)
end
parse() click to toggle source

Entry point

# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 40
def parse()
  arg_opts()
  return @result_hash
end
peek() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 27
def peek()
  if has_next?
    @elems[@idx + 1]
  else
    nil
  end
end
primary() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 126
def primary()
  case current_token.type
  when :ident
    return ident()
  when :string
    return string()
  when :num
    return num()
  else
    raise "the current token should be :ident, :string or :num."
  end
end
string() click to toggle source
# File lib/statsailr/block_builder/sts_block_parse_proc_opts.rb, line 157
def string()
  raise "the current token should be string" if current_token.type != :string 
  return type_adjust( current_token.e1, :string)
end