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.
@@ -1,43 +1,43 @@
1
- class Parser
2
-
3
- require 'treetop'
4
- Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'koi-reference-parser.treetop')))
5
- @@parser = KoiReferenceParserParser.new
6
-
7
- def self.parse(data)
8
- tree = @@parser.parse(data)
9
-
10
- if(tree.nil?)
11
- if( data.length > 80 )
12
- error_detail = "\n\n" + data.slice(@@parser.index - 120, 80).gsub("\n", ";")
13
- error_detail += "\n" + data.slice(@@parser.index - 40, 80).gsub("\n", ";")
14
- error_detail += "\n" + " "*40 + "^\n"
15
- else
16
- error_detail = "\n\n" + data.gsub("\n", ";")
17
- error_detail += "\n" + " " * @@parser.index + "^\n"
18
- end
19
- raise ParseError, "Parse error at index: #{@@parser.index}#{error_detail}"
20
- end
21
-
22
- # clean up the tree by removing all nodes of default type 'SyntaxNode'
23
- self.clean_tree(tree)
24
- # clean up the tree further by removing all whitespace nodes
25
- self.clean_whitespace(tree)
26
- return tree
27
- end
28
-
29
- private
30
-
31
- def self.clean_tree(root_node)
32
- return if(root_node.elements.nil?)
33
- root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
34
- root_node.elements.each {|node| self.clean_tree(node) }
35
- end
36
-
37
- def self.clean_whitespace(root_node)
38
- return if(root_node.elements.nil?)
39
- root_node.elements.delete_if{|node| node.class.name == "KoiReferenceParser::Whitespace" }
40
- root_node.elements.each {|node| self.clean_whitespace(node) }
41
- end
42
-
1
+ class Parser
2
+
3
+ require 'treetop'
4
+ Treetop.load(File.expand_path(File.join(File.dirname(__FILE__), 'koi-reference-parser.treetop')))
5
+ @@parser = KoiReferenceParserParser.new
6
+
7
+ def self.parse(data)
8
+ tree = @@parser.parse(data)
9
+
10
+ if(tree.nil?)
11
+ if( data.length > 80 )
12
+ error_detail = "\n\n" + data.slice(@@parser.index - 120, 80).gsub("\n", ";")
13
+ error_detail += "\n" + data.slice(@@parser.index - 40, 80).gsub("\n", ";")
14
+ error_detail += "\n" + " "*40 + "^\n"
15
+ else
16
+ error_detail = "\n\n" + data.gsub("\n", ";")
17
+ error_detail += "\n" + " " * @@parser.index + "^\n"
18
+ end
19
+ raise ParseError, "Parse error at index: #{@@parser.index}#{error_detail}"
20
+ end
21
+
22
+ # clean up the tree by removing all nodes of default type 'SyntaxNode'
23
+ self.clean_tree(tree)
24
+ # clean up the tree further by removing all whitespace nodes
25
+ self.clean_whitespace(tree)
26
+ return tree
27
+ end
28
+
29
+ private
30
+
31
+ def self.clean_tree(root_node)
32
+ return if(root_node.elements.nil?)
33
+ root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
34
+ root_node.elements.each {|node| self.clean_tree(node) }
35
+ end
36
+
37
+ def self.clean_whitespace(root_node)
38
+ return if(root_node.elements.nil?)
39
+ root_node.elements.delete_if{|node| node.class.name == "KoiReferenceParser::Whitespace" }
40
+ root_node.elements.each {|node| self.clean_whitespace(node) }
41
+ end
42
+
43
43
  end
