class Lox::LexicalAnalyzer

Attributes

input[R]

Public Class Methods

new(input) click to toggle source
# File lib/lox/lexical_analyzer.rb, line 11
def initialize(input)
  @input = input
end

Public Instance Methods

comment(_lexeme, character) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength

# File lib/lox/lexical_analyzer.rb, line 52
def comment(_lexeme, character)
  if character =~ /./
    :comment
  else
    :default
  end
end
default(_lexeme, character) { |character| ... } click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength

# File lib/lox/lexical_analyzer.rb, line 28
def default(_lexeme, character)
  case character
  when /\s/
    :default
  when /[#]/
    :comment
  when /\d/
    [:integer, character]
  when /["]/
    [:string, character]
  when /[a-zA-Z_]/
    [:identifier, character]
  when /[!=<>]/
    [:operator, character]
  when %r{[(){},.\-+;*/]}
    yield([character])
    :default
  else
    raise(InvalidCharacter, character)
  end
end
each_token(&block) click to toggle source
# File lib/lox/lexical_analyzer.rb, line 15
def each_token(&block)
  return enum_for(:each_token) unless block_given?

  state = :default
  lexeme = nil
  input.each_char do |character|
    state, lexeme = method(state).call(lexeme, character, &block)
  end
  eof(state, lexeme, &block)
end
eof(state, lexeme) { |:integer, Integer(lexeme)| ... } click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/lox/lexical_analyzer.rb, line 98
def eof(state, lexeme)
  case state
  when :default
    nil
  when :integer
    yield([:integer, Integer(lexeme)])
  when :string
    raise(UnterminatedString, lexeme)
  when :identifier
    yield([:identifier, lexeme])
  when :operator
    yield([lexeme])
  else
    raise(InvalidState, state)
  end
  # rubocop:enable Metrics/MethodLength
end
identifier(lexeme, character) { |:identifier, lexeme| ... } click to toggle source
# File lib/lox/lexical_analyzer.rb, line 78
def identifier(lexeme, character, &block)
  if character =~ /\w/
    [:identifier, lexeme + character]
  else
    yield([:identifier, lexeme])
    default(nil, character, &block)
  end
end
integer(lexeme, character) { |:integer, Integer(lexeme)| ... } click to toggle source
# File lib/lox/lexical_analyzer.rb, line 60
def integer(lexeme, character, &block)
  if character =~ /\d/
    [:integer, lexeme + character]
  else
    yield([:integer, Integer(lexeme)])
    default(nil, character, &block)
  end
end
operator(lexeme, character) { |lexeme| ... } click to toggle source
# File lib/lox/lexical_analyzer.rb, line 87
def operator(lexeme, character, &block)
  if character =~ /[=]/
    yield([lexeme + character])
    :default
  else
    yield([lexeme])
    default(nil, character, &block)
  end
end
string(lexeme, character) { |:string, lexeme| ... } click to toggle source
# File lib/lox/lexical_analyzer.rb, line 69
def string(lexeme, character)
  if character =~ /["]/
    yield([:string, lexeme[1..-1]])
    :default
  else
    [:string, lexeme + character]
  end
end