lowkey 0.7.2 → 0.7.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe9c998f04d4f9c90d6c11acd10f8be182daee76de053eaeda1ebf9146663c5d
4
- data.tar.gz: b33ffc9d37b12a893245c1ea9f33baa4c4e82b0e186f4e751635655d5e8dae57
3
+ metadata.gz: a3f67a40b7a6bac54260d413736dd90bc33a2c6f0bda31f80827236675d6ed05
4
+ data.tar.gz: 86a57542a05bd71806a9b1cb0e97cb1b399df974c5044cca6a03b86801be03ff
5
5
  SHA512:
6
- metadata.gz: 03e54f1fca8a5f4a64fae45c9fcddab88df626a1bc24c4abedd6f94e17e0c8adacebd7b0edf18a31caeba9548fd6cfb942512eedfca6216749fbf14f34f47204
7
- data.tar.gz: 5735d9c51975d40df1f2003fb83794ac05e91a54f88eaf68f42de974fb966aadebe4784429fa96b290a382156662c9311ac0401136b7877431de536b9c5d86bc
6
+ metadata.gz: ab5e11078fda54b8c9b7b47865b8a5881dc3f603ea30adb3d599ca1d2a93c88d594e83c05976d07cf73369d7ddea810e4c94e5c891f201f252f72abe0b7935eb
7
+ data.tar.gz: fb743d0414d28efd6cb7564322871cb6f08c802bed158020e41deb4b6bfd646c34652cb8d09c7c9a72feebfd90daa65d6892cf3f518b380d39a4050c7e19e9f1
data/lib/lowkey.rb CHANGED
@@ -1,12 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'adapters/adapter_loader'
4
- require_relative 'factories/proxy_factory'
5
- require_relative 'maps/parent_map'
6
- require_relative 'proxies/file_proxy'
7
3
  require_relative 'queries/value_parser'
8
4
 
9
5
  module Lowkey
6
+ # Loaded when first used, so reading values, like Antlers' templates do, loads only ValueParser.
7
+ { AdapterLoader: 'adapters/adapter_loader', SinatraAdapter: 'adapters/sinatra_adapter',
8
+ ProxyFactory: 'factories/proxy_factory', SourceFactory: 'factories/source_factory', Adapter: 'interfaces/adapter',
9
+ Proxy: 'interfaces/proxy', ReleasableNode: 'interfaces/releasable_node', ReleasedNodeError: 'interfaces/releasable_node',
10
+ ParentMap: 'maps/parent_map', Source: 'models/source', WriteError: 'models/source', BodyProxy: 'proxies/body_proxy',
11
+ ClassProxy: 'proxies/class_proxy', FileProxy: 'proxies/file_proxy', MethodCallProxy: 'proxies/method_call_proxy',
12
+ MethodProxy: 'proxies/method_proxy', ModuleProxy: 'proxies/module_proxy', ParamProxy: 'proxies/param_proxy',
13
+ ReturnProxy: 'proxies/return_proxy', Query: 'queries/query' }.each { |name, path| autoload(name, File.join(__dir__, path)) }
14
+
10
15
  NAMESPACE_PATTERN = /\A[A-Z]\w*(::[A-Z]\w*)*\z/
11
16
 
12
17
  class << self
@@ -4,15 +4,99 @@ module Lowkey
4
4
  # Reads Ruby values like nil, true, 'text', 3, -1.5 or :symbol from Prism nodes or text, without evaluating any code.
5
5
  # Ignores potentially malicious values like variables, method calls or interpolated strings.
6
6
  module ValueParser
7
- VALUE_KEYWORDS = %w[nil true false].freeze
7
+ KEYWORDS = { 'nil' => nil, 'true' => true, 'false' => false }.freeze
8
+ VALUE_KEYWORDS = KEYWORDS.keys.freeze
8
9
  VALUE_STARTS = ["'", '"', ':', '-', *'0'..'9'].freeze
9
10
 
