expressir 0.2.5 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/.cross_rubies +30 -0
  3. data/.github/workflows/rake.yml +14 -15
  4. data/.github/workflows/release.yml +24 -16
  5. data/.gitignore +3 -1
  6. data/Gemfile +0 -2
  7. data/Rakefile +2 -8
  8. data/expressir.gemspec +7 -4
  9. data/ext/express-parser/express_parser.cpp +12 -10
  10. data/ext/express-parser/extconf.rb +48 -25
  11. data/lib/expressir/express_exp/formatter.rb +74 -23
  12. data/lib/expressir/express_exp/parser.rb +20 -2
  13. data/lib/expressir/express_exp/visitor.rb +201 -96
  14. data/lib/expressir/model.rb +3 -0
  15. data/lib/expressir/model/attribute.rb +3 -1
  16. data/lib/expressir/model/constant.rb +3 -1
  17. data/lib/expressir/model/entity.rb +12 -19
  18. data/lib/expressir/model/enumeration_item.rb +3 -1
  19. data/lib/expressir/model/expressions/aggregate_initializer.rb +2 -2
  20. data/lib/expressir/model/expressions/aggregate_item.rb +1 -1
  21. data/lib/expressir/model/expressions/attribute_reference.rb +1 -1
  22. data/lib/expressir/model/expressions/binary_expression.rb +1 -1
  23. data/lib/expressir/model/expressions/call.rb +2 -2
  24. data/lib/expressir/model/expressions/entity_constructor.rb +2 -2
  25. data/lib/expressir/model/expressions/group_reference.rb +1 -1
  26. data/lib/expressir/model/expressions/index_reference.rb +1 -1
  27. data/lib/expressir/model/expressions/interval.rb +1 -1
  28. data/lib/expressir/model/expressions/query_expression.rb +5 -3
  29. data/lib/expressir/model/expressions/simple_reference.rb +1 -1
  30. data/lib/expressir/model/expressions/unary_expression.rb +1 -1
  31. data/lib/expressir/model/function.rb +32 -38
  32. data/lib/expressir/model/identifier.rb +1 -0
  33. data/lib/expressir/model/informal_proposition.rb +13 -0
  34. data/lib/expressir/model/interface.rb +2 -2
  35. data/lib/expressir/model/literals/binary.rb +1 -1
  36. data/lib/expressir/model/literals/integer.rb +1 -1
  37. data/lib/expressir/model/literals/logical.rb +1 -1
  38. data/lib/expressir/model/literals/real.rb +1 -1
  39. data/lib/expressir/model/literals/string.rb +1 -1
  40. data/lib/expressir/model/model_element.rb +67 -0
  41. data/lib/expressir/model/parameter.rb +3 -1
  42. data/lib/expressir/model/procedure.rb +33 -39
  43. data/lib/expressir/model/renamed_ref.rb +1 -1
  44. data/lib/expressir/model/repository.rb +3 -3
  45. data/lib/expressir/model/rule.rb +35 -38
  46. data/lib/expressir/model/schema.rb +35 -46
  47. data/lib/expressir/model/scope.rb +49 -3
  48. data/lib/expressir/model/statements/alias.rb +4 -2
  49. data/lib/expressir/model/statements/assignment.rb +1 -1
  50. data/lib/expressir/model/statements/call.rb +2 -2
  51. data/lib/expressir/model/statements/case.rb +2 -2
  52. data/lib/expressir/model/statements/case_action.rb +2 -2
  53. data/lib/expressir/model/statements/compound.rb +2 -2
  54. data/lib/expressir/model/statements/escape.rb +1 -1
  55. data/lib/expressir/model/statements/if.rb +3 -3
  56. data/lib/expressir/model/statements/null.rb +1 -1
  57. data/lib/expressir/model/statements/repeat.rb +4 -2
  58. data/lib/expressir/model/statements/return.rb +1 -1
  59. data/lib/expressir/model/statements/skip.rb +1 -1
  60. data/lib/expressir/model/subtype_constraint.rb +3 -1
  61. data/lib/expressir/model/type.rb +13 -3
  62. data/lib/expressir/model/types/aggregate.rb +3 -1
  63. data/lib/expressir/model/types/array.rb +1 -1
  64. data/lib/expressir/model/types/bag.rb +1 -1
  65. data/lib/expressir/model/types/binary.rb +1 -1
  66. data/lib/expressir/model/types/boolean.rb +1 -1
  67. data/lib/expressir/model/types/enumeration.rb +3 -3
  68. data/lib/expressir/model/types/generic.rb +3 -1
  69. data/lib/expressir/model/types/generic_entity.rb +3 -1
  70. data/lib/expressir/model/types/integer.rb +1 -1
  71. data/lib/expressir/model/types/list.rb +1 -1
  72. data/lib/expressir/model/types/logical.rb +1 -1
  73. data/lib/expressir/model/types/number.rb +1 -1
  74. data/lib/expressir/model/types/real.rb +1 -1
  75. data/lib/expressir/model/types/select.rb +3 -3
  76. data/lib/expressir/model/types/set.rb +1 -1
  77. data/lib/expressir/model/types/string.rb +1 -1
  78. data/lib/expressir/model/unique.rb +4 -2
  79. data/lib/expressir/model/variable.rb +3 -1
  80. data/lib/expressir/model/where.rb +3 -1
  81. data/lib/expressir/version.rb +1 -1
  82. data/original/examples/syntax/remark.exp +64 -20
  83. data/original/examples/syntax/remark_formatted.exp +63 -24
  84. data/original/examples/syntax/simple.exp +3 -0
  85. data/original/examples/syntax/source.exp +16 -0
  86. data/original/examples/syntax/syntax.exp +194 -181
  87. data/original/examples/syntax/syntax_formatted.exp +354 -787
  88. data/rakelib/cross-ruby.rake +308 -0
  89. data/spec/expressir/express_exp/head_source_spec.rb +38 -0
  90. data/spec/expressir/express_exp/parse_multiple_spec.rb +32 -0
  91. data/spec/expressir/express_exp/parse_remark_spec.rb +122 -56
  92. data/spec/expressir/express_exp/parse_syntax_spec.rb +1619 -1569
  93. data/spec/expressir/express_exp/source_spec.rb +29 -0
  94. data/spec/expressir/model/model_element_spec.rb +59 -0
  95. data/spec/expressir/model/{find_spec.rb → scope_spec.rb} +20 -7
  96. metadata +45 -21
