jievro-parser 0.8.0 → 0.9.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.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +3 -3
  3. data/Rakefile +1 -1
  4. data/jievro-parser.gemspec +1 -1
  5. data/lib/jievro/parser/binary_operator.rb +27 -38
  6. data/lib/jievro/parser/grammar/code_block.treetop +1 -1
  7. data/lib/jievro/parser/grammar/declaration/constant_declaration.treetop +2 -8
  8. data/lib/jievro/parser/grammar/declaration/declaration.treetop +47 -5
  9. data/lib/jievro/parser/grammar/declaration/function_declaration.treetop +26 -105
  10. data/lib/jievro/parser/grammar/declaration/initializer_declaration.treetop +16 -0
  11. data/lib/jievro/parser/grammar/declaration/protocol_declaration.treetop +164 -0
  12. data/lib/jievro/parser/grammar/declaration/struct_declaration.treetop +49 -0
  13. data/lib/jievro/parser/grammar/declaration/typealias_declaration.treetop +64 -0
  14. data/lib/jievro/parser/grammar/declaration/variable_declaration.treetop +113 -9
  15. data/lib/jievro/parser/grammar/expression/binary_expression.treetop +5 -22
  16. data/lib/jievro/parser/grammar/expression/closure_expression.treetop +28 -86
  17. data/lib/jievro/parser/grammar/expression/expression.treetop +13 -13
  18. data/lib/jievro/parser/grammar/expression/function_call_expression.treetop +2 -6
  19. data/lib/jievro/parser/grammar/expression/literal_expression.treetop +1 -1
  20. data/lib/jievro/parser/grammar/expression/parenthesized_expression.treetop +15 -54
  21. data/lib/jievro/parser/grammar/expression/postfix_expression.treetop +1 -1
  22. data/lib/jievro/parser/grammar/expression/prefix_expression.treetop +1 -1
  23. data/lib/jievro/parser/grammar/expression/primary_expression.treetop +1 -1
  24. data/lib/jievro/parser/grammar/expression/range_expression.treetop +4 -18
  25. data/lib/jievro/parser/grammar/expression/self_expression.treetop +2 -7
  26. data/lib/jievro/parser/grammar/expression/super_expression.treetop +2 -7
  27. data/lib/jievro/parser/grammar/expression/wildcard_expression.treetop +1 -1
  28. data/lib/jievro/parser/grammar/generics/generic_argument_clause.treetop +8 -34
  29. data/lib/jievro/parser/grammar/generics/generics.treetop +2 -2
  30. data/lib/jievro/parser/grammar/grammar.treetop +35 -28
  31. data/lib/jievro/parser/grammar/identifier.treetop +2 -2
  32. data/lib/jievro/parser/grammar/implicit_parameter_name.treetop +1 -1
  33. data/lib/jievro/parser/grammar/literal/literal.treetop +25 -24
  34. data/lib/jievro/parser/grammar/literal/literal_array.treetop +12 -40
  35. data/lib/jievro/parser/grammar/literal/literal_boolean.treetop +5 -13
  36. data/lib/jievro/parser/grammar/literal/literal_decimal_floating_point.treetop +3 -9
  37. data/lib/jievro/parser/grammar/literal/literal_dictionary.treetop +18 -57
  38. data/lib/jievro/parser/grammar/literal/literal_hexadecimal_floating_point.treetop +3 -9
  39. data/lib/jievro/parser/grammar/literal/literal_integer_binary.treetop +3 -9
  40. data/lib/jievro/parser/grammar/literal/literal_integer_decimal.treetop +3 -9
  41. data/lib/jievro/parser/grammar/literal/literal_integer_hexadecimal.treetop +3 -9
  42. data/lib/jievro/parser/grammar/literal/literal_integer_octal.treetop +3 -9
  43. data/lib/jievro/parser/grammar/literal/literal_nil.treetop +2 -7
  44. data/lib/jievro/parser/grammar/literal/literal_numeric.treetop +11 -18
  45. data/lib/jievro/parser/grammar/literal/literal_string.treetop +14 -46
  46. data/lib/jievro/parser/grammar/operator.treetop +1 -1
  47. data/lib/jievro/parser/grammar/pattern/pattern.treetop +4 -4
  48. data/lib/jievro/parser/grammar/pattern/pattern_identifier.treetop +1 -1
  49. data/lib/jievro/parser/grammar/pattern/pattern_initializer.treetop +18 -70
  50. data/lib/jievro/parser/grammar/pattern/pattern_wildcard.treetop +1 -1
  51. data/lib/jievro/parser/grammar/statement/branch/branch.treetop +2 -2
  52. data/lib/jievro/parser/grammar/statement/branch/if.treetop +1 -1
  53. data/lib/jievro/parser/grammar/statement/loop/do_while.treetop +1 -1
  54. data/lib/jievro/parser/grammar/statement/loop/for.treetop +1 -1
  55. data/lib/jievro/parser/grammar/statement/loop/for_in.treetop +1 -1
  56. data/lib/jievro/parser/grammar/statement/loop/loop.treetop +5 -5
  57. data/lib/jievro/parser/grammar/statement/loop/while.treetop +1 -1
  58. data/lib/jievro/parser/grammar/statement/statement.treetop +3 -3
  59. data/lib/jievro/parser/grammar/type/type.treetop +7 -5
  60. data/lib/jievro/parser/grammar/type/type_annotation.treetop +1 -1
  61. data/lib/jievro/parser/grammar/type/type_array.treetop +1 -1
  62. data/lib/jievro/parser/grammar/type/type_dictionary.treetop +1 -1
  63. data/lib/jievro/parser/grammar/type/type_identifier.treetop +7 -14
  64. data/lib/jievro/parser/grammar/type/type_inheritance_clause.treetop +57 -0
  65. data/lib/jievro/parser/grammar/whitespace.treetop +1 -1
  66. data/lib/jievro/parser/grammar/wildcard.treetop +1 -1
  67. data/lib/jievro/parser/string_parser.rb +2 -7
  68. data/lib/jievro/parser/tools/converter/binary_string_to_int_converter.rb +6 -7
  69. data/lib/jievro/parser/tools/converter/converter.rb +1 -1
  70. data/lib/jievro/parser/tools/converter/decimal_float_string_to_float_converter.rb +5 -6
  71. data/lib/jievro/parser/tools/converter/decimal_string_to_int_converter.rb +5 -6
  72. data/lib/jievro/parser/tools/converter/hexadecimal_float_string_to_float_converter.rb +9 -14
  73. data/lib/jievro/parser/tools/converter/hexadecimal_string_to_int_converter.rb +5 -6
  74. data/lib/jievro/parser/tools/converter/octal_string_to_int_converter.rb +5 -6
  75. data/lib/jievro/parser/tools/quote_stripper.rb +1 -2
  76. data/lib/jievro/parser/tools/shunting_yard.rb +18 -32
  77. data/lib/jievro/parser/tools/tokens.rb +159 -0
  78. data/lib/jievro/parser/version.rb +1 -1
  79. metadata +24 -19
