expressir 0.2.13-x86-mingw32 → 0.2.18-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/exe/format +56 -11
  3. data/lib/expressir/express_exp/2.4/express_parser.so +0 -0
  4. data/lib/expressir/express_exp/2.5/express_parser.so +0 -0
  5. data/lib/expressir/express_exp/2.6/express_parser.so +0 -0
  6. data/lib/expressir/express_exp/2.7/express_parser.so +0 -0
  7. data/lib/expressir/express_exp/3.0/express_parser.so +0 -0
  8. data/lib/expressir/express_exp/formatter.rb +201 -147
  9. data/lib/expressir/express_exp/hyperlink_formatter.rb +6 -4
  10. data/lib/expressir/express_exp/parser.rb +38 -25
  11. data/lib/expressir/express_exp/schema_head_formatter.rb +2 -7
  12. data/lib/expressir/express_exp/visitor.rb +27 -54
  13. data/lib/expressir/model.rb +2 -1
  14. data/lib/expressir/model/entity.rb +6 -6
  15. data/lib/expressir/model/expressions/query_expression.rb +3 -3
  16. data/lib/expressir/model/function.rb +15 -11
  17. data/lib/expressir/model/informal_proposition.rb +4 -1
  18. data/lib/expressir/model/{renamed_ref.rb → interface_item.rb} +2 -2
  19. data/lib/expressir/model/interfaced_item.rb +26 -0
  20. data/lib/expressir/model/model_element.rb +28 -7
  21. data/lib/expressir/model/procedure.rb +15 -11
  22. data/lib/expressir/model/repository.rb +3 -3
  23. data/lib/expressir/model/rule.rb +16 -12
  24. data/lib/expressir/model/schema.rb +60 -29
  25. data/lib/expressir/model/statements/alias.rb +3 -3
  26. data/lib/expressir/model/statements/repeat.rb +3 -3
  27. data/lib/expressir/model/subtype_constraint.rb +1 -6
  28. data/lib/expressir/model/type.rb +9 -5
  29. data/lib/expressir/version.rb +1 -1
  30. data/original/examples/syntax/multiple.exp +23 -0
  31. data/original/examples/syntax/multiple.yaml +184 -0
  32. data/original/examples/syntax/multiple_formatted.exp +71 -0
  33. data/original/examples/syntax/multiple_hyperlink_formatted.exp +71 -0
  34. data/original/examples/syntax/multiple_schema_head_hyperlink_formatted.exp +13 -0
  35. data/original/examples/syntax/remark.exp +41 -41
  36. data/original/examples/syntax/remark.yaml +446 -0
  37. data/original/examples/syntax/remark_formatted.exp +62 -50
  38. data/original/examples/syntax/{simple.exp → single.exp} +1 -1
  39. data/original/examples/syntax/single.yaml +9 -0
  40. data/original/examples/syntax/single_formatted.exp +6 -0
  41. data/original/examples/syntax/single_formatted.yaml +19 -0
  42. data/original/examples/syntax/single_root_path.yaml +9 -0
  43. data/original/examples/syntax/syntax.exp +29 -19
  44. data/original/examples/syntax/syntax.yaml +3439 -0
  45. data/original/examples/syntax/syntax_formatted.exp +271 -131
  46. data/original/examples/syntax/syntax_schema_head_formatted.exp +18 -0
  47. data/spec/expressir/express_exp/formatter_spec.rb +111 -0
  48. data/spec/expressir/express_exp/parser_spec.rb +121 -0
  49. data/spec/expressir/model/{model_element/find_spec.rb → model_element_spec.rb} +110 -10
  50. metadata +20 -18
  51. data/original/examples/syntax/hyperlink.exp +0 -8
  52. data/original/examples/syntax/hyperlink_formatted.exp +0 -19
  53. data/original/examples/syntax/source.exp +0 -16
  54. data/spec/expressir/express_exp/formatter/remark_spec.rb +0 -28
  55. data/spec/expressir/express_exp/formatter/syntax_spec.rb +0 -28
  56. data/spec/expressir/express_exp/hyperlink_formatter_spec.rb +0 -24
  57. data/spec/expressir/express_exp/parser/head_source_spec.rb +0 -38
  58. data/spec/expressir/express_exp/parser/multiple_spec.rb +0 -32
  59. data/spec/expressir/express_exp/parser/remark_spec.rb +0 -411
  60. data/spec/expressir/express_exp/parser/source_spec.rb +0 -29
  61. data/spec/expressir/express_exp/parser/syntax_spec.rb +0 -3080
  62. data/spec/expressir/express_exp/schema_head_formatter_spec.rb +0 -36
  63. data/spec/expressir/model/model_element/hash_spec.rb +0 -66