@@ -1,10 +1,15 @@
1
- require 'express_parser'
1
+ begin
2
+ RUBY_VERSION =~ /(\d+\.\d+)/
3
+ require_relative "#{$1}/express_parser"
4
+ rescue LoadError
5
+ require_relative "express_parser"
6
+ end
2
7
  require 'expressir/express_exp/visitor'
3
8
 
4
9
  module Expressir
5
10
  module ExpressExp
6
11
  class Parser
7
- def self.from_exp(file)
12
+ def self.from_file(file)
8
13
  input = File.read(file)
9
14
 
10
15
  =begin
@@ -31,6 +36,19 @@ module Expressir
31
36
 
32
37
  repo
33
38
  end
39
+
40
+ def self.from_files(files)
41
+ schemas = files.map{|file| self.from_file(file).schemas}.flatten
42
+
43
+ Model::Repository.new({
44
+ schemas: schemas
45
+ })
46
+ end
47
+
48
+ # deprecated
49
+ def self.from_exp(file)
50
+ self.from_file(file)
51
+ end
34
52
  end
35
53
  end
36
54
  end
@@ -1,9 +1,12 @@
1
- require 'express_parser'
2
- require 'expressir/model'
1
+ begin
2
+ RUBY_VERSION =~ /(\d+\.\d+)/
3
+ require_relative "#{$1}/express_parser"
4
+ rescue LoadError
5
+ require_relative "express_parser"
6
+ end
7
+ require "expressir/model"
8
+ require "set"
3
9
 
4
- # static shorthands are unwrapped
5
- # - entity attributes, function/procedure parameters, local variables
6
- #
7
10
  # reference type is not recognized
8
11
  # see note in A.1.5 Interpreted identifiers
9
12
  # > It is expected that identifiers matching these syntax rules are known to an implementation.
@@ -20,6 +23,13 @@ require 'expressir/model'
20
23
  # > A syntactic construct such as ARRAY[1:3] OF REAL satisfies two syntactic productions —
