Class: RubyJsonParser::Lexer

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Enumerable
Defined in:
lib/ruby_json_parser/lexer.rb

Overview

A lexical analyzer (tokenizer) for JSON

Constant Summary collapse

Elem =

Type parameter for ‘Enumerable` Declares the type that the lexer returns for tokens

type_member { { fixed: Token } }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Lexer

Returns a new instance of Lexer.



27
28
29
30
31
32
33
34
# File 'lib/ruby_json_parser/lexer.rb', line 27

def initialize(source)
  @source = source

  # offset of the first character of the current lexeme
  @start_cursor = T.let(0, Integer)
  # offset of the next character
  @cursor = T.let(0, Integer)
end

Class Method Details

.lex(source) ⇒ Object



21
22
23
# File 'lib/ruby_json_parser/lexer.rb', line 21

def lex(source)
  new(source).to_a
end

Instance Method Details

#each(&block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_json_parser/lexer.rb', line 44

def each(&block)
  return enum_for(T.must(__method__)) unless block

  loop do
    tok = self.next
    break if tok.type == Token::END_OF_FILE

    block.call(tok)
  end

  self
end

#nextObject



37
38
39
40
41
# File 'lib/ruby_json_parser/lexer.rb', line 37

def next
  return Token.new(Token::END_OF_FILE) unless more_tokens?

  scan_token
end