Class: JSON5::DocumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/document_parser.rb

Defined Under Namespace

Classes: ElementParts, MemberParts

Instance Method Summary collapse

Constructor Details

#initialize(source, limits:, diagnostics:, filename:, preserve_trivia: true, collected_diagnostics: []) ⇒ DocumentParser

Returns a new instance of DocumentParser.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/json5/document_parser.rb', line 8

def initialize(source, limits:, diagnostics:, filename:, preserve_trivia: true, collected_diagnostics: [])
  @source = source
  @limits = limits
  @input = Input.new(source, limits: limits, filename: filename)
  @lexer = Lexer.new(
    @input,
    limits: limits,
    diagnostics: diagnostics,
    filename: filename,
    preserve_trivia: preserve_trivia
  )
  @filename = filename
  @diagnostics = collected_diagnostics
  @value_tokens = {}
  @eof_token = nil
end

Instance Method Details

#append_element(parts, element, comma) ⇒ Object



142
143
144
145
146
147
# File 'lib/json5/document_parser.rb', line 142

def append_element(parts, element, comma)
  enforce_entry_limit(parts.elements.length + 1)
  parts.commas << comma
  parts.elements << element
  parts
end

#append_member(members, member, comma) ⇒ Object



131
132
133
134
135
# File 'lib/json5/document_parser.rb', line 131

def append_member(members, member, comma)
  enforce_entry_limit(members.length + 1)
  members.last.comma = comma
  members << member
end

#array(parts, open, close, trailing_comma = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/json5/document_parser.rb', line 108

def array(parts, open, close, trailing_comma = nil)
  element_parts = parts.is_a?(ElementParts) ? parts : ElementParts.new(parts, [])
  element_parts.commas << trailing_comma if trailing_comma
  trailing = element_parts.elements.each_index.map do |index|
    comma = element_parts.commas[index]
    comma ? comma.leading_trivia : []
  end
  ArrayNode.new(
    elements: element_parts.elements,
    element_trailing_trivia: trailing,
    source: @source,
    start_byte: open.start_byte,
    end_byte: close.end_byte,
    leading_trivia: open.leading_trivia,
    trailing_trivia: close.leading_trivia
  )
end

#element_list(element) ⇒ Object



137
138
139
140
# File 'lib/json5/document_parser.rb', line 137

def element_list(element)
  enforce_entry_limit(1)
  ElementParts.new([element], [])
end

#literal(value, result) ⇒ Object



57
58
59
# File 'lib/json5/document_parser.rb', line 57

def literal(value, result)
  scalar_literal(take_value_token(value), result)
end

#member(name, colon, value) ⇒ Object



79
80
81
# File 'lib/json5/document_parser.rb', line 79

def member(name, colon, value)
  MemberParts.new(name, value, colon, nil)
end

#member_list(member) ⇒ Object



126
127
128
129
# File 'lib/json5/document_parser.rb', line 126

def member_list(member)
  enforce_entry_limit(1)
  [member]
end

#member_name(value) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/json5/document_parser.rb', line 61

def member_name(value)
  token = take_value_token(value)
  return scalar_string(token) if token.kind == :STRING

  code_units = if value.is_a?(ECMAString)
                 value
               else
                 ECMAString.new(value.raw.each_codepoint.to_a)
               end
  IdentifierNameNode.new(
    code_units: code_units,
    source: @source,
    start_byte: token.start_byte,
    end_byte: token.end_byte,
    leading_trivia: token.leading_trivia
  )
end

#number(value) ⇒ Object



53
54
55
# File 'lib/json5/document_parser.rb', line 53

def number(value)
  scalar_number(take_value_token(value))
end

#object(members, open, close, trailing_comma = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/json5/document_parser.rb', line 83

def object(members, open, close, trailing_comma = nil)
  members.last.comma = trailing_comma if trailing_comma && !members.empty?
  nodes = members.map do |parts|
    MemberNode.new(
      name: parts.name,
      value: parts.value,
      colon_start_byte: parts.colon.start_byte,
      colon_end_byte: parts.colon.end_byte,
      source: @source,
      start_byte: parts.name.start_byte,
      end_byte: parts.value.end_byte,
      trailing_trivia: parts.comma ? parts.comma.leading_trivia : [],
      colon_leading_trivia: parts.colon.leading_trivia
    )
  end
  ObjectNode.new(
    members: nodes,
    source: @source,
    start_byte: open.start_byte,
    end_byte: close.end_byte,
    leading_trivia: open.leading_trivia,
    trailing_trivia: close.leading_trivia
  )
end

#observe_token(token) ⇒ Object



39
40
41
42
43
# File 'lib/json5/document_parser.rb', line 39

def observe_token(token)
  @last_token = token
  @value_tokens[token.value.object_id] = token if token.value
  @eof_token = token if token.kind == :EOF
end

#parseObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json5/document_parser.rb', line 25

def parse
  root = GeneratedParser.new(@lexer, self).parse_tokens
  Document.new(
    source: @source,
    root: root,
    diagnostics: @diagnostics,
    trailing_trivia: @eof_token&.leading_trivia || []
  )
rescue JSON5::Error => error
  raise enrich_error(error)
rescue Ibex::Runtime::ParseError => error
  raise enrich_error(translate_parse_error(error))
end

#retain_punctuation_tokens?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/json5/document_parser.rb', line 45

def retain_punctuation_tokens?
  true
end

#string(value) ⇒ Object



49
50
51
# File 'lib/json5/document_parser.rb', line 49

def string(value)
  scalar_string(take_value_token(value))
end