minijson 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7703cf0153b14573b6e54b0f39ca09cb9827ee2d9113d6c8b62f2aea64c05ebd
4
+ data.tar.gz: 10d5906783d025365fdae233ba1125bf9c86a6b7b639ff0a0b9b47ed21f0c48e
5
+ SHA512:
6
+ metadata.gz: df00bbb8b57df38725bc1f17983f5a3b866dc876c436371e234b5671c8854f4c4405c265fb712dc818798fd48c5336af7fb7f2a29fcb8cad1bc4ab031fbdd645
7
+ data.tar.gz: 7a476f3d86f4be940aa94cbe900066600ef8fda048dd273e604a599a2e8de139986c743e0f3766ce5528eb648aa25bc7643f624ba6d4042b870979ca610bf1bf
@@ -0,0 +1,31 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: rake
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request: {}
14
+
15
+ jobs:
16
+ rake:
17
+ runs-on: ubuntu-latest
18
+ strategy:
19
+ matrix:
20
+ ruby-version: ['2.6', '2.7', '3.0']
21
+ task: ['rspec', 'rspec-opal']
22
+
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - name: Set up Ruby
26
+ uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby-version }}
29
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
30
+ - name: Run tests
31
+ run: bundle exec rake ${{ matrix.task }}
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rspec-opal ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ -I ./lib
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 3.0.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in minijson.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "opal-rspec", "~> 0.8a"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Minijson
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/minijson`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'minijson'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install minijson
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/minijson.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:rspec)
5
+
6
+ require 'opal/rspec/rake_task'
7
+ Opal::RSpec::RakeTask.new(:"rspec-opal") do |_, task|
8
+ require 'opal'
9
+ Opal.append_path File.expand_path('../lib', __FILE__)
10
+
11
+ task.default_path = 'spec'
12
+ task.pattern = 'spec/**/*_spec.{rb,opal}'
13
+ # ENV['SPEC_OPTS'] ||= "--format documentation --color"
14
+ end
15
+
16
+ task :spec => [:rspec, :"rspec-opal"]
17
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "minijson"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module MiniJSON
2
+ VERSION = "0.1.0"
3
+ end
data/lib/minijson.rb ADDED
@@ -0,0 +1,177 @@
1
+ require "minijson/version"
2
+
3
+ module MiniJSON
4
+ class ParserError < StandardError; end
5
+
6
+ class << self
7
+ def parse(str)
8
+ parser(lexer(str))
9
+ end
10
+
11
+ alias load parse
12
+
13
+ private
14
+
15
+ def lexer_regexp
16
+ @lexer_regexp ||= begin
17
+ meaningful_characters = /[()\[\]{}",:]/
18
+ string_escapes = /\\(?:[\\\/"bfnrt]|u[0-9a-fA-F]{4})/
19
+ numbers = /-?[0-9]+(\.[0-9]*)?([eE][+-]?[0-9]+)?/
20
+ space = /[ \r\n\t]+/
21
+ rest = /[^"\\: \r\n\t]+/
22
+ very_rest = /.+/
23
+
24
+ /(#{
25
+ [meaningful_characters, string_escapes, numbers, space, rest, very_rest].join('|')
26
+ })/
27
+ end
28
+ end
29
+
30
+ def lexer(str)
31
+ str.scan(lexer_regexp).map(&:first)
32
+ end
33
+
34
+ EMPTY_BYTES = " \r\n\t"
35
+
36
+ NUMBER_REGEXP = /\A[0-9-]/
37
+ KEY_REGEXP = /\A[0-9a-zA-Z_]\z/
38
+
39
+ ESCAPE_TO_VALUE = Hash.new do |_,x|
40
+ x[2..-1].to_i(16).chr('utf-8')
41
+ end.merge(
42
+ '\"' => '"',
43
+ '\t' => "\t",
44
+ '\b' => "\b",
45
+ '\f' => "\f",
46
+ '\n' => "\n",
47
+ '\r' => "\r",
48
+ '\/' => "/",
49
+ '\\\\' => "\\",
50
+ )
51
+
52
+ def is_empty?(tok)
53
+ tok.each_char.all? { |i| EMPTY_BYTES.include? i }
54
+ end
55
+
56
+ def parser_error(tok)
57
+ raise ParserError, "unexpected token at '#{tok}'"
58
+ end
59
+
60
+ def parser(toks)
61
+ state = :value
62
+
63
+ # popping is cheaper than shifting
64
+ toks = toks.reverse
65
+
66
+ value = nil
67
+ finalizers = [proc { |i| value = i }]
68
+ structs = []
69
+ hash_keys = []
70
+
71
+ finalizer = proc { |i| finalizers.pop.(i) }
72
+
73
+ until toks.empty?
74
+ tok = toks.pop
75
+
76
+ case state
77
+ when :value, :top_value, :struct_value
78
+ case tok
79
+ when '{'
80
+ structs << {}
81
+ toks << ','
82
+ when '['
83
+ structs << []
84
+ toks << ','
85
+ when '}', ']'
86
+ parser_error(tok) if structs.empty?
87
+ parser_error(tok) if structs.last.class == Array && tok != ']'
88
+ parser_error(tok) if structs.last.class == Hash && tok != '}'
89
+ finalizer.(structs.pop)
90
+ when ','
91
+ # warning: [,,,,] will cause a weird behavior, but will be caught
92
+ case structs.last
93
+ when nil
94
+ parser_error(tok)
95
+ when Array
96
+ finalizers << proc do |i|
97
+ parser_error(tok) unless structs.last
98
+ structs.last << i
99
+ end
100
+ state = :struct_value
101
+ when Hash
102
+ finalizers << proc do |i|
103
+ parser_error(tok) unless structs.last && hash_keys.last
104
+ structs.last[hash_keys.pop] = i
105
+ end
106
+ state = :key
107
+ end
108
+ when 'true'
109
+ finalizer.(true)
110
+ when 'false'
111
+ finalizer.(false)
112
+ when 'null'
113
+ finalizer.(nil)
114
+ when method(:is_empty?).to_proc
115
+ # nothing
116
+ when '"'
117
+ finalizer.(receive_string(toks))
118
+ when NUMBER_REGEXP
119
+ finalizer.(receive_number(tok))
120
+ else
121
+ parser_error(tok)
122
+ end
123
+ when :key
124
+ case tok
125
+ when '"'
126
+ hash_keys << receive_string(toks)
127
+ when method(:is_empty?).to_proc
128
+ next
129
+ when KEY_REGEXP
130
+ hash_keys << tok
131
+ else
132
+ parser_error(tok)
133
+ end
134
+ state = :colon
135
+ when :colon
136
+ case tok
137
+ when ':'
138
+ state = :value
139
+ when method(:is_empty?).to_proc
140
+ # nothing
141
+ else
142
+ parser_error(tok)
143
+ end
144
+ end
145
+ end
146
+
147
+ parser_error("END") unless [finalizers, structs, hash_keys].all?(&:empty?)
148
+
149
+ value
150
+ end
151
+
152
+ def receive_string(toks)
153
+ str = []
154
+ while true
155
+ tok = toks.pop
156
+ if tok == nil
157
+ parser_error('"'+toks.join)
158
+ elsif tok == '"'
159
+ break
160
+ elsif tok.start_with? '\\'
161
+ str << ESCAPE_TO_VALUE[tok]
162
+ else
163
+ str << tok
164
+ end
165
+ end
166
+ str.join
167
+ end
168
+
169
+ def receive_number(tok)
170
+ if tok.match?(/[e.]/)
171
+ tok.to_f
172
+ else
173
+ tok.to_i
174
+ end
175
+ end
176
+ end
177
+ end
data/minijson.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/minijson/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "minijson"
5
+ spec.version = MiniJSON::VERSION
6
+ spec.authors = ["hmdne"]
7
+ #spec.email = ["TODO: Write your email address"]
8
+
9
+ spec.summary = %q{A mini JSON parser}
10
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
11
+ #spec.homepage = "TODO: Put your gem's website or public repo URL here."
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ #spec.metadata["homepage_uri"] = spec.homepage
18
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minijson
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hmdne
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".github/workflows/rake.yml"
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".rspec-opal"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/minijson.rb
31
+ - lib/minijson/version.rb
32
+ - minijson.gemspec
33
+ homepage:
34
+ licenses:
35
+ - MIT
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.2.22
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: A mini JSON parser
56
+ test_files: []