@@ -0,0 +1,20 @@
1
+ module KoiReferenceParser
2
+
3
+ class Treetop::Runtime::SyntaxNode
4
+
5
+ def to_hash()
6
+ hash = {}
7
+ hash[:offset] = self.interval.first
8
+ hash[:text_value] = self.text_value
9
+ hash[:name] = self.class.name.split("::").last
10
+ unless( self.elements.nil? )
11
+ hash[:elements] = self.elements.map {|element| element.to_hash() }
12
+ else
13
+ hash[:elements] = nil
14
+ end
15
+ return hash
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -1,114 +1,114 @@
1
- module KoiReferenceParser
2
-
3
- class Block < Treetop::Runtime::SyntaxNode
4
- end
5
-
6
- class Statement < Treetop::Runtime::SyntaxNode
7
- end
8
-
9
- class HashAssignment < Treetop::Runtime::SyntaxNode
10
- end
11
-
12
- class Assignment < Treetop::Runtime::SyntaxNode
13
- end
14
-
15
- class FunctionDefinition < Treetop::Runtime::SyntaxNode
16
- end
17
-
18
- class If < Treetop::Runtime::SyntaxNode
19
- end
20
-
21
- class Unless < Treetop::Runtime::SyntaxNode
22
- end
23
-
24
- class FunctionCall < Treetop::Runtime::SyntaxNode
25
- end
26
-
27
- class NilLiteral < Treetop::Runtime::SyntaxNode
28
- end
29
-
30
- class TrueLiteral < Treetop::Runtime::SyntaxNode
31
- end
32
-
33
- class FalseLiteral < Treetop::Runtime::SyntaxNode
34
- end
35
-
36
- class IntegerLiteral < Treetop::Runtime::SyntaxNode
37
- end
38
-
39
- class FloatLiteral < Treetop::Runtime::SyntaxNode
40
- end
41
-
42
- class StringLiteral < Treetop::Runtime::SyntaxNode
43
- end
44
-
45
- class HashAccessorList < Treetop::Runtime::SyntaxNode
46
- end
47
-
48
- class HashAccessor < Treetop::Runtime::SyntaxNode
49
- end
50
-
51
- class HashLiteral < Treetop::Runtime::SyntaxNode
52
- end
53
-
54
- class KeyValueList < Treetop::Runtime::SyntaxNode
55
- end
56
-
57
- class KeyValue < Treetop::Runtime::SyntaxNode
58
- end
59
-
60
- class Key < Treetop::Runtime::SyntaxNode
61
- end
62
-
63
- class Value < Treetop::Runtime::SyntaxNode
64
- end
65
-
66
- class Identifier < Treetop::Runtime::SyntaxNode
67
- end
68
-
69
- class Whitespace < Treetop::Runtime::SyntaxNode
70
- end
71
-
72
- class AssignmentOperator < Treetop::Runtime::SyntaxNode
73
- end
74
-
75
- class EqualityOperator < Treetop::Runtime::SyntaxNode
76
- end
77
-
78
- class InequalityOperator < Treetop::Runtime::SyntaxNode
79
- end
80
-
81
- class GreaterThanOperator < Treetop::Runtime::SyntaxNode
82
- end
83
-
84
- class LessThanOperator < Treetop::Runtime::SyntaxNode
85
- end
86
-
87
- class AdditionOperator < Treetop::Runtime::SyntaxNode
88
- end
89
-
90
- class SubtractionOperator < Treetop::Runtime::SyntaxNode
91
- end
92
-
93
- class MultiplicationOperator < Treetop::Runtime::SyntaxNode
94
- end
95
-
96
- class DivisionOperator < Treetop::Runtime::SyntaxNode
97
- end
98
-
99
- class Expression < Treetop::Runtime::SyntaxNode
100
- end
101
-
102
- class HashAccess < Treetop::Runtime::SyntaxNode
103
- end
104
-
105
- class ComparativeExpression < Treetop::Runtime::SyntaxNode
106
- end
107
-
108
- class AdditiveExpression < Treetop::Runtime::SyntaxNode
109
- end
110
-
111
- class MultitiveExpression < Treetop::Runtime::SyntaxNode
112
- end
113
-
1
+ module KoiReferenceParser
2
+
3
+ class Block < Treetop::Runtime::SyntaxNode
4
+ end
5
+
6
+ class Statement < Treetop::Runtime::SyntaxNode
7
+ end
8
+
9
+ class HashAssignment < Treetop::Runtime::SyntaxNode
10
+ end
11
+
12
+ class Assignment < Treetop::Runtime::SyntaxNode
13
+ end
14
+
15
+ class FunctionDefinition < Treetop::Runtime::SyntaxNode
16
+ end
17
+
18
+ class If < Treetop::Runtime::SyntaxNode
19
+ end
20
+
21
+ class Unless < Treetop::Runtime::SyntaxNode
22
+ end
23
+
24
+ class FunctionCall < Treetop::Runtime::SyntaxNode
25
+ end
26
+
27
+ class NilLiteral < Treetop::Runtime::SyntaxNode
28
+ end
29
+
30
+ class TrueLiteral < Treetop::Runtime::SyntaxNode
31
+ end
32
+
33
+ class FalseLiteral < Treetop::Runtime::SyntaxNode
34
+ end
35
+
36
+ class IntegerLiteral < Treetop::Runtime::SyntaxNode
37
+ end
38
+
39
+ class FloatLiteral < Treetop::Runtime::SyntaxNode
40
+ end
41
+
42
+ class StringLiteral < Treetop::Runtime::SyntaxNode
43
+ end
44
+
45
+ class HashAccessorList < Treetop::Runtime::SyntaxNode
46
+ end
47
+
48
+ class HashAccessor < Treetop::Runtime::SyntaxNode
49
+ end
50
+
51
+ class HashLiteral < Treetop::Runtime::SyntaxNode
52
+ end
53
+
54
+ class KeyValueList < Treetop::Runtime::SyntaxNode
55
+ end
56
+
57
+ class KeyValue < Treetop::Runtime::SyntaxNode
58
+ end
59
+
60
+ class Key < Treetop::Runtime::SyntaxNode
61
+ end
62
+
63
+ class Value < Treetop::Runtime::SyntaxNode
64
+ end
65
+
66
+ class Identifier < Treetop::Runtime::SyntaxNode
67
+ end
68
+
69
+ class Whitespace < Treetop::Runtime::SyntaxNode
70
+ end
71
+
72
+ class AssignmentOperator < Treetop::Runtime::SyntaxNode
73
+ end
74
+
75
+ class EqualityOperator < Treetop::Runtime::SyntaxNode
76
+ end
77
+
78
+ class InequalityOperator < Treetop::Runtime::SyntaxNode
79
+ end
80
+
81
+ class GreaterThanOperator < Treetop::Runtime::SyntaxNode
82
+ end
83
+
84
+ class LessThanOperator < Treetop::Runtime::SyntaxNode
85
+ end
86
+
87
+ class AdditionOperator < Treetop::Runtime::SyntaxNode
88
+ end
89
+
90
+ class SubtractionOperator < Treetop::Runtime::SyntaxNode
91
+ end
92
+
93
+ class MultiplicationOperator < Treetop::Runtime::SyntaxNode
94
+ end
95
+
96
+ class DivisionOperator < Treetop::Runtime::SyntaxNode
97
+ end
98
+
99
+ class Expression < Treetop::Runtime::SyntaxNode
100
+ end
101
+
102
+ class HashAccess < Treetop::Runtime::SyntaxNode
103
+ end
104
+
105
+ class ComparativeExpression < Treetop::Runtime::SyntaxNode
106
+ end
107
+
108
+ class AdditiveExpression < Treetop::Runtime::SyntaxNode
109
+ end
110
+
111
+ class MultitiveExpression < Treetop::Runtime::SyntaxNode
112
+ end
113
+
114
114
  end