@@ -1,13 +1,15 @@
1
- require "expressir/express_exp/formatter"
2
-
3
1
  module Expressir
4
2
  module ExpressExp
5
- class HyperlinkFormatter < Formatter
3
+ module HyperlinkFormatter
6
4
  def format_expressions_simple_reference(node)
7
5
  return node.id if node.parent.is_a? Model::Expressions::AttributeReference
8
6
 
9
7
  # skip hyperlink if target node can't be found
10
- target_node = node.find(node.id)
8
+ target_node = if node.parent.is_a? Model::InterfaceItem
9
+ node.find("#{node.parent.parent.schema.id}.#{node.parent.ref.id}")
10
+ else
11
+ node.find(node.id)
12
+ end
11
13
  return node.id unless target_node
12
14
 
13
15
  # skip hyperlink for implicit scopes
@@ -5,49 +5,62 @@ rescue LoadError
5
5
  require_relative "express_parser"
6
6
  end
7
7
  require 'expressir/express_exp/visitor'
8
-
9
- module Expressir
10
- module ExpressExp
11
- class Parser
12
- def self.from_file(file)
13
- input = File.read(file)
8
+ require 'pathname'
14
9
 
15
10
  =begin
16
- char_stream = Antlr4::Runtime::CharStreams.from_string(input, 'String')
17
- lexer = ::ExpressParser::Lexer.new(char_stream)
18
- token_stream = Antlr4::Runtime::CommonTokenStream.new(lexer)
19
- parser = ::ExpressParser::Parser.new(token_stream)
11
+ char_stream = Antlr4::Runtime::CharStreams.from_string(input, 'String')
12
+ lexer = ::ExpressParser::Lexer.new(char_stream)
13
+ token_stream = Antlr4::Runtime::CommonTokenStream.new(lexer)
14
+ parser = ::ExpressParser::Parser.new(token_stream)
20
15
 
21
- # don't attempt to recover from any parsing error
22
- parser.instance_variable_set(:@_err_handler, Antlr4::Runtime::BailErrorStrategy.new)
16
+ # don't attempt to recover from any parsing error
17
+ parser.instance_variable_set(:@_err_handler, Antlr4::Runtime::BailErrorStrategy.new)
23
18
 
24
- parse_tree = parser.syntax()
19
+ parse_tree = parser.syntax()
25
20
 
26
- visitor = Visitor.new(token_stream)
27
- repo = visitor.visit(parse_tree)
21
+ visitor = Visitor.new(token_stream)
22
+ repo = visitor.visit(parse_tree)
28
23
  =end
29
24
 
25
+ module Expressir
26
+ module ExpressExp
27
+ class Parser
28
+ def self.from_file(file, options = {})
29
+ root_path = options[:root_path]
30
+
31
+ input = File.read(file)
32
+
30
33
  parser = ::ExpressParser::Parser.parse(input)
31
34
 
32
35
  parse_tree = parser.syntax()
33
36
 
34
- visitor = Visitor.new(parser.tokens)
35
- repo = visitor.visit(parse_tree)
37
+ visitor = Visitor.new(parser.tokens, options)
38
+ repository = visitor.visit(parse_tree)
36
39
 
37
- repo
40
+ repository.schemas.each do |schema|
41
+ schema.file = root_path ? Pathname.new(file).relative_path_from(root_path).to_s : File.basename(file)
42
+ end
43
+
44
+ repository
38
45
  end
39
46
 
