Class: Jade::Parsing::Combinators::P
- Inherits:
-
Object
- Object
- Jade::Parsing::Combinators::P
- Defined in:
- lib/jade/parsing/combinators.rb
Instance Method Summary collapse
- #>>(other) ⇒ Object
- #call(tokens) ⇒ Object
- #commit ⇒ Object
- #context(name) ⇒ Object
-
#initialize(&block) ⇒ P
constructor
A new instance of P.
- #map(&block) ⇒ Object
- #map_error(&block) ⇒ Object
- #skip ⇒ Object
- #|(other) ⇒ Object
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 |
#commit ⇒ Object
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 |
#skip ⇒ Object
277 278 279 |
# File 'lib/jade/parsing/combinators.rb', line 277 def skip self.map { |_| :skip } end |