lua-literal 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 07f66fc9b4403eb46e870cf8c10f2e5276ccc91cfeb1a97c107143942875d34e
4
+ data.tar.gz: 5d844615dd7ab5dc9c217a60376875a2311111cc505612de16e2adc77e0dd0dd
5
+ SHA512:
6
+ metadata.gz: 023a3ed726e61843bc68a7d21df59cfed1162c9942ba884cc66b2fc9467327294702e09a6725edb3f6ba24f20fa08cf9d0e5c046e3eeaccdb3a642e29e467e7f
7
+ data.tar.gz: 597cf6e0a42df7a3ac9695abd8218a73052119988ec624b68667a71cfa130c0e0902d0b57278e1d294779d6011951d3fad08f8337f7e3c960e3926101b1f5e2a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
14
+ /vendor/bundle/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,139 @@
1
+ require: 'rubocop-rspec'
2
+ inherit_from: .rubocop_todo.yml
3
+
4
+ AllCops:
5
+ DefaultFormatter: simple
6
+ DisplayCopNames: true
7
+ DisplayStyleGuide: true
8
+ EnabledByDefault: true
9
+ Exclude:
10
+ - bin/*
11
+ - spec/spec_helper.rb
12
+ - vendor/bundle/**/*
13
+ ExtraDetails: true
14
+ TargetRubyVersion: 2.7
15
+ UseCache: true
16
+
17
+ Layout/EmptyLineAfterMagicComment:
18
+ Enabled: true
19
+
20
+ Layout/LineLength:
21
+ Enabled: true
22
+ AllowHeredoc: true
23
+ AllowURI: true
24
+ IgnoreCopDirectives: true
25
+ IgnoredPatterns:
26
+ - '%x'
27
+ URISchemes:
28
+ - http
29
+ - https
30
+
31
+ Layout/MultilineAssignmentLayout:
32
+ Enabled: true
33
+
34
+ Layout/MultilineOperationIndentation:
35
+ Enabled: false
36
+ # EnforcedStyle: aligned
37
+ # This does not work as expected for vertically aligned _alternative_ rules.
38
+ # And it won't be fixed: https://github.com/rubocop-hq/rubocop/issues/7094
39
+
40
+ Layout/SpaceAroundOperators:
41
+ Enabled: true
42
+
43
+ Layout/SpaceInsideBlockBraces:
44
+ Enabled: true
45
+ EnforcedStyle: space
46
+ EnforcedStyleForEmptyBraces: no_space
47
+ SpaceBeforeBlockParameters: false
48
+
49
+ Layout/SpaceInsideHashLiteralBraces:
50
+ Enabled: true
51
+ EnforcedStyle: no_space
52
+
53
+ Lint/NumberConversion:
54
+ Enabled: true
55
+
56
+ Metrics/BlockLength:
57
+ Enabled: true
58
+ Exclude:
59
+ - spec/**/*_spec.rb
60
+
61
+ RSpec/EmptyLineAfterExample:
62
+ Enabled: true
63
+
64
+ RSpec/ExpectActual:
65
+ Enabled: true
66
+
67
+ Style/AsciiComments:
68
+ Enabled: true
69
+
70
+ Style/BlockDelimiters:
71
+ Enabled: true
72
+ IgnoredMethods:
73
+ - rule
74
+
75
+ Style/CommandLiteral:
76
+ Enabled: true
77
+ EnforcedStyle: percent_x
78
+
79
+ Style/CommentAnnotation:
80
+ Enabled: true
81
+
82
+ Style/CollectionMethods:
83
+ Enabled: false
84
+
85
+ Style/Copyright:
86
+ Enabled: false
87
+
88
+ Style/FrozenStringLiteralComment:
89
+ Enabled: true
90
+
91
+ Style/HashSyntax:
92
+ EnforcedStyle: ruby19
93
+
94
+ Style/MethodCallWithArgsParentheses:
95
+ Enabled: true
96
+ IgnoredMethods:
97
+ # language core
98
+ - require
99
+ - require_relative
100
+ # gem/bundler
101
+ - gem
102
+ - source
103
+ - add_development_dependency
104
+ - add_runtime_dependency
105
+ # rake
106
+ - task
107
+ # rspec
108
+ - be
109
+ - describe
110
+ - eq
111
+ - it
112
+ - to
113
+ - not_to
114
+
115
+ Style/MutableConstant:
116
+ Enabled: true
117
+
118
+ Style/PercentLiteralDelimiters:
119
+ Enabled: true
120
+ PreferredDelimiters:
121
+ '%q': '|'
122
+
123
+ Style/RedundantPercentQ:
124
+ Enabled: true
125
+
126
+ Style/RegexpLiteral:
127
+ Enabled: true
128
+ EnforcedStyle: slashes
129
+ AllowInnerSlashes: true
130
+
131
+ Style/SingleLineBlockParams:
132
+ Enabled: false
133
+
134
+ Style/StringHashKeys:
135
+ Enabled: true
136
+
137
+ Style/StringLiterals:
138
+ Enabled: true
139
+ EnforcedStyle: single_quotes
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,31 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config --no-auto-gen-timestamp`
3
+ # using RuboCop version 0.80.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 3
10
+ Style/Documentation:
11
+ Exclude:
12
+ - 'spec/**/*'
13
+ - 'test/**/*'
14
+ - 'lib/lua/literal/converter.rb'
15
+ - 'lib/lua/literal/transform.rb'
16
+ - 'lib/lua/literal/version.rb'
17
+
18
+ # Offense count: 2
19
+ # Configuration parameters: RequireForNonPublicMethods.
20
+ Style/DocumentationMethod:
21
+ Exclude:
22
+ - 'spec/**/*'
23
+ - 'test/**/*'
24
+ - 'lib/lua/literal/converter.rb'
25
+
26
+ # Offense count: 6
27
+ # Cop supports --auto-correct.
28
+ # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
29
+ # URISchemes: http, https
30
+ Layout/LineLength:
31
+ Max: 118
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 OZAWA Sakuro
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,82 @@
1
+ # Lua::Literal
2
+
3
+ Lua::Literal provides a class which converts literals written in [Lua](https://www.lua.org/)'s syntax to Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lua-literal'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lua-literal
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'lua/literal/converter'
25
+
26
+ converter = Lua::Literal::Converter.new
27
+ converter.convert('{a=1, b=2, ["c"]={d=3}, {e=4}, {f=5}}')
28
+ #=> {:a=>1, :b=>2, "c"=>{:d=>3}, 1=>{:e=>4}, 2=>{:f=>5}}
29
+ ```
30
+
31
+ ## Supported literals
32
+
33
+ ### `true`, `false` and `nil`
34
+
35
+ These are converted to Ruby's `true`, `false` and `nil` respectively.
36
+
37
+ ### Numerals
38
+
39
+ Only decimal numerals are recognized and converted Ruby's `Integer` or `Float`.
40
+ As in Lua, unary plus operator is not allowed.
41
+
42
+ ### Strings
43
+
44
+ Only short string literals are converted.
45
+
46
+ Following escape sequences are supported.
47
+
48
+ * C-like escape sequences
49
+ * Hexadecimal and decimal escape sequences
50
+ * Unicode escape sequences
51
+
52
+ ### Tables
53
+
54
+ Lua's tables are converted to Ruby's `Hash`.
55
+
56
+ * Expression only fields, which are indexed by integer keys in Lua, are also stored as key-value pairs.
57
+ * Name only keys (not in `[...]`) are converted to Ruby's `Symbol`s.
58
+
59
+ ### Comments
60
+
61
+ Short comments are recognized and removed.
62
+
63
+ ## Unsupported literals
64
+
65
+ * Hexadecimal numerals.
66
+ * Long string literals (denoted by `[[`, `[=[` and such)
67
+ * Long comments.
68
+
69
+ ## Development
70
+
71
+ 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.
72
+
73
+ 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).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sakuro/lua-literal.
78
+
79
+
80
+ ## License
81
+
82
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lua/literal"
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/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/rubocop ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lua/literal/version'
4
+ require 'lua/literal/converter'
5
+ require 'lua/literal/parser'
6
+ require 'lua/literal/transform'
7
+
8
+ module Lua
9
+ module Literal
10
+ class Error < StandardError; end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lua/literal/parser'
4
+ require 'lua/literal/transform'
5
+
6
+ module Lua
7
+ module Literal
8
+ class Converter
9
+ def initialize
10
+ @parser = Lua::Literal::Parser.new
11
+ @transform = Lua::Literal::Transform.new
12
+ end
13
+
14
+ def convert(lua_literal)
15
+ @transform.apply(@parser.parse(lua_literal))
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parslet'
4
+
5
+ module Lua
6
+ module Literal
7
+ # https://www.lua.org/manual/5.3/manual.html
8
+ class Parser < Parslet::Parser # rubocop:disable Metrics/ClassLength
9
+ root :exp
10
+
11
+ # table_constructor ::= '{' [fieldlist] '}'
12
+ rule(:table_constructor) {
13
+ (str('{') >> space? >> fieldlist.maybe >> space? >> str('}')).as(:table)
14
+ }
15
+
16
+ # fieldlist ::= field {fieldsep field} [fieldsep]
17
+ rule(:fieldlist) {
18
+ (field >> space? >> (fieldsep >> space? >> field).repeat).as(:fields) >> space? >> fieldsep.maybe
19
+ }
20
+
21
+ # field ::= '[' exp ']' '=' exp | Name '=' exp | exp
22
+ rule(:field) {
23
+ (str('[') >> space? >> exp.as(:key) >> space? >> str(']') >> space? >> str('=') >> space? >> exp.as(:value)) |
24
+ (name.as(:key) >> space? >> str('=') >> space? >> exp.as(:value)) |
25
+ exp.as(:indexed)
26
+ }
27
+
28
+ # fieldsep ::= ',' | ';'
29
+ rule(:fieldsep) {
30
+ str(',') | str(';')
31
+ }
32
+
33
+ # -- exp ::= prefixexp
34
+ # exp ::= nil | false | true
35
+ # exp ::= Numeral
36
+ # exp ::= LiteralString
37
+ # -- exp ::= functiondef
38
+ # exp ::= table_constructor
39
+ # -- exp ::= '...'
40
+ # -- exp ::= exp binop exp
41
+ # -- exp ::= unop exp
42
+ # -- prefixexp ::= var | functioncall | '(' exp ')'
43
+ rule(:exp) {
44
+ space? >>
45
+ (
46
+ table_constructor |
47
+ literal_nil |
48
+ literal_false |
49
+ literal_true |
50
+ numeral |
51
+ literal_string
52
+ ) >> space?
53
+ }
54
+
55
+ rule(:literal_nil) {
56
+ str('nil')
57
+ }
58
+
59
+ rule(:literal_false) {
60
+ str('false')
61
+ }
62
+
63
+ rule(:literal_true) {
64
+ str('true')
65
+ }
66
+
67
+ # numeral
68
+ rule(:numeral) {
69
+ decimal_numeral
70
+ # TODO: hexadecimal_numeral
71
+ }
72
+
73
+ rule(:decimal_numeral) {
74
+ (str('-').maybe >> decimal_float).as(:float) |
75
+ (str('-').maybe >> decimal_integer).as(:integer)
76
+ }
77
+
78
+ rule(:decimal_figure) {
79
+ match('[0-9]')
80
+ }
81
+
82
+ rule(:decimal_integer) {
83
+ decimal_figure.repeat(1)
84
+ }
85
+
86
+ rule(:decimal_integer?) {
87
+ decimal_figure.repeat(0)
88
+ }
89
+
90
+ rule(:decimal_float) {
91
+ decimal_integer >> str('.') >> decimal_integer? >> decimal_exponent.maybe |
92
+ decimal_integer? >> str('.') >> decimal_integer >> decimal_exponent.maybe |
93
+ decimal_integer >> decimal_exponent
94
+ }
95
+
96
+ rule(:decimal_exponent) {
97
+ match('[eE]') >> match('[-+]').maybe >> decimal_integer
98
+ }
99
+
100
+ # literal_string
101
+ rule(:literal_string) {
102
+ short_literal_string
103
+ # TODO: long_literal_string
104
+ }
105
+
106
+ rule(:short_literal_string) {
107
+ q >> character.repeat.as(:string) >> q |
108
+ qq >> character.repeat.as(:string) >> qq
109
+ }
110
+
111
+ rule(:q) {
112
+ str('\'')
113
+ }
114
+
115
+ rule(:qq) {
116
+ str('"')
117
+ }
118
+
119
+ rule(:esc) {
120
+ str('\\')
121
+ }
122
+
123
+ rule(:character) {
124
+ escape_sequence |
125
+ hexadecimal_sequence |
126
+ decimal_sequence |
127
+ unicode_sequence |
128
+ raw_character
129
+ }
130
+
131
+ rule(:escape_sequence) {
132
+ esc >> (q | qq | esc | match('[abfnrtv]')).as(:escape_sequence)
133
+ # TODO: \z
134
+ }
135
+
136
+ rule(:hexadecimal_sequence) {
137
+ esc >> match('[xX]') >> hexadecimal_digit.repeat(2, 2).as(:hexadecimal_sequence)
138
+ }
139
+
140
+ rule(:decimal_sequence) {
141
+ esc >> decimal_digit.repeat(1, 3).as(:decimal_sequence)
142
+ }
143
+
144
+ rule(:unicode_sequence) {
145
+ esc >> match('[uU]') >> str('{') >> hexadecimal_digit.repeat(1).as(:unicode_sequence) >> str('}')
146
+ }
147
+
148
+ rule(:raw_character) {
149
+ match('[^\\\'"]').as(:raw_character)
150
+ }
151
+
152
+ rule(:hexadecimal_digit) {
153
+ match('[0-9a-fA-F]')
154
+ }
155
+
156
+ rule(:decimal_digit) {
157
+ match('[0-9]')
158
+ }
159
+
160
+ rule(:name) {
161
+ (match('[A-Za-z_]') >> match('[A-Za-z_0-9]').repeat).as(:name)
162
+ }
163
+
164
+ rule(:space) {
165
+ match('\s')
166
+ }
167
+
168
+ rule(:comment) {
169
+ short_comment
170
+ # TODO: long_comment
171
+ }
172
+
173
+ rule(:short_comment) {
174
+ str('--') >> match('[^\n]').repeat >> str("\n").maybe
175
+ }
176
+
177
+ rule(:space?) {
178
+ (space | comment).repeat
179
+ }
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'parslet'
4
+
5
+ module Lua
6
+ module Literal
7
+ class Transform < Parslet::Transform
8
+ ESCAPE_SEQUENCES = {
9
+ # rubocop:disable Style/StringHashKeys
10
+ '\'' => '\'',
11
+ '"' => '"',
12
+ '\\' => '\\',
13
+ 'a' => "\a",
14
+ 'b' => "\b",
15
+ 'f' => "\f",
16
+ 'n' => "\n",
17
+ 'r' => "\r",
18
+ 't' => "\t",
19
+ 'v' => "\v"
20
+ # rubocop:enable Style/StringHashKeys
21
+ }.freeze
22
+ private_constant :ESCAPE_SEQUENCES
23
+
24
+ rule(integer: simple(:v)) {
25
+ Integer(v)
26
+ }
27
+
28
+ rule(float: simple(:v)) {
29
+ Float(v)
30
+ }
31
+
32
+ rule(name: simple(:v)) {
33
+ v.to_sym
34
+ }
35
+
36
+ rule(raw_character: simple(:v)) {
37
+ v.to_s
38
+ }
39
+
40
+ rule(escape_sequence: simple(:v)) {
41
+ ESCAPE_SEQUENCES.fetch(v.to_s)
42
+ }
43
+
44
+ rule(hexadecimal_sequence: simple(:v)) {
45
+ [v].pack('H2')
46
+ }
47
+
48
+ rule(decimal_sequence: simple(:v)) {
49
+ [Integer(v)].pack('C')
50
+ }
51
+
52
+ rule(unicode_sequence: simple(:v)) {
53
+ [v].pack('H*').force_encoding('UTF-16BE').encode('UTF-8')
54
+ }
55
+
56
+ rule(string: sequence(:vs)) {
57
+ vs.join
58
+ }
59
+
60
+ rule('nil') {
61
+ nil
62
+ }
63
+
64
+ rule('true') {
65
+ true
66
+ }
67
+
68
+ rule('false') {
69
+ false
70
+ }
71
+
72
+ rule(table: {fields: subtree(:fields)}) {
73
+ pairs = (fields.is_a?(Hash) ? [fields] : fields)
74
+ indexed, kvs = pairs.partition {|pair| pair.key?(:indexed) }
75
+ hash = kvs.inject({}) {|acc, pair| acc.merge(pair) }
76
+ indexed.each.with_index(1).each {|v, i| hash[i] = v[:indexed] }
77
+ hash
78
+ }
79
+
80
+ rule(table: '{}') {
81
+ Hash[]
82
+ }
83
+
84
+ rule(key: subtree(:key), value: subtree(:value)) {
85
+ {key => value}
86
+ }
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lua
4
+ module Literal
5
+ VERSION = '0.1.0'
6
+ public_constant :VERSION
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/lua/literal/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'lua-literal'
7
+ spec.version = Lua::Literal::VERSION
8
+ spec.authors = ['OZAWA Sakuro']
9
+ spec.email = ['sakuro@2238.club']
10
+
11
+ spec.summary = 'Converter for Lua literals'
12
+ spec.description = <<~DESCRIPTION
13
+ Lua-literal provides a class for conversion of Lua's literals to Ruby.
14
+ DESCRIPTION
15
+ spec.homepage = 'https://github.com/sakuro/lua-literal'
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/sakuro/lua-literal.git'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/sakuro/lua-literal/blob/master/CHANGES.md'
22
+
23
+ spec.files = Dir.chdir(__dir__) { %x(git ls-files -z).split("\x0").grep_v(/^spec\//) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'pry'
27
+ spec.add_development_dependency 'pry-byebug'
28
+ spec.add_development_dependency 'rake', '~> 12.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.9'
30
+ spec.add_development_dependency 'rubocop', '~> 0.80.0'
31
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.38.1'
32
+ spec.add_development_dependency 'yard'
33
+
34
+ spec.add_runtime_dependency 'parslet', '~> 2.0.0'
35
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lua-literal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - OZAWA Sakuro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
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: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.80.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.80.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.38.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.38.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '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'
111
+ - !ruby/object:Gem::Dependency
112
+ name: parslet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.0.0
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.0.0
125
+ description: 'Lua-literal provides a class for conversion of Lua''s literals to Ruby.
126
+
127
+ '
128
+ email:
129
+ - sakuro@2238.club
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".gitignore"
135
+ - ".rspec"
136
+ - ".rubocop.yml"
137
+ - ".rubocop_todo.yml"
138
+ - ".travis.yml"
139
+ - Gemfile
140
+ - LICENSE.txt
141
+ - README.md
142
+ - Rakefile
143
+ - bin/console
144
+ - bin/rake
145
+ - bin/rspec
146
+ - bin/rubocop
147
+ - bin/setup
148
+ - lib/lua/literal.rb
149
+ - lib/lua/literal/converter.rb
150
+ - lib/lua/literal/parser.rb
151
+ - lib/lua/literal/transform.rb
152
+ - lib/lua/literal/version.rb
153
+ - lua-literal.gemspec
154
+ homepage: https://github.com/sakuro/lua-literal
155
+ licenses:
156
+ - MIT
157
+ metadata:
158
+ homepage_uri: https://github.com/sakuro/lua-literal
159
+ source_code_uri: https://github.com/sakuro/lua-literal.git
160
+ changelog_uri: https://github.com/sakuro/lua-literal/blob/master/CHANGES.md
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: 2.7.0
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubygems_version: 3.1.2
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Converter for Lua literals
180
+ test_files: []