Module: CultomePlayer::Command::Processor

Included in:
CultomePlayer::Command
Defined in:
lib/cultome_player/command/processor.rb

Instance Method Summary collapse

Instance Method Details

#get_command_format(type, tokens) ⇒ String

Creates a string representation of the command prototype.

Parameters:

  • type (Symbol)

    The Language structure you try to match.

  • tokens (List<Hash>)

    The Language structure you try to match.

Returns:

  • (String)

    The string representation of the command prototype.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cultome_player/command/processor.rb', line 86

def get_command_format(type, tokens)
  format = guess_command_format(type, tokens)

  return format if format.class == Symbol

  langs = format.split
  # partimos el formato y validamos cada pedazo
  tks = tokens.clone

  cmd_format = ""
  while !langs.empty? do
    # extraemos el primer elemento del formato
    lang = langs.shift

    if langs.empty?
      # volvemos a validar con el nuevo elemento del lenguaje
      cmd_format << " " << get_command_format(lang.to_sym, tks).to_s
    else
      tk = tks.shift
      cmd_format << " " << get_command_format(lang.to_sym, tk).to_s
    end
  end
  # limpiamos el formato final
  return cmd_format.strip.gsub(" ", " ")
end

#get_tokens(user_input) ⇒ List<String>

Split the user input into tokens.

Parameters:

  • user_input (String)

    The user input.

Returns:

  • (List<String>)

    The detected tokens.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cultome_player/command/processor.rb', line 20

def get_tokens(user_input)
  tokens = []
  token = ""
  capturing_string = false

  user_input.each_char do |char|
    case char
    when /[\d\w\/:@]/
      token << char
    when /["']/
      capturing_string = !capturing_string
    when /[\s]/
      if capturing_string
        token << char
      else
        tokens << token
        token = ""
      end
    else
      token << char
    end # case
  end # each

  tokens << token unless token.empty?
  raise "invalid command:unclosed string" if capturing_string

  return tokens
end

#identify_tokens(tokens) ⇒ List<Hash>

Identify detected tokens.

Parameters:

  • tokens (List<String>)

    The detected tokens.

Returns:

  • (List<Hash>)

    The hash contains keys :type and :value.



53
54
55
56
57
58
# File 'lib/cultome_player/command/processor.rb', line 53

def identify_tokens(tokens)
  tokens.map do |token|
    id = guess_token_id(token)
    id.nil? ?  {type: :unknown, value: token} : get_token_value(token, id)
  end
end

#parse(user_input) ⇒ Command

Parse a user input into a command

Parameters:

  • user_input (String)

    The user input to be parsed.

Returns:

  • (Command)

    The parsed command.



8
9
10
11
12
13
14
# File 'lib/cultome_player/command/processor.rb', line 8

def parse(user_input)
  return user_input.split("&&").collect do |usr_in|
    tokens = identify_tokens(get_tokens(usr_in.strip))
    validate_command(:command, tokens)
    CultomePlayer::Objects::Command.new(tokens.shift, tokens)
  end
end

#validate_command(type, tokens) ⇒ Boolean

Check that a the tokens identifed correspond to a player command.

Parameters:

  • type (Symbol)

    The language structure you try to match.

  • tokens (List<Hash>)

    The list of tokens identified.

Returns:

  • (Boolean)

    True if the user command match with a player command format.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cultome_player/command/processor.rb', line 65

def validate_command(type, tokens)
  current_format = get_command_format(type, tokens)
  # extraemos el primer token, que debe ser el comando
  cmd = tokens.first[:value]
  
  valid_format = semantics[cmd]
  if valid_format.nil?
    if plugins_respond_to?(cmd)
      valid_format = plugin_command_sintax(cmd)
    else
      raise 'invalid command:invalid action'
    end
  end
  return current_format =~ valid_format 
end