jievro-parser 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.travis.yml +3 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +30 -0
  6. data/README.md +1 -0
  7. data/Rakefile +7 -0
  8. data/lib/parser/binary_operator.rb +64 -0
  9. data/lib/parser/grammar/code_block.treetop +46 -0
  10. data/lib/parser/grammar/declaration/constant_declaration.treetop +38 -0
  11. data/lib/parser/grammar/declaration/declaration.treetop +16 -0
  12. data/lib/parser/grammar/declaration/function_declaration.treetop +302 -0
  13. data/lib/parser/grammar/declaration/variable_declaration.treetop +38 -0
  14. data/lib/parser/grammar/expression/binary_expression.treetop +75 -0
  15. data/lib/parser/grammar/expression/closure_expression.treetop +188 -0
  16. data/lib/parser/grammar/expression/expression.treetop +83 -0
  17. data/lib/parser/grammar/expression/function_call_expression.treetop +32 -0
  18. data/lib/parser/grammar/expression/literal_expression.treetop +8 -0
  19. data/lib/parser/grammar/expression/parenthesized_expression.treetop +146 -0
  20. data/lib/parser/grammar/expression/postfix_expression.treetop +8 -0
  21. data/lib/parser/grammar/expression/prefix_expression.treetop +29 -0
  22. data/lib/parser/grammar/expression/primary_expression.treetop +8 -0
  23. data/lib/parser/grammar/expression/range_expression.treetop +53 -0
  24. data/lib/parser/grammar/expression/self_expression.treetop +25 -0
  25. data/lib/parser/grammar/expression/super_expression.treetop +25 -0
  26. data/lib/parser/grammar/expression/wildcard_expression.treetop +8 -0
  27. data/lib/parser/grammar/generics/generic_argument_clause.treetop +89 -0
  28. data/lib/parser/grammar/generics/generics.treetop +9 -0
  29. data/lib/parser/grammar/grammar.treetop +76 -0
  30. data/lib/parser/grammar/identifier.treetop +56 -0
  31. data/lib/parser/grammar/implicit_parameter_name.treetop +26 -0
  32. data/lib/parser/grammar/literal/literal.treetop +45 -0
  33. data/lib/parser/grammar/literal/literal_array.treetop +98 -0
  34. data/lib/parser/grammar/literal/literal_boolean.treetop +50 -0
  35. data/lib/parser/grammar/literal/literal_decimal_floating_point.treetop +49 -0
  36. data/lib/parser/grammar/literal/literal_dictionary.treetop +157 -0
  37. data/lib/parser/grammar/literal/literal_hexadecimal_floating_point.treetop +47 -0
  38. data/lib/parser/grammar/literal/literal_integer_binary.treetop +47 -0
  39. data/lib/parser/grammar/literal/literal_integer_decimal.treetop +47 -0
  40. data/lib/parser/grammar/literal/literal_integer_hexadecimal.treetop +47 -0
  41. data/lib/parser/grammar/literal/literal_integer_octal.treetop +47 -0
  42. data/lib/parser/grammar/literal/literal_nil.treetop +25 -0
  43. data/lib/parser/grammar/literal/literal_numeric.treetop +69 -0
  44. data/lib/parser/grammar/literal/literal_string.treetop +207 -0
  45. data/lib/parser/grammar/operator.treetop +35 -0
  46. data/lib/parser/grammar/pattern/pattern.treetop +16 -0
  47. data/lib/parser/grammar/pattern/pattern_identifier.treetop +8 -0
  48. data/lib/parser/grammar/pattern/pattern_initializer.treetop +189 -0
  49. data/lib/parser/grammar/pattern/pattern_wildcard.treetop +8 -0
  50. data/lib/parser/grammar/statement/branch/branch.treetop +16 -0
  51. data/lib/parser/grammar/statement/branch/if.treetop +63 -0
  52. data/lib/parser/grammar/statement/loop/do_while.treetop +32 -0
  53. data/lib/parser/grammar/statement/loop/for.treetop +127 -0
  54. data/lib/parser/grammar/statement/loop/for_in.treetop +35 -0
  55. data/lib/parser/grammar/statement/loop/loop.treetop +22 -0
  56. data/lib/parser/grammar/statement/loop/while.treetop +34 -0
  57. data/lib/parser/grammar/statement/statement.treetop +97 -0
  58. data/lib/parser/grammar/type/type.treetop +18 -0
  59. data/lib/parser/grammar/type/type_annotation.treetop +29 -0
  60. data/lib/parser/grammar/type/type_array.treetop +43 -0
  61. data/lib/parser/grammar/type/type_dictionary.treetop +59 -0
  62. data/lib/parser/grammar/type/type_identifier.treetop +39 -0
  63. data/lib/parser/grammar/whitespace.treetop +40 -0
  64. data/lib/parser/grammar/wildcard.treetop +25 -0
  65. data/lib/parser/parse_result.rb +17 -0
  66. data/lib/parser/parser.rb +34 -0
  67. data/lib/parser/tools/converter/binary_string_to_int_converter.rb +20 -0
  68. data/lib/parser/tools/converter/converter.rb +12 -0
  69. data/lib/parser/tools/converter/decimal_float_string_to_float_converter.rb +22 -0
  70. data/lib/parser/tools/converter/decimal_string_to_int_converter.rb +20 -0
  71. data/lib/parser/tools/converter/hexadecimal_float_string_to_float_converter.rb +42 -0
  72. data/lib/parser/tools/converter/hexadecimal_string_to_int_converter.rb +20 -0
  73. data/lib/parser/tools/converter/octal_string_to_int_converter.rb +20 -0
  74. data/lib/parser/tools/quote_stripper.rb +18 -0
  75. data/lib/parser/tools/shunting_yard.rb +80 -0
  76. data/lib/parser/tools/tokens.rb +112 -0
  77. data/lib/parser/version.rb +5 -0
  78. data/lib/parser.rb +2 -0
  79. data/parser.gemspec +25 -0
  80. metadata +163 -0