@@ -0,0 +1,16 @@
1
+ module Jievro
2
+ grammar InitializerDeclaration
3
+
4
+ rule initializer_head
5
+ 'init' {
6
+ def tokens
7
+ [T_INIT]
8
+ end
9
+
10
+ def ast
11
+ []
12
+ end
13
+ }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,164 @@
1
+ module Jievro
2
+ grammar ProtocolDeclaration
3
+
4
+ rule protocol_declaration
5
+ 'protocol' ws1:whitespaces identifier:identifier ws2:_ inheritances:type_inheritance_clause? ws3:_ '{' ws4:_ declarations:protocol_member_declarations? ws5:_ '}' {
6
+ def tokens
7
+ tokens = []
8
+
9
+ tokens.push(T_PROTOCOL)
10
+ tokens.concat(ws1.tokens)
11
+ tokens.concat(identifier.tokens)
12
+ tokens.concat(ws2.tokens) unless ws2.text_value.empty?
13
+ tokens.concat(inheritances.tokens) unless inheritances.text_value.empty?
14
+ tokens.concat(ws3.tokens) unless ws3.text_value.empty?
15
+ tokens.push(T_OPEN_CURLY_BRACKET)
16
+ tokens.concat(ws4.tokens) unless ws4.text_value.empty?
17
+ tokens.concat(declarations.tokens) unless declarations.text_value.empty?
18
+ tokens.concat(ws5.tokens) unless ws5.text_value.empty?
19
+ tokens.push(T_CLOSE_CURLY_BRACKET)
20
+
21
+ tokens
22
+ end
23
+
24
+ def ast
25
+ if inheritances.text_value.empty?
26
+ inhers = []
27
+ else
28
+ inhers = inheritances.ast
29
+ end
30
+
31
+ if declarations.text_value.empty?
32
+ decls = []
33
+ else
34
+ decls = declarations.ast
35
+ end
36
+
37
+ [
38
+ {
39
+ __type: 'protocol-declaration',
40
+ name: identifier.ast.first,
41
+ inheritances: inhers,
42
+ declarations: decls
43
+ }
44
+ ]
45
+ end
46
+ }
47
+ end
48
+
49
+ rule protocol_member_declarations
50
+ (ws1:_ declaration:protocol_member_declaration ws2:_)+ {
51
+ def tokens
52
+ tokens = []
53
+
54
+ elements.each { |e|
55
+ tokens.concat(e.ws1.tokens) unless e.ws1.text_value.empty?
56
+ tokens.concat(e.declaration.tokens)
57
+ tokens.concat(e.ws2.tokens) unless e.ws2.text_value.empty?
58
+ }
59
+
60
+ tokens
61
+ end
62
+
63
+ def ast
64
+ ast = []
65
+
66
+ elements.each { |e|
67
+ ast.concat(e.declaration.ast)
68
+ }
69
+
70
+ ast
71
+ end
72
+ }
73
+ end
74
+
75
+ rule protocol_member_declaration
76
+ protocol_property_declaration
77
+ /
78
+ protocol_method_declaration
79
+ /
80
+ protocol_initializer_declaration
81
+ end
82
+
83
+ rule protocol_property_declaration
84
+ head:variable_declaration_head ws1:_ name:variable_name ws2:_ type:type_annotation ws3:_ accessors:getter_setter_keyword_block {
85
+ def tokens
86
+ tokens = []
87
+
88
+ tokens.concat(head.tokens)
89
+ tokens.concat(ws1.tokens) unless ws1.text_value.empty?
90
+ tokens.concat(name.tokens)
91
+ tokens.concat(ws2.tokens) unless ws2.text_value.empty?
92
+ tokens.concat(type.tokens)
93
+ tokens.concat(ws3.tokens) unless ws3.text_value.empty?
94
+ tokens.concat(accessors.tokens)
95
+
96
+ tokens
97
+ end
98
+
99
+ def ast
100
+ [
101
+ {
102
+ __type: 'property-declaration',
103
+ name: name.ast.first,
104
+ type: type.ast.first,
105
+ accessors: accessors.ast
106
+ }
107
+ ]
108
+ end
109
+ }
110
+ end
111
+
112
+ rule protocol_method_declaration
113
+ head:function_head ws1:whitespaces name:function_name ws2:_ signature:function_signature {
114
+ def tokens
115
+ tokens = []
116
+
117
+ tokens.concat(head.tokens)
118
+ tokens.concat(ws1.tokens)
119
+ tokens.concat(name.tokens)
120
+ tokens.concat(ws2.tokens) unless ws2.text_value.empty?
121
+ tokens.concat(signature.tokens)
122
+
123
+ tokens
124
+ end
125
+
126
+ def ast
127
+ sign = signature.ast
128
+
129
+ [
130
+ {
131
+ __type: 'function-declaration',
132
+ name: name.ast.first,
133
+ parameters: sign[:parameters],
134
+ return_type: sign[:return_type]
135
+ }
136
+ ]
137
+ end
138
+ }
139
+ end
140
+
141
+ rule protocol_initializer_declaration
142
+ head:initializer_head ws:_ parameter_clause:parameter_clause {
143
+ def tokens
144
+ tokens = []
145
+
146
+ tokens.concat(head.tokens)
147
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
148
+ tokens.concat(parameter_clause.tokens)
149
+
150
+ tokens
151
+ end
152
+
153
+ def ast
154
+ [
155
+ {
156
+ __type: 'init-declaration',
157
+ parameters: parameter_clause.ast
158
+ }
159
+ ]
160
+ end
161
+ }
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,49 @@
1
+ module Jievro
2
+ grammar StructDeclaration
3
+
4
+ rule struct_declaration
5
+ 'struct' ws1:whitespaces identifier:identifier ws2:_ inheritances:type_inheritance_clause? ws3:_ '{' ws4:_ declarations:declarations? ws5:_ '}' {
6
+ def tokens
7
+ tokens = []
8
+
9
+ tokens.push(T_STRUCT)
10
+ tokens.concat(ws1.tokens)
11
+ tokens.concat(identifier.tokens)
12
+ tokens.concat(ws2.tokens) unless ws2.text_value.empty?
13
+ tokens.concat(inheritances.tokens) unless inheritances.text_value.empty?
14
+ tokens.concat(ws3.tokens) unless ws3.text_value.empty?
15
+ tokens.push(T_OPEN_CURLY_BRACKET)
16
+ tokens.concat(ws4.tokens) unless ws4.text_value.empty?
17
+ tokens.concat(declarations.tokens) unless declarations.text_value.empty?
18
+ tokens.concat(ws5.tokens) unless ws5.text_value.empty?
19
+ tokens.push(T_CLOSE_CURLY_BRACKET)
20
+
21
+ tokens
22
+ end
23
+
24
+ def ast
25
+ if declarations.text_value.empty?
26
+ decls = []
27
+ else
28
+ decls = declarations.ast
29
+ end
30
+
31
+ if inheritances.text_value.empty?
32
+ inhers = []
33
+ else
34
+ inhers = inheritances.ast
35
+ end
36
+
37
+ [
38
+ {
39
+ __type: 'struct-declaration',
40
+ name: identifier.ast.first,
41
+ inheritances: inhers,
42
+ declarations: decls
43
+ }
44
+ ]
45
+ end
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,64 @@
1
+ module Jievro
2
+ grammar TypealiasDeclaration
3
+
4
+ rule typealias_declaration
5
+ head:typealias_head ws:_ typealias:typealias_assignment {
6
+ def tokens
7
+ tokens = []
8
+
9
+ tokens.concat(head.tokens)
10
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
11
+ tokens.concat(typealias.tokens)
12
+
13
+ tokens
14
+ end
15
+
16
+ def ast
17
+ [
18
+ {
19
+ __type: "typealias-declaration",
20
+ name: head.ast.first,
21
+ type: typealias.ast.first
22
+ }
23
+ ]
24
+ end
25
+ }
26
+ end
27
+
28
+ rule typealias_head
29
+ 'typealias' ws:_ identifier {
30
+ def tokens
31
+ tokens = []
32
+
33
+ tokens.push(T_TYPEALIAS)
34
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
35
+ tokens.concat(identifier.tokens)
36
+
37
+ tokens
38
+ end
39
+
40
+ def ast
41
+ identifier.ast
42
+ end
43
+ }
44
+ end
45
+
46
+ rule typealias_assignment
47
+ '=' ws:_ type {
48
+ def tokens
49
+ tokens = []
50
+
51
+ tokens.push(T_OPERATOR('='))
52
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
53
+ tokens.concat(type.tokens)
54
+
55
+ tokens
56
+ end
57
+
58
+ def ast
59
+ type.ast
60
+ end
61
+ }
62
+ end
63
+ end
64
+ end
@@ -1,17 +1,12 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar VariableDeclaration
3
3
 
4
4
  rule variable_declaration
5
- 'var' whitespaces:whitespaces pattern_initializer_list:pattern_initializer_list {
5
+ head:variable_declaration_head whitespaces:whitespaces pattern_initializer_list:pattern_initializer_list {
6
6
  def tokens
7
-
8
7
  tokens = []
9
8
 
10
- tokens.push({
11
- type: 'T_VAR',
12
- value: 'var'
13
- })
14
-
9
+ tokens.concat(head.tokens)
15
10
  tokens.concat(whitespaces.tokens)
16
11
  tokens.concat(pattern_initializer_list.tokens)
17
12
 
@@ -19,7 +14,6 @@ module Kauri
19
14
  end
20
15
 
21
16
  def ast
22
-
23
17
  ast = []
24
18
 
25
19
  pattern_initializer_list.ast.each { |e|
@@ -34,5 +28,115 @@ module Kauri
34
28
  end
35
29
  }
36
30
  end
31
+
32
+ rule variable_declaration_head
33
+ 'var' {
34
+ def tokens
35
+ [T_VAR]
36
+ end
37
+ }
38
+ end
39
+
40
+ rule variable_name
41
+ identifier
42
+ end
43
+
44
+ rule getter_setter_keyword_block
45
+ '{' ws1:_ block:(getter_setter_keyword_clause / setter_getter_keyword_clause / getter_keyword_clause / setter_keyword_clause) ws2:_ '}' {
46
+ def tokens
47
+ tokens = []
48
+
49
+ tokens.push(T_OPEN_CURLY_BRACKET)
50
+ tokens.concat(ws1.tokens) unless ws1.text_value.empty?
51
+ tokens.concat(block.tokens)
52
+ tokens.concat(ws2.tokens) unless ws2.text_value.empty?
53
+ tokens.push(T_CLOSE_CURLY_BRACKET)
54
+
55
+ tokens
56
+ end
57
+
58
+ def ast
59
+ block.ast
60
+ end
61
+ }
62
+ end
63
+
64
+ rule getter_setter_keyword_clause
65
+ getter:getter_keyword_clause ws:whitespaces setter:setter_keyword_clause {
66
+ def tokens
67
+ tokens = []
68
+
69
+ tokens.concat(getter.tokens)
70
+ tokens.concat(ws.tokens)
71
+ tokens.concat(setter.tokens)
72
+
73
+ tokens
74
+ end
75
+
76
+ def ast
77
+ ast = []
78
+
79
+ ast.concat(getter.ast)
80
+ ast.concat(setter.ast)
81
+
82
+ ast
83
+ end
84
+ }
85
+ end
86
+
87
+ rule setter_getter_keyword_clause
88
+ setter:setter_keyword_clause ws:whitespaces getter:getter_keyword_clause {
89
+ def tokens
90
+ tokens = []
91
+
92
+ tokens.concat(setter.tokens)
93
+ tokens.concat(ws.tokens)
94
+ tokens.concat(getter.tokens)
95
+
96
+ tokens
97
+ end
98
+
99
+ def ast
100
+ ast = []
101
+
102
+ ast.concat(setter.ast)
103
+ ast.concat(getter.ast)
104
+
105
+ ast
106
+ end
107
+ }
108
+ end
109
+
110
+ rule getter_keyword_clause
111
+ 'get' {
112
+ def tokens
113
+ [T_GET]
114
+ end
115
+
116
+ def ast
117
+ [
118
+ {
119
+ __type: 'getter-accessor'
120
+ }
121
+ ]
122
+ end
123
+ }
124
+ end
125
+
126
+ rule setter_keyword_clause
127
+ 'set' {
128
+ def tokens
129
+ [T_SET]
130
+ end
131
+
132
+ def ast
133
+ [
134
+ {
135
+ __type: 'setter-accessor'
136
+ }
137
+ ]
138
+ end
139
+ }
140
+ end
37
141
  end
38
142
  end
@@ -1,37 +1,28 @@
1
1
  require 'jievro/parser/tools/shunting_yard'
2
2
 
3
- module Kauri
3
+ module Jievro
4
4
  grammar BinaryExpression
5
5
 
6
6
  rule binary_expression
7
7
  left:infix_operation_chain ws:_ right:primary_expression {
8
8
  def tokens
9
-
10
9
  tokens = []
11
10
 
12
11
  tokens.concat(left.tokens)
13
-
14
- unless ws.text_value.empty?
15
- tokens.concat(ws.tokens)
16
- end
17
-
12
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
18
13
  tokens.concat(right.tokens)
19
14
 
20
15
  tokens
21
16
  end
22
17
 
23
18
  def ast
24
-
25
19
  sy = Jievro::Parser::Tools::ShuntingYard.new
26
-
27
20
  flatten_ast = []
28
21
 
29
22
  flatten_ast.concat(left.flatten_ast)
30
23
  flatten_ast.push(right.ast.first)
31
24
 
32
- ast = sy.generate_ast(flatten_ast)
33
-
34
- ast
25
+ sy.generate_ast(flatten_ast)
35
26
  end
36
27
  }
37
28
  end
@@ -42,18 +33,10 @@ module Kauri
42
33
  tokens = []
43
34
 
44
35
  elements.each { |e|
45
-
46
36
  tokens.concat(e.primary.tokens)
47
-
48
- unless e.ws1.text_value.empty?
49
- tokens.concat(e.ws1.tokens)
50
- end
51
-
37
+ tokens.concat(e.ws1.tokens) unless e.ws1.text_value.empty?
52
38
  tokens.concat(e.operator.tokens)
53
-
54
- unless e.ws2.text_value.empty?
55
- tokens.concat(e.ws2.tokens)
56
- end
39
+ tokens.concat(e.ws2.tokens) unless e.ws2.text_value.empty?
57
40
  }
58
41
 
59
42
  tokens