@@ -1,17 +1,17 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
-
3
- class FunctionCallAsArgumentTest < Test::Unit::TestCase
4
-
5
- include KoiReferenceParser
6
-
7
- test "should parse statement with function call as argument" do
8
- assert_nothing_raised do
9
- Parser.parse(<<-EOD)
10
-
11
- print( to_string( 1 ))
12
-
13
- EOD
14
- end
15
- end
16
-
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
+
3
+ class FunctionCallAsArgumentTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceParser
6
+
7
+ test "should parse statement with function call as argument" do
8
+ assert_nothing_raised do
9
+ Parser.parse(<<-EOD)
10
+
11
+ print( to_string( 1 ))
12
+
13
+ EOD
14
+ end
15
+ end
16
+
17
17
  end
@@ -1,35 +1,35 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
-
3
- class SimpleProgramTest < Test::Unit::TestCase
4
-
5
- include KoiReferenceParser
6
-
7
- test "should parse simple program with 'if' flow control" do
8
- assert_nothing_raised do
9
- Parser.parse(<<-EOD)
10
-
11
- test = 1
12
-
13
- if(test == 1)
14
- test = "yay"
15
- end
16
-
17
- EOD
18
- end
19
- end
20
-
21
- test "should parse simple program with 'unless' flow control" do
22
- assert_nothing_raised do
23
- Parser.parse(<<-EOD)
24
-
25
- test = 1
26
-
27
- unless(test == 1)
28
- test = "yay"
29
- end
30
-
31
- EOD
32
- end
33
- end
34
-
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
+
3
+ class SimpleProgramTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceParser
6
+
7
+ test "should parse simple program with 'if' flow control" do
8
+ assert_nothing_raised do
9
+ Parser.parse(<<-EOD)
10
+
11
+ test = 1
12
+
13
+ if(test == 1)
14
+ test = "yay"
15
+ end
16
+
17
+ EOD
18
+ end
19
+ end
20
+
21
+ test "should parse simple program with 'unless' flow control" do
22
+ assert_nothing_raised do
23
+ Parser.parse(<<-EOD)
24
+
25
+ test = 1
26
+
27
+ unless(test == 1)
28
+ test = "yay"
29
+ end
30
+
31
+ EOD
32
+ end
33
+ end
34
+
35
35
  end