11
+ NUMBER = /\A-?\d/
12
+ FLOAT = /\A-?(?:0|[1-9][\d_]*)(?:\.\d[\d_]*)?(?:[eE][-+]?\d+)?\z/
13
+ SINGLE_QUOTED = /\A'((?:[^'\\]|\\.)*)'\z/m
14
+ DOUBLE_QUOTED = /\A"((?:[^"\\]|\\.)*)"\z/m
15
+ # #{}, #@ and #$ interpolate, unless escaped.
16
+ INTERPOLATION = /(?<!\\)(?:\\\\)*\#[{@$]/
17
+ # A double-quoted string's escapes: Unicode, hex, octal, then a character.
18
+ ESCAPE = /\\(u\{[\h ]+\}|u\h{4}|x\h{1,2}|[0-7]{1,3}|.)/m
19
+ ESCAPES = { 'n' => "\n", 't' => "\t", 'r' => "\r", 's' => ' ', 'e' => "\e", 'a' => "\a", 'b' => "\b", 'f' => "\f",
20
+ 'v' => "\v", "\n" => '' }.freeze
21
+ SYMBOL = %r{\A:([A-Za-z_]\w*[?!=]?|\[\]=?|\*\*|[-+]@|<=>|===?|=~|![=~]?|<[<=]?|>[>=]?|[-+*/%&|^~])\z}
22
+
10
23
  module_function
11
24
 
12
- # Returns the AST node's value or yields a block.
25
+ # Returns the value, or yields a block when the input isn't a value. It's read as Ruby reads it, without Prism,
26
+ # which only parses files, so reading a value never loads it.
13
27
  def parse(input)
14
28
  return yield(input) unless possible_value?(input)
15
29
 
30
+ text = input.strip
31
+ return KEYWORDS[text] if KEYWORDS.key?(text)
32
+
33
+ found = literal(text)
34
+ return yield(input) if found.empty?
35
+
36
+ found.first
37
+ end
38
+
39
+ # The literal's value in an array, or an empty one when it isn't one: a number, a string or a symbol.
40
+ def literal(text)
41
+ return number(text) if text.match?(NUMBER)
42
+ return [single_quoted(text)].compact if text.start_with?("'")
43
+ return [double_quoted(text)].compact if text.start_with?('"')
44
+
45
+ symbol(text)
46
+ end
47
+
48
+ # Integers as Ruby writes them, like 1_000, 0x1F or 007, then floats, like 1.5e-3.
49
+ def number(text)
50
+ [Integer(text)]
51
+ rescue ArgumentError
52
+ float(text)
53
+ end
54
+
55
+ def float(text)
56
+ return [] unless text.match?(FLOAT)
57
+
58
+ [Float(text)]
59
+ rescue ArgumentError
60
+ []
61
+ end
62
+
63
+ # Only \\ and \' are escapes in single quotes.
64
+ def single_quoted(text)
65
+ text[SINGLE_QUOTED, 1]&.gsub(/\\([\\'])/, '\1')
66
+ end
67
+
68
+ # With its escapes, like \t, \u00e9 or \101, and nil for an interpolation, or a control escape, like \C-a.
69
+ def double_quoted(text)
70
+ body = text[DOUBLE_QUOTED, 1]
71
+ return if body.nil? || body.match?(INTERPOLATION) || body.match?(/\\[cCM]/)
72
+
73
+ body.gsub(ESCAPE) { escaped(Regexp.last_match(1)) }
74
+ end
75
+
76
+ def escaped(escape)
77
+ return unicode(escape[1..]) if escape.match?(/\Au./)
78
+ return escape[1..].hex.chr if escape.match?(/\Ax./)
79
+ return escape.oct.chr if escape.match?(/\A[0-7]/)
80
+
81
+ ESCAPES.fetch(escape, escape)
82
+ end
83
+
84
+ # Like 00e9, or {1F600 1F601}, as UTF-8.
85
+ def unicode(codes)
86
+ codes.delete('{}').split.map { it.hex.chr(Encoding::UTF_8) }.join
87
+ end
88
+
89
+ def symbol(text)
90
+ return [text[SYMBOL, 1].to_sym] if text.match?(SYMBOL)
91
+
92
+ quoted = double_quoted(text.delete_prefix(':')) if text.start_with?(':"')
93
+ return [quoted.to_sym] if quoted
94
+
95
+ []
96
+ end
97
+
98
+ # Prism's reading of the text, which parse reads as, for checking it against.
99
+ def prism_parse(input)
16
100
  Lowkey.load_prism
17
101
  result = Prism.parse(input)
18
102
  statements = result.value.statements.body
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lowkey
4
- VERSION = '0.7.2'
4
+ VERSION = '0.7.3'
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lowkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2026-11-10 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: prism
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.6.2
79
+ rubygems_version: 4.0.20
80
80
  specification_version: 4
81
81
  summary: An API for accessing PRISM metadata easily and efficiently
82
82
  test_files: []