Module: Condenser::ParseHelpers
- Included in:
- CSSMediaCombinerProcessor, JSAnalyzer, SVGTransformer::Template
- Defined in:
- lib/condenser/helpers/parse_helpers.rb
Instance Attribute Summary collapse
-
#matched ⇒ Object
Returns the value of attribute matched.
Instance Method Summary collapse
- #current_line ⇒ Object
- #cursor ⇒ Object
- #eos? ⇒ Boolean
- #forward(by = 1) ⇒ Object
- #gobble(r) ⇒ Object
- #next_word ⇒ Object
- #peek(n = 1) ⇒ Object
- #pre_match ⇒ Object
- #rewind(by = 1) ⇒ Object
- #scan_until(r) ⇒ Object
- #seek(pos) ⇒ Object
Instance Attribute Details
#matched ⇒ Object
Returns the value of attribute matched.
5 6 7 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 5 def matched @matched end |
Instance Method Details
#current_line ⇒ Object
47 48 49 50 51 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 47 def current_line start = (@source.rindex(/(\n|\z)/, @old_index) || 0) + 1 uptop = @source.index(/(\n|\z)/, @index) || (@old_index + @matched.length) @source[start..uptop] end |
#cursor ⇒ Object
53 54 55 56 57 58 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 53 def cursor start = (@source.rindex("\n", @old_index) || 0) + 1 uptop = @source.index("\n", @index) || (@old_index + @matched.length) lineno = @source[0..start].count("\n") + 1 "#{lineno.to_s.rjust(4)}: " + @source[start..uptop] + "\n #{'-'* (@index-1-start)}#{'^'*(@matched.length)}" end |
#eos? ⇒ Boolean
7 8 9 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 7 def eos? @index >= @source.size end |
#forward(by = 1) ⇒ Object
32 33 34 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 32 def forward(by=1) @index += by end |
#gobble(r) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 60 def gobble(r) if r.is_a?(Regexp) m = @source.match(r, @index) if m&.begin(0) == @index scan_until(r) end else forward(1) @source[@index-1]; end end |
#next_word ⇒ Object
42 43 44 45 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 42 def next_word nw = @source.match(/\s*(\S+)/, @index) nw.nil? ? nil : nw[1] end |
#peek(n = 1) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 72 def peek(n=1) if n.is_a?(Regexp) @source.match(n, @index) else @source.slice(@index, n) end end |
#pre_match ⇒ Object
24 25 26 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 24 def pre_match @source[@old_index...(@index-@matched.size)] end |
#rewind(by = 1) ⇒ Object
28 29 30 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 28 def rewind(by=1) @index -= by end |
#scan_until(r) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 11 def scan_until(r) index = @source.index(r, @index) match = @source.match(r, @index) if match @matched = match.to_s @old_index = @index @index = index + @matched.size else @matched = nil end match end |
#seek(pos) ⇒ Object
36 37 38 39 40 |
# File 'lib/condenser/helpers/parse_helpers.rb', line 36 def seek(pos) @old_index = nil @matched = nil @index = pos end |