expressir 1.2.5-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/.cross_rubies +15 -0
  3. data/.github/workflows/rake.yml +300 -0
  4. data/.github/workflows/release.yml +120 -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 +17 -0
  10. data/.yardopts +11 -0
  11. data/Gemfile +4 -0
  12. data/README.adoc +147 -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/3.1/express_parser.so +0 -0
  27. data/lib/expressir/express/cache.rb +51 -0
  28. data/lib/expressir/express/formatter.rb +1608 -0
  29. data/lib/expressir/express/hyperlink_formatter.rb +36 -0
  30. data/lib/expressir/express/model_visitor.rb +24 -0
  31. data/lib/expressir/express/parser.rb +84 -0
  32. data/lib/expressir/express/resolve_references_model_visitor.rb +31 -0
  33. data/lib/expressir/express/schema_head_formatter.rb +23 -0
  34. data/lib/expressir/express/visitor.rb +2578 -0
  35. data/lib/expressir/model/cache.rb +17 -0
  36. data/lib/expressir/model/data_type.rb +9 -0
  37. data/lib/expressir/model/data_types/aggregate.rb +31 -0
  38. data/lib/expressir/model/data_types/array.rb +31 -0
  39. data/lib/expressir/model/data_types/bag.rb +25 -0
  40. data/lib/expressir/model/data_types/binary.rb +22 -0
  41. data/lib/expressir/model/data_types/boolean.rb +10 -0
  42. data/lib/expressir/model/data_types/enumeration.rb +25 -0
  43. data/lib/expressir/model/data_types/enumeration_item.rb +26 -0
  44. data/lib/expressir/model/data_types/generic.rb +26 -0
  45. data/lib/expressir/model/data_types/generic_entity.rb +26 -0
  46. data/lib/expressir/model/data_types/integer.rb +10 -0
  47. data/lib/expressir/model/data_types/list.rb +28 -0
  48. data/lib/expressir/model/data_types/logical.rb +10 -0
  49. data/lib/expressir/model/data_types/number.rb +10 -0
  50. data/lib/expressir/model/data_types/real.rb +19 -0
  51. data/lib/expressir/model/data_types/select.rb +28 -0
  52. data/lib/expressir/model/data_types/set.rb +25 -0
  53. data/lib/expressir/model/data_types/string.rb +22 -0
  54. data/lib/expressir/model/declaration.rb +9 -0
  55. data/lib/expressir/model/declarations/attribute.rb +47 -0
  56. data/lib/expressir/model/declarations/constant.rb +34 -0
  57. data/lib/expressir/model/declarations/entity.rb +53 -0
  58. data/lib/expressir/model/declarations/function.rb +67 -0
  59. data/lib/expressir/model/declarations/interface.rb +28 -0
  60. data/lib/expressir/model/declarations/interface_item.rb +23 -0
  61. data/lib/expressir/model/declarations/interfaced_item.rb +37 -0
  62. data/lib/expressir/model/declarations/parameter.rb +34 -0
  63. data/lib/expressir/model/declarations/procedure.rb +64 -0
  64. data/lib/expressir/model/declarations/remark_item.rb +21 -0
  65. data/lib/expressir/model/declarations/rule.rb +71 -0
  66. data/lib/expressir/model/declarations/schema.rb +117 -0
  67. data/lib/expressir/model/declarations/schema_version.rb +22 -0
  68. data/lib/expressir/model/declarations/schema_version_item.rb +22 -0
  69. data/lib/expressir/model/declarations/subtype_constraint.rb +40 -0
  70. data/lib/expressir/model/declarations/type.rb +45 -0
  71. data/lib/expressir/model/declarations/unique_rule.rb +31 -0
  72. data/lib/expressir/model/declarations/variable.rb +34 -0
  73. data/lib/expressir/model/declarations/where_rule.rb +31 -0
  74. data/lib/expressir/model/expression.rb +9 -0
  75. data/lib/expressir/model/expressions/aggregate_initializer.rb +19 -0
  76. data/lib/expressir/model/expressions/aggregate_initializer_item.rb +22 -0
  77. data/lib/expressir/model/expressions/binary_expression.rb +53 -0
  78. data/lib/expressir/model/expressions/entity_constructor.rb +22 -0
  79. data/lib/expressir/model/expressions/function_call.rb +22 -0
  80. data/lib/expressir/model/expressions/interval.rb +34 -0
  81. data/lib/expressir/model/expressions/query_expression.rb +35 -0
  82. data/lib/expressir/model/expressions/unary_expression.rb +27 -0
  83. data/lib/expressir/model/identifier.rb +34 -0
  84. data/lib/expressir/model/literal.rb +9 -0
  85. data/lib/expressir/model/literals/binary.rb +19 -0
  86. data/lib/expressir/model/literals/integer.rb +19 -0
  87. data/lib/expressir/model/literals/logical.rb +23 -0
  88. data/lib/expressir/model/literals/real.rb +19 -0
  89. data/lib/expressir/model/literals/string.rb +22 -0
  90. data/lib/expressir/model/model_element.rb +208 -0
  91. data/lib/expressir/model/reference.rb +9 -0
  92. data/lib/expressir/model/references/attribute_reference.rb +22 -0
  93. data/lib/expressir/model/references/group_reference.rb +22 -0
  94. data/lib/expressir/model/references/index_reference.rb +27 -0
  95. data/lib/expressir/model/references/simple_reference.rb +24 -0
  96. data/lib/expressir/model/repository.rb +23 -0
  97. data/lib/expressir/model/statement.rb +9 -0
  98. data/lib/expressir/model/statements/alias.rb +35 -0
  99. data/lib/expressir/model/statements/assignment.rb +22 -0
  100. data/lib/expressir/model/statements/case.rb +25 -0
  101. data/lib/expressir/model/statements/case_action.rb +22 -0
  102. data/lib/expressir/model/statements/compound.rb +19 -0
  103. data/lib/expressir/model/statements/escape.rb +10 -0
  104. data/lib/expressir/model/statements/if.rb +25 -0
  105. data/lib/expressir/model/statements/null.rb +10 -0
  106. data/lib/expressir/model/statements/procedure_call.rb +22 -0
  107. data/lib/expressir/model/statements/repeat.rb +47 -0
  108. data/lib/expressir/model/statements/return.rb +19 -0
  109. data/lib/expressir/model/statements/skip.rb +10 -0
  110. data/lib/expressir/model/supertype_expression.rb +9 -0
  111. data/lib/expressir/model/supertype_expressions/binary_supertype_expression.rb +29 -0
  112. data/lib/expressir/model/supertype_expressions/oneof_supertype_expression.rb +19 -0
  113. data/lib/expressir/model.rb +79 -0
  114. data/lib/expressir/version.rb +3 -0
  115. data/lib/expressir.rb +20 -0
  116. data/rakelib/antlr4-native.rake +63 -0
  117. data/rakelib/cross-ruby.rake +367 -0
  118. data/spec/acceptance/version_spec.rb +17 -0
  119. data/spec/expressir/express/cache_spec.rb +67 -0
  120. data/spec/expressir/express/formatter_spec.rb +135 -0
  121. data/spec/expressir/express/parser_spec.rb +104 -0
  122. data/spec/expressir/model/model_element_spec.rb +274 -0
  123. data/spec/spec_helper.rb +17 -0
  124. data/spec/support/console_helper.rb +29 -0
  125. data/spec/syntax/multiple.exp +23 -0
  126. data/spec/syntax/multiple.yaml +198 -0
  127. data/spec/syntax/multiple_formatted.exp +71 -0
  128. data/spec/syntax/multiple_hyperlink_formatted.exp +71 -0
  129. data/spec/syntax/multiple_schema_head_hyperlink_formatted.exp +13 -0
  130. data/spec/syntax/remark.exp +191 -0
  131. data/spec/syntax/remark.yaml +466 -0
  132. data/spec/syntax/remark_formatted.exp +227 -0
  133. data/spec/syntax/single.exp +4 -0
  134. data/spec/syntax/single.yaml +18 -0
  135. data/spec/syntax/single_formatted.exp +10 -0
  136. data/spec/syntax/single_formatted.yaml +36 -0
  137. data/spec/syntax/syntax.exp +333 -0
  138. data/spec/syntax/syntax.yaml +3509 -0
  139. data/spec/syntax/syntax_formatted.exp +902 -0
  140. data/spec/syntax/syntax_hyperlink_formatted.exp +902 -0
  141. data/spec/syntax/syntax_schema_head_formatted.exp +18 -0
  142. metadata +387 -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.2.5".freeze