21
24
  # > aggregation_type and general_aggregation_type. It is considered to be instantiable no matter which
22
25
  # > production it is required to satisfy in the syntax.
26
+ #
27
+ # static shorthands are unwrapped
28
+ # - entity attributes, function/procedure parameters, local variables
29
+ #
30
+ # all access to ctx members must happen before calling other visitor code
31
+ # - prevents segfault in ANTLR4 C++ runtime, not sure why they are caused
32
+ # - e.g. see visit_schema_decl
23
33
 
24
34
  module Expressir
25
35
  module ExpressExp
@@ -28,91 +38,148 @@ module Expressir
28
38
 
29
39
  def initialize(tokens)
30
40
  @tokens = tokens
31
- @attached_remarks = Set.new
41
+ @attached_remark_tokens = ::Set.new
32
42
 
33
43
  super()
34
44
  end
35
45
 
36
46
  def visit(ctx)
37
47
  result = super(ctx)
48
+ attach_source(ctx, result)
38
49
  attach_parent(ctx, result)
39
50
  attach_remarks(ctx, result)
40
51
  result
41
52
  end
42
53
 
43
- def visit_if(ctx)
44
- visit(ctx) if ctx
54
+ def visit_if(ctx, default = nil)
55
+ if ctx
56
+ visit(ctx)
57
+ else
58
+ default
59
+ end
45
60
  end
46
61
 
47
62
  def visit_if_map(ctx)
48
- ctx.map{|ctx2| visit(ctx2)} if ctx
63
+ if ctx
64
+ ctx.map{|ctx2| visit(ctx2)}
65
+ else
66
+ []
67
+ end
49
68
  end
50
69
 
51
70
  def visit_if_map_flatten(ctx)
52
- ctx.map{|ctx2| visit(ctx2)}.flatten if ctx
71
+ if ctx
72
+ ctx.map{|ctx2| visit(ctx2)}.flatten
73
+ else
74
+ []
75
+ end
76
+ end
77
+
78
+ def get_tokens_source(tokens)
79
+ if tokens.last.text == '<EOF>'
80
+ tokens.pop
81
+ end
82
+
83
+ tokens.map{|x| x.text}.join('').force_encoding('UTF-8')
84
+ end
85
+
86
+ def get_tokens(ctx)
87
+ start_index, stop_index = if ctx.instance_of? ::ExpressParser::SyntaxContext
88
+ [0, @tokens.size - 1]
89
+ else
90
+ [ctx.start.token_index, ctx.stop.token_index]
91
+ end
92
+
93
+ @tokens[start_index..stop_index]
94
+ end
95
+
96
+ def get_head_tokens(ctx)
97
+ start_index, stop_index = if ctx.instance_of? ::ExpressParser::SchemaDeclContext
98
+ start_index = ctx.start.token_index
99
+ stop_index = if ctx.schema_body.interface_specification.length > 0
100
+ ctx.schema_body.interface_specification.last.stop.token_index
101
+ elsif ctx.schema_version_id
102
+ ctx.schema_version_id.stop.token_index + 1
103
+ else
104
+ ctx.schema_id.stop.token_index + 1
105
+ end
106
+
107
+ [start_index, stop_index]
108
+ end
109
+
110
+ if start_index and stop_index
111
+ @tokens[start_index..stop_index]
112
+ end
113
+ end
114
+
115
+ def attach_source(ctx, node)
116
+ if node.class.method_defined? :source
117
+ tokens = get_tokens(ctx)
118
+ node.source = get_tokens_source(tokens)
119
+ end
120
+
121
+ if node.class.method_defined? :head_source
122
+ tokens = get_head_tokens(ctx)
123
+ node.head_source = get_tokens_source(tokens)
124
+ end
53
125
  end
54
126
 
55
127
  def attach_parent(ctx, node)
56
128
  if node.class.method_defined? :children
57
129
  node.children.each do |child_node|
58
- if child_node.class.method_defined? :parent
130
+ if child_node.class.method_defined? :parent and !child_node.parent
59
131
  child_node.parent = node
60
132
  end
61
133
  end
62
134
  end
63
135
  end
64
136
 
