Class: StringScanner

Inherits:
Object show all
Defined in:
opal/stdlib/strscan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ StringScanner

Returns a new instance of StringScanner


5
6
7
8
9
10
11
# File 'opal/stdlib/strscan.rb', line 5

def initialize(string)
  @string  = string
  @pos     = 0
  @matched = nil
  @working = string
  @match = []
end

Instance Attribute Details

#matchedObject (readonly)

Returns the value of attribute matched


3
4
5
# File 'opal/stdlib/strscan.rb', line 3

def matched
  @matched
end

#posObject

Returns the value of attribute pos


2
3
4
# File 'opal/stdlib/strscan.rb', line 2

def pos
  @pos
end

#stringObject (readonly)

Returns the value of attribute string


13
14
15
# File 'opal/stdlib/strscan.rb', line 13

def string
  @string
end

Instance Method Details

#[](idx) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'opal/stdlib/strscan.rb', line 49

def [](idx)
  %x{
    var match = #@match;

    if (idx < 0) {
      idx += match.length;
    }

    if (idx < 0 || idx >= match.length) {
      return nil;
    }

    if (match[idx] == null) {
      return nil;
    }

    return match[idx];
  }
end

#beginning_of_line?Boolean Also known as: bol?

Returns:


15
16
17
# File 'opal/stdlib/strscan.rb', line 15

def beginning_of_line?
  `#@pos === 0 || #@string.charAt(#@pos - 1) === "\n"`
end

#check(regex) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
# File 'opal/stdlib/strscan.rb', line 69

def check(regex)
  %x{
    var regexp = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)),
        result = regexp.exec(#@working);

    if (result == null) {
      return #{self}.matched = nil;
    }

    return #{self}.matched = result[0];
  }
end

#eos?Boolean

Returns:


86
87
88
# File 'opal/stdlib/strscan.rb', line 86

def eos?
  `#@working.length === 0`
end

#get_byteObject Also known as: getch


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'opal/stdlib/strscan.rb', line 110

def get_byte()
  %x{
    var result = nil;
    if (#{self}.pos < #{self}.string.length) {
      self.prev_pos = self.pos;
      #{self}.pos += 1;
      result = #{self}.matched = #{self}.working.substring(0, 1);
      #{self}.working = #{self}.working.substring(1);
    }
    else {
      #{self}.matched = nil;
    }

    return result;
  }
end

#peek(length) ⇒ Object


82
83
84
# File 'opal/stdlib/strscan.rb', line 82

def peek(length)
  `#@working.substring(0, length)`
end

#resetObject


141
142
143
144
145
# File 'opal/stdlib/strscan.rb', line 141

def reset
  @working = @string
  @matched = nil
  @pos     = 0
end

#restObject


147
148
149
# File 'opal/stdlib/strscan.rb', line 147

def rest
  @working
end

#rest?Boolean

Returns:


151
152
153
# File 'opal/stdlib/strscan.rb', line 151

def rest?
  `#@working.length !== 0`
end

#scan(regex) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'opal/stdlib/strscan.rb', line 20

def scan(regex)
  %x{
    var regex  = new RegExp('^' + regex.toString().substring(1, regex.toString().length - 1)),
        result = regex.exec(#@working);

    if (result == null) {
      return #{self}.matched = nil;
    }
    else if (typeof(result) === 'object') {
      #@prev_pos = #@pos;
      #@pos     += result[0].length;
      #@working  = #@working.substring(result[0].length);
      #@matched  = result[0];
      #@match    = result;

      return result[0];
    }
    else if (typeof(result) === 'string') {
      #@pos     += result.length;
      #@working  = #@working.substring(result.length);

      return result;
    }
    else {
      return nil;
    }
  }
end

#skip(re) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'opal/stdlib/strscan.rb', line 90

def skip(re)
  %x{
    re = new RegExp('^' + re.source)
    var result = re.exec(#@working);

    if (result == null) {
      return #{self}.matched = nil;
    }
    else {
      var match_str = result[0];
      var match_len = match_str.length;
      #{self}.matched = match_str;
      self.prev_pos = self.pos;
      #{self}.pos += match_len;
      #{self}.working = #{self}.working.substring(match_len);
      return match_len;
    }
  }
end

#terminateObject


155
156
157
158
# File 'opal/stdlib/strscan.rb', line 155

def terminate
  @match = nil
  self.pos = @string.length
end

#unscanObject


160
161
162
163
164
165
# File 'opal/stdlib/strscan.rb', line 160

def unscan
  @pos = @prev_pos
  @prev_pos = nil
  @match = nil
  self
end