3
+ end
data/lib/expressir.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "expressir/version"
2
+
3
+ require "expressir/cli"
4
+ require "expressir/config"
5
+
6
+ module Expressir
7
+ class Error < StandardError; end
8
+
9
+ def self.ui
10
+ Expressir::Cli::UI
11
+ end
12
+
13
+ def self.root
14
+ File.dirname(__dir__)
15
+ end
16
+
17
+ def self.root_path
18
+ @root_path ||= Pathname.new(Expressir.root)
19
+ end
20
+ end
@@ -0,0 +1,63 @@
1
+ require "fileutils"
2
+ require "antlr4-native"
3
+ require "rake"
4
+
5
+ def create_tokens_api(parser_source_lines)
6
+ # - add ParserProxy tokens method, simple compensation for missing exposed BufferedTokenStream
7
+ i = parser_source_lines.index { |x| x == " Object syntax() {" }
8
+ parser_source_lines[i + 6] += <<~CPP.split("\n").map { |x| x == "" ? x : " #{x}" }.join("\n")
9
+
10
+ Array getTokens() {
11
+ Array a;
12
+
13
+ std::vector<Token*> tokens = this -> tokens -> getTokens();
14
+
15
+ for (auto &token : tokens) {
16
+ a.push(token);
17
+ }
18
+
19
+ return a;
20
+ }
21
+
22
+ CPP
23
+ end
24
+
25
+ def create_tokens_method(parser_source_lines)
26
+ i = parser_source_lines.index { |x| x == ' .define_method("syntax", &ParserProxy::syntax, Return().keepAlive())' }
27
+ parser_source_lines[i] += <<~CPP.split("\n").map { |x| x == "" ? x : " #{x}" }.join("\n")
28
+
29
+ .define_method("tokens", &ParserProxy::getTokens)
30
+ CPP
31
+ end
32
+
33
+ desc "Generate parser (Usage: 'rake generate <grammar_file>')"
34
+ task "generate" do
35
+ grammar_file = ARGV[1]
36
+ if grammar_file.nil?
37
+ grammar_file = File.expand_path(File.join("..", "ext", "express-grammar", "Express.g4"), __dir__)
38
+ end
39
+
40
+ puts "Generating parser from '#{grammar_file}'"
41
+
42
+ # ANTLR does weird things if the grammar file isn't in the current working directory
43
+ temp_grammar_file = File.join(FileUtils.pwd, File.basename(grammar_file))
44
+ FileUtils.cp(grammar_file, temp_grammar_file)
45
+
46
+ # generate parser
47
+ generator = Antlr4Native::Generator.new(
48
+ grammar_files: [File.basename(temp_grammar_file)],
49
+ output_dir: "ext",
50
+ parser_root_method: "syntax",
51
+ )
52
+ generator.generate
53
+
54
+ # fix issues with generated parser
55
+ parser_source_file = File.join("ext", "express-parser", "express_parser.cpp")
56
+ parser_source_lines = File.read(parser_source_file).split("\n")
57
+ create_tokens_api(parser_source_lines)
58
+ create_tokens_method(parser_source_lines)
59
+ File.write(parser_source_file, "#{parser_source_lines.join("\n")}\n")
60
+
61
+ # cleanup
62
+ FileUtils.rm(temp_grammar_file)
63
+ end