Class: Cooklang::Parsers::IngredientParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cooklang/parsers/ingredient_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ IngredientParser

Returns a new instance of IngredientParser.



6
7
8
# File 'lib/cooklang/parsers/ingredient_parser.rb', line 6

def initialize(stream)
  @stream = stream
end

Instance Method Details

#parseObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cooklang/parsers/ingredient_parser.rb', line 10

def parse
  return nil unless @stream.current&.type == :ingredient_marker
  @stream.consume(:ingredient_marker) # Skip the @ marker

  return nil if invalid_syntax?

  brace_index = find_next_brace

  if brace_index
    parse_braced_ingredient(brace_index)
  else
    parse_simple_ingredient
  end
end