Class: ShellOpts::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/shellopts/lexer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lineno, charno, source) ⇒ Line

Returns a new instance of Line.



9
10
11
12
13
# File 'lib/shellopts/lexer.rb', line 9

def initialize(lineno, charno, source)
  @lineno, @source = lineno, source
  @charno = charno + ((@source =~ /(\S.*?)\s*$/) || 0)
  @text = $1 || ""
end

Instance Attribute Details

#charnoObject (readonly)

Returns the value of attribute charno.



6
7
8
# File 'lib/shellopts/lexer.rb', line 6

def charno
  @charno
end

#linenoObject (readonly)

Returns the value of attribute lineno.



5
6
7
# File 'lib/shellopts/lexer.rb', line 5

def lineno
  @lineno
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/shellopts/lexer.rb', line 4

def source
  @source
end

#textObject (readonly)

Returns the value of attribute text.



7
8
9
# File 'lib/shellopts/lexer.rb', line 7

def text
  @text
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


15
# File 'lib/shellopts/lexer.rb', line 15

def blank?() @text == "" end

#dumpObject



34
# File 'lib/shellopts/lexer.rb', line 34

def dump() puts "#{lineno}:#{charno} #{text.inspect}" end

#to_sObject



33
# File 'lib/shellopts/lexer.rb', line 33

def to_s() text end

#wordsObject

Split on whitespace while keeping track of character position. Returns array of char, word tuples



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shellopts/lexer.rb', line 21

def words
  return @words if @words
  @words = []
  charno = self.charno
  text.scan(/(\s*)(\S*)/)[0..-2].each { |spaces, word|
    charno += spaces.size
    @words << [charno, word] if word != ""
    charno += word.size
  }
  @words
end