65
- def attach_remarks(ctx, node)
66
- # get remark tokens
67
- start_index, stop_index = if ctx.instance_of? ::ExpressParser::SyntaxContext
68
- [0, @tokens.size - 1]
69
- else
70
- [ctx.start.token_index, ctx.stop.token_index]
137
+ def find_remark_target(node, path)
138
+ current_node = node
139
+ target_node = nil
140
+
141
+ if current_node.class.method_defined? :find_or_create
142
+ target_node = current_node.find_or_create(path)
143
+ end
144
+ while !target_node and current_node.class.method_defined? :parent and current_node.parent.class.method_defined? :find_or_create
145
+ current_node = current_node.parent
146
+ target_node = current_node.find_or_create(path)
71
147
  end
72
- # puts [start_index, stop_index, ctx.class].inspect
73
-
74
- remark_tokens = @tokens[start_index..stop_index].select{|x| x.channel == REMARK_CHANNEL}
75
- if remark_tokens
76
- remark_tokens.each do |remark_token|
77
- remark_text = remark_token.text
78
-
79
- # check if it is tagged remark
80
- match = if remark_text.start_with?('--')
81
- remark_text[2..-1].match(/^"([^"]*)"(.*)$/)
82
- elsif remark_text.start_with?('(*') and remark_text.end_with?('*)')
83
- remark_text[2..-3].match(/^"([^"]*)"(.*)$/m)
84
- end
85
- if !match
86
- next
87
- end
88
148
 
89
- # don't attach already attached tagged remark
90
- if @attached_remarks.include? remark_token
91
- next
92
- end
149
+ target_node
150
+ end
93
151
 
94
- # attach tagged remark
95
- remark_tag = match[1]
96
- remark_content = match[2].strip
152
+ def attach_remarks(ctx, node)
153
+ remark_tokens = get_tokens(ctx).select{|x| x.channel == REMARK_CHANNEL}
97
154
 
98
- target_node = nil
99
- current_node = node
100
- if current_node.class.method_defined? :find
101
- target_node = current_node.find(remark_tag)
102
- end
103
- while !target_node and current_node.class.method_defined? :parent and current_node.parent.class.method_defined? :find
104
- current_node = current_node.parent
105
- target_node = current_node.find(remark_tag)
106
- end
155
+ # skip already attached remarks
156
+ remark_tokens = remark_tokens.select{|x| !@attached_remark_tokens.include?(x)}
107
157
 
108
- if target_node
109
- target_node.remarks ||= []
110
- target_node.remarks << remark_content
158
+ # parse remarks, find remark targets
159
+ tagged_remark_tokens = remark_tokens.map do |remark_token|
160
+ _, remark_tag, remark_text = if remark_token.text.start_with?('--')
161
+ remark_token.text.match(/^--"([^"]*)"(.*)$/).to_a
162
+ else
163
+ remark_token.text.match(/^\(\*"([^"]*)"(.*)\*\)$/m).to_a
164
+ end
111
165
 
112
- # mark remark as attached, so that it is not attached again at higher nesting level
113
- @attached_remarks << remark_token
114
- end
166
+ if remark_tag
167
+ remark_target = find_remark_target(node, remark_tag)
168
+ end
169
+ if remark_text
170
+ remark_text = remark_text.strip.force_encoding('UTF-8')
115
171
  end
172
+
173
+ [remark_token, remark_target, remark_text]
174
+ end.select{|x| x[1]}
175
+
176
+ tagged_remark_tokens.each do |remark_token, remark_target, remark_text|
177
+ # attach remark
178
+ remark_target.remarks ||= []
179
+ remark_target.remarks << remark_text
180
+
181
+ # mark remark as attached, so that it is not attached again at higher nesting level
182
+ @attached_remark_tokens << remark_token
116
183
  end
117
184
  end
118
185
 
@@ -365,8 +432,8 @@ module Expressir
365
432
 
366
433
  bound1 = visit_if(ctx__bound_spec__bound1)
367
434
  bound2 = visit_if(ctx__bound_spec__bound2)
368
- optional = !!ctx__OPTIONAL
369
- unique = !!ctx__UNIQUE
435
+ optional = ctx__OPTIONAL && true
436
+ unique = ctx__UNIQUE && true
370
437
  base_type = visit_if(ctx__instantiable_type)
