jievro-parser 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar PatternWildcard
3
3
 
4
4
  rule pattern_wildcard
@@ -1,11 +1,11 @@
1
1
  require 'jievro/parser/grammar/statement/branch/if'
2
2
 
3
- module Kauri
3
+ module Jievro
4
4
  module Grammar
5
5
  module Statement
6
6
  grammar Branch
7
7
 
8
- include Kauri::StatementIf
8
+ include Jievro::StatementIf
9
9
 
10
10
  rule statement_branch
11
11
  statement_if
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar StatementIf
3
3
 
4
4
  rule statement_if
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar StatementDoWhile
3
3
 
4
4
  rule statement_do_while
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar StatementFor
3
3
 
4
4
  rule statement_for
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar StatementForIn
3
3
 
4
4
  rule statement_for_in
@@ -3,15 +3,15 @@ require 'jievro/parser/grammar/statement/loop/for_in'
3
3
  require 'jievro/parser/grammar/statement/loop/while'
4
4
  require 'jievro/parser/grammar/statement/loop/do_while'
5
5
 
6
- module Kauri
6
+ module Jievro
7
7
  module Grammar
8
8
  module Statement
9
9
  grammar Loop
10
10
 
11
- include Kauri::StatementFor
12
- include Kauri::StatementForIn
13
- include Kauri::StatementWhile
14
- include Kauri::StatementDoWhile
11
+ include Jievro::StatementFor
12
+ include Jievro::StatementForIn
13
+ include Jievro::StatementWhile
14
+ include Jievro::StatementDoWhile
15
15
 
16
16
  rule statement_loop
17
17
  statement_for_in / statement_for / statement_while / statement_do_while
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar StatementWhile
3
3
 
4
4
  rule statement_while
@@ -1,11 +1,11 @@
1
1
  require 'jievro/parser/grammar/statement/loop/loop'
2
2
  require 'jievro/parser/grammar/statement/branch/branch'
3
3
 
4
- module Kauri
4
+ module Jievro
5
5
  grammar Statement
6
6
 
7
- include Kauri::Grammar::Statement::Loop
8
- include Kauri::Grammar::Statement::Branch
7
+ include Jievro::Grammar::Statement::Loop
8
+ include Jievro::Grammar::Statement::Branch
9
9
 
10
10
  rule statement
11
11
  statement_loop / statement_branch / expression_statement / declaration_statement
@@ -2,14 +2,16 @@ require 'jievro/parser/grammar/type/type_identifier'
2
2
  require 'jievro/parser/grammar/type/type_array'
3
3
  require 'jievro/parser/grammar/type/type_dictionary'
4
4
  require 'jievro/parser/grammar/type/type_annotation'
5
+ require 'jievro/parser/grammar/type/type_inheritance_clause'
5
6
 
6
- module Kauri
7
+ module Jievro
7
8
  grammar Type
8
9
 
9
- include Kauri::TypeIdentifier
10
- include Kauri::TypeArray
11
- include Kauri::TypeDictionary
12
- include Kauri::TypeAnnotation
10
+ include Jievro::TypeIdentifier
11
+ include Jievro::TypeArray
12
+ include Jievro::TypeDictionary
13
+ include Jievro::TypeAnnotation
14
+ include Jievro::TypeInheritanceClause
13
15
 
14
16
  rule type
15
17
  type_identifier / type_dictionary / type_array
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar TypeAnnotation
3
3
 
4
4
  rule type_annotation
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar TypeArray
3
3
 
4
4
  rule type_array
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar TypeDictionary
3
3
 
4
4
  rule type_dictionary
@@ -1,31 +1,24 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar TypeIdentifier
3
3
 
4
4
  rule type_identifier
