Class: Cooklang::Lexer

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

Constant Summary collapse

TOKENS =
{
  ingredient_marker: "@",
  cookware_marker: "#",
  timer_marker: "~",
  open_brace: "{",
  close_brace: "}",
  open_paren: "(",
  close_paren: ")",
  percent: "%",
  comment_line: "--",
  comment_block_start: "[-",
  comment_block_end: "-]",
  metadata_marker: ">>",
  equals: "=",
  note_marker: ">",
  newline: "\n",
  yaml_delimiter: "---"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Lexer

Returns a new instance of Lexer.



32
33
34
35
36
37
38
# File 'lib/cooklang/lexer.rb', line 32

def initialize(input)
  @input = input
  @scanner = StringScanner.new(input)
  @line = 1
  @column = 1
  @tokens = []
end

Instance Method Details

#tokenizeObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cooklang/lexer.rb', line 40

def tokenize
  @tokens = []

  # Check for YAML frontmatter at the beginning
  if @scanner.check(/^---/)
    handle_yaml_frontmatter
  end

  until @scanner.eos?
    if match_yaml_delimiter
    elsif match_comment_block
    elsif match_comment_line
    elsif 
    elsif match_equals
    elsif match_note_marker
    elsif match_special_chars
    elsif match_newline
    elsif match_text
    elsif match_hyphen
    else
      # Skip unrecognized character
      advance_position(@scanner.getch)
    end
  end

  @tokens
end