40
- def self.from_files(files)
41
- schemas = files.map{|file| self.from_file(file).schemas}.flatten
47
+ def self.from_files(files, options = {})
48
+ schemas = files.each_with_index.map do |file, i|
49
+ # start = Time.now
50
+ repository = self.from_file(file, options)
51
+ # STDERR.puts "#{i+1}/#{files.length} #{file} #{Time.now - start}"
52
+ repository.schemas
53
+ end.flatten
42
54
 
43
- Model::Repository.new({
55
+ repository = Model::Repository.new({
44
56
  schemas: schemas
45
57
  })
46
- end
47
58
 
48
- # deprecated
49
- def self.from_exp(file)
50
- self.from_file(file)
59
+ repository.schemas.each do |schema|
60
+ schema.parent = repository
61
+ end
62
+
63
+ repository
51
64
  end
52
65
  end
53
66
  end
@@ -1,13 +1,8 @@
1
- require "expressir/express_exp/formatter"
2
-
3
1
  module Expressir
4
2
  module ExpressExp
5
- class SchemaHeadFormatter < Formatter
3
+ module SchemaHeadFormatter
6
4
  def format_schema(node)
7
- [
8
- "SCHEMA #{node.id}#{node.version ? " #{format(node.version)}" : ""};",
9
- *node.interfaces.map{|x| format(x)}
10
- ].join("\n")
5
+ format_schema_head(node)
11
6
  end
12
7
  end
13
8
  end
@@ -36,8 +36,10 @@ module Expressir
36
36
  class Visitor < ::ExpressParser::Visitor
37
37
  REMARK_CHANNEL = 2
38
38
 
39
- def initialize(tokens)
39
+ def initialize(tokens, options = {})
40
40
  @tokens = tokens
41
+ @include_source = options[:include_source]
42
+
41
43
  @attached_remark_tokens = ::Set.new
42
44
 
43
45
  super()
@@ -45,7 +47,9 @@ module Expressir
45
47
 
46
48
  def visit(ctx)
47
49
  node = super(ctx)
48
- attach_source(ctx, node)
50
+ if @include_source
51
+ attach_source(ctx, node)
52
+ end
49
53
  attach_remarks(ctx, node)
50
54
  node
51
55
  end
@@ -92,35 +96,11 @@ module Expressir
92
96
  @tokens[start_index..stop_index]
93
97
  end
94
98
 
95
- def get_head_tokens(ctx)
96
- start_index, stop_index = if ctx.is_a? ::ExpressParser::SchemaDeclContext
97
- start_index = ctx.start.token_index
98
- stop_index = if ctx.schema_body.interface_specification.length > 0
99
- ctx.schema_body.interface_specification.last.stop.token_index
100
- elsif ctx.schema_version_id
101
- ctx.schema_version_id.stop.token_index + 1
102
- else
103
- ctx.schema_id.stop.token_index + 1
104
- end
105
-
106
- [start_index, stop_index]
107
- end
108
-
109
- if start_index and stop_index
110
- @tokens[start_index..stop_index]
111
- end
112
- end
113
-
114
99
  def attach_source(ctx, node)
115
100
  if node.class.method_defined? :source
116
101
  tokens = get_tokens(ctx)
117
102
  node.source = get_tokens_source(tokens)
118
103
  end
119
-
120
- if node.class.method_defined? :head_source
121
- tokens = get_head_tokens(ctx)
122
- node.head_source = get_tokens_source(tokens)
123
- end
124
104
  end
125
105
 
126
106
  def find_remark_target(node, path)
@@ -145,6 +125,7 @@ module Expressir
145
125
  id: informal_proposition_id
146
126
  })
147
127
  target_node.informal_propositions << informal_proposition
128
+ target_node.reset_children_by_id
148
129
  informal_proposition.parent = target_node
149
130
  informal_proposition
150
131
  end
@@ -691,9 +672,9 @@ module Expressir
691
672
  expression = visit_if(ctx__expression)
692
673
 
