wexpr 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +76 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/wexprTool +211 -0
- data/lib/wexpr.rb +34 -0
- data/lib/wexpr/exception.rb +66 -0
- data/lib/wexpr/expression.rb +1375 -0
- data/lib/wexpr/object_ext.rb +13 -0
- data/lib/wexpr/private_parser_state.rb +38 -0
- data/lib/wexpr/uvlq64.rb +70 -0
- data/lib/wexpr/version.rb +3 -0
- data/wexpr.gemspec +39 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c20c67b59151ff8f72f6ad40c3f4097aa21e5c723b261b33ea21e77a04de3aa0
|
4
|
+
data.tar.gz: 8ed24f217b2c4fb25213bac3aae2020f2c0fd6e97e8cf108585302615c567b85
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bccfac837caf12cca63d0bd90a6d1f44b3c86c7eb5633d63bcedaa6c8b6983ae201b833c66c316ce2a720b7ee4842099c3216987aef77493228d83e92d9ad3ee
|
7
|
+
data.tar.gz: a74c880e0108745e609104e79a476667bec93fbd842420d46cc5395eb02ee59c3d79af1d2b2348ef744d9f6e66f6bd6598a696623be1b8fc464db26c18d2c005
|
data/.gitignore
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# Created by https://www.gitignore.io/api/ruby
|
11
|
+
# Edit at https://www.gitignore.io/?templates=ruby
|
12
|
+
|
13
|
+
### Ruby ###
|
14
|
+
*.gem
|
15
|
+
*.rbc
|
16
|
+
/.config
|
17
|
+
/coverage/
|
18
|
+
/InstalledFiles
|
19
|
+
/pkg/
|
20
|
+
/spec/reports/
|
21
|
+
/spec/examples.txt
|
22
|
+
/test/tmp/
|
23
|
+
/test/version_tmp/
|
24
|
+
/tmp/
|
25
|
+
|
26
|
+
# Used by dotenv library to load environment variables.
|
27
|
+
# .env
|
28
|
+
|
29
|
+
# Ignore Byebug command history file.
|
30
|
+
.byebug_history
|
31
|
+
|
32
|
+
## Specific to RubyMotion:
|
33
|
+
.dat*
|
34
|
+
.repl_history
|
35
|
+
build/
|
36
|
+
*.bridgesupport
|
37
|
+
build-iPhoneOS/
|
38
|
+
build-iPhoneSimulator/
|
39
|
+
|
40
|
+
## Specific to RubyMotion (use of CocoaPods):
|
41
|
+
#
|
42
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
43
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
44
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
45
|
+
# vendor/Pods/
|
46
|
+
|
47
|
+
## Documentation cache and generated files:
|
48
|
+
/.yardoc/
|
49
|
+
/_yardoc/
|
50
|
+
/doc/
|
51
|
+
/rdoc/
|
52
|
+
|
53
|
+
## Environment normalization:
|
54
|
+
/.bundle/
|
55
|
+
/vendor/bundle
|
56
|
+
/lib/bundler/man/
|
57
|
+
|
58
|
+
# for a library or gem, you might want to ignore these files since the code is
|
59
|
+
# intended to run in multiple environments; otherwise, check them in:
|
60
|
+
# Gemfile.lock
|
61
|
+
# .ruby-version
|
62
|
+
# .ruby-gemset
|
63
|
+
|
64
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
65
|
+
.rvmrc
|
66
|
+
|
67
|
+
# End of https://www.gitignore.io/api/ruby
|
68
|
+
|
69
|
+
# Created by https://www.gitignore.io/api/kdevelop4
|
70
|
+
# Edit at https://www.gitignore.io/?templates=kdevelop4
|
71
|
+
|
72
|
+
### KDevelop4 ###
|
73
|
+
*.kdev4
|
74
|
+
.kdev4/
|
75
|
+
|
76
|
+
# End of https://www.gitignore.io/api/kdevelop4
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
wexpr (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.11.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
bundler (~> 1.16)
|
17
|
+
minitest (~> 5.0)
|
18
|
+
rake (~> 10.0)
|
19
|
+
wexpr!
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Kenneth Perry (thothonegan)
|
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,38 @@
|
|
1
|
+
# Wexpr
|
2
|
+
|
3
|
+
Ruby parser for the Wexpr format. See [libWexpr](https://github.com/thothonegan/libWexpr) for more details of the format.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'wexpr', :git => "git://github.com/thothonegan/ruby-wexpr.git"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Currently it is not published to rubygems.
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
For simple wexpr expression to an equivilant ruby hash (similar to yaml/json libraries):
|
22
|
+
```ruby
|
23
|
+
require 'wexpr'
|
24
|
+
|
25
|
+
rubyHash = Wexpr.load("@(array #(1 2 3)")
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
For more complex, `Wexpr::Expression` acts like its libWexpr equivilant. See the files in [test](./test/) for examples.
|
30
|
+
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thothonegan/ruby-wexpr.
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wexpr"
|
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
data/bin/wexprTool
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wexpr"
|
5
|
+
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
WEXPR_BIN_VERSION = 0x00001000 # 0.1.0
|
9
|
+
|
10
|
+
# the first 8 bytes of the header
|
11
|
+
WEXPR_HEADER_MAGIC = [
|
12
|
+
0x83,
|
13
|
+
'B'.ord, 'W'.ord, 'E'.ord, 'X'.ord, 'P'.ord, 'R'.ord,
|
14
|
+
0x0A
|
15
|
+
]
|
16
|
+
WEXPR_HEADER_MAGIC_PACK = 'CCCCCCCC'
|
17
|
+
|
18
|
+
# helper functions
|
19
|
+
def readAllInputFrom(inputPath)
|
20
|
+
if inputPath == "-"
|
21
|
+
return STDIN.read()
|
22
|
+
else
|
23
|
+
return File.read(inputPath, :mode => "rb")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def writeAllOutputTo(outputPath, str)
|
28
|
+
if outputPath == "-"
|
29
|
+
STDOUT.write "#{str}"
|
30
|
+
else
|
31
|
+
File.open(outputPath, "w") do |f|
|
32
|
+
f.write "#{str}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def writeAllOutputWithFileHeaderTo(outputPath, data)
|
38
|
+
stream = STDOUT
|
39
|
+
if outputPath != "-"
|
40
|
+
stream = File.open(outputPath, "wb")
|
41
|
+
end
|
42
|
+
|
43
|
+
# TODO: Move writing header to Wexpr, since its part of the file format
|
44
|
+
|
45
|
+
header = WEXPR_HEADER_MAGIC # first 8 bytes
|
46
|
+
header += [
|
47
|
+
WEXPR_BIN_VERSION, #12
|
48
|
+
0,0,0,0,0,0,0,0 # reserved slots : header is 20 bytes
|
49
|
+
]
|
50
|
+
|
51
|
+
headerSpec=WEXPR_HEADER_MAGIC_PACK + 'NC*'
|
52
|
+
stream.write(header.pack(headerSpec))
|
53
|
+
|
54
|
+
# currently we have no aux chunks
|
55
|
+
|
56
|
+
# write the main chunk
|
57
|
+
stream.write(data)
|
58
|
+
|
59
|
+
# and done
|
60
|
+
stream.flush
|
61
|
+
end
|
62
|
+
|
63
|
+
#
|
64
|
+
# Similar to libWexpr's WexprTool written in ruby
|
65
|
+
#
|
66
|
+
options = {
|
67
|
+
:command => "humanReadable",
|
68
|
+
:input => "-",
|
69
|
+
:output => "-"
|
70
|
+
}
|
71
|
+
|
72
|
+
optparser = OptionParser.new do |opts|
|
73
|
+
opts.banner = "Usage: #{$0} [OPTIONS]"
|
74
|
+
opts.on("-c", "--cmd=COMMAND", "Perform the requested command. Default is 'humanReadable'") do |command|
|
75
|
+
options[:command] = command
|
76
|
+
end
|
77
|
+
|
78
|
+
opts.on("-i", "--input=FILE", "The input file to read from (default is -, stdin)") do |input|
|
79
|
+
options[:input] = input
|
80
|
+
end
|
81
|
+
|
82
|
+
opts.on("-o", "--output=FILE", "The output file to read from (default is -, stdout)") do |output|
|
83
|
+
options[:output] = output
|
84
|
+
end
|
85
|
+
|
86
|
+
opts.on("-h", "--help", "Display this help and exit") do
|
87
|
+
puts opts
|
88
|
+
puts ""
|
89
|
+
puts "Possible commands:"
|
90
|
+
puts " humanReadable - [default] Makes the wexpr input human readable and outputs."
|
91
|
+
puts " validate - Checks the wexpr. If valid outputs 'true' and returns 0, otherwise 'false' and 1."
|
92
|
+
puts " mini - Minifies the wexpr output"
|
93
|
+
puts " binary - Write the wexpr out as binary"
|
94
|
+
exit
|
95
|
+
end
|
96
|
+
|
97
|
+
opts.on("-v", "--version", "Output the version and exit") do
|
98
|
+
puts "wexprTool #{Wexpr::VERSION}"
|
99
|
+
exit
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
optparser.parse!
|
104
|
+
|
105
|
+
if options[:command] == "humanReadable" or
|
106
|
+
options[:command] == "validate" or
|
107
|
+
options[:command] == "mini" or
|
108
|
+
options[:command] == "binary"
|
109
|
+
|
110
|
+
isValidate = (options[:command] == "validate")
|
111
|
+
inputString = readAllInputFrom(options[:input])
|
112
|
+
|
113
|
+
expr = nil
|
114
|
+
|
115
|
+
# determine if binary or not.
|
116
|
+
# if so strip the header and do the hunk
|
117
|
+
begin
|
118
|
+
if inputString.size >= 1 and inputString[0].ord == 0x83
|
119
|
+
if inputString.size < 20
|
120
|
+
raise StandardError.new "Invalid binary header - not big enough"
|
121
|
+
end
|
122
|
+
|
123
|
+
if inputString.unpack(WEXPR_HEADER_MAGIC_PACK) != WEXPR_HEADER_MAGIC
|
124
|
+
raise StandardError.new "Invalid binary header - invalid magic"
|
125
|
+
end
|
126
|
+
|
127
|
+
version = inputString[8..-1].unpack('N')[0]
|
128
|
+
|
129
|
+
if version != WEXPR_BIN_VERSION
|
130
|
+
raise StandardError.new "Invalid binary header - unknown version 0x#{version.to_s(16)}"
|
131
|
+
end
|
132
|
+
|
133
|
+
# make sure reserved is blank
|
134
|
+
# thats the remaining 8 bytes
|
135
|
+
if inputString[12..-1].unpack('CCCCCCCC') != [0]*8
|
136
|
+
raise StandardError.new "Invalid binary header - unknown reserved bits"
|
137
|
+
end
|
138
|
+
|
139
|
+
# header seems valid, skip it
|
140
|
+
data = inputString
|
141
|
+
curPos = 20
|
142
|
+
endPos = data.size
|
143
|
+
|
144
|
+
while curPos < endPos
|
145
|
+
# read the size and type
|
146
|
+
size, dataNew = Wexpr::UVLQ64::read(data[curPos..-1])
|
147
|
+
|
148
|
+
# TODO: if !dataNewPos
|
149
|
+
|
150
|
+
# dont move past the size yet. just track it for later
|
151
|
+
sizeSize = data.size-curPos - dataNew.size
|
152
|
+
|
153
|
+
type = data[curPos+sizeSize].ord
|
154
|
+
|
155
|
+
# given: type >= 0x00
|
156
|
+
if type <= 0x04 # its a known type
|
157
|
+
# cool parse it
|
158
|
+
if expr != nil
|
159
|
+
raise StandardError.new ("Found multiple expression chunks")
|
160
|
+
end
|
161
|
+
|
162
|
+
# hand it the entire chunk including the size and the type
|
163
|
+
expr = Wexpr::Expression.create_from_binary_chunk(
|
164
|
+
data[curPos..-1]
|
165
|
+
)
|
166
|
+
else
|
167
|
+
STDERR.puts "Warning: Unknown chunk with type #{type} at byte 0x#{(curPos+sizeSize).to_s(16)} size is #{sizeSize}"
|
168
|
+
end
|
169
|
+
|
170
|
+
# move forward : pass the type, pass the size
|
171
|
+
curPos += Wexpr::Expression::SIZE_U8 + sizeSize
|
172
|
+
curPos += size
|
173
|
+
end
|
174
|
+
else
|
175
|
+
# assume string
|
176
|
+
expr = Wexpr::Expression.create_from_string(inputString)
|
177
|
+
end
|
178
|
+
|
179
|
+
rescue Wexpr::Exception => e
|
180
|
+
if isValidate
|
181
|
+
writeAllOutputTo(options[:output], "false\n")
|
182
|
+
else
|
183
|
+
STDERR.puts "wexprTool: Error occurred with wexpr:"
|
184
|
+
STDERR.puts "wexprTool: #{options[:input]}:#{e.line}:#{e.column}: #{e.message}"
|
185
|
+
end
|
186
|
+
|
187
|
+
exit 1
|
188
|
+
end
|
189
|
+
|
190
|
+
# loaded fine
|
191
|
+
if isValidate
|
192
|
+
writeAllOutputTo(options[:output], "true\n")
|
193
|
+
|
194
|
+
elsif options[:command] == "humanReadable"
|
195
|
+
writeAllOutputTo(options[:output], expr.create_string_representation(0, [:humanReadable]))
|
196
|
+
|
197
|
+
elsif options[:command] == "mini"
|
198
|
+
writeAllOutputTo(options[:output], expr.create_string_representation(0, []))
|
199
|
+
|
200
|
+
elsif options[:command] == "binary"
|
201
|
+
# FUTURE: create binary rep and write out
|
202
|
+
binData = expr.create_binary_representation()
|
203
|
+
|
204
|
+
writeAllOutputWithFileHeaderTo(options[:output], binData)
|
205
|
+
end
|
206
|
+
else
|
207
|
+
STDERR.puts "wexprTool: Unknown command"
|
208
|
+
exit 1
|
209
|
+
end
|
210
|
+
|
211
|
+
# success
|
data/lib/wexpr.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'wexpr/exception'
|
2
|
+
require 'wexpr/expression'
|
3
|
+
require 'wexpr/object_ext'
|
4
|
+
require 'wexpr/uvlq64'
|
5
|
+
require 'wexpr/version'
|
6
|
+
|
7
|
+
#
|
8
|
+
# Ruby-Wexpr library
|
9
|
+
#
|
10
|
+
# Currently does not handle Binary Wexpr.
|
11
|
+
#
|
12
|
+
module Wexpr
|
13
|
+
#
|
14
|
+
# Parse Wexpr and turn it into a ruby hash
|
15
|
+
# Will thrown an Exception on failure
|
16
|
+
#
|
17
|
+
def self.load(str, flags=[])
|
18
|
+
expr = Expression::create_from_string(str, flags)
|
19
|
+
return expr.to_ruby
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Emit a hash as the equivilant wexpr string, human readable or not.
|
24
|
+
# See possible writeflags in Expression.
|
25
|
+
#
|
26
|
+
def self.dump(variable, writeFlags=[])
|
27
|
+
# first step, go through the variable and create the equivilant wexpr expressions
|
28
|
+
expr = Expression::create_from_ruby(variable)
|
29
|
+
|
30
|
+
# then have it write out the string
|
31
|
+
return expr.create_string_representation(0, writeFlags)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|