libo11y 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -65,7 +65,7 @@ module Libo11y
65
65
  @docs = []
66
66
  @stack = []
67
67
  @anchors = {}
68
- @aliases = {}
68
+ @aliases = {}.compare_by_identity
69
69
  @doc_explicit = false
70
70
  @doc_version = nil
71
71
  @doc_root = nil
@@ -77,7 +77,7 @@ module Libo11y
77
77
  @doc_version = version && !version.empty? ? version : nil
78
78
  @doc_explicit = !implicit
79
79
  @anchors = {}
80
- @aliases = {}
80
+ @aliases = {}.compare_by_identity
81
81
  @stack = []
82
82
  @doc_root = nil
83
83
  end
@@ -91,7 +91,7 @@ module Libo11y
91
91
  )
92
92
  end
93
93
 
94
- def scalar(value, anchor, tag, plain, quoted, style)
94
+ def scalar(value, anchor, tag, plain, quoted, style) # rubocop:disable Metrics/ParameterLists -- Psych::Handler's signature
95
95
  quoted_or_block = quoted || !plain || (style != Psych::Nodes::Scalar::PLAIN)
96
96
  node = ScalarNode.new(tag: tag, value: value, quoted_or_block: quoted_or_block, anchor: anchor)
97
97
  @anchors[anchor] = node if anchor
@@ -103,7 +103,7 @@ module Libo11y
103
103
  raise SyntaxError, "unknown anchor '#{anchor}' referenced" unless target
104
104
 
105
105
  node = AliasNode.new(anchor: anchor)
106
- @aliases[node.object_id] = target
106
+ @aliases[node] = target
107
107
  attach(node)
108
108
  end
109
109
 
@@ -189,73 +189,95 @@ module Libo11y
189
189
  # - Reject `\/` (yaml.v3 rejects escaped slash; libyaml accepts it)
190
190
  # - Rewrite `\'` inside double-quoted strings to `'` (yaml.v3 accepts it; libyaml rejects it)
191
191
  # - Reject `\uD800`-`\uDFFF` surrogate escapes
192
+ # Checks the escapes of double-quoted scalars as yaml.v3 does, before
193
+ # the text reaches Psych: \/ is not an escape, \' is one, and \u may
194
+ # not name a surrogate. A quote opens a scalar only where a scalar can
195
+ # start, so an apostrophe inside a plain scalar ("the user's id") or
196
+ # anywhere in a block scalar is ordinary text.
192
197
  def preprocess_double_quoted_escapes(text)
193
198
  out = String.new(encoding: Encoding::UTF_8, capacity: text.bytesize)
199
+ quote = nil # the quote character of an open quoted scalar, which may span lines
200
+ block_indent = nil # the indentation of the line that opened a block scalar
201
+ text.each_line do |line|
202
+ if quote.nil? && block_indent
203
+ if line.strip.empty? || line[/\A */].length > block_indent
204
+ out << line
205
+ next
206
+ end
207
+ block_indent = nil
208
+ end
209
+ quote, opens_block = scan_escapes(line, quote, out)
210
+ block_indent = line[/\A */].length if opens_block
211
+ end
212
+ out
213
+ end
214
+
215
+ # Copies one line to out with its double-quoted escapes checked.
216
+ # Returns the quote still open at the end of the line, and whether the
217
+ # line opens a block scalar.
218
+ def scan_escapes(line, quote, out)
194
219
  i = 0
195
- len = text.length
196
- in_single = false
197
- in_double = false
198
- in_comment = false
199
-
200
- while i < len
201
- ch = text[i]
202
- if in_comment
220
+ while i < line.length
221
+ ch = line[i]
222
+ if quote == "'"
203
223
  out << ch
