Class: StringScanner
Instance Attribute Summary collapse
-
#matched ⇒ Object
readonly
Returns the value of attribute matched.
-
#pos ⇒ Object
Returns the value of attribute pos.
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #bol? ⇒ Boolean
- #check(regex) ⇒ Object
- #eos? ⇒ Boolean
- #get_byte ⇒ Object (also: #getch)
-
#initialize(string) ⇒ StringScanner
constructor
A new instance of StringScanner.
- #peek(length) ⇒ Object
- #rest ⇒ Object
- #scan(regex) ⇒ Object
- #skip(re) ⇒ Object
- #terminate ⇒ Object
- #unscan ⇒ Object
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
#matched ⇒ Object (readonly)
Returns the value of attribute matched
3 4 5 |
# File 'opal/stdlib/strscan.rb', line 3 def matched @matched end |
#pos ⇒ Object
Returns the value of attribute pos
2 3 4 |
# File 'opal/stdlib/strscan.rb', line 2 def pos @pos end |
Instance Method Details
#[](idx) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'opal/stdlib/strscan.rb', line 46 def [](idx) %x{ var match = #@match; if (idx < 0) { idx += match.length; } if (idx < 0 || idx >= match.length) { return nil; } return match[idx]; } end |
#bol? ⇒ Boolean
13 14 15 |
# File 'opal/stdlib/strscan.rb', line 13 def bol? `#@pos === 0 || #@string.charAt(#@pos - 1) === "\\n"` end |
#check(regex) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'opal/stdlib/strscan.rb', line 62 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
79 80 81 |
# File 'opal/stdlib/strscan.rb', line 79 def eos? `#@working.length === 0` end |
#get_byte ⇒ Object Also known as: getch
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'opal/stdlib/strscan.rb', line 103 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
75 76 77 |
# File 'opal/stdlib/strscan.rb', line 75 def peek(length) `#@working.substring(0, length)` end |
#rest ⇒ Object
134 135 136 |
# File 'opal/stdlib/strscan.rb', line 134 def rest @working end |
#scan(regex) ⇒ Object
17 18 19 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 |
# File 'opal/stdlib/strscan.rb', line 17 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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'opal/stdlib/strscan.rb', line 83 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 |
#terminate ⇒ Object
138 139 140 141 |
# File 'opal/stdlib/strscan.rb', line 138 def terminate @match = nil self.pos = @string.length end |
#unscan ⇒ Object
143 144 145 146 147 148 |
# File 'opal/stdlib/strscan.rb', line 143 def unscan @pos = @prev_pos @prev_pos = nil @match = nil self end |