Module: JSON5::PublicTokenNames

Defined in:
lib/json5/public_token_names.rb

Constant Summary collapse

VALUE_TOKENS =
["STRING", "NUMBER", "TRUE", "FALSE", "NULL", "INFINITY", "NAN", "'{'", "'['"].freeze
MEMBER_NAME_TOKENS =
%w[STRING IDENTIFIER_NAME TRUE FALSE NULL INFINITY NAN].freeze
NAMES =
{
  "$eof" => "end of input",
  "EOF" => "end of input",
  "STRING" => "string",
  "NUMBER" => "number",
  "IDENTIFIER_NAME" => "identifier",
  "TRUE" => "true",
  "FALSE" => "false",
  "NULL" => "null",
  "INFINITY" => "Infinity",
  "NAN" => "NaN"
}.freeze

Class Method Summary collapse

Class Method Details

.expected(token_names) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/json5/public_token_names.rb', line 30

def expected(token_names)
  remaining = Array(token_names).map(&:to_s)
  public_names = []
  if (VALUE_TOKENS - remaining).empty?
    public_names << "value"
    remaining -= VALUE_TOKENS
  elsif (MEMBER_NAME_TOKENS - remaining).empty?
    public_names << "member name"
    remaining -= MEMBER_NAME_TOKENS
  end
  public_names.concat(remaining.map { |token_name| name(token_name) }).uniq.freeze
end

.name(token_name) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/json5/public_token_names.rb', line 22

def name(token_name)
  internal_name = token_name.to_s
  return NAMES.fetch(internal_name) if NAMES.key?(internal_name)
  return internal_name[1...-1] if internal_name.start_with?("'") && internal_name.end_with?("'")

  internal_name.downcase.tr("_", " ")
end