371
438
 
372
439
  Model::Types::Array.new({
@@ -450,7 +517,7 @@ module Expressir
450
517
  ctx__width_spec__FIXED = ctx__width_spec&.FIXED
451
518
 
452
519
  width = visit_if(ctx__width_spec__width)
453
- fixed = !!ctx__width_spec__FIXED
520
+ fixed = ctx__width_spec__FIXED && true
454
521
 
455
522
  Model::Types::Binary.new({
456
523
  width: width,
@@ -702,7 +769,7 @@ module Expressir
702
769
  ctx__entity_body__where_clause = ctx__entity_body&.where_clause
703
770
 
704
771
  id = visit_if(ctx__entity_head__entity_id)
705
- abstract = !!(ctx__entity_head__subsuper__supertype_constraint__abstract_entity_declaration || ctx__entity_head__subsuper__supertype_constraint__abstract_supertype_declaration)
772
+ abstract = (ctx__entity_head__subsuper__supertype_constraint__abstract_entity_declaration || ctx__entity_head__subsuper__supertype_constraint__abstract_supertype_declaration) && true
706
773
  supertype_expression = visit_if(ctx__entity_head__subsuper__supertype_constraint__abstract_supertype_declaration || ctx__entity_head__subsuper__supertype_constraint__supertype_rule)
707
774
  subtype_of = visit_if(ctx__entity_head__subsuper__subtype_declaration)
708
775
  attributes = [
@@ -710,8 +777,8 @@ module Expressir
710
777
  *visit_if(ctx__entity_body__derive_clause),
711
778
  *visit_if(ctx__entity_body__inverse_clause)
712
779
  ]
713
- unique = visit_if(ctx__entity_body__unique_clause)
714
- where = visit_if(ctx__entity_body__where_clause)
780
+ unique = visit_if(ctx__entity_body__unique_clause, [])
781
+ where = visit_if(ctx__entity_body__where_clause, [])
715
782
 
716
783
  Model::Entity.new({
717
784
  id: id,
@@ -784,10 +851,10 @@ module Expressir
784
851
  ctx__enumeration_extension__type_ref = ctx__enumeration_extension&.type_ref
785
852
  ctx__enumeration_extension__enumeration_items = ctx__enumeration_extension&.enumeration_items
786
853
 
787
- extensible = !!ctx__EXTENSIBLE
788
- items = visit_if(ctx__enumeration_items)
854
+ extensible = ctx__EXTENSIBLE && true
855
+ items = visit_if(ctx__enumeration_items, [])
789
856
  extension_type = visit_if(ctx__enumeration_extension__type_ref)
790
- extension_items = visit_if(ctx__enumeration_extension__enumeration_items)
857
+ extension_items = visit_if(ctx__enumeration_extension__enumeration_items, [])
791
858
 
792
859
  Model::Types::Enumeration.new({
793
860
  extensible: extensible,
@@ -807,7 +874,7 @@ module Expressir
807
874
  ctx__parameter_type = ctx.parameter_type
808
875
 
809
876
  attributes = visit_if_map(ctx__attribute_decl)
810
- optional = !!ctx__OPTIONAL
877
+ optional = ctx__OPTIONAL && true
811
878
  type = visit_if(ctx__parameter_type)
812
879
 
813
880
  attributes.map do |attribute|
@@ -881,7 +948,7 @@ module Expressir
881
948
  ctx__actual_parameter_list = ctx.actual_parameter_list
882
949
 
883
950
  ref = visit_if(ctx__built_in_function || ctx__function_ref)
884
- parameters = visit_if(ctx__actual_parameter_list)
951
+ parameters = visit_if(ctx__actual_parameter_list, [])
885
952
 
886
953
  Model::Expressions::Call.new({
887
954
  ref: ref,
@@ -904,15 +971,24 @@ module Expressir
904
971
  parameters = visit_if_map_flatten(ctx__function_head__formal_parameter)
905
972
  return_type = visit_if(ctx__function_head__parameter_type)
906
973
  declarations = visit_if_map(ctx__algorithm_head__declaration)
907
- constants = visit_if(ctx__algorithm_head__constant_decl)
908
- variables = visit_if(ctx__algorithm_head__local_decl)
974
+ types = declarations.select{|x| x.instance_of? Model::Type}
975
+ entities = declarations.select{|x| x.instance_of? Model::Entity}
976
+ subtype_constraints = declarations.select{|x| x.instance_of? Model::SubtypeConstraint}
977
+ functions = declarations.select{|x| x.instance_of? Model::Function}
978
+ procedures = declarations.select{|x| x.instance_of? Model::Procedure}
979
+ constants = visit_if(ctx__algorithm_head__constant_decl, [])
980
+ variables = visit_if(ctx__algorithm_head__local_decl, [])
909
981
  statements = visit_if_map(ctx__stmt)
910
982
 
911
983
  Model::Function.new({
912
984
  id: id,
913
985
  parameters: parameters,
914
986
  return_type: return_type,
915
- declarations: declarations,
987
+ types: types,
988
+ entities: entities,
989
+ subtype_constraints: subtype_constraints,
990
+ functions: functions,
991
+ procedures: procedures,
916
992
  constants: constants,
917
993
  variables: variables,
918
994
  statements: statements
@@ -957,8 +1033,8 @@ module Expressir
957
1033
 
958
1034
  bound1 = visit_if(ctx__bound_spec__bound1)
959
1035
  bound2 = visit_if(ctx__bound_spec__bound2)
960
- optional = !!ctx__OPTIONAL
961
- unique = !!ctx__UNIQUE
1036
+ optional = ctx__OPTIONAL && true
1037
+ unique = ctx__UNIQUE && true
962
1038
  base_type = visit_if(ctx__parameter_type)
963
1039
 
964
1040
  Model::Types::Array.new({
@@ -996,7 +1072,7 @@ module Expressir
996
1072
 
997
1073
  bound1 = visit_if(ctx__bound_spec__bound1)
998
1074
  bound2 = visit_if(ctx__bound_spec__bound2)
999
- unique = !!ctx__UNIQUE
1075
+ unique = ctx__UNIQUE && true
1000
1076
  base_type = visit_if(ctx__parameter_type)
1001
1077
 
1002
1078
  Model::Types::List.new({
@@ -1071,8 +1147,8 @@ module Expressir
1071
1147
  ctx__if_stmt_else_statements = ctx.if_stmt_else_statements
1072
1148
 
1073
1149
  expression = visit_if(ctx__logical_expression)
1074
- statements = visit_if(ctx__if_stmt_statements)
1075
- else_statements = visit_if(ctx__if_stmt_else_statements)
1150
+ statements = visit_if(ctx__if_stmt_statements, [])
1151
+ else_statements = visit_if(ctx__if_stmt_else_statements, [])
1076
1152
 
1077
1153
  Model::Statements::If.new({
1078
1154
  expression: expression,
@@ -1286,7 +1362,7 @@ module Expressir
1286
1362
 
1287
1363
  bound1 = visit_if(ctx__bound_spec__bound1)
1288
1364
  bound2 = visit_if(ctx__bound_spec__bound2)
1289
- unique = !!ctx__UNIQUE
1365
+ unique = ctx__UNIQUE && true
1290
1366
  base_type = visit_if(ctx__instantiable_type)
1291
1367
 
1292
1368
  Model::Types::List.new({
@@ -1502,7 +1578,7 @@ module Expressir
1502
1578
  ctx__actual_parameter_list = ctx.actual_parameter_list
1503
1579
 
1504
1580
  ref = visit_if(ctx__built_in_procedure || ctx__procedure_ref)
1505
- parameters = visit_if(ctx__actual_parameter_list)
1581
+ parameters = visit_if(ctx__actual_parameter_list, [])
1506
1582
 
1507
1583
  Model::Statements::Call.new({
1508
1584
  ref: ref,
@@ -1523,14 +1599,23 @@ module Expressir
1523
1599
  id = visit_if(ctx__procedure_head__procedure_id)
1524
1600
  parameters = visit_if_map_flatten(ctx__procedure_head__procedure_head_parameter)
1525
1601
  declarations = visit_if_map(ctx__algorithm_head__declaration)
1526
- constants = visit_if(ctx__algorithm_head__constant_decl)
1527
- variables = visit_if(ctx__algorithm_head__local_decl)
1602
+ types = declarations.select{|x| x.instance_of? Model::Type}
1603
+ entities = declarations.select{|x| x.instance_of? Model::Entity}
1604
+ subtype_constraints = declarations.select{|x| x.instance_of? Model::SubtypeConstraint}
1605
+ functions = declarations.select{|x| x.instance_of? Model::Function}
1606
+ procedures = declarations.select{|x| x.instance_of? Model::Procedure}
1607
+ constants = visit_if(ctx__algorithm_head__constant_decl, [])
1608
+ variables = visit_if(ctx__algorithm_head__local_decl, [])
1528
1609
  statements = visit_if_map(ctx__stmt)
1529
1610
 
1530
1611
  Model::Procedure.new({
1531
1612
  id: id,
1532
1613
  parameters: parameters,
1533
- declarations: declarations,
1614
+ types: types,
1615
+ entities: entities,
1616
+ subtype_constraints: subtype_constraints,
1617
+ functions: functions,
1618
+ procedures: procedures,
1534
1619
  constants: constants,
1535
1620
  variables: variables,
1536
1621
  statements: statements
@@ -1609,12 +1694,12 @@ module Expressir
1609
1694
  ctx__logical_expression = ctx.logical_expression
1610
1695
 
1611
1696
  id = visit_if(ctx__variable_id)
1612
- source = visit_if(ctx__aggregate_source)
1697
+ aggregate_source = visit_if(ctx__aggregate_source)
1613
1698
  expression = visit_if(ctx__logical_expression)
1614
1699
 
1615
1700
  Model::Expressions::QueryExpression.new({
1616
1701
  id: id,
1617
- source: source,
1702
+ aggregate_source: aggregate_source,
1618
1703
  expression: expression
1619
1704
  })
1620
1705
  end
@@ -1807,15 +1892,24 @@ module Expressir
1807
1892
  id = visit_if(ctx__rule_head__rule_id)
1808
1893
  applies_to = visit_if_map(ctx__rule_head__entity_ref)
1809
1894
  declarations = visit_if_map(ctx__algorithm_head__declaration)
1810
- constants = visit_if(ctx__algorithm_head__constant_decl)
1811
- variables = visit_if(ctx__algorithm_head__local_decl)
1895
+ types = declarations.select{|x| x.instance_of? Model::Type}
1896
+ entities = declarations.select{|x| x.instance_of? Model::Entity}
1897
+ subtype_constraints = declarations.select{|x| x.instance_of? Model::SubtypeConstraint}
1898
+ functions = declarations.select{|x| x.instance_of? Model::Function}
1899
+ procedures = declarations.select{|x| x.instance_of? Model::Procedure}
1900
+ constants = visit_if(ctx__algorithm_head__constant_decl, [])
1901
+ variables = visit_if(ctx__algorithm_head__local_decl, [])
1812
1902
  statements = visit_if_map(ctx__stmt)
1813
- where = visit_if(ctx__where_clause)
1903
+ where = visit_if(ctx__where_clause, [])
1814
1904
 
1815
1905
  Model::Rule.new({
1816
1906
  id: id,
1817
1907
  applies_to: applies_to,
1818
- declarations: declarations,
1908
+ types: types,
1909
+ entities: entities,
1910
+ subtype_constraints: subtype_constraints,
1911
+ functions: functions,
1912
+ procedures: procedures,
1819
1913
  constants: constants,
1820
1914
  variables: variables,
1821
1915
  statements: statements,
@@ -1861,15 +1955,26 @@ module Expressir
1861
1955
  id = visit_if(ctx__schema_id)
1862
1956
  version = visit_if(ctx__schema_version_id)
1863
1957
  interfaces = visit_if_map(ctx__schema_body__interface_specification)
1864
- constants = visit_if(ctx__schema_body__constant_decl)
1958
+ constants = visit_if(ctx__schema_body__constant_decl, [])
1865
1959
  declarations = visit_if_map(ctx__schema_body__schema_body_declaration)
1960
+ types = declarations.select{|x| x.instance_of? Model::Type}
1961
+ entities = declarations.select{|x| x.instance_of? Model::Entity}
1962
+ subtype_constraints = declarations.select{|x| x.instance_of? Model::SubtypeConstraint}
1963
+ functions = declarations.select{|x| x.instance_of? Model::Function}
1964
+ procedures = declarations.select{|x| x.instance_of? Model::Procedure}
1965
+ rules = declarations.select{|x| x.instance_of? Model::Rule}
1866
1966
 
1867
1967
  Model::Schema.new({
1868
1968
  id: id,
1869
1969
  version: version,
1870
1970
  interfaces: interfaces,
1871
1971
  constants: constants,
1872
- declarations: declarations
1972
+ types: types,
1973
+ entities: entities,
1974
+ subtype_constraints: subtype_constraints,
1975
+ functions: functions,
1976
+ procedures: procedures,
1977
+ rules: rules
1873
1978
  })
1874
1979
  end
1875
1980
 
@@ -1909,11 +2014,11 @@ module Expressir
1909
2014
  ctx__select_extension__type_ref = ctx.select_extension&.type_ref
1910
2015
  ctx__select_extension__select_list = ctx__select_extension&.select_list
1911
2016
 
1912
- extensible = !!ctx__EXTENSIBLE
1913
- generic_entity = !!ctx__GENERIC_ENTITY
1914
- items = visit_if(ctx__select_list)
2017
+ extensible = ctx__EXTENSIBLE && true
2018
+ generic_entity = ctx__GENERIC_ENTITY && true
2019
+ items = visit_if(ctx__select_list, [])
1915
2020
  extension_type = visit_if(ctx__select_extension__type_ref)
1916
- extension_items = visit_if(ctx__select_extension__select_list)
2021
+ extension_items = visit_if(ctx__select_extension__select_list, [])
1917
2022
 
1918
2023
  Model::Types::Select.new({
1919
2024
  extensible: extensible,
@@ -2046,7 +2151,7 @@ module Expressir
2046
2151
  ctx__width_spec__FIXED = ctx__width_spec&.FIXED
2047
2152
 
2048
2153
  width = visit_if(ctx__width_spec__width)
2049
- fixed = !!ctx__width_spec__FIXED
2154
+ fixed = ctx__width_spec__FIXED && true
2050
2155
 
2051
2156
  Model::Types::String.new({
2052
2157
  width: width,
@@ -2079,7 +2184,7 @@ module Expressir
2079
2184
 
2080
2185
  id = visit_if(ctx__subtype_constraint_head__subtype_constraint_id)
2081
2186
  applies_to = visit_if(ctx__subtype_constraint_head__entity_ref)
2082
- abstract = !!ctx__subtype_constraint_body__abstract_supertype
2187
+ abstract = ctx__subtype_constraint_body__abstract_supertype && true
2083
2188
  total_over = visit_if(ctx__subtype_constraint_body__total_over)
2084
2189
  supertype_expression = visit_if(ctx__subtype_constraint_body__supertype_expression)
2085
2190
 
@@ -2215,7 +2320,7 @@ module Expressir
2215
2320
 
2216
2321
  id = visit_if(ctx__type_id)
2217
2322
  type = visit_if(ctx__underlying_type)
2218
- where = visit_if(ctx__where_clause)
2323
+ where = visit_if(ctx__where_clause, [])
2219
2324
 
2220
2325
  Model::Type.new({
2221
2326
  id: id,
@@ -2429,7 +2534,7 @@ module Expressir
2429
2534
  def handle_simple_string_literal(ctx)
2430
2535
  ctx__text = ctx.text
2431
2536
 
2432
- value = ctx__text[1..(ctx__text.length - 2)]
2537
+ value = ctx__text[1..(ctx__text.length - 2)].force_encoding('UTF-8')
2433
2538
 
2434
2539
  Model::Literals::String.new({
2435
2540
  value: value
@@ -2439,7 +2544,7 @@ module Expressir
2439
2544
  def handle_encoded_string_literal(ctx)
2440
2545
  ctx__text = ctx.text
2441
2546
 
2442
- value = ctx__text[1..(ctx__text.length - 2)]
2547
+ value = ctx__text[1..(ctx__text.length - 2)].force_encoding('UTF-8')
2443
2548
 
2444
2549
  Model::Literals::String.new({
2445
2550
  value: value,