@@ -1,115 +1,115 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
-
3
- class AssignmentTest < Test::Unit::TestCase
4
-
5
- include KoiReferenceParser
6
-
7
- test "assignment of integer" do
8
- tree = Parser.parse('test = 1')
9
- assert_assigns_expression(IntegerLiteral, '1', tree)
10
- end
11
-
12
- test "assignment of explicitly positive integer" do
13
- tree = Parser.parse('test = +1')
14
- assert_assigns_expression(IntegerLiteral, '+1', tree)
15
- end
16
-
17
- test "assignment of explicitly negative integer" do
18
- tree = Parser.parse('test = -1')
19
- assert_assigns_expression(IntegerLiteral, '-1', tree)
20
- end
21
-
22
- test "assignment of float" do
23
- tree = Parser.parse('test = 99.0009')
24
- assert_assigns_expression(FloatLiteral, '99.0009', tree)
25
- end
26
-
27
- test "assignment of explicitly positive float" do
28
- tree = Parser.parse('test = +99.0009')
29
- assert_assigns_expression(FloatLiteral, '+99.0009', tree)
30
- end
31
-
32
- test "assignment of explicitly negative float" do
33
- tree = Parser.parse('test = -99.0009')
34
- assert_assigns_expression(FloatLiteral, '-99.0009', tree)
35
- end
36
-
37
- test "assignment of string" do
38
- tree = Parser.parse('test = "test_string"')
39
- assert_assigns_expression(StringLiteral, '"test_string"', tree)
40
- end
41
-
42
- test "assignment of string containing escaped quote" do
43
- tree = Parser.parse('test = "test_\"string"')
44
- assert_assigns_expression(StringLiteral, '"test_\"string"', tree)
45
- end
46
-
47
- test "assignment of function definition" do
48
- tree = Parser.parse('test = function(args) blah = 1 end')
49
- assert_assigns(FunctionDefinition, nil, tree)
50
- end
51
-
52
- test "assignment of result of function call" do
53
- tree = Parser.parse('test = function(args)')
54
- assert_assigns_expression(FunctionCall, nil, tree)
55
- end
56
-
57
- test "assignment of nil" do
58
- tree = Parser.parse('test = nil')
59
- assert_assigns_expression(NilLiteral, nil, tree)
60
- end
61
-
62
- test "assignment of boolean true" do
63
- tree = Parser.parse('test = true')
64
- assert_assigns_expression(TrueLiteral, nil, tree)
65
- end
66
-
67
- test "assignment of boolean false" do
68
- tree = Parser.parse('test = false')
69
- assert_assigns_expression(FalseLiteral, nil, tree)
70
- end
71
-
72
- test "assignment of empty hash literal" do
73
- tree = Parser.parse('test = {}')
74
- assert_assigns(HashLiteral, nil, tree)
75
- end
76
-
77
- test "assignment of hash literal with single key-value pair" do
78
- tree = Parser.parse('test = { 1 => 2 }')
79
- assert_assigns(HashLiteral, nil, tree)
80
- end
81
-
82
- test "assignment of hash literal with 2 key-value pairs" do
83
- tree = Parser.parse('test = { 1 => 2, 2 => 2 }')
84
- assert_assigns(HashLiteral, nil, tree)
85
- end
86
-
87
- test "assignment of hash literal with 3 key-value pairs" do
88
- tree = Parser.parse('test = { 1 => 2, 2 => 2, 3 => 2 }')
89
- assert_assigns(HashLiteral, nil, tree)
90
- end
91
-
92
- private
93
-
94
- def assert_assigns(node_type, value, tree)
95
- assert_kind_of Block, tree
96
- assert_kind_of Statement, tree.elements.last
97
- assert_kind_of Assignment, tree.elements.last.elements.last
98
- assert_kind_of Identifier, tree.elements.last.elements.last.elements[0]
99
- assert_kind_of AssignmentOperator, tree.elements.last.elements.last.elements[1]
100
- assert_kind_of node_type, tree.elements.last.elements.last.elements[2]
101
- assert_equal value, tree.elements.last.elements.last.elements[2].text_value unless(value.nil?)
102
- end
103
-
104
- def assert_assigns_expression(node_type, value, tree)
105
- assert_kind_of Block, tree
106
- assert_kind_of Statement, tree.elements.last
107
- assert_kind_of Assignment, tree.elements.last.elements.last
108
- assert_kind_of Identifier, tree.elements.last.elements.last.elements[0]
109
- assert_kind_of AssignmentOperator, tree.elements.last.elements.last.elements[1]
110
- assert_kind_of Expression, tree.elements.last.elements.last.elements[2]
111
- assert_kind_of node_type, tree.elements.last.elements.last.elements[2].elements.first
112
- assert_equal value, tree.elements.last.elements.last.elements[2].elements.first.text_value unless(value.nil?)
113
- end
114
-
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb'))
2
+
3
+ class AssignmentTest < Test::Unit::TestCase
4
+
5
+ include KoiReferenceParser
6
+
7
+ test "assignment of integer" do
8
+ tree = Parser.parse('test = 1')
9
+ assert_assigns_expression(IntegerLiteral, '1', tree)
10
+ end
11
+
12
+ test "assignment of explicitly positive integer" do
13
+ tree = Parser.parse('test = +1')
14
+ assert_assigns_expression(IntegerLiteral, '+1', tree)
15
+ end
16
+
17
+ test "assignment of explicitly negative integer" do
18
+ tree = Parser.parse('test = -1')
19
+ assert_assigns_expression(IntegerLiteral, '-1', tree)
20
+ end
21
+
22
+ test "assignment of float" do
23
+ tree = Parser.parse('test = 99.0009')
24
+ assert_assigns_expression(FloatLiteral, '99.0009', tree)
25
+ end
26
+
27
+ test "assignment of explicitly positive float" do
28
+ tree = Parser.parse('test = +99.0009')
29
+ assert_assigns_expression(FloatLiteral, '+99.0009', tree)
30
+ end
31
+
32
+ test "assignment of explicitly negative float" do
33
+ tree = Parser.parse('test = -99.0009')
34
+ assert_assigns_expression(FloatLiteral, '-99.0009', tree)
35
+ end
36
+
37
+ test "assignment of string" do
38
+ tree = Parser.parse('test = "test_string"')
39
+ assert_assigns_expression(StringLiteral, '"test_string"', tree)
40
+ end
41
+
42
+ test "assignment of string containing escaped quote" do
43
+ tree = Parser.parse('test = "test_\"string"')
44
+ assert_assigns_expression(StringLiteral, '"test_\"string"', tree)
45
+ end
46
+
47
+ test "assignment of function definition" do
48
+ tree = Parser.parse('test = function(args) blah = 1 end')
49
+ assert_assigns(FunctionDefinition, nil, tree)
50
+ end
51
+
52
+ test "assignment of result of function call" do
53
+ tree = Parser.parse('test = function(args)')
54
+ assert_assigns_expression(FunctionCall, nil, tree)
55
+ end
56
+
57
+ test "assignment of nil" do
58
+ tree = Parser.parse('test = nil')
59
+ assert_assigns_expression(NilLiteral, nil, tree)
60
+ end
61
+
62
+ test "assignment of boolean true" do
63
+ tree = Parser.parse('test = true')
64
+ assert_assigns_expression(TrueLiteral, nil, tree)
65
+ end
66
+
67
+ test "assignment of boolean false" do
68
+ tree = Parser.parse('test = false')
69
+ assert_assigns_expression(FalseLiteral, nil, tree)
70
+ end
71
+
72
+ test "assignment of empty hash literal" do
73
+ tree = Parser.parse('test = {}')
74
+ assert_assigns(HashLiteral, nil, tree)
75
+ end
76
+
77
+ test "assignment of hash literal with single key-value pair" do
78
+ tree = Parser.parse('test = { 1 => 2 }')
79
+ assert_assigns(HashLiteral, nil, tree)
80
+ end
81
+
82
+ test "assignment of hash literal with 2 key-value pairs" do
83
+ tree = Parser.parse('test = { 1 => 2, 2 => 2 }')
84
+ assert_assigns(HashLiteral, nil, tree)
85
+ end
86
+
87
+ test "assignment of hash literal with 3 key-value pairs" do
88
+ tree = Parser.parse('test = { 1 => 2, 2 => 2, 3 => 2 }')
89
+ assert_assigns(HashLiteral, nil, tree)
90
+ end
91
+
92
+ private
93
+
94
+ def assert_assigns(node_type, value, tree)
95
+ assert_kind_of Block, tree
96
+ assert_kind_of Statement, tree.elements.last
97
+ assert_kind_of Assignment, tree.elements.last.elements.last
98
+ assert_kind_of Identifier, tree.elements.last.elements.last.elements[0]
99
+ assert_kind_of AssignmentOperator, tree.elements.last.elements.last.elements[1]
100
+ assert_kind_of node_type, tree.elements.last.elements.last.elements[2]
101
+ assert_equal value, tree.elements.last.elements.last.elements[2].text_value unless(value.nil?)
102
+ end
103
+
104
+ def assert_assigns_expression(node_type, value, tree)
105
+ assert_kind_of Block, tree
106
+ assert_kind_of Statement, tree.elements.last
107
+ assert_kind_of Assignment, tree.elements.last.elements.last
108
+ assert_kind_of Identifier, tree.elements.last.elements.last.elements[0]
109
+ assert_kind_of AssignmentOperator, tree.elements.last.elements.last.elements[1]
110
+ assert_kind_of Expression, tree.elements.last.elements.last.elements[2]
111
+ assert_kind_of node_type, tree.elements.last.elements.last.elements[2].elements.first
112
+ assert_equal value, tree.elements.last.elements.last.elements[2].elements.first.text_value unless(value.nil?)
113
+ end
114
+
115
115
  end