expressir 1.3.0.pre.1-x86_64-linux-gnu

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 (146) hide show
  1. checksums.yaml +7 -0
  2. data/.cross_rubies +20 -0
  3. data/.github/workflows/rake.yml +312 -0
  4. data/.github/workflows/release.yml +124 -0
  5. data/.gitignore +23 -0
  6. data/.gitmodules +6 -0
  7. data/.hound.yml +3 -0
  8. data/.rspec +2 -0
  9. data/.rubocop.yml +18 -0
  10. data/.yardopts +11 -0
  11. data/Gemfile +4 -0
  12. data/README.adoc +155 -0
  13. data/Rakefile +17 -0
  14. data/bin/console +11 -0
  15. data/bin/rspec +29 -0
  16. data/bin/setup +8 -0
  17. data/docs/development.md +90 -0
  18. data/exe/expressir +22 -0
  19. data/exe/format +18 -0
  20. data/exe/format-test +81 -0
  21. data/exe/generate-parser +51 -0
  22. data/expressir.gemspec +48 -0
  23. data/lib/expressir/cli/ui.rb +36 -0
  24. data/lib/expressir/cli.rb +21 -0
  25. data/lib/expressir/config.rb +23 -0
  26. data/lib/expressir/express/2.7/express_parser.so +0 -0
  27. data/lib/expressir/express/3.0/express_parser.so +0 -0
  28. data/lib/expressir/express/3.1/express_parser.so +0 -0
  29. data/lib/expressir/express/3.2/express_parser.so +0 -0
  30. data/lib/expressir/express/cache.rb +51 -0
  31. data/lib/expressir/express/extension.rb +30 -0
  32. data/lib/expressir/express/formatter.rb +1608 -0
  33. data/lib/expressir/express/hyperlink_formatter.rb +36 -0
  34. data/lib/expressir/express/model_visitor.rb +24 -0
  35. data/lib/expressir/express/parser.rb +79 -0
  36. data/lib/expressir/express/resolve_references_model_visitor.rb +31 -0
  37. data/lib/expressir/express/schema_head_formatter.rb +23 -0
  38. data/lib/expressir/express/visitor.rb +2581 -0
  39. data/lib/expressir/model/cache.rb +17 -0
  40. data/lib/expressir/model/data_type.rb +9 -0
  41. data/lib/expressir/model/data_types/aggregate.rb +31 -0
  42. data/lib/expressir/model/data_types/array.rb +31 -0
  43. data/lib/expressir/model/data_types/bag.rb +25 -0
  44. data/lib/expressir/model/data_types/binary.rb +22 -0
  45. data/lib/expressir/model/data_types/boolean.rb +10 -0
  46. data/lib/expressir/model/data_types/enumeration.rb +25 -0
  47. data/lib/expressir/model/data_types/enumeration_item.rb +26 -0
  48. data/lib/expressir/model/data_types/generic.rb +26 -0
  49. data/lib/expressir/model/data_types/generic_entity.rb +26 -0
  50. data/lib/expressir/model/data_types/integer.rb +10 -0
  51. data/lib/expressir/model/data_types/list.rb +28 -0
  52. data/lib/expressir/model/data_types/logical.rb +10 -0
  53. data/lib/expressir/model/data_types/number.rb +10 -0
  54. data/lib/expressir/model/data_types/real.rb +19 -0
  55. data/lib/expressir/model/data_types/select.rb +28 -0
  56. data/lib/expressir/model/data_types/set.rb +25 -0
  57. data/lib/expressir/model/data_types/string.rb +22 -0
  58. data/lib/expressir/model/declaration.rb +9 -0
  59. data/lib/expressir/model/declarations/attribute.rb +47 -0
  60. data/lib/expressir/model/declarations/constant.rb +34 -0
  61. data/lib/expressir/model/declarations/entity.rb +53 -0
  62. data/lib/expressir/model/declarations/function.rb +67 -0
  63. data/lib/expressir/model/declarations/interface.rb +28 -0
  64. data/lib/expressir/model/declarations/interface_item.rb +23 -0
  65. data/lib/expressir/model/declarations/interfaced_item.rb +37 -0
  66. data/lib/expressir/model/declarations/parameter.rb +34 -0
  67. data/lib/expressir/model/declarations/procedure.rb +64 -0
  68. data/lib/expressir/model/declarations/remark_item.rb +21 -0
  69. data/lib/expressir/model/declarations/rule.rb +71 -0
  70. data/lib/expressir/model/declarations/schema.rb +117 -0
  71. data/lib/expressir/model/declarations/schema_version.rb +22 -0
  72. data/lib/expressir/model/declarations/schema_version_item.rb +22 -0
  73. data/lib/expressir/model/declarations/subtype_constraint.rb +40 -0
  74. data/lib/expressir/model/declarations/type.rb +45 -0
  75. data/lib/expressir/model/declarations/unique_rule.rb +31 -0
  76. data/lib/expressir/model/declarations/variable.rb +34 -0
  77. data/lib/expressir/model/declarations/where_rule.rb +31 -0
  78. data/lib/expressir/model/expression.rb +9 -0
  79. data/lib/expressir/model/expressions/aggregate_initializer.rb +19 -0
  80. data/lib/expressir/model/expressions/aggregate_initializer_item.rb +22 -0
  81. data/lib/expressir/model/expressions/binary_expression.rb +53 -0
  82. data/lib/expressir/model/expressions/entity_constructor.rb +22 -0
  83. data/lib/expressir/model/expressions/function_call.rb +22 -0
  84. data/lib/expressir/model/expressions/interval.rb +34 -0
  85. data/lib/expressir/model/expressions/query_expression.rb +35 -0
  86. data/lib/expressir/model/expressions/unary_expression.rb +27 -0
  87. data/lib/expressir/model/identifier.rb +34 -0
  88. data/lib/expressir/model/literal.rb +9 -0
  89. data/lib/expressir/model/literals/binary.rb +19 -0
  90. data/lib/expressir/model/literals/integer.rb +19 -0
  91. data/lib/expressir/model/literals/logical.rb +23 -0
  92. data/lib/expressir/model/literals/real.rb +19 -0
  93. data/lib/expressir/model/literals/string.rb +22 -0
  94. data/lib/expressir/model/model_element.rb +208 -0
  95. data/lib/expressir/model/reference.rb +9 -0
  96. data/lib/expressir/model/references/attribute_reference.rb +22 -0
  97. data/lib/expressir/model/references/group_reference.rb +22 -0
  98. data/lib/expressir/model/references/index_reference.rb +27 -0
  99. data/lib/expressir/model/references/simple_reference.rb +24 -0
  100. data/lib/expressir/model/repository.rb +23 -0
  101. data/lib/expressir/model/statement.rb +9 -0
  102. data/lib/expressir/model/statements/alias.rb +35 -0
  103. data/lib/expressir/model/statements/assignment.rb +22 -0
  104. data/lib/expressir/model/statements/case.rb +25 -0
  105. data/lib/expressir/model/statements/case_action.rb +22 -0
  106. data/lib/expressir/model/statements/compound.rb +19 -0
  107. data/lib/expressir/model/statements/escape.rb +10 -0
  108. data/lib/expressir/model/statements/if.rb +25 -0
  109. data/lib/expressir/model/statements/null.rb +10 -0
  110. data/lib/expressir/model/statements/procedure_call.rb +22 -0
  111. data/lib/expressir/model/statements/repeat.rb +47 -0
  112. data/lib/expressir/model/statements/return.rb +19 -0
  113. data/lib/expressir/model/statements/skip.rb +10 -0
  114. data/lib/expressir/model/supertype_expression.rb +9 -0
  115. data/lib/expressir/model/supertype_expressions/binary_supertype_expression.rb +29 -0
  116. data/lib/expressir/model/supertype_expressions/oneof_supertype_expression.rb +19 -0
  117. data/lib/expressir/model.rb +79 -0
  118. data/lib/expressir/version.rb +3 -0
  119. data/lib/expressir.rb +24 -0
  120. data/rakelib/antlr4-native.rake +161 -0
  121. data/rakelib/cross-ruby.rake +383 -0
  122. data/spec/acceptance/version_spec.rb +27 -0
  123. data/spec/expressir/express/cache_spec.rb +89 -0
  124. data/spec/expressir/express/formatter_spec.rb +173 -0
  125. data/spec/expressir/express/parser_spec.rb +141 -0
  126. data/spec/expressir/model/model_element_spec.rb +318 -0
  127. data/spec/spec_helper.rb +24 -0
  128. data/spec/support/console_helper.rb +29 -0
  129. data/spec/syntax/multiple.exp +23 -0
  130. data/spec/syntax/multiple.yaml +198 -0
  131. data/spec/syntax/multiple_formatted.exp +71 -0
  132. data/spec/syntax/multiple_hyperlink_formatted.exp +71 -0
  133. data/spec/syntax/multiple_schema_head_hyperlink_formatted.exp +13 -0
  134. data/spec/syntax/remark.exp +193 -0
  135. data/spec/syntax/remark.yaml +471 -0
  136. data/spec/syntax/remark_formatted.exp +228 -0
  137. data/spec/syntax/single.exp +4 -0
  138. data/spec/syntax/single.yaml +18 -0
  139. data/spec/syntax/single_formatted.exp +10 -0
  140. data/spec/syntax/single_formatted.yaml +36 -0
  141. data/spec/syntax/syntax.exp +333 -0
  142. data/spec/syntax/syntax.yaml +3509 -0
  143. data/spec/syntax/syntax_formatted.exp +902 -0
  144. data/spec/syntax/syntax_hyperlink_formatted.exp +902 -0
  145. data/spec/syntax/syntax_schema_head_formatted.exp +18 -0
  146. metadata +391 -0
