lunar_cheese 0.2.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 +7 -0
- data/.yardopts +3 -0
- data/LICENSE +14 -0
- data/README.md +44 -0
- data/Rakefile +5 -0
- data/lib/lunar_cheese.rb +6 -0
- data/lib/lunar_cheese/ast.rb +105 -0
- data/lib/lunar_cheese/dump.rb +144 -0
- data/lib/lunar_cheese/grammar.rb +680 -0
- data/lib/lunar_cheese/grammar.treetop +45 -0
- data/lunar_cheese.gemspec +20 -0
- data/spec/lunar_cheese/assignments_spec.rb +71 -0
- data/spec/lunar_cheese/booleans_spec.rb +40 -0
- data/spec/lunar_cheese/floats_spec.rb +67 -0
- data/spec/lunar_cheese/identifiers_spec.rb +63 -0
- data/spec/lunar_cheese/integers_spec.rb +85 -0
- data/spec/lunar_cheese/strings_spec.rb +81 -0
- data/spec/lunar_cheese/tables_spec.rb +114 -0
- data/spec/spec_helper.rb +14 -0
- metadata +101 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
module LunarCheese
|
2
|
+
grammar LuaTableDump
|
3
|
+
|
4
|
+
rule table
|
5
|
+
'{' child:(assignment / value)? other_children:(',' child:(assignment / value))* '}' <AST::LuaTable>
|
6
|
+
end
|
7
|
+
|
8
|
+
rule prefix
|
9
|
+
'+' / '-'
|
10
|
+
end
|
11
|
+
|
12
|
+
rule integer
|
13
|
+
prefix? [0-9]+ <AST::IntegerLiteral>
|
14
|
+
end
|
15
|
+
|
16
|
+
rule float
|
17
|
+
integer '.' [0-9]+ <AST::FloatLiteral>
|
18
|
+
end
|
19
|
+
|
20
|
+
rule boolean
|
21
|
+
'false' <AST::BooleanLit>
|
22
|
+
/
|
23
|
+
'true' <AST::BooleanLit>
|
24
|
+
end
|
25
|
+
|
26
|
+
rule string
|
27
|
+
'"' text:('\"' / !'"' .)* '"' <AST::StringLiteral>
|
28
|
+
end
|
29
|
+
|
30
|
+
rule identifier
|
31
|
+
[a-zA-Z_] [a-zA-Z0-9_]* <AST::IdentifierLiteral>
|
32
|
+
/
|
33
|
+
'[' [^\[\]]+ ']' <AST::IdentifierLiteral>
|
34
|
+
end
|
35
|
+
|
36
|
+
rule value
|
37
|
+
(float / integer / string / boolean / table)
|
38
|
+
end
|
39
|
+
|
40
|
+
rule assignment
|
41
|
+
identifier '=' value <AST::Assignment>
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'lunar_cheese'
|
5
|
+
s.version = '0.2.3'
|
6
|
+
s.summary = 'Treetop grammar & additions to core classes to convert "Don\'t Starve" save games to Ruby and vice versa.'
|
7
|
+
s.description = 'A gem consisting of two parts: A Treetop-based grammar which allows to parse save games of the game "Don\'t Starve" to Ruby objects, and additions to Ruby core classes to dump those back into the same format.'
|
8
|
+
|
9
|
+
s.author = 'Michael Senn'
|
10
|
+
s.email = 'morrolan@morrolan.ch'
|
11
|
+
s.homepage = 'https://bitbucket.org/Lavode/lunar-cheese'
|
12
|
+
s.license = 'Apache 2.0'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files spec/`.split("\n")
|
16
|
+
|
17
|
+
s.add_dependency 'treetop'
|
18
|
+
|
19
|
+
s.add_development_dependency 'rspec'
|
20
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module LunarCheese
|
6
|
+
|
7
|
+
describe LuaTableDumpParser do
|
8
|
+
let(:p) { LuaTableDumpParser.new }
|
9
|
+
|
10
|
+
context 'parsing' do
|
11
|
+
before(:each) { p.root = :assignment }
|
12
|
+
|
13
|
+
context 'valid assignments' do
|
14
|
+
|
15
|
+
it 'should parse assignments of numerics' do
|
16
|
+
expect(parse('x=12')).to eq({ :x => 12 })
|
17
|
+
expect(parse('foo=21.5')).to eq({ :foo => 21.5 })
|
18
|
+
expect(parse('salary=9.5')).to eq({ :salary => 9.5 })
|
19
|
+
expect(parse('[3]=15.345')).to eq({ :'[3]' => 15.345 })
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should parse assignments of strings' do
|
23
|
+
expect(parse('race="Human"')).to eq({ :race => 'Human' })
|
24
|
+
expect(parse('greeting="Hello World!"')).to eq({ :greeting => 'Hello World!' })
|
25
|
+
expect(parse('[e=]="m*c^2"')).to eq({ :'[e=]' => 'm*c^2' })
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should parse assignments of booleans' do
|
29
|
+
expect(parse('is_working=true')).to eq({ :is_working => true })
|
30
|
+
expect(parse('on_fire=false')).to eq({ :on_fire => false })
|
31
|
+
expect(parse('[x && true]=true')).to eq({ :'[x && true]' => true })
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should parse assignment of tables' do
|
35
|
+
expect(parse('person={name="John",age=55}')).to eq({ :person => { :name => 'John', :age => 55 } })
|
36
|
+
expect(parse('coords={10,5,10}')).to eq({ :coords => { 0 => 10, 1 => 5, 2 => 10 } })
|
37
|
+
expect(parse('coords={foo="bar",1,2}')).to eq({ :coords => { :foo => 'bar', 0 => 1, 1 => 2 } })
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'invalid assignments' do
|
43
|
+
|
44
|
+
it 'should not parse assignments which are missing the lhs' do
|
45
|
+
expect(parse('=3')).to be_nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should not parse assignments which are missing the rhs' do
|
49
|
+
expect(parse('foo=')).to be_nil
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should not parse assignments without an 'equals' sign" do
|
53
|
+
expect(parse('foo123')).to be_nil
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should not parse assignments with multiple 'equals' signs" do
|
57
|
+
expect(parse('foo=="bar"')).to be_nil
|
58
|
+
expect(parse('foo="bar"="baz"')).to be_nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should not parse the 'equals' sign alone" do
|
62
|
+
expect(parse('=')).to be_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module LunarCheese
|
6
|
+
|
7
|
+
describe LuaTableDumpParser do
|
8
|
+
let(:p) { LuaTableDumpParser.new }
|
9
|
+
|
10
|
+
context 'parsing' do
|
11
|
+
before(:each) { p.root = :boolean }
|
12
|
+
|
13
|
+
it 'should parse booleans' do
|
14
|
+
result = parse('false')
|
15
|
+
expect(result).to be_false
|
16
|
+
expect(result).to_not be_nil
|
17
|
+
|
18
|
+
expect(parse('true')).to be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should not parse invalid booleans' do
|
22
|
+
expect(parse('False')).to be_nil
|
23
|
+
expect(parse('True')).to be_nil
|
24
|
+
expect(parse('fAtrue')).to be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'dumping' do
|
30
|
+
|
31
|
+
it 'should dump booleans' do
|
32
|
+
expect(false.to_lua).to eq 'false'
|
33
|
+
expect(true.to_lua).to eq 'true'
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module LunarCheese
|
6
|
+
|
7
|
+
describe LuaTableDumpParser do
|
8
|
+
let(:p) { LuaTableDumpParser.new }
|
9
|
+
|
10
|
+
context 'parsing' do
|
11
|
+
before(:each) { p.root = :float }
|
12
|
+
|
13
|
+
context 'valid floats' do
|
14
|
+
|
15
|
+
it 'should parse basic floats' do
|
16
|
+
expect(parse('0.512')).to eq 0.512
|
17
|
+
expect(parse('21.27')).to eq 21.27
|
18
|
+
expect(parse('0.00')).to eq 0
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should parse floats with a prefix' do
|
22
|
+
expect(parse('-193.2')).to eq -193.2
|
23
|
+
expect(parse('+9.1')).to eq 9.1
|
24
|
+
expect(parse('-211.286')).to eq -211.286
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'invalid floats' do
|
30
|
+
|
31
|
+
it 'should not parse floats with more than one period' do
|
32
|
+
expect(parse('15..6')).to be_nil
|
33
|
+
expect(parse('14.5.7')).to be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should not parse floats with a period in the beginning or end' do
|
37
|
+
expect(parse('132.')).to be_nil
|
38
|
+
expect(parse('.25')).to be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'dumping' do
|
46
|
+
|
47
|
+
it 'should dump basic floats' do
|
48
|
+
expect(0.0.to_lua).to eq '0.0'
|
49
|
+
expect(928.51.to_lua).to eq '928.51'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should dump floats with a prefix' do
|
53
|
+
expect(-50.2.to_lua).to eq '-50.2'
|
54
|
+
expect(-21.5.to_lua).to eq '-21.5'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should not print the prefix for numbers >= 0' do
|
58
|
+
expect(-0.to_lua).to eq '0'
|
59
|
+
expect(0.5.to_lua).to eq '0.5'
|
60
|
+
expect(+213.21.to_lua).to eq '213.21'
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module LunarCheese
|
6
|
+
|
7
|
+
describe LuaTableDumpParser do
|
8
|
+
let(:p) { LuaTableDumpParser.new }
|
9
|
+
|
10
|
+
context 'parsing' do
|
11
|
+
before(:each) { p.root = :identifier }
|
12
|
+
|
13
|
+
context 'valid identifiers' do
|
14
|
+
|
15
|
+
it 'should parse identifiers' do
|
16
|
+
expect(parse('x')).to eq :x
|
17
|
+
expect(parse('age')).to eq :age
|
18
|
+
expect(parse('last_name')).to eq :last_name
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should parse identifiers in brackets' do
|
22
|
+
expect(parse('[1]')).to eq :'[1]'
|
23
|
+
expect(parse('[25]')).to eq :'[25]'
|
24
|
+
expect(parse('[*]')).to eq :'[*]'
|
25
|
+
expect(parse('[liar liar pants on fire!]')).to eq :'[liar liar pants on fire!]'
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'invalid identifiers' do
|
31
|
+
|
32
|
+
it 'should not parse non-alphanumeric (+underscore) identifiers without brackets' do
|
33
|
+
expect(parse('ab*c')).to be_nil
|
34
|
+
expect(parse('foo-bar_baz')).to be_nil
|
35
|
+
expect(parse('Hello there. :D')).to be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'dumping' do
|
43
|
+
|
44
|
+
it 'should dump alphanumeric (+underscore) identifiers' do
|
45
|
+
expect(:last_name.to_lua).to eq 'last_name'
|
46
|
+
expect(:age.to_lua).to eq 'age'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should dump non-alphanumeric (+underscore) identifiers' do
|
50
|
+
expect(:'e=m*c^2'.to_lua).to eq '[e=m*c^2]'
|
51
|
+
expect(:'a big horse'.to_lua).to eq '[a big horse]'
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should dump identifiers inside square brackets' do
|
55
|
+
expect(:'[1]'.to_lua).to eq '[1]'
|
56
|
+
expect(:'[liar liar pants on fire!]'.to_lua).to eq '[liar liar pants on fire!]'
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module LunarCheese
|
6
|
+
|
7
|
+
describe LuaTableDumpParser do
|
8
|
+
let(:p) { LuaTableDumpParser.new }
|
9
|
+
|
10
|
+
context 'parsing' do
|
11
|
+
before(:each) { p.root = :integer }
|
12
|
+
|
13
|
+
context 'valid integers' do
|
14
|
+
|
15
|
+
it 'should parse basic integers' do
|
16
|
+
expect(parse('0')).to eq 0
|
17
|
+
expect(parse('21')).to eq 21
|
18
|
+
expect(parse('928')).to eq 928
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should parse integers with a prefix' do
|
22
|
+
expect(parse('+0')).to eq 0
|
23
|
+
expect(parse('-0')).to eq 0
|
24
|
+
expect(parse('+216')).to eq 216
|
25
|
+
expect(parse('-31')).to eq -31
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'invalid integers' do
|
31
|
+
|
32
|
+
it 'should not parse integers with more than one prefix' do
|
33
|
+
expect(parse('--21')).to be_nil
|
34
|
+
expect(parse('-+18')).to be_nil
|
35
|
+
expect(parse('+-157')).to be_nil
|
36
|
+
expect(parse('++24')).to be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should not parse integers with an invalid prefix' do
|
40
|
+
expect(parse('*21')).to be_nil
|
41
|
+
expect(parse('§18')).to be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should not parse integers with a prefix anywhere but the very beginning' do
|
45
|
+
expect(parse('2+1')).to be_nil
|
46
|
+
expect(parse('-14+5')).to be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should not parse prefixes alone' do
|
50
|
+
expect(parse('-')).to be_nil
|
51
|
+
expect(parse('+')).to be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should not parse integers which consist of non-numeric characters' do
|
55
|
+
expect(parse('12a')).to be_nil
|
56
|
+
expect(parse('a51§')).to be_nil
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'dumping' do
|
64
|
+
|
65
|
+
it 'should dump basic integers' do
|
66
|
+
expect(0.to_lua).to eq '0'
|
67
|
+
expect(928.to_lua).to eq '928'
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should dump integers with a prefix' do
|
71
|
+
expect(-50.to_lua).to eq '-50'
|
72
|
+
expect(-21.to_lua).to eq '-21'
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should not print the prefix for numbers >= 0' do
|
76
|
+
expect(-0.to_lua).to eq '0'
|
77
|
+
expect(0.to_lua).to eq '0'
|
78
|
+
expect(+213.to_lua).to eq '213'
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
|
3
|
+
require_relative '../spec_helper'
|
4
|
+
|
5
|
+
module LunarCheese
|
6
|
+
|
7
|
+
describe LuaTableDumpParser do
|
8
|
+
let(:p) { LuaTableDumpParser.new }
|
9
|
+
|
10
|
+
context 'parsing' do
|
11
|
+
before(:each) { p.root = :string }
|
12
|
+
|
13
|
+
context 'valid strings' do
|
14
|
+
|
15
|
+
it 'should parse empty strings' do
|
16
|
+
expect(parse('""')).to eq ''
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should parse alphanumeric one-word strings' do
|
20
|
+
expect(parse('"foobar"')).to eq 'foobar'
|
21
|
+
expect(parse('"Michael"')).to eq 'Michael'
|
22
|
+
expect(parse('"Parsing4Life"')).to eq 'Parsing4Life'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should parse non-alphanumeric strings' do
|
26
|
+
expect(parse('"@¼|¬§éLA12^"')).to eq '@¼|¬§éLA12^'
|
27
|
+
expect(parse('"l\'église"')).to eq "l'église"
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should parse multi-word strings' do
|
31
|
+
expect(parse('"Hello world, how are you today?"')).to eq 'Hello world, how are you today?'
|
32
|
+
expect(parse('"e = m * c ^ 2"')).to eq 'e = m * c ^ 2'
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should parse strings with escaped quotation marks' do
|
36
|
+
expect(parse('"He called her \"Honeymoon\""')).to eq 'He called her "Honeymoon"'
|
37
|
+
expect(parse('"Yes, \"John\" is his name, now kill him!"')).to eq 'Yes, "John" is his name, now kill him!'
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'invalid strings' do
|
43
|
+
|
44
|
+
it 'should not parse strings without quotation marks' do
|
45
|
+
expect(parse('hello there')).to be_nil
|
46
|
+
expect(parse('but I want to be parsed D:')).to be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should not parse strings with the leading or trailing quotation mark missing' do
|
50
|
+
expect(parse('"Hello wor...')).to be_nil
|
51
|
+
expect(parse('..lo world"')).to be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should not parse strings with non-escaped quotation marks' do
|
55
|
+
expect(parse('"He called her \"Honeymoon""')).to be_nil
|
56
|
+
expect(parse('"Yes, "John\" is his name, now kill him!"')).to be_nil
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'dumping' do
|
64
|
+
|
65
|
+
it 'should dump empty strings' do
|
66
|
+
expect(''.to_lua).to eq '""'
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should dump strings' do
|
70
|
+
expect('Hello World!'.to_lua).to eq '"Hello World!"'
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should escape quotation marks' do
|
74
|
+
expect('He called her "Honeymoon"'.to_lua).to eq '"He called her \"Honeymoon\""'
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|