693
674
  Model::Attribute.new({
694
- id: attribute.id,
675
+ id: attribute.id, # reuse
695
676
  kind: Model::Attribute::DERIVED,
696
- supertype_attribute: attribute.supertype_attribute,
677
+ supertype_attribute: attribute.supertype_attribute, # reuse
697
678
  type: type,
698
679
  expression: expression
699
680
  })
@@ -879,9 +860,9 @@ module Expressir
879
860
 
880
861
  attributes.map do |attribute|
881
862
  Model::Attribute.new({
882
- id: attribute.id,
863
+ id: attribute.id, # reuse
883
864
  kind: Model::Attribute::EXPLICIT,
884
- supertype_attribute: attribute.supertype_attribute,
865
+ supertype_attribute: attribute.supertype_attribute, # reuse
885
866
  optional: optional,
886
867
  type: type
887
868
  })
@@ -1306,9 +1287,9 @@ module Expressir
1306
1287
  end
1307
1288
 
1308
1289
  Model::Attribute.new({
1309
- id: attribute.id,
1290
+ id: attribute.id, # reuse
1310
1291
  kind: Model::Attribute::INVERSE,
1311
- supertype_attribute: attribute.supertype_attribute,
1292
+ supertype_attribute: attribute.supertype_attribute, # reuse
1312
1293
  type: type,
1313
1294
  expression: expression
1314
1295
  })
@@ -1490,14 +1471,10 @@ module Expressir
1490
1471
  ref = visit_if(ctx__named_types)
1491
1472
  id = visit_if(ctx__entity_id || ctx__type_id)
1492
1473
 
1493
- if id
1494
- Model::RenamedRef.new({
1495
- ref: ref,
1496
- id: id
1497
- })
1498
- else
1499
- ref
1500
- end
1474
+ Model::InterfaceItem.new({
1475
+ ref: ref,
1476
+ id: id
1477
+ })
1501
1478
  end
1502
1479
 
1503
1480
  def visit_null_stmt(ctx)
@@ -1674,9 +1651,9 @@ module Expressir
1674
1651
  ref: Model::Expressions::SimpleReference.new({
1675
1652
  id: id
1676
1653
  }),
1677
- entity: group_reference.entity
1654
+ entity: group_reference.entity # reuse
1678
1655
  }),
1679
- attribute: attribute_reference.attribute
1656
+ attribute: attribute_reference.attribute # reuse
1680
1657
  })
1681
1658
  end
1682
1659
 
@@ -1844,14 +1821,10 @@ module Expressir
1844
1821
  ref = visit_if(ctx__resource_ref)
1845
1822
  id = visit_if(ctx__rename_id)
1846
1823
 
1847
- if id
1848
- Model::RenamedRef.new({
1849
- ref: ref,
1850
- id: id
1851
- })
1852
- else
1853
- ref
1854
- end
1824
+ Model::InterfaceItem.new({
1825
+ ref: ref,
1826
+ id: id
1827
+ })
1855
1828
  end
1856
1829
 
1857
1830
  def visit_resource_ref(ctx)
@@ -1957,8 +1930,8 @@ module Expressir
1957
1930
  entities = declarations.select{|x| x.is_a? Model::Entity}
1958
1931
  subtype_constraints = declarations.select{|x| x.is_a? Model::SubtypeConstraint}
1959
1932
  functions = declarations.select{|x| x.is_a? Model::Function}
1960
- procedures = declarations.select{|x| x.is_a? Model::Procedure}
1961
1933
  rules = declarations.select{|x| x.is_a? Model::Rule}
1934
+ procedures = declarations.select{|x| x.is_a? Model::Procedure}
1962
1935
 
1963
1936
  Model::Schema.new({
1964
1937
  id: id,
@@ -1969,8 +1942,8 @@ module Expressir
1969
1942
  entities: entities,
1970
1943
  subtype_constraints: subtype_constraints,
1971
1944
  functions: functions,
1972
- procedures: procedures,
1973
- rules: rules
1945
+ rules: rules,
1946
+ procedures: procedures
1974
1947
  })
1975
1948
  end
1976
1949
 
@@ -2181,7 +2154,7 @@ module Expressir
2181
2154
  id = visit_if(ctx__subtype_constraint_head__subtype_constraint_id)