@@ -0,0 +1,9 @@
1
+ module Expressir
2
+ module Model
3
+ # Specified in ISO 10303-11:2004
4
+ # - section 13 Executable statements
5
+ # @abstract
6
+ class Statement < ModelElement
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.2 Alias statement
6
+ class Alias < Statement
7
+ include Identifier
8
+
9
+ model_attr_accessor :expression, 'Expression'
10
+ model_attr_accessor :statements, 'Array<Statement>'
11
+
12
+ # @param [Hash] options
13
+ # @option (see Identifier#initialize_identifier)
14
+ # @option options [Expression] :expression
15
+ # @option options [Array<Statement>] :statements
16
+ def initialize(options = {})
17
+ initialize_identifier(options)
18
+
19
+ @expression = options[:expression]
20
+ @statements = options[:statements] || []
21
+
22
+ super
23
+ end
24
+
25
+ # @return [Array<Declaration>]
26
+ def children
27
+ [
28
+ self,
29
+ *remark_items
30
+ ]
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.3 Assignment
6
+ class Assignment < Statement
7
+ model_attr_accessor :ref, 'Reference'
8
+ model_attr_accessor :expression, 'Expression'
9
+
10
+ # @param [Hash] options
11
+ # @option options [Reference] :ref
12
+ # @option options [Expression] :expression
13
+ def initialize(options = {})
14
+ @ref = options[:ref]
15
+ @expression = options[:expression]
16
+
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.4 Case statement
6
+ class Case < Statement
7
+ model_attr_accessor :expression, 'Expression'
8
+ model_attr_accessor :actions, 'Array<CaseAction>'
9
+ model_attr_accessor :otherwise_statement, 'Statement'
10
+
11
+ # @param [Hash] options
12
+ # @option options [Expression] :expression
13
+ # @option options [Array<CaseAction>] :statements
14
+ # @option options [Statement] :otherwise_statement
15
+ def initialize(options = {})
16
+ @expression = options[:expression]
17
+ @actions = options[:actions] || []
18
+ @otherwise_statement = options[:otherwise_statement]
19
+
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.4 Case statement
6
+ class CaseAction < ModelElement
7
+ model_attr_accessor :labels, 'Array<Expression>'
8
+ model_attr_accessor :statement, 'Statement'
9
+
10
+ # @param [Hash] options
11
+ # @option options [Array<Expression>] :labels
12
+ # @option options [Statement] :statement
13
+ def initialize(options = {})
14
+ @labels = options[:labels] || []
15
+ @statement = options[:statement]
16
+
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.5 Compound statement
6
+ class Compound < Statement
7
+ model_attr_accessor :statements, 'Array<Statement>'
8
+
9
+ # @param [Hash] options
10
+ # @option options [Array<Statement>] :statements
11
+ def initialize(options = {})
12
+ @statements = options[:statements] || []
13
+
14
+ super
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.6 Escape statement
6
+ class Escape < Statement
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.7 If ... Then ... Else statement
6
+ class If < Statement
7
+ model_attr_accessor :expression, 'Expression'
8
+ model_attr_accessor :statements, 'Array<Statement>'
9
+ model_attr_accessor :else_statements, 'Array<Statement>'
10
+
11
+ # @param [Hash] options
12
+ # @option options [Expression] :expression
13
+ # @option options [Array<Statement>] :statements
14
+ # @option options [Array<Statement>] :else_statements
15
+ def initialize(options = {})
16
+ @expression = options[:expression]
17
+ @statements = options[:statements] || []
18
+ @else_statements = options[:else_statements] || []
19
+
20
+ super
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.1 Null (statement)
6
+ class Null < Statement
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.8 Procedure call statement
6
+ class ProcedureCall < Statement
7
+ model_attr_accessor :procedure, 'Reference'
8
+ model_attr_accessor :parameters, 'Array<Expression>'
9
+
10
+ # @param [Hash] options
11
+ # @option options [Reference] :procedure
12
+ # @option options [Array<Expression>] :parameters
13
+ def initialize(options = {})
14
+ @procedure = options[:procedure]
15
+ @parameters = options[:parameters] || []
16
+
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,47 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.9 Repeat statement
6
+ class Repeat < Statement
7
+ include Identifier
8
+
9
+ model_attr_accessor :bound1, 'Expression'
10
+ model_attr_accessor :bound2, 'Expression'
11
+ model_attr_accessor :increment, 'Expression'
12
+ model_attr_accessor :while_expression, 'Expression'
13
+ model_attr_accessor :until_expression, 'Expression'
14
+ model_attr_accessor :statements, 'Array<Statement>'
15
+
16
+ # @param [Hash] options
17
+ # @option (see Identifier#initialize_identifier)
18
+ # @option options [Expression] :bound1
19
+ # @option options [Expression] :bound2
20
+ # @option options [Expression] :increment
21
+ # @option options [Expression] :while_expression
22
+ # @option options [Expression] :until_expression
23
+ # @option options [Array<Statement>] :statements
24
+ def initialize(options = {})
25
+ initialize_identifier(options)
26
+
27
+ @bound1 = options[:bound1]
28
+ @bound2 = options[:bound2]
29
+ @increment = options[:increment]
30
+ @while_expression = options[:while_expression]
31
+ @until_expression = options[:until_expression]
32
+ @statements = options[:statements] || []
33
+
34
+ super
35
+ end
36
+
37
+ # @return [Array<Declaration>]
38
+ def children
39
+ [
40
+ self,
41
+ *remark_items
42
+ ]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.10 Return statement
6
+ class Return < Statement
7
+ model_attr_accessor :expression, 'Expression'
8
+
9
+ # @param [Hash] options
10
+ # @option options [Expression] :expression
11
+ def initialize(options = {})
12
+ @expression = options[:expression]
13
+
14
+ super
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ module Expressir
2
+ module Model
3
+ module Statements
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 13.11 Skip statement
6
+ class Skip < Statement
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Expressir
2
+ module Model
3
+ # Specified in ISO 10303-11:2004
4
+ # - section 9.2.5 Subtype/supertype constraints
5
+ # @abstract
6
+ class SupertypeExpression < ModelElement
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ module Expressir
2
+ module Model
3
+ module SupertypeExpressions
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 9.2.5.3 ANDOR
6
+ # - section 9.2.5.4 AND
7
+ class BinarySupertypeExpression < SupertypeExpression
8
+ AND = :AND
9
+ ANDOR = :ANDOR
10
+
11
+ model_attr_accessor :operator, ':AND, :ANDOR'
12
+ model_attr_accessor :operand1, 'SupertypeExpression'
13
+ model_attr_accessor :operand2, 'SupertypeExpression'
14
+
15
+ # @param [Hash] options
16
+ # @option options [:AND, :ANDOR] :operator
17
+ # @option options [SupertypeExpression] :operand1
18
+ # @option options [SupertypeExpression] :operand2
19
+ def initialize(options = {})
20
+ @operator = options[:operator]
21
+ @operand1 = options[:operand1]
22
+ @operand2 = options[:operand2]
23
+
24
+ super
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,19 @@
1
+ module Expressir
2
+ module Model
3
+ module SupertypeExpressions
4
+ # Specified in ISO 10303-11:2004
5
+ # - section 9.2.5.2 ONEOF
6
+ class OneofSupertypeExpression < SupertypeExpression
7
+ model_attr_accessor :operands, 'Array<SupertypeExpression>'
8
+
9
+ # @param [Hash] options
10
+ # @option options [Array<SupertypeExpression>] :operands
11
+ def initialize(options = {})
12
+ @operands = options[:operands]
13
+
14
+ super
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,79 @@
1
+ require "expressir/model/model_element"
2
+
3
+ require "expressir/model/cache"
4
+ require "expressir/model/data_type"
5
+ require "expressir/model/declaration"
6
+ require "expressir/model/expression"
7
+ require "expressir/model/identifier"
8
+ require "expressir/model/literal"
9
+ require "expressir/model/reference"
10
+ require "expressir/model/repository"
11
+ require "expressir/model/statement"
12
+ require "expressir/model/supertype_expression"
13
+ require "expressir/model/data_types/aggregate"
14
+ require "expressir/model/data_types/array"
15
+ require "expressir/model/data_types/bag"
16
+ require "expressir/model/data_types/binary"
17
+ require "expressir/model/data_types/boolean"
18
+ require "expressir/model/data_types/enumeration"
19
+ require "expressir/model/data_types/enumeration_item"
20
+ require "expressir/model/data_types/generic_entity"
21
+ require "expressir/model/data_types/generic"
22
+ require "expressir/model/data_types/integer"
23
+ require "expressir/model/data_types/list"
24
+ require "expressir/model/data_types/logical"
25
+ require "expressir/model/data_types/number"
26
+ require "expressir/model/data_types/real"
27
+ require "expressir/model/data_types/set"
28
+ require "expressir/model/data_types/select"
29
+ require "expressir/model/data_types/string"
30
+ require "expressir/model/declarations/attribute"
31
+ require "expressir/model/declarations/constant"
32
+ require "expressir/model/declarations/entity"
33
+ require "expressir/model/declarations/function"
34
+ require "expressir/model/declarations/interface"
35
+ require "expressir/model/declarations/interface_item"
36
+ require "expressir/model/declarations/interfaced_item"
37
+ require "expressir/model/declarations/parameter"
38
+ require "expressir/model/declarations/procedure"
39
+ require "expressir/model/declarations/remark_item"
40
+ require "expressir/model/declarations/rule"
41
+ require "expressir/model/declarations/schema"
42
+ require "expressir/model/declarations/schema_version"
43
+ require "expressir/model/declarations/schema_version_item"
44
+ require "expressir/model/declarations/subtype_constraint"
45
+ require "expressir/model/declarations/type"
46
+ require "expressir/model/declarations/unique_rule"
47
+ require "expressir/model/declarations/variable"
48
+ require "expressir/model/declarations/where_rule"
49
+ require "expressir/model/expressions/aggregate_initializer"
50
+ require "expressir/model/expressions/aggregate_initializer_item"
51
+ require "expressir/model/expressions/binary_expression"
52
+ require "expressir/model/expressions/entity_constructor"
53
+ require "expressir/model/expressions/function_call"
54
+ require "expressir/model/expressions/interval"
55
+ require "expressir/model/expressions/query_expression"
56
+ require "expressir/model/expressions/unary_expression"
57
+ require "expressir/model/literals/binary"
58
+ require "expressir/model/literals/integer"
59
+ require "expressir/model/literals/logical"
60
+ require "expressir/model/literals/real"
61
+ require "expressir/model/literals/string"
62
+ require "expressir/model/references/attribute_reference"
63
+ require "expressir/model/references/group_reference"
64
+ require "expressir/model/references/index_reference"
65
+ require "expressir/model/references/simple_reference"
66
+ require "expressir/model/statements/alias"
67
+ require "expressir/model/statements/assignment"
68
+ require "expressir/model/statements/case"
69
+ require "expressir/model/statements/case_action"
70
+ require "expressir/model/statements/compound"
71
+ require "expressir/model/statements/escape"
72
+ require "expressir/model/statements/if"
73
+ require "expressir/model/statements/null"
74
+ require "expressir/model/statements/procedure_call"
75
+ require "expressir/model/statements/repeat"
76
+ require "expressir/model/statements/return"
77
+ require "expressir/model/statements/skip"
78
+ require "expressir/model/supertype_expressions/binary_supertype_expression"
79
+ require "expressir/model/supertype_expressions/oneof_supertype_expression"
@@ -0,0 +1,3 @@
1
+ module Expressir
2
+ VERSION = "1.3.0.pre.1".freeze
3
+ end
data/lib/expressir.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "expressir/version"
2
+
3
+ require "expressir/cli"
4
+ require "expressir/config"
5
+
6
+ Dir[File.join(__dir__, "expressir", "express", "*.rb")].sort.each do |fea|
7
+ require fea
8
+ end
9
+
10
+ module Expressir
11
+ class Error < StandardError; end
12
+
13
+ def self.ui
14
+ Expressir::Cli::UI
15
+ end
16
+
17
+ def self.root
18
+ File.dirname(__dir__)
19
+ end
20
+
21
+ def self.root_path
22
+ @root_path ||= Pathname.new(Expressir.root)
23
+ end
24
+ end
@@ -0,0 +1,161 @@
1
+ require "fileutils"
2
+ require "antlr4-native"
3
+ require "rake"
4
+
5
+ def create_class_declarations(parser_source_lines)
6
+ i = parser_source_lines.index { |x| x == "Class rb_cContextProxy;" }
7
+ parser_source_lines[i] += <<~CPP.split("\n").map { |x| x == "" ? x : x.to_s }.join("\n")
8
+
9
+ Class rb_cParserExt;
10
+ Class rb_cTokenExt;
11
+
12
+ CPP
13
+ end
14
+
15
+ def create_tp_class_definition(parser_source_lines)
16
+ i = parser_source_lines.index { |x| x == "class ContextProxy {" }
17
+ parser_source_lines[i - 2] += <<~CPP.split("\n").map { |x| x == "" ? x : x.to_s }.join("\n")
18
+
19
+
20
+ class TokenProxy : public Object {
21
+ public:
22
+ TokenProxy(Token* orig) {
23
+ this -> orig = orig;
24
+ }
25
+
26
+ std::string getText() {
27
+ return orig->getText();
28
+ }
29
+
30
+ size_t getChannel() {
31
+ return orig->getChannel();
32
+ }
33
+
34
+ size_t getTokenIndex() {
35
+ return orig->getTokenIndex();
36
+ }
37
+
38
+ private:
39
+ Token * orig = nullptr;
40
+ };
41
+
42
+ CPP
43
+ end
44
+
45
+ def create_pp_class_definition(parser_source_lines)
46
+ i = parser_source_lines.index { |x| x == "extern \"C\"" }
47
+ parser_source_lines[i - 2] += <<~CPP.split("\n").map { |x| x == "" ? x : x.to_s }.join("\n")
48
+
49
+ class ParserProxyExt : public Object {
50
+ public:
51
+ ParserProxyExt(string file) {
52
+ ifstream stream;
53
+ stream.open(file);
54
+ input = new ANTLRInputStream(stream);
55
+ lexer = new ExpressLexer(input);
56
+ tokens = new CommonTokenStream(lexer);
57
+ parser = new ExpressParser(tokens);
58
+ stream.close();
59
+ };
60
+
61
+ ~ParserProxyExt() {
62
+ delete parser;
63
+ delete tokens;
64
+ delete lexer;
65
+ delete input;
66
+ }
67
+
68
+ Object syntax() {
69
+ auto ctx = parser -> syntax();
70
+
71
+ SyntaxContextProxy proxy((ExpressParser::SyntaxContext*) ctx);
72
+ return detail::To_Ruby<SyntaxContextProxy>().convert(proxy);
73
+ }
74
+
75
+ Array getTokens() {
76
+ Array a;
77
+ for (auto token : tokens -> getTokens()) {
78
+ a.push(new TokenProxy(token));
79
+ }
80
+ return a;
81
+ }
82
+
83
+ Object visit(VisitorProxy* visitor) {
84
+ auto result = visitor -> visit(parser -> syntax());
85
+
86
+ lexer -> reset();
87
+ parser -> reset();
88
+
89
+ try {
90
+ return std::any_cast<Object>(result);
91
+ } catch(std::bad_cast) {
92
+ return Qnil;
93
+ }
94
+ }
95
+
96
+ private:
97
+ ANTLRInputStream* input;
98
+ ExpressLexer* lexer;
99
+ CommonTokenStream* tokens;
100
+ ExpressParser* parser;
101
+ };
102
+
103
+
104
+ CPP
105
+ end
106
+
107
+ def create_class_api(parser_source_lines)
108
+ i = parser_source_lines.index { |x| x == " .define_method(\"visit\", &ParserProxy::visit, Return().keepAlive());" }
109
+ parser_source_lines[i] += <<~CPP.split("\n").map { |x| x == "" ? x : " #{x}" }.join("\n")
110
+
111
+
112
+ rb_cTokenExt = define_class_under<TokenProxy>(rb_mExpressParser, "TokenExt")
113
+ .define_method("text", &TokenProxy::getText)
114
+ .define_method("channel", &TokenProxy::getChannel)
115
+ .define_method("token_index", &TokenProxy::getTokenIndex);
116
+
117
+ rb_cParserExt = define_class_under<ParserProxyExt>(rb_mExpressParser, "ParserExt")
118
+ .define_constructor(Constructor<ParserProxyExt, string>())
119
+ .define_method("syntax", &ParserProxyExt::syntax, Return().keepAlive())
120
+ .define_method("tokens", &ParserProxyExt::getTokens)
121
+ .define_method("visit", &ParserProxyExt::visit, Return().keepAlive());
122
+
123
+ CPP
124
+ end
125
+
126
+ def generate_extended_parser
127
+ # Generate extended parser that provide Ruby access to token stream
128
+ parser_source_file = File.join("ext", "express-parser", "express_parser.cpp")
129
+ parser_source_lines = File.read(parser_source_file).split("\n")
130
+ create_class_declarations(parser_source_lines)
131
+ create_tp_class_definition(parser_source_lines)
132
+ create_pp_class_definition(parser_source_lines)
133
+ create_class_api(parser_source_lines)
134
+ File.write(parser_source_file, "#{parser_source_lines.join("\n")}\n")
135
+ end
136
+
137
+ desc "Generate parser (Usage: 'rake generate <grammar_file>')"
138
+ task "generate" do
139
+ grammar_file = ARGV[1]
140
+ if grammar_file.nil?
141
+ grammar_file = File.expand_path(File.join("..", "ext", "express-grammar", "Express.g4"), __dir__)
142
+ end
143
+
144
+ puts "Generating parser from '#{grammar_file}'"
145
+ # ANTLR does weird things if the grammar file isn't in the current working directory
146
+ temp_grammar_file = File.join(FileUtils.pwd, File.basename(grammar_file))
147
+ FileUtils.cp(grammar_file, temp_grammar_file)
148
+
149
+ # generate parser
150
+ generator = Antlr4Native::Generator.new(
151
+ grammar_files: [File.basename(temp_grammar_file)],
152
+ output_dir: "ext",
153
+ parser_root_method: "syntax",
154
+ )
155
+ generator.generate
156
+
157
+ puts "Generating extended parser"
158
+ generate_extended_parser
159
+ # cleanup
160
+ FileUtils.rm(temp_grammar_file)
161
+ end