Class: MetaParse::MetaScanner

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/meta_parse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, parser = nil) ⇒ MetaScanner

Initialize MetaScanner with supplied string as input. Optionally-supplied parser will be receiver of Symbol message sent by subclass, FunctionMatcher.



96
97
98
99
# File 'lib/meta_parse.rb', line 96

def initialize(string, parser=nil)
  super string
  @parser = parser
end

Instance Attribute Details

#parserObject

Returns the value of attribute parser.



91
92
93
# File 'lib/meta_parse.rb', line 91

def parser
  @parser
end

Instance Method Details

#match_char(char) ⇒ Object

NOTE: This is a special case and could actually be handled by match_string if necessary.



111
112
113
114
115
116
117
# File 'lib/meta_parse.rb', line 111

def match_char(char)
  c = peek(1)
  if c == char
    self.pos += 1
    c
  end
end

#match_string(str, position2 = 0) ⇒ Object

Match and return a string or return nil, updating internal position on match.



122
123
124
125
126
127
# File 'lib/meta_parse.rb', line 122

def match_string(str, position2=0)
  if (string.equal_at(pos, str, position2))
    self.pos += str.length
    str
  end
end

#metaObject

MetaScanner is already a MetaScanner



103
104
105
# File 'lib/meta_parse.rb', line 103

def meta
  self
end

#scan(spec) ⇒ Object

Scan for a Regexp or string, returning any match, or nil, and updating internal position on match.



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/meta_parse.rb', line 132

def scan(spec)
  case spec
  when Regexp
    result = super spec
    matched
  when String
    if spec.length > 0
      match_string(spec)
    else
      match_char(spec)
    end
  end
end