Class: Rsec::Seq

Inherits:
Unary
  • Object
show all
Defined in:
lib/rsec/helpers.rb,
lib/rsec/parsers/seq.rb

Overview

sequence combinator
result in an array

Instance Attribute Summary

Attributes inherited from Unary

#some

Attributes included from Parser

#name

Instance Method Summary collapse

Methods included from Parser

#&, #*, #<<, #>>, #^, #cached, #eof, #fail, #inspect, #join, #map, #maybe, #parse, #parse!, #star, #|

Instance Method Details

#[](idx, &p) ⇒ Object

@ desc.seq, seq_ Returns the parse result at idx, shorter and faster than map{|array| array[idx]} @ example assert_equal 'b', seq('a', 'b', 'c')[1].parse('abc')



314
315
316
317
318
319
320
321
# File 'lib/rsec/helpers.rb', line 314

def [] idx, &p
  raise 'index out of range' if (idx >= some().size or idx < 0)
  # optimize
  parsers = some().map.with_index do |p, i|
    i == idx ? p : Rsec.try_skip_pattern(p)
  end
  SeqOne[parsers, idx].map p
end

#_parse(ctx) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/rsec/parsers/seq.rb', line 6

def _parse ctx
  some.map do |e|
    res = e._parse ctx
    return INVALID if INVALID[res]
    res
  end
end

#inner(&p) ⇒ Object

@ desc Think about "innerHTML"! @ example parser = seq('<b>', /[\w\s]+/, '</b>').inner parser.parse('<b>the inside</b>')



334
335
336
# File 'lib/rsec/helpers.rb', line 334

def inner &p
  Inner[self].map p
end

#unbox(&p) ⇒ Object

@ desc.seq, seq_, join, join.even, join.odd If parse result contains only 1 element, return the element instead of the array



325
326
327
# File 'lib/rsec/helpers.rb', line 325

def unbox &p
  Unbox[self].map p
end