Class: SportDb::Parser::Tokens

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/parser/parser.rb

Overview

token iter/find better name

e.g. TokenBuffer/Scanner or such ??

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Tokens

Returns a new instance of Tokens.



16
17
18
19
# File 'lib/sportdb/parser/parser.rb', line 16

def initialize( tokens )
    @tokens = tokens
    @pos = 0
end

Instance Method Details

#collect(&blk) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sportdb/parser/parser.rb', line 81

def collect( &blk )
    tokens = []
    loop do
      break if eos?
      tokens <<  if block_given?
                    blk.call( self.next )
                 else
                    self.next
                 end
    end
    tokens
end

#curObject

return token type (e.g. :text, :num, etc.)



48
# File 'lib/sportdb/parser/parser.rb', line 48

def cur()           peek(0); end

#eos?Boolean

Returns:

  • (Boolean)


22
# File 'lib/sportdb/parser/parser.rb', line 22

def eos?() @pos >= @tokens.size; end

#include?(*types) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/sportdb/parser/parser.rb', line 25

def include?( *types )
    pos = @pos
    ## puts "  starting include? #{types.inspect} @ #{pos}"
    while pos < @tokens.size do
        return true   if types.include?( @tokens[pos][0] )
        pos +=1
    end
    false
end

#match?(*pattern) ⇒ Boolean

pattern e.g. [:text, [:vs,:score], :text]

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/sportdb/parser/parser.rb', line 36

def match?( *pattern )
    ## puts "  starting match? #{pattern.inspect} @ #{@pos}"
    pattern.each_with_index do |types,offset|
        ## if single symbol wrap in array
        types = types.is_a?(Array) ? types : [types]
        return false  unless types.include?( peek(offset) )
    end
    true
end

#nextObject

note - returns complete token



70
71
72
73
74
75
76
77
78
79
# File 'lib/sportdb/parser/parser.rb', line 70

def next
   # if @pos >= @tokens.size
   #     raise ArgumentError, "end of array - #{@pos} >= #{@tokens.size}"
   # end
   #   throw (standard) end of iteration here why? why not?

    t = @tokens[@pos]
    @pos += 1
    t
end

#peek(offset = 1) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/sportdb/parser/parser.rb', line 60

def peek(offset=1)
    ## return nil if eos
    if @pos+offset >= @tokens.size
        nil
    else
       @tokens[@pos+offset][0]
    end
end

#posObject



21
# File 'lib/sportdb/parser/parser.rb', line 21

def pos()  @pos; end

#text(offset = 0) ⇒ Object

return content (assumed to be text)



50
51
52
53
54
55
56
57
# File 'lib/sportdb/parser/parser.rb', line 50

def text(offset=0)
    ## raise error - why? why not?
    ##   return nil?
    if peek( offset ) != :text
        raise ArgumentError, "text(#{offset}) - token not a text type"
    end
    @tokens[@pos+offset][1]
end