class JGrep::Scanner

Attributes

arguments[RW]
token_index[RW]

Public Class Methods

new(arguments) click to toggle source
# File lib/parser/scanner.rb, line 5
def initialize(arguments)
  @token_index = 0
  @arguments = arguments
end

Public Instance Methods

get_token() click to toggle source

Scans the input string and identifies single language tokens

# File lib/parser/scanner.rb, line 11
def get_token
  return nil if @token_index >= @arguments.size

  begin
    case chr(@arguments[@token_index])
    when "["
      return "statement", gen_substatement

    when "]"
      return "]"

    when "("
      return "(", "("

    when ")"
      return ")", ")"

    when "n"
      if (chr(@arguments[@token_index + 1]) == "o") && (chr(@arguments[@token_index + 2]) == "t") && ((chr(@arguments[@token_index + 3]) == " ") || (chr(@arguments[@token_index + 3]) == "("))
        @token_index += 2
        return "not", "not"
      else
        gen_statement
      end

    when "!"
      return "not", "not"

    when "a"
      if (chr(@arguments[@token_index + 1]) == "n") && (chr(@arguments[@token_index + 2]) == "d") && ((chr(@arguments[@token_index + 3]) == " ") || (chr(@arguments[@token_index + 3]) == "("))
        @token_index += 2
        return "and", "and"
      else
        gen_statement
      end

    when "&"
      if chr(@arguments[@token_index + 1]) == "&"
        @token_index += 1
        return "and", "and"
      else
        gen_statement
      end

    when "o"
      if (chr(@arguments[@token_index + 1]) == "r") && ((chr(@arguments[@token_index + 2]) == " ") || (chr(@arguments[@token_index + 2]) == "("))
        @token_index += 1
        return "or", "or"
      else
        gen_statement
      end

    when "|"
      if chr(@arguments[@token_index + 1]) == "|"
        @token_index += 1
        return "or", "or"
      else
        gen_statement
      end

    when "+"
      value = ""
      i = @token_index + 1

      begin
        value += chr(@arguments[i])
        i += 1
      end until (i >= @arguments.size) || (chr(@arguments[i]) =~ /\s|\)/)

      @token_index = i - 1
      return "+", value

    when "-"
      value = ""
      i = @token_index + 1

      begin
        value += chr(@arguments[i])
        i += 1
      end until (i >= @arguments.size) || (chr(@arguments[i]) =~ /\s|\)/)

      @token_index = i - 1
      return "-", value

    when " "
      return " ", " "

    else
      gen_statement
    end
  end
rescue NoMethodError
  raise "Error. Expression cannot be parsed."
end

Private Instance Methods

chr(character) click to toggle source

Compatibility with 1.8.7, which returns a Fixnum from String#[]

# File lib/parser/scanner.rb, line 160
def chr(character)
  character.chr unless character.nil?
end
gen_statement() click to toggle source
# File lib/parser/scanner.rb, line 120
def gen_statement
  current_token_value = ""
  j = @token_index

  begin
    if chr(@arguments[j]) == "/"
      begin
        current_token_value << chr(@arguments[j])
        j += 1
        if chr(@arguments[j]) == "/"
          current_token_value << "/"
          break
        end
      end until (j >= @arguments.size) || (chr(@arguments[j]) =~ /\//)
    else
      begin
        current_token_value << chr(@arguments[j])
        j += 1
        if chr(@arguments[j]) =~ /'|"/
          begin
            current_token_value << chr(@arguments[j])
            j += 1
          end until (j >= @arguments.size) || (chr(@arguments[j]) =~ /'|"/)
        end
      end until (j >= @arguments.size) || (chr(@arguments[j]) =~ /\s|\)|\]/)
    end
  rescue
    raise "Invalid token found - '#{current_token_value}'"
  end

  if current_token_value =~ /^(and|or|not|!)$/
    raise "Class name cannot be 'and', 'or', 'not'. Found '#{current_token_value}'"
  end

  @token_index += current_token_value.size - 1

  ["statement", current_token_value]
end
gen_substatement() click to toggle source
# File lib/parser/scanner.rb, line 108
def gen_substatement
  @token_index += 1
  returnval = []

  while (val = get_token) != "]"
    @token_index += 1
    returnval << val unless val[0] == " "
  end

  returnval
end