Class: Strling::Core::Cursor

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

Overview

Cursor for tracking parser position and state

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, i = 0, extended_mode = false, in_class = 0) ⇒ Cursor

Returns a new instance of Cursor.



32
33
34
35
36
37
# File 'lib/strling/core/parser.rb', line 32

def initialize(text, i = 0, extended_mode = false, in_class = 0)
  @text = text
  @i = i
  @extended_mode = extended_mode
  @in_class = in_class
end

Instance Attribute Details

#extended_modeObject

Returns the value of attribute extended_mode.



30
31
32
# File 'lib/strling/core/parser.rb', line 30

def extended_mode
  @extended_mode
end

#iObject

Returns the value of attribute i.



30
31
32
# File 'lib/strling/core/parser.rb', line 30

def i
  @i
end

#in_classObject

Returns the value of attribute in_class.



30
31
32
# File 'lib/strling/core/parser.rb', line 30

def in_class
  @in_class
end

#textObject

Returns the value of attribute text.



30
31
32
# File 'lib/strling/core/parser.rb', line 30

def text
  @text
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/strling/core/parser.rb', line 39

def eof?
  @i >= @text.length
end

#match(s) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/strling/core/parser.rb', line 56

def match(s)
  if @text[@i...(@i + s.length)] == s
    @i += s.length
    true
  else
    false
  end
end

#peek(n = 0) ⇒ Object



43
44
45
46
# File 'lib/strling/core/parser.rb', line 43

def peek(n = 0)
  j = @i + n
  j >= @text.length ? '' : @text[j]
end

#skip_ws_and_commentsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/strling/core/parser.rb', line 65

def skip_ws_and_comments
  return if !@extended_mode || @in_class > 0

  # In free-spacing mode, ignore spaces/tabs/newlines and #-to-EOL comments
  until eof?
    ch = peek
    if " \t\r\n".include?(ch)
      @i += 1
      next
    end
    if ch == '#'
      # skip comment to end of line
      @i += 1 until eof? || "\r\n".include?(peek)
      next
    end
    break
  end
end

#takeObject



48
49
50
51
52
53
54
# File 'lib/strling/core/parser.rb', line 48

def take
  return '' if eof?

  ch = @text[@i]
  @i += 1
  ch
end