204
- in_comment = false if ["\n", "\r"].include?(ch)
205
- i += 1
206
- elsif in_single
207
- out << ch
208
- if ch == "'"
209
- if i + 1 < len && text[i + 1] == "'"
210
- out << "'"
211
- i += 2
212
- else
213
- in_single = false
214
- i += 1
215
- end
216
- else
224
+ if ch == "'" && line[i + 1] == "'"
225
+ out << "'"
217
226
  i += 1
227
+ elsif ch == "'"
228
+ quote = nil
218
229
  end
219
- elsif in_double
230
+ elsif quote == "\""
220
231
  if ch == "\\"
221
- raise SyntaxError, "unterminated escape in double-quoted scalar" if i + 1 >= len
222
-
223
- nxt = text[i + 1]
224
- if nxt == "/"
225
- raise SyntaxError, "found unknown escape character '/'"
226
- elsif nxt == "'"
227
- out << "'"
228
- i += 2
229
- elsif nxt == "u" && i + 5 < len
230
- hex = text[i + 2, 4]
231
- if /\A[0-9a-fA-F]{4}\z/.match?(hex)
232
- cp = hex.to_i(16)
233
- raise SyntaxError, "found invalid Unicode character escape code" if cp.between?(0xD800, 0xDFFF)
234
- end
235
- out << ch << nxt
236
- i += 2
237
- else
238
- out << ch << nxt
239
- i += 2
240
- end
241
- else
242
- out << ch
243
- in_double = false if ch == "\""
244
- i += 1
232
+ i = copy_escape(line, i, out)
233
+ next
245
234
  end
235
+ out << ch
236
+ quote = nil if ch == "\""
237
+ elsif ch == "#" && (i.zero? || [" ", "\t"].include?(line[i - 1]))
238
+ out << line[i..]
239
+ break
246
240
  else
247
- if ch == "#" && (i.zero? || text[i - 1] == " " || text[i - 1] == "\t" || text[i - 1] == "\n" || text[i - 1] == "\r")
248
- in_comment = true
249
- elsif ch == "'"
250
- in_single = true
251
- elsif ch == "\""
252
- in_double = true
253
- end
241
+ quote = ch if ["'", "\""].include?(ch) && scalar_start?(line, i)
254
242
  out << ch
255
- i += 1
256
243
  end
244
+ i += 1
257
245
  end
258
- out
246
+ [quote, quote.nil? && block_header?(line)]
247
+ end
248
+
249
+ # Copies the escape that starts at i, and returns the index after it.
250
+ def copy_escape(line, i, out)
251
+ nxt = line[i + 1]
252
+ raise SyntaxError, "unterminated escape in double-quoted scalar" if nxt.nil? && !line.end_with?("\n")
253
+ raise SyntaxError, "found unknown escape character '/'" if nxt == "/"
254
+
255
+ if nxt == "'"
256
+ out << "'"
257
+ else
258
+ hex = line[i + 2, 4].to_s
259
+ if nxt == "u" && /\A[0-9a-fA-F]{4}\z/.match?(hex) && hex.to_i(16).between?(0xD800, 0xDFFF)
260
+ raise SyntaxError, "found invalid Unicode character escape code"
261
+ end
262
+
263
+ out << "\\" << nxt.to_s
264
+ end
265
+ i + 2
266
+ end
267
+
268
+ # Whether a quote at index i opens a quoted scalar: it is the first
269
+ # thing in a value, a sequence entry or a flow collection.
270
+ def scalar_start?(line, i)
271
+ before = line[0, i].rstrip
272
+ return true if before.empty? || before.end_with?("[", "{", ",")
273
+
274
+ before.end_with?(":", "-", "?") && before.length < i
275
+ end
276
+
277
+ # Whether the line's value is a block scalar header, such as "key: |" or "- >-".
278
+ def block_header?(line)
279
+ code = line.sub(/(\A|\s)#.*\z/m, "").rstrip
280
+ code.match?(/(\A\s*|[:-]\s+)[|>][-+0-9]*\z/)
259
281
  end
260
282
 
261
283
  def parse_documents(text)