koi-reference-parser 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,3 @@
1
- *.gem
2
- *.gemspec
3
- *.swp
1
+ *.gem
2
+ *.gemspec
3
+ *.swp
@@ -1,20 +1,20 @@
1
- Copyright (c) 2010 Aaron Gough (http://thingsaaronmade.com/)
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1
+ Copyright (c) 2010 Aaron Gough (http://thingsaaronmade.com/)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,44 +1,44 @@
1
- = koi-reference-parser
2
-
3
- This is the reference parser implementation for the programming language {Koi}[http://github.com/aarongough/koi]. The parser is implemented using a {Parsing Expression Grammar (PEG)}[http://en.wikipedia.org/wiki/Parsing_expression_grammar] that is run by a PEG engine called {Treetop}[http://treetop.rubyforge.org/].
4
-
5
- === Example
6
-
7
- The parser takes a text string containing a program like:
8
-
9
- test = 1 + 2
10
-
11
- And turns it into an {Abstract Syntax Tree (AST)}[http://en.wikipedia.org/wiki/Abstract_syntax_tree] that unambiguously represents the program's structure and meaning. The AST is represented as a series of Ruby objects that subclass a generic SyntaxNode class. The code above transformed into AST would look like:
12
-
13
- <Block "test = 1 + 2">
14
- <Statement "test = 1 + 2">
15
- <Assignment "test = 1 + 2">
16
- <Identifier "test">
17
- <AssignmentOperator "=">
18
- <Expression "1 + 2">
19
- <AdditiveExpression "1 + 2">
20
- <IntegerLiteral "1">
21
- <AdditionOperator "+">
22
- <IntegerLiteral "2">
23
-
24
-
25
- === Installation
26
-
27
- This parser is normally installed as part of Koi's default toolchain. However if you would like to install it on it's own you can do so by installing the gem like so:
28
-
29
- gem install koi-reference-parser
30
-
31
- === Usage
32
-
33
- require 'rubygems'
34
- require 'koi-reference-parser'
35
-
36
- include KoiReferenceParser
37
-
38
- ast = Parser.parse( program_text )
39
-
40
- === Author & Credits
41
-
42
- Author:: {Aaron Gough}[mailto:aaron@aarongough.com]
43
-
1
+ = koi-reference-parser
2
+
3
+ This is the reference parser implementation for the programming language {Koi}[http://github.com/aarongough/koi]. The parser is implemented using a {Parsing Expression Grammar (PEG)}[http://en.wikipedia.org/wiki/Parsing_expression_grammar] that is run by a PEG engine called {Treetop}[http://treetop.rubyforge.org/].
4
+
5
+ === Example
6
+
7
+ The parser takes a text string containing a program like:
8
+
9
+ test = 1 + 2
10
+
11
+ And turns it into an {Abstract Syntax Tree (AST)}[http://en.wikipedia.org/wiki/Abstract_syntax_tree] that unambiguously represents the program's structure and meaning. The AST is represented as a series of Ruby objects that subclass a generic SyntaxNode class. The code above transformed into AST would look like:
12
+
13
+ <Block "test = 1 + 2">
14
+ <Statement "test = 1 + 2">
15
+ <Assignment "test = 1 + 2">
16
+ <Identifier "test">
17
+ <AssignmentOperator "=">
18
+ <Expression "1 + 2">
19
+ <AdditiveExpression "1 + 2">
20
+ <IntegerLiteral "1">
21
+ <AdditionOperator "+">
22
+ <IntegerLiteral "2">
23
+
24
+
25
+ === Installation
26
+
27
+ This parser is normally installed as part of Koi's default toolchain. However if you would like to install it on it's own you can do so by installing the gem like so:
28
+
29
+ gem install koi-reference-parser
30
+
31
+ === Usage
32
+
33
+ require 'rubygems'
34
+ require 'koi-reference-parser'
35
+
36
+ include KoiReferenceParser
37
+
38
+ ast = Parser.parse( program_text )
39
+
40
+ === Author & Credits
41
+
42
+ Author:: {Aaron Gough}[mailto:aaron@aarongough.com]
43
+
44
44
  Copyright (c) 2010 {Aaron Gough}[http://thingsaaronmade.com/] ({thingsaaronmade.com}[http://thingsaaronmade.com/]), released under the MIT license
data/Rakefile CHANGED
@@ -1,43 +1,43 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- desc 'Default: run unit tests.'
6
- task :default => :test
7
-
8
- begin
9
- require 'jeweler'
10
- Jeweler::Tasks.new do |gemspec|
11
- gemspec.name = "koi-reference-parser"
12
- gemspec.summary = "A reference parser for the Koi language."
13
- gemspec.description = "A reference parser for the Koi language."
14
- gemspec.email = "aaron@aarongough.com"
15
- gemspec.homepage = "http://github.com/aarongough/koi-reference-parser"
16
- gemspec.authors = ["Aaron Gough"]
17
- gemspec.rdoc_options << '--line-numbers' << '--inline-source'
18
- gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
19
- gemspec.add_dependency('treetop')
20
- end
21
- rescue LoadError
22
- puts "Jeweler not available. Install it with: gem install jeweler"
23
- end
24
-
25
-
26
- desc 'Test koi-reference-parser.'
27
- Rake::TestTask.new(:test) do |t|
28
- t.libs << 'lib/*.rb'
29
- t.libs << 'test'
30
- t.pattern = 'test/**/*_test.rb'
31
- t.verbose = true
32
- end
33
-
34
-
35
- desc 'Generate documentation for koi-reference-parser.'
36
- Rake::RDocTask.new(:rdoc) do |rdoc|
37
- rdoc.rdoc_dir = 'rdoc'
38
- rdoc.title = 'koi-reference-parser'
39
- rdoc.options << '--line-numbers' << '--inline-source'
40
- rdoc.rdoc_files.include('README.rdoc')
41
- rdoc.rdoc_files.include('lib/**/*.rb')
42
- rdoc.rdoc_files.include('app/**/*.rb')
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ begin
9
+ require 'jeweler'
10
+ Jeweler::Tasks.new do |gemspec|
11
+ gemspec.name = "koi-reference-parser"
12
+ gemspec.summary = "A reference parser for the Koi language."
13
+ gemspec.description = "A reference parser for the Koi language."
14
+ gemspec.email = "aaron@aarongough.com"
15
+ gemspec.homepage = "http://github.com/aarongough/koi-reference-parser"
16
+ gemspec.authors = ["Aaron Gough"]
17
+ gemspec.rdoc_options << '--line-numbers' << '--inline-source'
18
+ gemspec.extra_rdoc_files = ['README.rdoc', 'MIT-LICENSE']
19
+ gemspec.add_dependency('treetop')
20
+ end
21
+ rescue LoadError
22
+ puts "Jeweler not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+
26
+ desc 'Test koi-reference-parser.'
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib/*.rb'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = true
32
+ end
33
+
34
+
35
+ desc 'Generate documentation for koi-reference-parser.'
36
+ Rake::RDocTask.new(:rdoc) do |rdoc|
37
+ rdoc.rdoc_dir = 'rdoc'
38
+ rdoc.title = 'koi-reference-parser'
39
+ rdoc.options << '--line-numbers' << '--inline-source'
40
+ rdoc.rdoc_files.include('README.rdoc')
41
+ rdoc.rdoc_files.include('lib/**/*.rb')
42
+ rdoc.rdoc_files.include('app/**/*.rb')
43
43
  end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -1,8 +1,8 @@
1
- require 'rubygems'
2
-
3
- require_files = []
4
- require_files.concat Dir[File.join(File.dirname(__FILE__), 'parser', '**', '*.rb')]
5
-
6
- require_files.each do |file|
7
- require File.expand_path(file)
8
- end
1
+ require 'rubygems'
2
+
3
+ require_files = []
4
+ require_files.concat Dir[File.join(File.dirname(__FILE__), 'parser', '**', '*.rb')]
5
+
6
+ require_files.each do |file|
7
+ require File.expand_path(file)
8
+ end
@@ -1,2 +1,2 @@
1
- class ParseError < Exception
1
+ class ParseError < Exception
2
2
  end
@@ -1,197 +1,197 @@
1
- grammar KoiReferenceParser
2
-
3
- rule block
4
- statement+ <Block>
5
- end
6
-
7
- rule statement
8
- space? ( hash_assignment / assignment / if / unless / function_call ) ';'? space? <Statement>
9
- end
10
-
11
- rule nil
12
- "nil" <NilLiteral>
13
- end
14
-
15
- rule true
16
- "true" <TrueLiteral>
17
- end
18
-
19
- rule false
20
- "false" <FalseLiteral>
21
- end
22
-
23
- rule integer
24
- ('+' / '-')? [0-9]+ <IntegerLiteral>
25
- end
26
-
27
- rule float
28
- ('+' / '-')? [0-9]+ '.' [0-9]+ <FloatLiteral>
29
- end
30
-
31
- rule string
32
- '"' ([^"\\] / "\\" . )* '"' <StringLiteral>
33
- end
34
-
35
- rule identifier
36
- '$'? [a-zA-Z] [a-zA-Z0-9_]* <Identifier>
37
- end
38
-
39
- rule hash_assignment
40
- identifier hash_accessor_list space? assignment_operator space? ( hash / function_definition / expression ) <HashAssignment>
41
- end
42
-
43
- rule assignment
44
- identifier space? assignment_operator space? ( hash / function_definition / expression ) <Assignment>
45
- end
46
-
47
- ##################
48
- # Flow control
49
-
50
- rule if
51
- 'if(' space? expression space? ')' space
52
- block
53
- space? 'end' space? <If>
54
- end
55
-
56
- rule unless
57
- 'unless(' space? expression space? ')' space
58
- block
59
- space? 'end' space? <Unless>
60
- end
61
-
62
- ##################
63
- # Functions
64
-
65
- rule function_call
66
- identifier space? '(' space? expression? space? ','? expression? space? ')' <FunctionCall>
67
- end
68
-
69
- rule function_definition
70
- 'function(' space? identifier space? ')'
71
- block
72
- space? 'end' <FunctionDefinition>
73
- end
74
-
75
- ##################
76
- # Whitespace
77
-
78
- rule space
79
- [\s]+ <Whitespace>
80
- end
81
-
82
- ##################
83
- # Hashes
84
-
85
- rule hash_accessor_list
86
- hash_accessor+ <HashAccessorList>
87
- end
88
-
89
- rule hash_accessor
90
- '[' expression ']' <HashAccessor>
91
- end
92
-
93
- rule hash
94
- '{' key_value_list? '}' <HashLiteral>
95
- end
96
-
97
- rule key_value_list
98
- key_value+ <KeyValueList>
99
- end
100
-
101
- rule key_value
102
- space? expression space? '=>' space? expression space? ','? <KeyValue>
103
- end
104
-
105
- rule key
106
- space? expression space? <Key>
107
- end
108
-
109
- rule value
110
- space? expression space? <Value>
111
- end
112
-
113
- ##################
114
- # Expressions
115
-
116
- rule expression
117
- space? (hash_access / comparative / additive) <Expression>
118
- end
119
-
120
- rule hash_access
121
- identifier hash_accessor_list <HashAccess>
122
- end
123
-
124
- rule comparative
125
- additive space? (equality_operator / inequality_operator / greater_than_operator / less_than_operator) space? additive <ComparativeExpression>
126
- end
127
-
128
- rule additive
129
- multitive space? additive_operator space? additive <AdditiveExpression>
130
- /
131
- multitive
132
- end
133
-
134
- rule additive_operator
135
- addition_operator
136
- /
137
- subtraction_operator
138
- end
139
-
140
- rule multitive
141
- primary space? multitive_operator space? multitive <MultitiveExpression>
142
- /
143
- primary
144
- end
145
-
146
- rule multitive_operator
147
- multiplication_operator
148
- /
149
- division_operator
150
- end
151
-
152
- rule primary
153
- function_call / nil / false / true / identifier / float / integer / string
154
- /
155
- '(' space? expression space? ')' <Expression>
156
- end
157
-
158
- ##################
159
- # Operators
160
-
161
- rule assignment_operator
162
- '=' <AssignmentOperator>
163
- end
164
-
165
- rule addition_operator
166
- '+' <AdditionOperator>
167
- end
168
-
169
- rule subtraction_operator
170
- '-' <SubtractionOperator>
171
- end
172
-
173
- rule multiplication_operator
174
- '*' <MultiplicationOperator>
175
- end
176
-
177
- rule division_operator
178
- '/' <DivisionOperator>
179
- end
180
-
181
- rule equality_operator
182
- '==' <EqualityOperator>
183
- end
184
-
185
- rule inequality_operator
186
- '!=' <InequalityOperator>
187
- end
188
-
189
- rule greater_than_operator
190
- '>' <GreaterThanOperator>
191
- end
192
-
193
- rule less_than_operator
194
- '<' <LessThanOperator>
195
- end
196
-
197
- end
1
+ grammar KoiReferenceParser
2
+
3
+ rule block
4
+ statement+ <Block>
5
+ end
6
+
7
+ rule statement
8
+ space? ( hash_assignment / assignment / if / unless / function_call ) ';'? space? <Statement>
9
+ end
10
+
11
+ rule nil
12
+ "nil" <NilLiteral>
13
+ end
14
+
15
+ rule true
16
+ "true" <TrueLiteral>
17
+ end
18
+
19
+ rule false
20
+ "false" <FalseLiteral>
21
+ end
22
+
23
+ rule integer
24
+ ('+' / '-')? [0-9]+ <IntegerLiteral>
25
+ end
26
+
27
+ rule float
28
+ ('+' / '-')? [0-9]+ '.' [0-9]+ <FloatLiteral>
29
+ end
30
+
31
+ rule string
32
+ '"' ([^"\\] / "\\" . )* '"' <StringLiteral>
33
+ end
34
+
35
+ rule identifier
36
+ '$'? [a-zA-Z] [a-zA-Z0-9_]* <Identifier>
37
+ end
38
+
39
+ rule hash_assignment
40
+ identifier hash_accessor_list space? assignment_operator space? ( hash / function_definition / expression ) <HashAssignment>
41
+ end
42
+
43
+ rule assignment
44
+ identifier space? assignment_operator space? ( hash / function_definition / expression ) <Assignment>
45
+ end
46
+
47
+ ##################
48
+ # Flow control
49
+
50
+ rule if
51
+ 'if(' space? expression space? ')' space
52
+ block
53
+ space? 'end' space? <If>
54
+ end
55
+
56
+ rule unless
57
+ 'unless(' space? expression space? ')' space
58
+ block
59
+ space? 'end' space? <Unless>
60
+ end
61
+
62
+ ##################
63
+ # Functions
64
+
65
+ rule function_call
66
+ identifier space? '(' space? expression? space? ','? expression? space? ')' <FunctionCall>
67
+ end
68
+
69
+ rule function_definition
70
+ 'function(' space? identifier space? ')'
71
+ block
72
+ space? 'end' <FunctionDefinition>
73
+ end
74
+
75
+ ##################
76
+ # Whitespace
77
+
78
+ rule space
79
+ [\s]+ <Whitespace>
80
+ end
81
+
82
+ ##################
83
+ # Hashes
84
+
85
+ rule hash_accessor_list
86
+ hash_accessor+ <HashAccessorList>
87
+ end
88
+
89
+ rule hash_accessor
90
+ '[' expression ']' <HashAccessor>
91
+ end
92
+
93
+ rule hash
94
+ '{' key_value_list? '}' <HashLiteral>
95
+ end
96
+
97
+ rule key_value_list
98
+ key_value+ <KeyValueList>
99
+ end
100
+
101
+ rule key_value
102
+ space? expression space? '=>' space? expression space? ','? <KeyValue>
103
+ end
104
+
105
+ rule key
106
+ space? expression space? <Key>
107
+ end
108
+
109
+ rule value
110
+ space? expression space? <Value>
111
+ end
112
+
113
+ ##################
114
+ # Expressions
115
+
116
+ rule expression
117
+ space? (hash_access / comparative / additive) <Expression>
118
+ end
119
+
120
+ rule hash_access
121
+ identifier hash_accessor_list <HashAccess>
122
+ end
123
+
124
+ rule comparative
125
+ additive space? (equality_operator / inequality_operator / greater_than_operator / less_than_operator) space? additive <ComparativeExpression>
126
+ end
127
+
128
+ rule additive
129
+ multitive space? additive_operator space? additive <AdditiveExpression>
130
+ /
131
+ multitive
132
+ end
133
+
134
+ rule additive_operator
135
+ addition_operator
136
+ /
137
+ subtraction_operator
138
+ end
139
+
140
+ rule multitive
141
+ primary space? multitive_operator space? multitive <MultitiveExpression>
142
+ /
143
+ primary
144
+ end
145
+
146
+ rule multitive_operator
147
+ multiplication_operator
148
+ /
149
+ division_operator
150
+ end
151
+
152
+ rule primary
153
+ function_call / nil / false / true / identifier / float / integer / string
154
+ /
155
+ '(' space? expression space? ')' <Expression>
156
+ end
157
+
158
+ ##################
159
+ # Operators
160
+
161
+ rule assignment_operator
162
+ '=' <AssignmentOperator>
163
+ end
164
+
165
+ rule addition_operator
166
+ '+' <AdditionOperator>
167
+ end
168
+
169
+ rule subtraction_operator
170
+ '-' <SubtractionOperator>
171
+ end
172
+
173
+ rule multiplication_operator
174
+ '*' <MultiplicationOperator>
175
+ end
176
+
177
+ rule division_operator
178
+ '/' <DivisionOperator>
179
+ end
180
+
181
+ rule equality_operator
182
+ '==' <EqualityOperator>
183
+ end
184
+
185
+ rule inequality_operator
186
+ '!=' <InequalityOperator>
187
+ end
188
+
189
+ rule greater_than_operator
190
+ '>' <GreaterThanOperator>
191
+ end
192
+
193
+ rule less_than_operator
194
+ '<' <LessThanOperator>
195
+ end
196
+
197
+ end