dracoon 1.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,228 @@
1
+
2
+ require 'treetop'
3
+
4
+ module DracoonGrammar
5
+
6
+ class Treetop::Runtime::SyntaxNode
7
+ public
8
+ attr_accessor :oid
9
+
10
+ def cutdown
11
+ if self.elements.nil? then return end
12
+ self.elements.each do |e| e.cutdown end
13
+ if (self.respond_to? :customCutdown)
14
+ nodes = Array.new
15
+ self.elements.each do |e| self.customCutdown(e, nodes) end
16
+ self.elements.clear
17
+ self.elements.concat nodes
18
+ end
19
+ end
20
+ end
21
+
22
+
23
+ class ModuleNode < Treetop::Runtime::SyntaxNode
24
+ public
25
+ def customCutdown(node, children)
26
+ if node.is_a? HeaderNode or node.is_a? SceneNode or node.is_a? ItemNode
27
+ children << node
28
+ elsif node.nonterminal?
29
+ node.elements.each do |e| self.customCutdown(e, children) end
30
+ end
31
+ end
32
+ end
33
+
34
+ class HeaderNode < Treetop::Runtime::SyntaxNode
35
+ public
36
+ def customCutdown(node, children)
37
+ if node.is_a? IdentifierNode or node.is_a? StateNode or node.is_a? SummaryNode or node.is_a? ScriptNode
38
+ children << node
39
+ elsif node.nonterminal?
40
+ node.elements.each do |e| self.customCutdown(e, children) end
41
+ end
42
+ end
43
+ end
44
+
45
+ class SceneNode < Treetop::Runtime::SyntaxNode
46
+ public
47
+ def customCutdown(node, children)
48
+ if node.is_a? HeaderNode or node.is_a? PassageNode or node.is_a? ScriptNode
49
+ children << node
50
+ elsif node.nonterminal?
51
+ node.elements.each do |e| self.customCutdown(e, children) end
52
+ end
53
+ end
54
+ end
55
+
56
+ class PassageNode < Treetop::Runtime::SyntaxNode
57
+ public
58
+ def customCutdown(node, children)
59
+ if node.is_a? ContentNode or node.is_a? HeaderNode or node.is_a? ActionNode then
60
+ children << node
61
+ elsif node.nonterminal?
62
+ node.elements.each do |e| self.customCutdown(e, children) end
63
+ end
64
+ end
65
+ end
66
+
67
+ class ItemNode < Treetop::Runtime::SyntaxNode
68
+ public
69
+ def customCutdown(node, children)
70
+ if node.is_a? ContentNode or node.is_a? HeaderNode or node.is_a? ActionNode then
71
+ children << node
72
+ elsif node.nonterminal?
73
+ node.elements.each do |e| self.customCutdown(e, children) end
74
+ end
75
+ end
76
+ end
77
+
78
+ class SummaryNode < Treetop::Runtime::SyntaxNode
79
+ public
80
+ def cutdown
81
+ self.elements.clear if not self.elements.nil?
82
+ end
83
+ end
84
+
85
+ class EndParagraphNode < Treetop::Runtime::SyntaxNode
86
+ public
87
+ def cutdown
88
+ self.elements.clear if not self.elements.nil?
89
+ end
90
+ end
91
+
92
+ class ContentNode < Treetop::Runtime::SyntaxNode
93
+ public
94
+ def customCutdown(node, children)
95
+ if node.is_a? WordNode or node.is_a? PunctuationNode or node.is_a? EndParagraphNode or node.is_a? NewLineNode or node.is_a? LinkNode or node.is_a? InlineTagNode or node.is_a? IfTagNode or node.is_a? ScriptTagNode then
96
+ children << node
97
+ elsif node.nonterminal?
98
+ node.elements.each do |e| self.customCutdown(e, children) end
99
+ end
100
+ end
101
+ end
102
+
103
+ class ActionNode < Treetop::Runtime::SyntaxNode
104
+ public
105
+ def customCutdown(node, children)
106
+ if node.is_a? KeywordNode or node.is_a? ScriptNode then
107
+ children << node
108
+ elsif node.nonterminal?
109
+ node.elements.each do |e| self.customCutdown(e, children) end
110
+ end
111
+ end
112
+ end
113
+
114
+ class NewLineNode < Treetop::Runtime::SyntaxNode
115
+ public
116
+ def cutdown
117
+ self.elements.clear if not self.elements.nil?
118
+ end
119
+ end
120
+
121
+ class LinkNode < Treetop::Runtime::SyntaxNode
122
+ public
123
+ def customCutdown(node, children)
124
+ if node.is_a? WordNode or node.is_a? KeywordNode then
125
+ children << node
126
+ elsif node.nonterminal?
127
+ node.elements.each do |e| self.customCutdown(e, children) end
128
+ end
129
+ end
130
+ end
131
+
132
+ class IdentifierNode < Treetop::Runtime::SyntaxNode
133
+ public
134
+ def cutdown
135
+ self.elements.clear unless self.elements.nil?
136
+ end
137
+ end
138
+
139
+ class StateNode < Treetop::Runtime::SyntaxNode
140
+ public
141
+ def cutdown
142
+ self.elements.clear unless self.elements.nil?
143
+ end
144
+ end
145
+
146
+ class StringNode < Treetop::Runtime::SyntaxNode
147
+ public
148
+ def cutdown
149
+ self.elements.clear if not self.elements.nil?
150
+ end
151
+ end
152
+
153
+ class KeywordNode < Treetop::Runtime::SyntaxNode
154
+ public
155
+ def cutdown
156
+ self.elements.clear if not self.elements.nil?
157
+ end
158
+ end
159
+
160
+ class WordNode < Treetop::Runtime::SyntaxNode
161
+ public
162
+ def cutdown
163
+ self.elements.clear unless self.elements.nil?
164
+ end
165
+ end
166
+
167
+ class NumberNode < Treetop::Runtime::SyntaxNode
168
+ public
169
+ def cutdown
170
+ self.elements.clear unless self.elements.nil?
171
+ end
172
+ end
173
+
174
+ class PunctuationNode < Treetop::Runtime::SyntaxNode
175
+ public
176
+ def cutdown
177
+ self.elements.clear if not self.elements.nil?
178
+ end
179
+ end
180
+
181
+
182
+ class ScriptNode < Treetop::Runtime::SyntaxNode
183
+ public
184
+ def cutdown
185
+ self.elements.clear if not self.elements.nil?
186
+ end
187
+ end
188
+
189
+ class InlineTagNode < Treetop::Runtime::SyntaxNode
190
+ public
191
+ def cutdown
192
+ body = nil
193
+ self.elements.each do |e|
194
+ if e.is_a? ScriptNode then
195
+ body = e
196
+ break
197
+ end
198
+ end
199
+ self.elements.clear unless self.elements.nil?
200
+ if not body.nil? then
201
+ body.cutdown
202
+ self.elements << body
203
+ end
204
+ end
205
+ end
206
+
207
+ class IfTagNode < Treetop::Runtime::SyntaxNode
208
+ public
209
+ def customCutdown(node, children)
210
+ if node.is_a? JSExprNode or node.is_a? ContentNode then
211
+ children << node
212
+ elsif node.nonterminal?
213
+ node.elements.each do |e| self.customCutdown(e, children) end
214
+ end
215
+ end
216
+ end
217
+
218
+ class ScriptTagNode < Treetop::Runtime::SyntaxNode
219
+ end
220
+
221
+ class JSExprNode < Treetop::Runtime::SyntaxNode
222
+ public
223
+ def cutdown
224
+ self.elements.clear if not self.elements.nil?
225
+ end
226
+ end
227
+
228
+ end
@@ -0,0 +1,222 @@
1
+
2
+ require 'sqlite3'
3
+ require 'uglifier'
4
+
5
+ load 'dracoon_nodes.rb'
6
+
7
+
8
+ module Dracoon
9
+
10
+ class SQLiteWriter
11
+ include DracoonGrammar
12
+
13
+ TYPE_ROOT = 1
14
+ TYPE_MODULE = 2
15
+ TYPE_SCENE = 3
16
+ TYPE_PASSAGE = 4
17
+ TYPE_ITEM = 5
18
+ TYPE_HEADER = 6
19
+ TYPE_STATE = 7
20
+ TYPE_SUMMARY = 8
21
+ TYPE_CONTENT = 9
22
+ TYPE_ACTION = 10
23
+ TYPE_NEWLINE = 11
24
+ TYPE_ENDPARAGRAPH = 12
25
+ TYPE_LINK = 13
26
+ TYPE_IDENTIFIER = 14
27
+ TYPE_STRING = 15
28
+ TYPE_KEYWORD = 16
29
+ TYPE_WORD = 17
30
+ TYPE_NUMBER = 18
31
+ TYPE_PUNCTUATION = 19
32
+ TYPE_SCRIPT = 20
33
+ TYPE_JSEXPR = 21
34
+ TYPE_INLINETAG = 22
35
+ TYPE_IFTAG = 23
36
+ TYPE_SCRIPTTAG = 24
37
+
38
+
39
+ @db = nil
40
+ @currentModule = nil
41
+ @currentItem = nil
42
+ @currentScene = nil
43
+ @currentPassage = nil
44
+
45
+
46
+ def initialize
47
+ File::delete('gamebook.db') if File::exist? 'gamebook.db'
48
+ @db = SQLite3::Database.new('gamebook.db')
49
+ self.createTables
50
+ end
51
+
52
+ def writeAST(node, parent)
53
+ type = nil
54
+ content = node.text_value
55
+ if node.is_a? ModuleNode then self.writeModule(node, parent)
56
+ elsif node.is_a? ItemNode then self.writeItem(node, parent)
57
+ elsif node.is_a? SceneNode then self.writeScene(node, parent)
58
+ elsif node.is_a? PassageNode then self.writePassage(node, parent)
59
+ elsif node.is_a? HeaderNode then self.writeNode(node, parent, TYPE_HEADER)
60
+ elsif node.is_a? SummaryNode then self.writeNode(node, parent, TYPE_SUMMARY, content)
61
+ elsif node.is_a? EndParagraphNode then self.writeNode(node, parent, TYPE_ENDPARAGRAPH)
62
+ elsif node.is_a? ContentNode then self.writeNode(node, parent, TYPE_CONTENT)
63
+ elsif node.is_a? ActionNode then self.writeNode(node, parent, TYPE_ACTION)
64
+ elsif node.is_a? NewLineNode then self.writeNode(node, parent, TYPE_NEWLINE)
65
+ elsif node.is_a? LinkNode then self.writeNode(node, parent, TYPE_LINK)
66
+ elsif node.is_a? IdentifierNode then self.writeNode(node, parent, TYPE_IDENTIFIER, content)
67
+ elsif node.is_a? StateNode then self.writeNode(node, parent, TYPE_STATE, content)
68
+ elsif node.is_a? StringNode then self.writeNode(node, parent, TYPE_STRING, content)
69
+ elsif node.is_a? KeywordNode then self.writeNode(node, parent, TYPE_KEYWORD, content)
70
+ elsif node.is_a? WordNode then self.writeNode(node, parent, TYPE_WORD, content)
71
+ elsif node.is_a? NumberNode then self.writeNode(node, parent, TYPE_NUMBER, content)
72
+ elsif node.is_a? PunctuationNode then self.writeNode(node, parent, TYPE_PUNCTUATION, content)
73
+ elsif node.is_a? ScriptNode then self.writeScript(node, parent)
74
+ elsif node.is_a? InlineTagNode then self.writeNode(node, parent, TYPE_INLINETAG)
75
+ elsif node.is_a? IfTagNode then self.writeNode(node, parent, TYPE_IFTAG)
76
+ elsif node.is_a? ScriptTagNode then self.writeNode(node, parent, TYPE_SCRIPTTAG)
77
+ elsif node.is_a? JSExprNode then self.writeNode(node, parent, TYPE_JSEXPR, content)
78
+ end
79
+
80
+ node.elements.each { |e|
81
+ self.writeAST(e, node)
82
+ } unless node.elements.nil?
83
+
84
+ # Updating all modules to point to root node
85
+ @db.execute("update Nodes set idParent = 1 where type = ? and idParent is NULL", TYPE_MODULE)
86
+ end
87
+
88
+ def writeNode(node, parent, type, content=nil)
89
+ if parent.nil?
90
+ if content.nil?
91
+ @db.execute("insert into Nodes (type) values (?)", type)
92
+ else
93
+ @db.execute("insert into Nodes (type, content) values (?, ?)", type, content)
94
+ end
95
+ else
96
+ if content.nil?
97
+ @db.execute("insert into Nodes (idParent, type) values (?, ?)", parent.oid, type)
98
+ else
99
+ @db.execute("insert into Nodes (idParent, type, content) values (?, ?, ?)", parent.oid, type, content)
100
+ end
101
+ end
102
+ node.oid = @db.last_insert_row_id
103
+ end
104
+
105
+ def writeModule(node, parent)
106
+ moduleName = self.getHeaderNameOfNode(node)
107
+ idNode = @db.get_first_row("select idNode from Modules where name = ?", moduleName)
108
+ if idNode.nil?
109
+ self.writeNode(node, parent, TYPE_MODULE)
110
+ node.oid = @db.last_insert_row_id
111
+ @db.execute("insert into Modules (idNode, name) values (?, ?)", node.oid, moduleName)
112
+ else
113
+ removeHeaderOfNode(node)
114
+ node.oid = idNode
115
+ end
116
+
117
+ @currentModule = node
118
+ @currentItem = nil
119
+ @currentScene = nil
120
+ @currentPassage = nil
121
+ end
122
+
123
+ def writeItem(node, parent)
124
+ itemName = self.getHeaderNameOfNode(node)
125
+ idNode = @db.get_first_row("select idNode from Items where idModule = ? and name = ?", @currentModule.oid, itemName)
126
+ if idNode.nil?
127
+ self.writeNode(node, parent, TYPE_ITEM)
128
+ node.oid = @db.last_insert_row_id
129
+ @db.execute("insert into Items (idNode, idModule, name) values (?, ?, ?)", node.oid, @currentModule.oid, itemName)
130
+ else
131
+ removeHeaderOfNode(node)
132
+ node.oid = idNode
133
+ end
134
+
135
+ @currentItem = node
136
+ @currentScene = nil
137
+ @currentPassage = nil
138
+ end
139
+
140
+ def writeScene(node, parent)
141
+ sceneName = self.getHeaderNameOfNode(node)
142
+ idNode = @db.get_first_row("select idNode from Scenes where idModule = ? and name = ?", @currentModule.oid, sceneName)
143
+ if idNode.nil?
144
+ self.writeNode(node, parent, TYPE_SCENE)
145
+ node.oid = @db.last_insert_row_id
146
+ @db.execute("insert into Scenes (idNode, idModule, name) values (?, ?, ?)", node.oid, @currentModule.oid, sceneName)
147
+ else
148
+ removeHeaderOfNode(node)
149
+ node.oid = idNode
150
+ end
151
+
152
+ @currentScene = node
153
+ @currentPassage = nil
154
+ @currentItem = nil
155
+ end
156
+
157
+ def writePassage(node, parent)
158
+ passageName = self.getHeaderNameOfNode(node)
159
+ idNode = @db.get_first_row("select idNode from Passages where idScene = ? and name = ?", @currentScene.oid, passageName)
160
+ if idNode.nil?
161
+ self.writeNode(node, parent, TYPE_PASSAGE)
162
+ node.oid = @db.last_insert_row_id
163
+ @db.execute("insert into Passages (idNode, idScene, name) values (?, ?, ?)", node.oid, @currentScene.oid, passageName)
164
+ else
165
+ removeHeaderOfNode(node)
166
+ node.oid = idNode
167
+ end
168
+
169
+ @currentPassage = node
170
+ end
171
+
172
+ def writeScript(node, parent)
173
+ jscode = node.text_value
174
+ self.writeNode(node, parent, TYPE_SCRIPT)
175
+ jscode = "var o#{node.oid} = {#{jscode}} ;"
176
+ jscode = Uglifier.compile(jscode, :mangle => false)
177
+ @db.execute("update Nodes set content = ? where oid = ?", jscode, node.oid)
178
+ end
179
+
180
+
181
+ def getHeaderNameOfNode(node)
182
+ name = nil
183
+ node.elements.each do |e|
184
+ if e.is_a? HeaderNode
185
+ e.elements.each do |he|
186
+ if he.is_a? IdentifierNode
187
+ name = he.text_value
188
+ break;
189
+ end
190
+ end
191
+ break;
192
+ end
193
+ end
194
+ return name;
195
+ end
196
+
197
+ def removeHeaderOfNode(node)
198
+ header = nil
199
+ node.elements.each do |e|
200
+ if e.is_a? HeaderNode
201
+ header = e
202
+ break
203
+ end
204
+ end
205
+ node.elements.delete(header) unless header.nil?
206
+ end
207
+
208
+
209
+ def createTables
210
+ # Create a database
211
+ @db.execute("create table Nodes ( idParent integer, type integer, content text )")
212
+ @db.execute("create table Passages ( idNode integer, idScene integer, name text )")
213
+ @db.execute("create table Scenes ( idNode integer, idModule integer, name text )")
214
+ @db.execute("create table Items ( idNode integer, idModule integer, name text )")
215
+ @db.execute("create table Modules ( idNode integer, name text )")
216
+ # Inserting root node
217
+ @db.execute("insert into Nodes (type) values (?)", TYPE_ROOT)
218
+ end
219
+
220
+ end
221
+
222
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dracoon
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta2
5
+ platform: ruby
6
+ authors:
7
+ - Pedro L. Morales
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: treetop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uglifier
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ description: Dracoon is a language and compiler to write interactive fiction books.
56
+ email: piterlouis@gmail.com
57
+ executables:
58
+ - dracoon
59
+ extensions: []
60
+ extra_rdoc_files:
61
+ - LICENSE
62
+ - README.md
63
+ files:
64
+ - LICENSE
65
+ - README.md
66
+ - bin/dracoon
67
+ - dracoon.gemspec
68
+ - lib/dracoon.rb
69
+ - lib/dracoon_core.rb
70
+ - lib/dracoon_nodes.rb
71
+ - lib/dracoon_sqlite.rb
72
+ homepage: https://github.com/piterlouis/dracoon
73
+ licenses:
74
+ - GPL-2
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>'
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Dracoon, the amaizing tool to compile interactive fiction books.
96
+ test_files: []