5
5
  type_name:type_name ws:_ generic_argument_clause:generic_argument_clause? {
6
6
  def tokens
7
-
8
7
  tokens = []
9
8
 
10
9
  tokens.concat(type_name.tokens)
11
-
12
- unless ws.text_value.empty?
13
- tokens.concat(ws.tokens)
14
- end
15
-
16
- unless generic_argument_clause.text_value.empty?
17
- tokens.concat(generic_argument_clause.tokens)
18
- end
10
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
11
+ tokens.concat(generic_argument_clause.tokens) \
12
+ unless generic_argument_clause.text_value.empty?
19
13
 
20
14
  tokens
21
15
  end
22
- def ast
23
16
 
17
+ def ast
24
18
  ast = type_name.ast
25
19
 
26
- unless generic_argument_clause.text_value.empty?
27
- ast.first[:parameters] = generic_argument_clause.ast
28
- end
20
+ ast.first[:parameters] = generic_argument_clause.ast \
21
+ unless generic_argument_clause.text_value.empty?
29
22
 
30
23
  ast
31
24
  end
@@ -0,0 +1,57 @@
1
+ module Jievro
2
+ grammar TypeInheritanceClause
3
+
4
+ rule type_inheritance_clause
5
+ ':' ws:_ list:type_inheritance_list {
6
+ def tokens
7
+ tokens = []
8
+
9
+ tokens.push(T_COLON)
10
+ tokens.concat(ws.tokens) unless ws.text_value.empty?
11
+ tokens.concat(list.tokens)
12
+
13
+ tokens
14
+ end
15
+
16
+ def ast
17
+ list.ast
18
+ end
19
+ }
20
+ end
21
+
22
+ rule type_inheritance_list
23
+ head:(type:type_identifier ws1:_ ',' ws2:_)* tail:type_identifier {
24
+ def tokens
25
+ tokens = []
26
+
27
+ unless head.text_value.empty?
28
+ head.elements.each { |e|
29
+ tokens.concat(e.type.tokens)
30
+ tokens.concat(e.ws1.tokens) unless e.ws1.text_value.empty?
31
+ tokens.push(T_COMMA)
32
+ tokens.concat(e.ws2.tokens) unless e.ws2.text_value.empty?
33
+ }
34
+ end
35
+
36
+ tokens.concat(tail.tokens)
37
+
38
+ tokens
39
+ end
40
+
41
+ def ast
42
+ ast = []
43
+
44
+ unless head.text_value.empty?
45
+ head.elements.each { |e|
46
+ ast.concat(e.type.ast)
47
+ }
48
+ end
49
+
50
+ ast.concat(tail.ast)
51
+
52
+ ast
53
+ end
54
+ }
55
+ end
56
+ end
57
+ end
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar Whitespace
3
3
 
4
4
  rule _
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  grammar Wildcard
3
3
 
4
4
  rule wildcard
@@ -8,9 +8,7 @@ module Jievro
8
8
  module Parser
9
9
  class StringParser
10
10
  def parse(source)
11
-
12
11
  parser = Jievro::SwiftParser.new
13
-
14
12
  tokens = []
15
13
  ast = {
16
14
  __type: 'program',
@@ -18,13 +16,10 @@ module Jievro
18
16
  }
19
17
 
20
18
  unless source.empty?
21
-
22
19
  parsed = parser.parse(source)
23
20
 
24
- if parsed.nil?
25
- raise Exception, parser.failure_reason
26
- end
27
-
21
+ fail Exception, parser.failure_reason if parsed.nil?
22
+
28
23
  tokens = parsed.tokens
29
24
  ast = parsed.ast
30
25
  end
@@ -1,18 +1,17 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  class BinaryStringToIntConverter
5
5
  def convert(binary_string)
6
+ fail Exception, 'Cannot convert non `String\' values' \
7
+ unless binary_string.is_a?(String)
6
8
 
7
- raise Exception, 'Cannot convert non `String\' values' \
8
- unless binary_string.kind_of?(String)
9
-
10
- raise Exception, 'String to convert do not match the regular format' \
9
+ fail Exception, 'String to convert do not match the regular format' \
11
10
  unless binary_string =~ /^0b[01_]+$/i
12
11
 
13
- clean_string = binary_string.gsub! /0b|_*/, ''
12
+ clean_string = binary_string.gsub!(/0b|_*/, '')
14
13
 
15
- clean_string.to_i(base=2)
14
+ clean_string.to_i(2)
16
15
  end
17
16
  end
18
17
  end
@@ -1,4 +1,4 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  require 'jievro/parser/tools/converter/binary_string_to_int_converter'
@@ -1,18 +1,17 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  class DecimalFloatStringToFloatConverter
5
5
  def convert(decimal_float_string)
6
-
7
- raise Exception, 'Cannot convert non `String\' values' \
8
- unless decimal_float_string.kind_of?(String)
6
+ fail Exception, 'Cannot convert non `String\' values' \
7
+ unless decimal_float_string.is_a?(String)
9
8
 
10
9
  # the regex that match a decimal floating number is not exact
11
10
  # but for this purpose should be fine
12
- raise Exception, 'String do not match the regular format' \
11
+ fail Exception, 'String do not match the regular format' \
13
12
  unless decimal_float_string =~ /^[0-9_]+.?[0-9_]*e?[+-]?[0-9_]*$/i
14
13
 
15
- clean_string = decimal_float_string.gsub! /_*/, ''
14
+ clean_string = decimal_float_string.gsub!(/_*/, '')
16
15
 
17
16
  clean_string.to_f
18
17
  end
@@ -1,16 +1,15 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  class DecimalStringToIntConverter
5
5
  def convert(decimal_string)
6
+ fail Exception, 'Cannot convert non `String\' values' \
7
+ unless decimal_string.is_a?(String)
6
8
 
7
- raise Exception, 'Cannot convert non `String\' values' \
8
- unless decimal_string.kind_of?(String)
9
-
10
- raise Exception, 'String to convert do not match the regular format' \
9
+ fail Exception, 'String to convert do not match the regular format' \
11
10
  unless decimal_string =~ /^[0-9_]+$/i
12
11
 
13
- clean_string = decimal_string.gsub! /_*/, ''
12
+ clean_string = decimal_string.gsub!(/_*/, '')
14
13
 
15
14
  clean_string.to_i
16
15
  end
@@ -1,40 +1,35 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  class HexadecimalFloatStringToFloatConverter
5
5
  def convert(hexadecimal_float_string)
6
+ fail Exception, 'Cannot convert non `String\' values' \
7
+ unless hexadecimal_float_string.is_a?(String)
6
8
 
7
- raise Exception, 'Cannot convert non `String\' values' \
8
- unless hexadecimal_float_string.kind_of?(String)
9
-
10
- clean_string = hexadecimal_float_string.gsub! /_*/, ''
9
+ clean_string = hexadecimal_float_string.gsub!(/\_*/, '')
11
10
 
12
11
  match = clean_string.match(/^0x([0-9a-f]+).?([0-9a-f]*)p([+-]?)([0-9]+)$/i)
13
12
 
14
- raise Exception, 'String to convert do not match the regular format' \
13
+ fail Exception, 'String to convert do not match the regular format' \
15
14
  unless match
16
15
 
17
16
  captures = match.captures
18
17
 
19
- integer = captures[0].to_i(base=16).to_f
18
+ integer = captures[0].to_i(16).to_f
20
19
  mantissa_hexadecimal = captures[1]
21
20
  negative_exponent = captures[2] == '-'
22
21
  exponent = captures[3].to_i
23
22
 
24
23
  mantissa = 0.0
25
-
26
- if negative_exponent
27
- exponent = -exponent
28
- end
29
-
24
+ exponent = -exponent if negative_exponent
30
25
  mantissa_current_bit_value = 16
31
26
 
32
27
  mantissa_hexadecimal.each_byte do |mantissa_hexadecimal_char|
33
- mantissa += mantissa_hexadecimal_char.chr.to_i(base=16).to_f / mantissa_current_bit_value
28
+ mantissa += mantissa_hexadecimal_char.chr.to_i(16).to_f / mantissa_current_bit_value
34
29
  mantissa_current_bit_value <<= 4
35
30
  end
36
31
 
37
- (integer + mantissa) * (2 ** exponent)
32
+ (integer + mantissa) * (2**exponent)
38
33
  end
39
34
  end
40
35
  end
@@ -1,18 +1,17 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  class HexadecimalStringToIntConverter
5
5
  def convert(hexadecimal_string)
6
+ fail Exception, 'Cannot convert non `String\' values' \
7
+ unless hexadecimal_string.is_a?(String)
6
8
 
7
- raise Exception, 'Cannot convert non `String\' values' \
8
- unless hexadecimal_string.kind_of?(String)
9
-
10
- raise Exception, 'String to convert do not match the regular format' \
9
+ fail Exception, 'String to convert do not match the regular format' \
11
10
  unless hexadecimal_string =~ /^0x[0-9a-f_]+$/i
12
11
 
13
12
  clean_string = hexadecimal_string.gsub!(/0x|_*/, '')
14
13
 
15
- clean_string.to_i(base=16)
14
+ clean_string.to_i(16)
16
15
  end
17
16
  end
18
17
  end
@@ -1,18 +1,17 @@
1
- module Kauri
1
+ module Jievro
2
2
  module Tools
3
3
  module Converter
4
4
  class OctalStringToIntConverter
5
5
  def convert(octal_string)
6
+ fail Exception, 'Cannot convert non `String\' values' \
7
+ unless octal_string.is_a?(String)
6
8
 
7
- raise Exception, 'Cannot convert non `String\' values' \
8
- unless octal_string.kind_of?(String)
9
-
10
- raise Exception, 'String to convert do not match the regular format' \
9
+ fail Exception, 'String to convert do not match the regular format' \
11
10
  unless octal_string =~ /^0o[0-7_]+$/i
12
11
 
13
12
  clean_string = octal_string.gsub!(/_*/, '')
14
13
 
15
- clean_string.to_i(base=8)
14
+ clean_string.to_i(8)
16
15
  end
17
16
  end
18
17
  end