2182
2155
  applies_to = visit_if(ctx__subtype_constraint_head__entity_ref)
2183
2156
  abstract = ctx__subtype_constraint_body__abstract_supertype && true
2184
- total_over = visit_if(ctx__subtype_constraint_body__total_over)
2157
+ total_over = visit_if(ctx__subtype_constraint_body__total_over, [])
2185
2158
  supertype_expression = visit_if(ctx__subtype_constraint_body__supertype_expression)
2186
2159
 
2187
2160
  Model::SubtypeConstraint.new({
@@ -9,9 +9,10 @@ require 'expressir/model/enumeration_item'
9
9
  require 'expressir/model/function'
10
10
  require 'expressir/model/informal_proposition'
11
11
  require 'expressir/model/interface'
12
+ require 'expressir/model/interface_item'
13
+ require 'expressir/model/interfaced_item'
12
14
  require 'expressir/model/parameter'
13
15
  require 'expressir/model/procedure'
14
- require 'expressir/model/renamed_ref'
15
16
  require 'expressir/model/repository'
16
17
  require 'expressir/model/rule'
17
18
  require 'expressir/model/schema'
@@ -28,12 +28,12 @@ module Expressir
28
28
  end
29
29
 
30
30
  def children
31
- items = []
32
- items.push(*@attributes)
33
- items.push(*@unique)
34
- items.push(*@where)
35
- items.push(*@informal_propositions)
36
- items
31
+ [
32
+ *attributes,
33
+ *unique,
34
+ *where,
35
+ *informal_propositions
36
+ ]
37
37
  end
38
38
  end
39
39
  end
@@ -19,9 +19,9 @@ module Expressir
19
19
  end
20
20
 
21
21
  def children
22
- items = []
23
- items.push(self)
24
- items
22
+ [
23
+ self
24
+ ]
25
25
  end
26
26
  end
27
27
  end
@@ -33,18 +33,22 @@ module Expressir
33
33
  super
34
34
  end
35
35
 
36
+ def enumeration_items
37
+ types.flat_map{|x| x.enumeration_items}
38
+ end
39
+
36
40
  def children
37
- items = []
38
- items.push(*@parameters)
39
- items.push(*@types)
40
- items.push(*@types.flat_map{|x| x.type.is_a?(Expressir::Model::Types::Enumeration) ? x.type.items : []})
41
- items.push(*@entities)
42
- items.push(*@subtype_constraints)
43
- items.push(*@functions)
44
- items.push(*@procedures)
45
- items.push(*@constants)
46
- items.push(*@variables)
47
- items
41
+ [
42
+ *parameters,
43
+ *types,
44
+ *enumeration_items,
45
+ *entities,
46
+ *subtype_constraints,
47
+ *functions,
48
+ *procedures,
49
+ *constants,
50
+ *variables
51
+ ]
48
52
  end
49
53
  end
50
54
  end
@@ -3,10 +3,13 @@ module Expressir
3
3
  class InformalProposition < ModelElement
4
4
  include Identifier
5
5
 
6
+ undef :source
7
+ undef :source=
8
+
6
9
  def initialize(options = {})
7
10
  @id = options[:id]
8
11
  @remarks = options.fetch(:remarks, [])
9
- @source = options[:source]
12
+ # @source = options[:source]
10
13
 
11
14
  super
12
15
  end
@@ -1,11 +1,11 @@
1
1
  module Expressir
2
2
  module Model
3
- class RenamedRef < ModelElement
3
+ class InterfaceItem < ModelElement
4
4
  attr_accessor :ref
5
5
  attr_accessor :id
6
6
 
7
7
  def initialize(options = {})
8
- @ref = options[:ref]
8
+ @ref = options[:ref]
9
9
  @id = options[:id]
10
10
 
11
11
  super
@@ -0,0 +1,26 @@
1
+ module Expressir
2
+ module Model
3
+ class InterfacedItem < ModelElement
4
+ include Identifier
5
+
6
+ undef :source
7
+ undef :source=
8
+
9
+ attr_accessor :base_item
10
+
11
+ def initialize(options = {})
12
+ @id = options[:id]
13
+ @remarks = options.fetch(:remarks, [])
14
+ # @source = options[:source]
15
+
16
+ @base_item = options[:base_item]
17
+
18
+ super
19
+ end
20
+
21
+ def path
22
+ base_item.path
23
+ end
24
+ end
25
+ end
26
+ end
@@ -2,6 +2,9 @@ module Expressir
2
2
  module Model
3
3
  class ModelElement
4
4
  CLASS_KEY = '_class'
5
+ FILE_KEY = 'file'
6
+ PARENT_KEY = 'parent'
7
+ CHILDREN_BY_ID_KEY = 'children_by_id'
5
8
  SOURCE_KEY = 'source'
6
9
 
7
10
  attr_accessor :parent
@@ -10,6 +13,11 @@ module Expressir
10
13
  attach_parent_to_children
11
14
  end
12
15
 
16
+ def model_instance_variables
17
+ skip_variables = [FILE_KEY, PARENT_KEY, CHILDREN_BY_ID_KEY].map{|x| "@#{x}".to_sym}
18
+ instance_variables.select{|x| !skip_variables.include?(x)}
19
+ end
20
+
13
21
  def path
14
22
  return id if is_a? Statements::Alias or is_a? Statements::Repeat or is_a? Expressions::QueryExpression
15
23
 
@@ -28,7 +36,7 @@ module Expressir
28
36
  end
29
37
 
30
38
  def attach_parent_to_children
31
- instance_variables.select{|x| x != :@parent}.each do |variable|
39
+ model_instance_variables.each do |variable|
32
40
  value = instance_variable_get(variable)
33
41
 
34
42
  if value.is_a? Array
@@ -57,7 +65,7 @@ module Expressir
57
65
  # find in current scope
58
66
  current_node = current_scope
59
67
  path_parts.each do |current_path|
60
- current_node = current_node.children.find{|x| x.id and x.id.downcase == current_path}
68
+ current_node = current_node.children_by_id[current_path]
61
69
  break unless current_node
62
70
  end
63
71
  target_node = current_node
@@ -75,19 +83,32 @@ module Expressir
75
83
  []
76
84
  end
77
85
 
86
+ def children_by_id
87
+ @children_by_id ||= children.select{|x| x.id}.map{|x| [x.id.downcase, x]}.to_h
88
+ end
89
+
90
+ def reset_children_by_id
91
+ @children_by_id = nil
92
+ end
93
+
78
94
  def to_hash(options = {})
79
- skip_empty = options[:skip_empty]
95
+ root_path = options[:root_path]
80
96
  formatter = options[:formatter]
97
+ include_empty = options[:include_empty] || !options[:skip_empty] # TODO: remove skip_empty
81
98
 
82
99
  hash = {}
83
100
  hash[CLASS_KEY] = self.class.name
101
+ if self.is_a? Schema and file
102
+ hash[FILE_KEY] = root_path ? File.expand_path("#{root_path}/#{file}") : file
103
+ end
84
104
 
85
- instance_variables.select{|x| x != :@parent}.each_with_object(hash) do |variable, result|
105
+ model_instance_variables.each_with_object(hash) do |variable, result|
86
106
  key = variable.to_s.sub(/^@/, '')
87
107
  value = instance_variable_get(variable)
108
+ empty = value.nil? || (value.is_a?(Array) && value.count == 0)
88
109
 
89
- # skip empty values (nil, empty array)
90
- if !skip_empty or !(value.nil? or (value.is_a? Array and value.count == 0))
110
+ # skip empty values
111
+ if !empty or include_empty
91
112
  result[key] = if value.is_a? Array
92
113
  value.map do |value|
93
114
  if value.is_a? ModelElement
@@ -104,7 +125,7 @@ module Expressir
104
125
  end
105
126
  end
106
127
 
107
- if formatter
128
+ if self.class.method_defined? :source and formatter
108
129
  hash[SOURCE_KEY] = formatter.format(self)
109
130
  end
110
131