rb_json5 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/LICENSE +21 -0
- data/README.md +103 -0
- data/lib/rb_json5.rb +48 -0
- data/lib/rb_json5/escape_sequence.rb +51 -0
- data/lib/rb_json5/parse_error.rb +7 -0
- data/lib/rb_json5/parser.rb +83 -0
- data/lib/rb_json5/parser/array.rb +27 -0
- data/lib/rb_json5/parser/boolean.rb +12 -0
- data/lib/rb_json5/parser/misc.rb +21 -0
- data/lib/rb_json5/parser/null.rb +11 -0
- data/lib/rb_json5/parser/number.rb +77 -0
- data/lib/rb_json5/parser/object.rb +106 -0
- data/lib/rb_json5/parser/space.rb +45 -0
- data/lib/rb_json5/parser/string.rb +88 -0
- data/lib/rb_json5/version.rb +6 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 594540a8d9f462882ebf6af4d8af297efc07c46be1d00c2c02dc2d4b134d8f79
|
4
|
+
data.tar.gz: 7c3ccdc8e41af153eb5af8ac6bf644dc998067e2a32b1f712091d797c834ef42
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36cf35e8657d3a60e52afc012b2eba88f9fe19385f64eb56b9a18c8c514b7b54644a79b3893326136c863e8380290cc9e0b310f5609d8163a96aa1780adcaa14
|
7
|
+
data.tar.gz: 79eef5805af79907cce8fa20db34093dc3b9b57a5ef1f580c405c75ea0c9f28d761727764f02dffa5fc326d5b1f2cd879c1498210391eae93c6645bae381dc25
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at taichi730@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Taichi Ishitani
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/rb_json5.svg)](https://badge.fury.io/rb/rb_json5)
|
2
|
+
![CI](https://github.com/taichi-ishitani/rb_json5/workflows/CI/badge.svg)
|
3
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/5f19b310082c03475c83/maintainability)](https://codeclimate.com/github/taichi-ishitani/rb_json5/maintainability)
|
4
|
+
[![codecov](https://codecov.io/gh/taichi-ishitani/rb_json5/branch/master/graph/badge.svg)](https://codecov.io/gh/taichi-ishitani/rb_json5)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/taichi-ishitani/rb_json5.svg?branch=master)](http://inch-ci.org/github/taichi-ishitani/rb_json5)
|
6
|
+
|
7
|
+
# RbJSON5
|
8
|
+
|
9
|
+
[JSON5](https://json5.org/) parser for Ruby
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'rb_json5'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install rb_json5
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Use `RbJSON5.parse` method to parse JSON5 string.
|
30
|
+
You can convert property names into `Symbol` by setting `symbolize_names` optional argument to `true`.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'rb_json5'
|
34
|
+
|
35
|
+
# https://github.com/json5/json5#short-example
|
36
|
+
json5 = <<~'JSON5'
|
37
|
+
{
|
38
|
+
// comments
|
39
|
+
unquoted: 'and you can quote me on that',
|
40
|
+
singleQuotes: 'I can use "double quotes" here',
|
41
|
+
lineBreaks: "Look, Mom! \
|
42
|
+
No \\n's!",
|
43
|
+
hexadecimal: 0xdecaf,
|
44
|
+
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
|
45
|
+
positiveSign: +1,
|
46
|
+
trailingComma: 'in objects', andIn: ['arrays',],
|
47
|
+
"backwardsCompatible": "with JSON",
|
48
|
+
}
|
49
|
+
JSON5
|
50
|
+
|
51
|
+
RbJSON5.parse(json5) # =>
|
52
|
+
# {
|
53
|
+
# "unquoted"=>"and you can quote me on that",
|
54
|
+
# "singleQuotes"=>"I can use \"double quotes\" here",
|
55
|
+
# "lineBreaks"=>"Look, Mom! No \\n's!",
|
56
|
+
# "hexadecimal"=>912559,
|
57
|
+
# "leadingDecimalPoint"=>0.8675309,
|
58
|
+
# "andTrailing"=>8675309.0,
|
59
|
+
# "positiveSign"=>1,
|
60
|
+
# "trailingComma"=>"in objects",
|
61
|
+
# "andIn"=>["arrays"],
|
62
|
+
# "backwardsCompatible"=>"with JSON"
|
63
|
+
# }
|
64
|
+
RbJSON5.parse(json5, symbolize_names: true) # =>
|
65
|
+
# {
|
66
|
+
# :unquoted=>"and you can quote me on that",
|
67
|
+
# :singleQuotes=>"I can use \"double quotes\" here",
|
68
|
+
# :lineBreaks=>"Look, Mom! No \\n's!",
|
69
|
+
# :hexadecimal=>912559,
|
70
|
+
# :leadingDecimalPoint=>0.8675309,
|
71
|
+
# :andTrailing=>8675309.0,
|
72
|
+
# :positiveSign=>1,
|
73
|
+
# :trailingComma=>"in objects",
|
74
|
+
# :andIn=>["arrays"],
|
75
|
+
# :backwardsCompatible=>"with JSON"
|
76
|
+
# }
|
77
|
+
```
|
78
|
+
|
79
|
+
## Contributing & Contact
|
80
|
+
|
81
|
+
Bug reports, feature requests, pull requests and questions are welcome! You can use following methods:
|
82
|
+
|
83
|
+
* [Issue tracker](https://github.com/taichi-ishitani/rb_json5/issues)
|
84
|
+
* [Pull request](https://github.com/taichi-ishitani/rb_json5/pulls)
|
85
|
+
* [Mail](mailto:taichi730@gmail.com)
|
86
|
+
|
87
|
+
## Note
|
88
|
+
|
89
|
+
Test-suite and JSON5 code snippets for RSpec examples are originaly from:
|
90
|
+
|
91
|
+
* https://github.com/json5/json5-tests
|
92
|
+
* https://github.com/json5/json5/blob/master/test/parse.js
|
93
|
+
* https://github.com/json5/json5/blob/master/test/errors.js
|
94
|
+
* https://github.com/json5/json5#short-example
|
95
|
+
|
96
|
+
## Copyright & License
|
97
|
+
|
98
|
+
Copyright © 2020 Taichi Ishitani.
|
99
|
+
RbJSON5 is licensed under [MIT License](https://opensource.org/licenses/MIT), see [LICENSE](LICENSE) for futher dttails.
|
100
|
+
|
101
|
+
## Code of Conduct
|
102
|
+
|
103
|
+
Everyone interacting in the RbJson5 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rb_json5/blob/master/CODE_OF_CONDUCT.md).
|
data/lib/rb_json5.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'parslet'
|
4
|
+
require_relative 'rb_json5/version'
|
5
|
+
require_relative 'rb_json5/parse_error'
|
6
|
+
require_relative 'rb_json5/escape_sequence'
|
7
|
+
require_relative 'rb_json5/parser'
|
8
|
+
require_relative 'rb_json5/parser/space'
|
9
|
+
require_relative 'rb_json5/parser/misc'
|
10
|
+
require_relative 'rb_json5/parser/null'
|
11
|
+
require_relative 'rb_json5/parser/boolean'
|
12
|
+
require_relative 'rb_json5/parser/number'
|
13
|
+
require_relative 'rb_json5/parser/string'
|
14
|
+
require_relative 'rb_json5/parser/array'
|
15
|
+
require_relative 'rb_json5/parser/object'
|
16
|
+
|
17
|
+
# [JSON5](https://json5.org/) parser for Ruby
|
18
|
+
module RbJSON5
|
19
|
+
# Parses a JSON5 string into its Ruby data structure
|
20
|
+
#
|
21
|
+
# @param json5 [String]
|
22
|
+
# JSON5 string
|
23
|
+
# @param symbolize_names [Boolean]
|
24
|
+
# If set to true, converts names (keys) in a JSON5 object into Symbol
|
25
|
+
# @return [Object]
|
26
|
+
# Ruby data structure represented by the input
|
27
|
+
#
|
28
|
+
# @see RbJSON5.load_file
|
29
|
+
def self.parse(json5, symbolize_names: false)
|
30
|
+
Parser.new.parse(json5, symbolize_names)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Reads a JSON5 string from the given file and parses it into its Ruby data structure
|
34
|
+
#
|
35
|
+
# @param filename [String]
|
36
|
+
# name of the given file
|
37
|
+
# @param symbolize_names [Boolean]
|
38
|
+
# If set to true, converts names (keys) in a JSON5 object into Symbol
|
39
|
+
# @return [Object]
|
40
|
+
# Ruby data structure represented by the input
|
41
|
+
#
|
42
|
+
# @see RbJSON5.parse
|
43
|
+
def self.load_file(filename, symbolize_names: false)
|
44
|
+
File.open(filename, 'r') do |f|
|
45
|
+
parse(f.read, symbolize_names: symbolize_names)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
# @api private
|
5
|
+
# Utility class to unescape the given escape sequence
|
6
|
+
class EscapeSequence
|
7
|
+
# @param sequence [Parslet::Slice]
|
8
|
+
# escape sequence
|
9
|
+
# @param valid_patterns [Array<Regexp>]
|
10
|
+
# list of patterns for valid unescaped character
|
11
|
+
# @param ifinvalid [Proc]
|
12
|
+
# call back block called when unespaced character is not matched
|
13
|
+
# with the given valid_patterns
|
14
|
+
def initialize(sequence, valid_patterns = [], &ifinvalid)
|
15
|
+
@character = unescape(sequence.to_s, valid_patterns, ifinvalid)
|
16
|
+
end
|
17
|
+
|
18
|
+
# returns the character unescaped from the given escape sequence
|
19
|
+
#
|
20
|
+
# @return [String]
|
21
|
+
# unescaped character
|
22
|
+
def to_s
|
23
|
+
@character
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# list of charactors needing escape
|
29
|
+
ESCAPE_CHARACTERS = {
|
30
|
+
'b' => "\b", 'f' => "\f", 'n' => "\n",
|
31
|
+
'r' => "\r", 't' => "\t", 'v' => "\v",
|
32
|
+
'0' => "\0"
|
33
|
+
}.freeze
|
34
|
+
|
35
|
+
def unescape(sequence, valid_patterns, ifinvalid)
|
36
|
+
character =
|
37
|
+
if ['x', 'u'].include?(sequence[1])
|
38
|
+
instance_eval("\"#{sequence}\"", __FILE__, __LINE__)
|
39
|
+
else
|
40
|
+
ESCAPE_CHARACTERS[sequence[1]] || sequence[1]
|
41
|
+
end
|
42
|
+
valid_character?(character, valid_patterns) &&
|
43
|
+
character || ifinvalid.call(character)
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_character?(character, valid_patterns)
|
47
|
+
valid_patterns.empty? ||
|
48
|
+
valid_patterns.any? { |pattern| pattern.match?(character) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
# @api private
|
5
|
+
# JSON5 parser implementation using [Parslet](https://github.com/kschiess/parslet)
|
6
|
+
#
|
7
|
+
# Parsing process:
|
8
|
+
# 1. parses a JSON5 string into the intermediate structure
|
9
|
+
# by using Parser.parser
|
10
|
+
# 2. convert the intermediate structure into Ruby objects
|
11
|
+
# by using Parser.transform
|
12
|
+
class Parser
|
13
|
+
class << self
|
14
|
+
include Parslet
|
15
|
+
|
16
|
+
# returns the class to parse a JSON5 string
|
17
|
+
def parser
|
18
|
+
@parser ||= Class.new(Parslet::Parser)
|
19
|
+
end
|
20
|
+
|
21
|
+
# returns the class to convert the intermediate structure into Ruby objects
|
22
|
+
def transform
|
23
|
+
@transform ||= Class.new(Parslet::Transform)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# defines a rule how to parse the given JSON5 string
|
29
|
+
def parse_rule(rule_name, &body)
|
30
|
+
parser.class_eval { rule(rule_name, &body) }
|
31
|
+
end
|
32
|
+
|
33
|
+
# defines a helper method within Parser.parser
|
34
|
+
def parse_helper(helper_name, &body)
|
35
|
+
parser.class_eval { define_method(helper_name, &body) }
|
36
|
+
end
|
37
|
+
|
38
|
+
# defines a rule hot to convert tht intermediate strucure into Ruby objects
|
39
|
+
def transform_rule(expression, &body)
|
40
|
+
transform.class_eval { rule(expression, &body) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# @param root [Symbol]
|
45
|
+
# specifies the root rule of the Parser for testing parpose
|
46
|
+
def initialize(root = nil)
|
47
|
+
@root = root
|
48
|
+
end
|
49
|
+
|
50
|
+
# parses a JSON5 string into the Ruby
|
51
|
+
#
|
52
|
+
# @param (see RbJSON5.parse)
|
53
|
+
# @return (see RbJSON5.parse)
|
54
|
+
#
|
55
|
+
# @see RbJSON5.parse
|
56
|
+
# @see RbJSON5.load_file
|
57
|
+
def parse(json5, symbolize_names = false)
|
58
|
+
tree = parser.parse(json5)
|
59
|
+
transform.apply(tree, symbolize_names: symbolize_names)
|
60
|
+
rescue Parslet::ParseFailed => e
|
61
|
+
raise ParseError.new(e.message, e.parse_failure_cause)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def parser
|
67
|
+
parser = self.class.parser.new
|
68
|
+
@root && parser.__send__(@root) || parser
|
69
|
+
end
|
70
|
+
|
71
|
+
def transform
|
72
|
+
self.class.transform.new
|
73
|
+
end
|
74
|
+
|
75
|
+
parse_rule(:value) do
|
76
|
+
null | boolean | string | number | array | object
|
77
|
+
end
|
78
|
+
|
79
|
+
parse_rule(:root) do
|
80
|
+
space? >> value >> space?
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
parse_rule(:empty_array) do
|
6
|
+
(bra('[') >> cket(']')).as(:empty_array)
|
7
|
+
end
|
8
|
+
|
9
|
+
parse_rule(:array_elements) do
|
10
|
+
value >> (comma >> value).repeat >> comma.maybe
|
11
|
+
end
|
12
|
+
|
13
|
+
parse_rule(:non_empty_array) do
|
14
|
+
bra('[') >> array_elements.as(:array_elements) >> cket(']')
|
15
|
+
end
|
16
|
+
|
17
|
+
parse_rule(:array) do
|
18
|
+
empty_array | non_empty_array
|
19
|
+
end
|
20
|
+
|
21
|
+
transform_rule(empty_array: simple(:_)) { [] }
|
22
|
+
|
23
|
+
transform_rule(array_elements: subtree(:elements)) do
|
24
|
+
elements.is_a?(Array) && elements || [elements]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
parse_rule(:boolean) do
|
6
|
+
str('true').as(:true_literal) | str('false').as(:false_literal)
|
7
|
+
end
|
8
|
+
|
9
|
+
transform_rule(true_literal: simple(:_)) { true }
|
10
|
+
transform_rule(false_literal: simple(:_)) { false }
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
parse_rule(:comma) do
|
6
|
+
space? >> str(',') >> space?
|
7
|
+
end
|
8
|
+
|
9
|
+
parse_rule(:colon) do
|
10
|
+
space? >> str(':') >> space?
|
11
|
+
end
|
12
|
+
|
13
|
+
parse_helper(:bra) do |c|
|
14
|
+
str(c) >> space?
|
15
|
+
end
|
16
|
+
|
17
|
+
parse_helper(:cket) do |c|
|
18
|
+
space? >> str(c)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
parse_rule(:decimal_digit) do
|
6
|
+
match('[0-9]')
|
7
|
+
end
|
8
|
+
|
9
|
+
parse_rule(:non_zero_digit) do
|
10
|
+
match('[1-9]')
|
11
|
+
end
|
12
|
+
|
13
|
+
parse_rule(:sign?) do
|
14
|
+
(str('+') | str('-')).maybe
|
15
|
+
end
|
16
|
+
|
17
|
+
parse_rule(:decimal_digits) do
|
18
|
+
decimal_digit.repeat(1)
|
19
|
+
end
|
20
|
+
|
21
|
+
parse_rule(:decimal_digits?) do
|
22
|
+
decimal_digit.repeat
|
23
|
+
end
|
24
|
+
|
25
|
+
parse_rule(:exponent_part) do
|
26
|
+
match('[Ee]') >> sign? >> decimal_digits
|
27
|
+
end
|
28
|
+
|
29
|
+
parse_rule(:exponent_part?) do
|
30
|
+
exponent_part.maybe
|
31
|
+
end
|
32
|
+
|
33
|
+
parse_rule(:decimal_integer_literal) do
|
34
|
+
(non_zero_digit >> decimal_digits?) | decimal_digit
|
35
|
+
end
|
36
|
+
|
37
|
+
parse_rule(:decimal_fractional_literal) do
|
38
|
+
(decimal_integer_literal >> str('.') >> decimal_digits? >> exponent_part?) |
|
39
|
+
(str('.') >> decimal_digits >> exponent_part?) |
|
40
|
+
(decimal_integer_literal >> exponent_part)
|
41
|
+
end
|
42
|
+
|
43
|
+
parse_rule(:hex_integer_literal) do
|
44
|
+
str('0') >> match('[Xx]') >> match('\\h').repeat(1)
|
45
|
+
end
|
46
|
+
|
47
|
+
parse_rule(:integer_number) do
|
48
|
+
(sign? >> (hex_integer_literal | decimal_integer_literal)).as(:integer_number)
|
49
|
+
end
|
50
|
+
|
51
|
+
parse_rule(:fractional_number) do
|
52
|
+
(sign? >> decimal_fractional_literal).as(:fractional_number)
|
53
|
+
end
|
54
|
+
|
55
|
+
parse_rule(:infinity_literal) do
|
56
|
+
(sign? >> str('Infinity')).as(:infinity_literal)
|
57
|
+
end
|
58
|
+
|
59
|
+
parse_rule(:nan_literal) do
|
60
|
+
(sign? >> str('NaN')).as(:nan_literal)
|
61
|
+
end
|
62
|
+
|
63
|
+
parse_rule(:number) do
|
64
|
+
nan_literal | infinity_literal | fractional_number | integer_number
|
65
|
+
end
|
66
|
+
|
67
|
+
transform_rule(integer_number: simple(:n)) { Integer(n.str) }
|
68
|
+
|
69
|
+
transform_rule(fractional_number: simple(:n)) { n.to_f }
|
70
|
+
|
71
|
+
transform_rule(infinity_literal: simple(:n)) do
|
72
|
+
n.str[0] == '-' && -Float::INFINITY || Float::INFINITY
|
73
|
+
end
|
74
|
+
|
75
|
+
transform_rule(nan_literal: simple(:n)) { Float::NAN }
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
# list of patterns for start character of identifier
|
6
|
+
IDENTIFIER_START_PATTERNS = [
|
7
|
+
/[$_]/, /\p{Letter}/, /\p{Letter_Number}/
|
8
|
+
].freeze
|
9
|
+
|
10
|
+
# list of patterns for part character of identifier
|
11
|
+
IDENTIFIER_PART_PATTERNS = [
|
12
|
+
/[\u200c\u200d]/,
|
13
|
+
/\p{Nonspacing_Mark}/, /\p{Spacing_Mark}/,
|
14
|
+
/\p{Decimal_Number}/, /\p{Connector_Punctuation}/,
|
15
|
+
*IDENTIFIER_START_PATTERNS
|
16
|
+
].freeze
|
17
|
+
|
18
|
+
parse_helper(:compile_identifier_rule) do |patterns|
|
19
|
+
patterns
|
20
|
+
.map { |pattern| match(pattern.to_s) }
|
21
|
+
.reduce(:|)
|
22
|
+
end
|
23
|
+
|
24
|
+
parse_rule(:unicode_identifier_start) do
|
25
|
+
unicode_escape_sequence.as(:unicode_identifier_start)
|
26
|
+
end
|
27
|
+
|
28
|
+
parse_rule(:identifier_start) do
|
29
|
+
unicode_identifier_start |
|
30
|
+
compile_identifier_rule(IDENTIFIER_START_PATTERNS)
|
31
|
+
end
|
32
|
+
|
33
|
+
parse_rule(:unicode_identifier_part) do
|
34
|
+
unicode_escape_sequence.as(:unicode_identifier_part)
|
35
|
+
end
|
36
|
+
|
37
|
+
parse_rule(:identifier_part) do
|
38
|
+
unicode_identifier_part |
|
39
|
+
compile_identifier_rule(IDENTIFIER_PART_PATTERNS)
|
40
|
+
end
|
41
|
+
|
42
|
+
parse_rule(:identifier_name) do
|
43
|
+
(identifier_start >> identifier_part.repeat).as(:identifier_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
parse_rule(:object_member) do
|
47
|
+
(
|
48
|
+
(identifier_name | string).as(:key) >> colon >> value.as(:value)
|
49
|
+
).as(:object_member)
|
50
|
+
end
|
51
|
+
|
52
|
+
parse_rule(:object_members) do
|
53
|
+
object_member >> (comma >> object_member).repeat >> comma.maybe
|
54
|
+
end
|
55
|
+
|
56
|
+
parse_rule(:non_empty_object) do
|
57
|
+
bra('{') >> object_members.as(:object_members) >> cket('}')
|
58
|
+
end
|
59
|
+
|
60
|
+
parse_rule(:empty_object) do
|
61
|
+
(bra('{') >> cket('}')).as(:empty_object)
|
62
|
+
end
|
63
|
+
|
64
|
+
parse_rule(:object) do
|
65
|
+
empty_object | non_empty_object
|
66
|
+
end
|
67
|
+
|
68
|
+
transform_rule(unicode_identifier_start: simple(:sequence)) do
|
69
|
+
EscapeSequence.new(sequence, IDENTIFIER_START_PATTERNS) do |character|
|
70
|
+
Parslet::Cause.format(
|
71
|
+
sequence.line_cache, sequence.position.bytepos,
|
72
|
+
"#{character.inspect} cannot be used for identifier"
|
73
|
+
).raise
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
transform_rule(unicode_identifier_part: simple(:sequence)) do
|
78
|
+
EscapeSequence.new(sequence, IDENTIFIER_PART_PATTERNS) do |character|
|
79
|
+
Parslet::Cause.format(
|
80
|
+
sequence.line_cache, sequence.position.bytepos,
|
81
|
+
"#{character.inspect} cannot be used for identifier"
|
82
|
+
).raise
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
transform_rule(identifier_name: subtree(:name)) do
|
87
|
+
Array(name).join
|
88
|
+
end
|
89
|
+
|
90
|
+
# @api private
|
91
|
+
# structure to keep a name/value pair for JSON5Object
|
92
|
+
ObjectMember = Struct.new(:name, :value)
|
93
|
+
|
94
|
+
transform_rule(object_member: { key: simple(:key), value: subtree(:value) }) do
|
95
|
+
ObjectMember.new(symbolize_names && key.to_sym || key.to_s, value)
|
96
|
+
end
|
97
|
+
|
98
|
+
transform_rule(object_members: subtree(:members)) do
|
99
|
+
(members.is_a?(Array) && members || [members]).map(&:to_a).to_h
|
100
|
+
end
|
101
|
+
|
102
|
+
transform_rule(empty_object: simple(:_)) do
|
103
|
+
{}
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
{
|
6
|
+
lf: "\u000a", cr: "\u000d",
|
7
|
+
ls: "\u2028", ps: "\u2029"
|
8
|
+
}.each do |name, code|
|
9
|
+
parse_rule(name) { str(code) }
|
10
|
+
end
|
11
|
+
|
12
|
+
parse_rule(:line_terminator) do
|
13
|
+
lf | cr | ls | ps
|
14
|
+
end
|
15
|
+
|
16
|
+
# list of character codes of white space
|
17
|
+
WHITE_SPACE_CODES = [
|
18
|
+
"\u0009", "\u000b", "\u000c", "\u0020", "\u00a0", "\ufeff"
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
parse_rule(:white_space) do
|
22
|
+
WHITE_SPACE_CODES.map(&method(:str)).reduce(:|) |
|
23
|
+
match('\\p{Space_Separator}')
|
24
|
+
end
|
25
|
+
|
26
|
+
parse_rule(:single_line_comment) do
|
27
|
+
str('//') >> (line_terminator.absent? >> any).repeat
|
28
|
+
end
|
29
|
+
|
30
|
+
parse_rule(:multi_line_comment_chars) do
|
31
|
+
(str('*').absent? >> any) | (str('*') >> str('/').absent?)
|
32
|
+
end
|
33
|
+
|
34
|
+
parse_rule(:multi_line_comment) do
|
35
|
+
str('/*') >> multi_line_comment_chars.repeat >> str('*/')
|
36
|
+
end
|
37
|
+
|
38
|
+
parse_rule(:space?) do
|
39
|
+
(
|
40
|
+
white_space | line_terminator |
|
41
|
+
single_line_comment | multi_line_comment
|
42
|
+
).repeat
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RbJSON5
|
4
|
+
class Parser
|
5
|
+
parse_rule(:line_terminator_sequence) do
|
6
|
+
(cr >> lf) | line_terminator
|
7
|
+
end
|
8
|
+
|
9
|
+
parse_rule(:character_escape_sequence) do
|
10
|
+
str('\\') >> (match('[0-9xu]') | line_terminator).absent? >> any
|
11
|
+
end
|
12
|
+
|
13
|
+
parse_rule(:null_escape_sequence) do
|
14
|
+
str('\\') >> str('0') >> match('[1-9]').absent?
|
15
|
+
end
|
16
|
+
|
17
|
+
parse_rule(:hex_escape_sequence) do
|
18
|
+
str('\\') >> str('x') >> match('\\h').repeat(2, 2)
|
19
|
+
end
|
20
|
+
|
21
|
+
parse_rule(:unicode_escape_sequence) do
|
22
|
+
str('\\') >> str('u') >> match('\\h').repeat(4, 4)
|
23
|
+
end
|
24
|
+
|
25
|
+
parse_rule(:escape_sequence) do
|
26
|
+
(
|
27
|
+
hex_escape_sequence | unicode_escape_sequence |
|
28
|
+
null_escape_sequence | character_escape_sequence
|
29
|
+
).as(:escape_sequence)
|
30
|
+
end
|
31
|
+
|
32
|
+
parse_rule(:line_continuation) do
|
33
|
+
(
|
34
|
+
str('\\') >> line_terminator_sequence
|
35
|
+
).as(:line_continuation)
|
36
|
+
end
|
37
|
+
|
38
|
+
parse_rule(:single_string_normal_charactor) do
|
39
|
+
(
|
40
|
+
ls | ps |
|
41
|
+
((str("'") | str('\\') | line_terminator).absent? >> any)
|
42
|
+
).as(:normal_charactor)
|
43
|
+
end
|
44
|
+
|
45
|
+
parse_rule(:single_string_charactor) do
|
46
|
+
line_continuation | escape_sequence | single_string_normal_charactor
|
47
|
+
end
|
48
|
+
|
49
|
+
parse_rule(:single_string_charactors) do
|
50
|
+
str("'") >> single_string_charactor.repeat.as(:string_charactors) >> str("'")
|
51
|
+
end
|
52
|
+
|
53
|
+
parse_rule(:double_string_normal_charactor) do
|
54
|
+
(
|
55
|
+
ls | ps |
|
56
|
+
((str('"') | str('\\') | line_terminator).absent? >> any)
|
57
|
+
).as(:normal_charactor)
|
58
|
+
end
|
59
|
+
|
60
|
+
parse_rule(:double_string_charactor) do
|
61
|
+
line_continuation | escape_sequence | double_string_normal_charactor
|
62
|
+
end
|
63
|
+
|
64
|
+
parse_rule(:double_string_charactors) do
|
65
|
+
str('"') >> double_string_charactor.repeat.as(:string_charactors) >> str('"')
|
66
|
+
end
|
67
|
+
|
68
|
+
parse_rule(:string) do
|
69
|
+
single_string_charactors | double_string_charactors
|
70
|
+
end
|
71
|
+
|
72
|
+
transform_rule(escape_sequence: simple(:sequence)) do
|
73
|
+
EscapeSequence.new(sequence)
|
74
|
+
end
|
75
|
+
|
76
|
+
transform_rule(line_continuation: simple(:_)) do
|
77
|
+
''
|
78
|
+
end
|
79
|
+
|
80
|
+
transform_rule(normal_charactor: simple(:character)) do
|
81
|
+
character.to_s
|
82
|
+
end
|
83
|
+
|
84
|
+
transform_rule(string_charactors: subtree(:characters)) do
|
85
|
+
Array(characters).join
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rb_json5
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Taichi Ishitani
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: parslet
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redcarpet
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.5.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.5.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.9.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.9.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.88.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.88.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.18.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.18.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov-cobertura
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.3.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.3.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: yard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.9.0
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.9.0
|
139
|
+
description: JSON5 parser for Ruby
|
140
|
+
email:
|
141
|
+
- taichi730@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- CODE_OF_CONDUCT.md
|
147
|
+
- LICENSE
|
148
|
+
- README.md
|
149
|
+
- lib/rb_json5.rb
|
150
|
+
- lib/rb_json5/escape_sequence.rb
|
151
|
+
- lib/rb_json5/parse_error.rb
|
152
|
+
- lib/rb_json5/parser.rb
|
153
|
+
- lib/rb_json5/parser/array.rb
|
154
|
+
- lib/rb_json5/parser/boolean.rb
|
155
|
+
- lib/rb_json5/parser/misc.rb
|
156
|
+
- lib/rb_json5/parser/null.rb
|
157
|
+
- lib/rb_json5/parser/number.rb
|
158
|
+
- lib/rb_json5/parser/object.rb
|
159
|
+
- lib/rb_json5/parser/space.rb
|
160
|
+
- lib/rb_json5/parser/string.rb
|
161
|
+
- lib/rb_json5/version.rb
|
162
|
+
homepage: https://github.com/taichi-ishitani/rb_json5
|
163
|
+
licenses:
|
164
|
+
- MIT
|
165
|
+
metadata:
|
166
|
+
bug_tracker_uri: https://github.com/taichi-ishitani/rb_json5/issues
|
167
|
+
documentation_uri: https://www.rubydoc.info/gems/rb_json5
|
168
|
+
homepage_uri: https://github.com/taichi-ishitani/rb_json5
|
169
|
+
source_code_uri: https://github.com/taichi-ishitani/rb_json5
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: 2.4.0
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubygems_version: 3.1.2
|
186
|
+
signing_key:
|
187
|
+
specification_version: 4
|
188
|
+
summary: JSON5 parser for Ruby
|
189
|
+
test_files: []
|