Class: Jade::Parsing::Combinators::P

Inherits:
Object
  • Object
show all
Defined in:
lib/jade/parsing/combinators.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ P

Returns a new instance of P.



227
228
229
# File 'lib/jade/parsing/combinators.rb', line 227

def initialize(&block)
  @fn = block
end

Instance Method Details

#>>(other) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/jade/parsing/combinators.rb', line 263

def >>(other)
  P.new do |state|
    call(state).and_then do |(value1, state1)|
      other.call(state1)
        .map do |(value2, state2)|
          [[value1, value2].reject { it == :skip }.flatten(1), state2]
        end
        .map_error do |(err, err_state)|
          [err, state]
        end
    end
  end
end

#call(tokens) ⇒ Object



231
232
233
# File 'lib/jade/parsing/combinators.rb', line 231

def call(tokens)
  @fn.call(tokens)
end

#commitObject



281
282
283
# File 'lib/jade/parsing/combinators.rb', line 281

def commit
  map_error(&:commit)
end

#context(name) ⇒ Object



285
286
287
# File 'lib/jade/parsing/combinators.rb', line 285

def context(name)
  map_error { |err| err.with_context(name) }
end

#map(&block) ⇒ Object



235
236
237
238
239
240
# File 'lib/jade/parsing/combinators.rb', line 235

def map(&block)
  P.new do |state|
    call(state)
      .map { |(value, ok_state)| [block.call(value), ok_state] }
  end
end

#map_error(&block) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/jade/parsing/combinators.rb', line 242

def map_error(&block)
  P
    .new do |state|
      call(state)
        .map_error { |(err, err_state)| [block.call(err), err_state] }
    end
end

#skipObject



277
278
279
# File 'lib/jade/parsing/combinators.rb', line 277

def skip
  self.map { |_| :skip }
end

#|(other) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/jade/parsing/combinators.rb', line 250

def |(other)
  P.new do |state|
    call(state)
      .on_err do |(error, state2)|
        if error.committed?
          Err[[error, state2]]
        else
          other.call(state)
        end
      end
  end
end