@@ -0,0 +1,9 @@
1
+ require 'parser/grammar/generics/generic_argument_clause'
2
+
3
+ module Kauri
4
+ grammar Generics
5
+
6
+ include Kauri::GenericArgumentClause
7
+
8
+ end
9
+ end
@@ -0,0 +1,76 @@
1
+ require 'parser/grammar/whitespace'
2
+ require 'parser/grammar/literal/literal'
3
+ require 'parser/grammar/identifier'
4
+ require 'parser/grammar/declaration/declaration'
5
+ require 'parser/grammar/statement/statement'
6
+ require 'parser/grammar/expression/expression'
7
+ require 'parser/grammar/generics/generics'
8
+ require 'parser/grammar/type/type'
9
+ require 'parser/grammar/code_block'
10
+ require 'parser/grammar/pattern/pattern'
11
+ require 'parser/grammar/wildcard'
12
+ require 'parser/grammar/operator'
13
+
14
+ require 'parser/tools/tokens'
15
+
16
+ module Jievro
17
+ grammar Swift
18
+
19
+ include Kauri::Whitespace
20
+ include Kauri::Literal
21
+ include Kauri::Identifier
22
+ include Kauri::Declaration
23
+ include Kauri::Statement
24
+ include Kauri::Expression
25
+ include Kauri::Generics
26
+ include Kauri::Type
27
+ include Kauri::CodeBlock
28
+ include Kauri::Pattern
29
+ include Kauri::Wildcard
30
+ include Kauri::Operator
31
+
32
+ rule root
33
+ (
34
+ declaration:declaration
35
+ /
36
+ statement:statement
37
+ /
38
+ expression:expression
39
+ /
40
+ identifier:identifier
41
+ /
42
+ whitespaces:whitespaces
43
+ ) {
44
+ def tokens
45
+ unless elements.any?
46
+ tokens = []
47
+ else
48
+ tokens = elements[0].tokens
49
+ end
50
+
51
+ tokens
52
+ end
53
+
54
+ def ast
55
+ body = []
56
+
57
+ if elements.length
58
+ e = elements[0]
59
+ if e.respond_to?(:ast)
60
+ body = e.ast
61
+ end
62
+
63
+ {
64
+ block_type: 'program',
65
+ body: body
66
+ }
67
+ end
68
+ end
69
+ }
70
+ end
71
+
72
+ rule sign
73
+ '+' / '-'
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,56 @@
1
+ require 'parser/grammar/implicit_parameter_name'
2
+
3
+ module Kauri
4
+ grammar Identifier
5
+
6
+ include Kauri::ImplicitParameterName
7
+
8
+ rule identifier
9
+ implicit_parameter_name / backticked_identifier / regular_identifier
10
+ end
11
+
12
+ rule regular_identifier
13
+ '_' [A-Za-z0-9_]+ / [A-Za-z] [A-Za-z0-9_]* {
14
+ def tokens
15
+ [
16
+ {
17
+ type: 'T_IDENTIFIER',
18
+ value: text_value
19
+ }
20
+ ]
21
+ end
22
+
23
+ def ast
24
+ [
25
+ {
26
+ block_type: 'identifier',
27
+ value: text_value
28
+ }
29
+ ]
30
+ end
31
+ }
32
+ end
33
+
34
+ rule backticked_identifier
35
+ '`' regular_identifier:regular_identifier '`' {
36
+ def tokens
37
+ [
38
+ {
39
+ type: 'T_BACKTICKED_IDENTIFIER',
40
+ value: text_value
41
+ }
42
+ ]
43
+ end
44
+
45
+ def ast
46
+ [
47
+ {
48
+ block_type: 'identifier',
49
+ value: regular_identifier.text_value
50
+ }
51
+ ]
52
+ end
53
+ }
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,26 @@
1
+ module Kauri
2
+ grammar ImplicitParameterName
3
+
4
+ rule implicit_parameter_name
5
+ '$' literal_integer_decimal:literal_integer_decimal {
6
+ def tokens
7
+ [
8
+ {
9
+ type: 'T_IMPLICIT_PARAMETER_NAME',
10
+ value: text_value
11
+ }
12
+ ]
13
+ end
14
+
15
+ def ast
16
+ [
17
+ {
18
+ block_type: 'implicit-parameter-name',
19
+ value: literal_integer_decimal.text_value,
20
+ }
21
+ ]
22
+ end
23
+ }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,45 @@
1
+ require 'parser/grammar/literal/literal_nil'
2
+ require 'parser/grammar/literal/literal_boolean'
3
+ require 'parser/grammar/literal/literal_numeric'
4
+ require 'parser/grammar/literal/literal_string'
5
+ require 'parser/grammar/literal/literal_array'
6
+ require 'parser/grammar/literal/literal_dictionary'
7
+
8
+ module Kauri
9
+ grammar Literal
10
+
11
+ include Kauri::LiteralNil
12
+ include Kauri::LiteralBoolean
13
+ include Kauri::LiteralNumeric
14
+ include Kauri::LiteralString
15
+ include Kauri::LiteralArray
16
+ include Kauri::LiteralDictionary
17
+
18
+ rule literal
19
+ (literal_nil / literal_boolean / literal_numeric / literal_string / literal_array / literal_dictionary) {
20
+ def ast
21
+
22
+ if defined?(super)
23
+ return super
24
+ end
25
+
26
+ if elements.nil?
27
+ return []
28
+ end
29
+
30
+ unless elements.any?
31
+ return []
32
+ end
33
+
34
+ first_element = elements.first
35
+
36
+ if first_element.respond_to?(:ast)
37
+ return first_element.ast
38
+ end
39
+
40
+ []
41
+ end
42
+ }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,98 @@
1
+ module Kauri
2
+ grammar LiteralArray
3
+
4
+ rule literal_array
5
+ '[' ws1:_ items:literal_array_items? ws2:_ ']' {
6
+ def tokens
7
+ tokens = []
8
+
9
+ tokens.push({
10
+ type: 'T_OPEN_SQUARE_BRACKET',
11
+ value: '['
12
+ })
13
+
14
+ unless ws1.text_value.empty?
15
+ tokens.concat(ws1.tokens)
16
+ end
17
+
18
+ unless items.text_value.empty?
19
+ tokens.concat(items.tokens)
20
+ end
21
+
22
+ unless ws2.text_value.empty?
23
+ tokens.concat(ws2.tokens)
24
+ end
25
+
26
+ tokens.push({
27
+ type: 'T_CLOSE_SQUARE_BRACKET',
28
+ value: ']'
29
+ })
30
+
31
+ tokens
32
+ end
33
+
34
+ def ast
35
+
36
+ values = []
37
+
38
+ unless items.text_value.empty?
39
+ values = items.ast
40
+ end
41
+
42
+ [
43
+ {
44
+ block_type: 'literal-array',
45
+ values: values
46
+ }
47
+ ]
48
+ end
49
+ }
50
+ end
51
+
52
+ rule literal_array_items
53
+ head:(expression:expression ws1:_ ',' ws2:_)* tail:expression {
54
+ def tokens
55
+ tokens = []
56
+
57
+ unless head.text_value.empty?
58
+ head.elements.each { |e|
59
+
60
+ tokens.concat(e.expression.tokens)
61
+
62
+ unless e.ws1.text_value.empty?
63
+ tokens.concat(e.ws1.tokens)
64
+ end
65
+
66
+ tokens.push({
67
+ type: 'T_COMMA',
68
+ value: ','
69
+ })
70
+
71
+ unless e.ws2.text_value.empty?
72
+ tokens.concat(e.ws2.tokens)
73
+ end
74
+ }
75
+ end
76
+
77
+ tokens.concat(tail.tokens)
78
+
79
+ tokens
80
+ end
81
+
82
+ def ast
83
+ ast = []
84
+
85
+ unless head.text_value.empty?
86
+ head.elements.each { |e|
87
+ ast.concat(e.expression.ast)
88
+ }
89
+ end
90
+
91
+ ast.concat(tail.ast)
92
+
93
+ ast
94
+ end
95
+ }
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,50 @@
1
+ module Kauri
2
+ grammar LiteralBoolean
3
+
4
+ rule literal_boolean
5
+ literal_true / literal_false
6
+ end
7
+
8
+ rule literal_true
9
+ 'true' {
10
+ def tokens
11
+ [
12
+ {
13
+ type: 'T_TRUE',
14
+ value: text_value
15
+ }
16
+ ]
17
+ end
18
+ def ast
19
+ [
20
+ {
21
+ block_type: 'literal-boolean',
22
+ value: true
23
+ }
24
+ ]
25
+ end
26
+ }
27
+ end
28
+
29
+ rule literal_false
30
+ 'false' {
31
+ def tokens
32
+ [
33
+ {
34
+ type: 'T_FALSE',
35
+ value: text_value
36
+ }
37
+ ]
38
+ end
39
+ def ast
40
+ [
41
+ {
42
+ block_type: 'literal-boolean',
43
+ value: false
44
+ }
45
+ ]
46
+ end
47
+ }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,49 @@
1
+ require 'parser/tools/converter/converter'
2
+
3
+ module Kauri
4
+ grammar LiteralDecimalFloatingPoint
5
+
6
+ rule literal_decimal_floating_point
7
+ (
8
+ (literal_integer_decimal decimal_fraction decimal_exponent)
9
+ /
10
+ (literal_integer_decimal decimal_fraction)
11
+ /
12
+ (literal_integer_decimal decimal_exponent)
13
+ ) {
14
+ def tokens
15
+ [
16
+ {
17
+ type: 'T_LITERAL_FLOATING_POINT_DECIMAL',
18
+ value: text_value
19
+ }
20
+ ]
21
+ end
22
+
23
+ def ast
24
+
25
+ converter = Kauri::Tools::Converter::DecimalFloatStringToFloatConverter.new
26
+
27
+ [
28
+ {
29
+ block_type: 'literal-double',
30
+ value: converter.convert(text_value)
31
+ }
32
+ ]
33
+ end
34
+ }
35
+ end
36
+
37
+ rule decimal_fraction
38
+ '.' literal_integer_decimal
39
+ end
40
+
41
+ rule decimal_exponent
42
+ floating_point_e sign? literal_integer_decimal
43
+ end
44
+
45
+ rule floating_point_e
46
+ 'e' / 'E'
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,157 @@
1
+ module Kauri
2
+ grammar LiteralDictionary
3
+
4
+ rule literal_dictionary
5
+ '[' ws1:_ content:literal_dictionary_content ws2:_ ']' {
6
+ def tokens
7
+ tokens = []
8
+
9
+ tokens.push({
10
+ type: 'T_OPEN_SQUARE_BRACKET',
11
+ value: '['
12
+ })
13
+
14
+ unless ws1.text_value.empty?
15
+ tokens.concat(ws1.tokens)
16
+ end
17
+
18
+ tokens.concat(content.tokens)
19
+
20
+ unless ws2.text_value.empty?
21
+ tokens.concat(ws2.tokens)
22
+ end
23
+
24
+ tokens.push({
25
+ type: 'T_CLOSE_SQUARE_BRACKET',
26
+ value: ']'
27
+ })
28
+
29
+ tokens
30
+ end
31
+
32
+ def ast
33
+ content.ast
34
+ end
35
+ }
36
+ end
37
+
38
+ rule literal_dictionary_content
39
+ (items:literal_dictionary_items / ':') {
40
+ def tokens
41
+
42
+ if defined? items
43
+ tokens = items.tokens
44
+ else
45
+ tokens = [{
46
+ type: 'T_COLON',
47
+ value: ':'
48
+ }]
49
+ end
50
+
51
+ tokens
52
+ end
53
+
54
+ def ast
55
+
56
+ if defined? items
57
+ ast = items.ast
58
+ else
59
+ ast = [{
60
+ block_type: 'literal-dictionary',
61
+ pairs: []
62
+ }]
63
+ end
64
+
65
+ ast
66
+ end
67
+ }
68
+ end
69
+
70
+ rule literal_dictionary_item
71
+ key:expression ws1:_ ':' ws2:_ value:expression {
72
+ def tokens
73
+ tokens = []
74
+
75
+ tokens.concat(key.tokens)
76
+
77
+ unless ws1.text_value.empty?
78
+ tokens.concat(ws1.tokens)
79
+ end
80
+
81
+ tokens.push({
82
+ type: 'T_COLON',
83
+ value: ':'
84
+ })
85
+
86
+ unless ws2.text_value.empty?
87
+ tokens.concat(ws2.tokens)
88
+ end
89
+
90
+ tokens.concat(value.tokens)
91
+
92
+ tokens
93
+ end
94
+
95
+ def ast
96
+ [
97
+ {
98
+ key: key.ast.first,
99
+ value: value.ast.first
100
+ }
101
+ ]
102
+ end
103
+ }
104
+ end
105
+
106
+ rule literal_dictionary_items
107
+ head:(item:literal_dictionary_item ws1:_ ',' ws2:_)* tail:literal_dictionary_item {
108
+ def tokens
109
+ tokens = []
110
+
111
+ unless head.text_value.empty?
112
+ head.elements.each { |e|
113
+
114
+ tokens.concat(e.item.tokens)
115
+
116
+ unless e.ws1.text_value.empty?
117
+ tokens.concat(e.ws1.tokens)
118
+ end
119
+
120
+ tokens.push({
121
+ type: 'T_COMMA',
122
+ value: ','
123
+ })
124
+
125
+ unless e.ws2.text_value.empty?
126
+ tokens.concat(e.ws2.tokens)
127
+ end
128
+ }
129
+ end
130
+
131
+ tokens.concat(tail.tokens)
132
+
133
+ tokens
134
+ end
135
+
136
+ def ast
137
+ pairs = []
138
+
139
+ unless head.text_value.empty?
140
+ head.elements.each { |e|
141
+ pairs.concat(e.item.ast)
142
+ }
143
+ end
144
+
145
+ pairs.concat(tail.ast)
146
+
147
+ [
148
+ {
149
+ block_type: 'literal-dictionary',
150
+ pairs: pairs
151
+ }
152
+ ]
153
+ end
154
+ }
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,47 @@
1
+ require 'parser/tools/converter/converter'
2
+
3
+ module Kauri
4
+ grammar LiteralHexadecimalFloatingPoint
5
+
6
+ rule literal_hexadecimal_floating_point
7
+ (
8
+ (literal_integer_hexadecimal hexadecimal_fraction hexadecimal_exponent)
9
+ /
10
+ (literal_integer_hexadecimal hexadecimal_exponent)
11
+ ) {
12
+ def tokens
13
+ [
14
+ {
15
+ type: 'T_LITERAL_FLOATING_POINT_HEXADECIMAL',
16
+ value: text_value
17
+ }
18
+ ]
19
+ end
20
+
21
+ def ast
22
+
23
+ converter = Kauri::Tools::Converter::HexadecimalFloatStringToFloatConverter.new
24
+
25
+ [
26
+ {
27
+ block_type: 'literal-double',
28
+ value: converter.convert(text_value)
29
+ }
30
+ ]
31
+ end
32
+ }
33
+ end
34
+
35
+ rule hexadecimal_fraction
36
+ '.' hexadecimal_digit hexadecimal_literal_characters?
37
+ end
38
+
39
+ rule hexadecimal_exponent
40
+ floating_point_p sign? literal_integer_decimal
41
+ end
42
+
43
+ rule floating_point_p
44
+ 'p' / 'P'
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ require 'parser/tools/converter/converter'
2
+
3
+ module Kauri
4
+ grammar LiteralIntegerBinary
5
+
6
+ rule literal_integer_binary
7
+ '0b' binary_digit binary_literal_characters? {
8
+ def tokens
9
+ [
10
+ {
11
+ type: 'T_LITERAL_INTEGER_BINARY',
12
+ value: text_value
13
+ }
14
+ ]
15
+ end
16
+
17
+ def ast
18
+
19
+ converter = Kauri::Tools::Converter::BinaryStringToIntConverter.new
20
+
21
+ [
22
+ {
23
+ block_type: 'literal-integer',
24
+ value: converter.convert(text_value)
25
+ }
26
+ ]
27
+ end
28
+ }
29
+ end
30
+
31
+ rule binary_literal_characters
32
+ binary_literal_character+
33
+ end
34
+
35
+ rule binary_literal_character
36
+ binary_digit / '_'
37
+ end
38
+
39
+ rule binary_digits
40
+ binary_digit+
41
+ end
42
+
43
+ rule binary_digit
44
+ [0-1]+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,47 @@
1
+ require 'parser/tools/converter/converter'
2
+
3
+ module Kauri
4
+ grammar LiteralIntegerDecimal
5
+
6
+ rule literal_integer_decimal
7
+ decimal_digit decimal_literal_characters? {
8
+ def tokens
9
+ [
10
+ {
11
+ type: 'T_LITERAL_INTEGER_DECIMAL',
12
+ value: text_value
13
+ }
14
+ ]
15
+ end
16
+
17
+ def ast
18
+
19
+ converter = Kauri::Tools::Converter::DecimalStringToIntConverter.new
20
+
21
+ [
22
+ {
23
+ block_type: 'literal-integer',
24
+ value: converter.convert(text_value)
25
+ }
26
+ ]
27
+ end
28
+ }
29
+ end
30
+
31
+ rule decimal_literal_characters
32
+ decimal_literal_character+
33
+ end
34
+
35
+ rule decimal_literal_character
36
+ decimal_digit / '_'
37
+ end
38
+
39
+ rule decimal_digits
40
+ decimal_digit+
41
+ end
42
+
43
+ rule decimal_digit
44
+ [0-9]+
45
+ end
46
+ end
47
+ end