Skip to content
Kward Search API index

Class: Kward::NativeShell::Word

Inherits:
Struct
  • Object
show all
Defined in:
lib/kward/shell/native_syntax.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parts ⇒ Object

Returns the value of attribute parts

Returns:

  • (Object) —

    the current value of parts



15
16
17
# File 'lib/kward/shell/native_syntax.rb', line 15

def parts
  @parts
end

Instance Method Details

#array_element ⇒ Object



35
36
37
38
# File 'lib/kward/shell/native_syntax.rb', line 35

def array_element
  return unless parts.first&.kind == :literal && !parts.first.quoted && parts.first.text.start_with?("[")
  subscript_assignment(1, nil)
end

#assignment ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kward/shell/native_syntax.rb', line 22

def assignment
  first = parts.first
  return unless first && first.kind == :literal && !first.quoted
  if (array = first.text.match(/\A([A-Za-z_][A-Za-z0-9_]*)\[/))
    return subscript_assignment(array[0].length, array[1])
  end
  match = first.text.match(/\A([A-Za-z_][A-Za-z0-9_]*)=(.*)\z/m)
  return unless match

  value = [Part.new(kind: :literal, text: match[2], quoted: false)] + parts.drop(1)
  [match[1], Word.new(parts: value)]
end

#literal ⇒ Object



16
17
18
19
20
# File 'lib/kward/shell/native_syntax.rb', line 16

def literal
  return unless parts.all? { |part| part.kind == :literal && !part.quoted }

  parts.map(&:text).join
end

#subscript_assignment(prefix, name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kward/shell/native_syntax.rb', line 40

def subscript_assignment(prefix, name)
  key = []
  parts.each_with_index do |part, index|
    text = index.zero? ? part.text[prefix..] : part.text
    if part.kind == :literal && !part.quoted && (ending = text.match(/\](\+?)=/))
      finish = ending.begin(0)
      key << Part.new(kind: :literal, text: text[0...finish], quoted: false)
      value = [Part.new(kind: :literal, text: text[ending.end(0)..], quoted: false)] + parts.drop(index + 1)
      return [Reference.new(name: name, subscript: Word.new(parts: key), append: ending[1] == "+"), Word.new(parts: value)]
    end
    key << Part.new(kind: part.kind, text: text, quoted: part.quoted)
  end
  nil
end