Class: ShellOpts::Token

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

Constant Summary collapse

KINDS =

Each kind should have a corresponding Grammar class with the same name

[
    :program, :section, :option, :command, :spec, :argument, :usage,
    :usage_string, :brief, :text, :blank
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, lineno, charno, source) ⇒ Token

Returns a new instance of Token.



23
24
25
26
# File 'lib/shellopts/token.rb', line 23

def initialize(kind, lineno, charno, source)
  constrain kind, :program, *KINDS
  @kind, @lineno, @charno, @source = kind, lineno, charno, source
end

Instance Attribute Details

#charnoObject

Char number (one-based). The lexer may adjust the char number (eg. for blank lines)



18
19
20
# File 'lib/shellopts/token.rb', line 18

def charno
  @charno
end

#kindObject (readonly)

Kind of token



11
12
13
# File 'lib/shellopts/token.rb', line 11

def kind
  @kind
end

#linenoObject (readonly)

Line number (one-based)



14
15
16
# File 'lib/shellopts/token.rb', line 14

def lineno
  @lineno
end

#sourceObject (readonly)

Source of the token



21
22
23
# File 'lib/shellopts/token.rb', line 21

def source
  @source
end

Instance Method Details

#dumpObject



40
41
42
# File 'lib/shellopts/token.rb', line 40

def dump
  puts "#{kind}@#{lineno}:#{charno} #{source.inspect}"
end

#inspectObject



36
37
38
# File 'lib/shellopts/token.rb', line 36

def inspect()
  "<#{self.class.to_s.sub(/.*::/, "")} #{pos} #{kind.inspect} #{source.inspect}>"
end

#pos(start_lineno = 1, start_charno = 1) ⇒ Object



30
31
32
# File 'lib/shellopts/token.rb', line 30

def pos(start_lineno = 1, start_charno = 1)
  "#{start_lineno + lineno - 1}:#{start_charno + charno - 1}"
end

#to_sObject



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

def to_s() source end