tediparse 2.0.0

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 (176) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +319 -0
  3. data/LICENSE +26 -0
  4. data/README.md +308 -0
  5. data/Rakefile +33 -0
  6. data/bin/tediparse +102 -0
  7. data/doc/Defining.md +97 -0
  8. data/doc/Generating-Grammars.md +241 -0
  9. data/doc/Generating.md +332 -0
  10. data/doc/Navigating.md +670 -0
  11. data/doc/Parsing.md +133 -0
  12. data/doc/README.md +51 -0
  13. data/doc/Serializing.md +100 -0
  14. data/doc/Tokenizing.md +136 -0
  15. data/doc/Validating.md +140 -0
  16. data/lib/ruby/array.rb +226 -0
  17. data/lib/ruby/blank.rb +52 -0
  18. data/lib/ruby/exception.rb +14 -0
  19. data/lib/ruby/hash.rb +14 -0
  20. data/lib/ruby/module.rb +60 -0
  21. data/lib/ruby/object.rb +56 -0
  22. data/lib/ruby/string.rb +89 -0
  23. data/lib/ruby/to_d.rb +82 -0
  24. data/lib/ruby/to_date.rb +26 -0
  25. data/lib/ruby/to_time.rb +21 -0
  26. data/lib/ruby/try.rb +46 -0
  27. data/lib/stupidedi/builder.rb +6 -0
  28. data/lib/stupidedi/color.rb +94 -0
  29. data/lib/stupidedi/config/code_list_config.rb +45 -0
  30. data/lib/stupidedi/config/functional_group_config.rb +72 -0
  31. data/lib/stupidedi/config/interchange_config.rb +86 -0
  32. data/lib/stupidedi/config/transaction_set_config.rb +73 -0
  33. data/lib/stupidedi/config.rb +100 -0
  34. data/lib/stupidedi/either.rb +286 -0
  35. data/lib/stupidedi/exceptions/invalid_element_error.rb +7 -0
  36. data/lib/stupidedi/exceptions/invalid_schema_error.rb +7 -0
  37. data/lib/stupidedi/exceptions/missing_grammar_error.rb +50 -0
  38. data/lib/stupidedi/exceptions/output_error.rb +7 -0
  39. data/lib/stupidedi/exceptions/parse_error.rb +7 -0
  40. data/lib/stupidedi/exceptions/stupidedi_error.rb +7 -0
  41. data/lib/stupidedi/exceptions/tokenize_error.rb +7 -0
  42. data/lib/stupidedi/exceptions/zipper_error.rb +7 -0
  43. data/lib/stupidedi/exceptions.rb +13 -0
  44. data/lib/stupidedi/inspect.rb +26 -0
  45. data/lib/stupidedi/interchanges/element_types/separator_val.rb +79 -0
  46. data/lib/stupidedi/interchanges/element_types/special_val.rb +48 -0
  47. data/lib/stupidedi/interchanges/element_types.rb +11 -0
  48. data/lib/stupidedi/interchanges.rb +21 -0
  49. data/lib/stupidedi/parser/builder_dsl.rb +292 -0
  50. data/lib/stupidedi/parser/constraint_table.rb +566 -0
  51. data/lib/stupidedi/parser/generation.rb +147 -0
  52. data/lib/stupidedi/parser/identifier_stack.rb +248 -0
  53. data/lib/stupidedi/parser/instruction.rb +112 -0
  54. data/lib/stupidedi/parser/instruction_table.rb +222 -0
  55. data/lib/stupidedi/parser/navigation.rb +786 -0
  56. data/lib/stupidedi/parser/state_machine.rb +62 -0
  57. data/lib/stupidedi/parser/states/abstract_state.rb +385 -0
  58. data/lib/stupidedi/parser/states/failure_state.rb +69 -0
  59. data/lib/stupidedi/parser/states/functional_group_state.rb +101 -0
  60. data/lib/stupidedi/parser/states/initial_state.rb +62 -0
  61. data/lib/stupidedi/parser/states/interchange_state.rb +96 -0
  62. data/lib/stupidedi/parser/states/loop_state.rb +74 -0
  63. data/lib/stupidedi/parser/states/table_state.rb +90 -0
  64. data/lib/stupidedi/parser/states/transaction_set_state.rb +120 -0
  65. data/lib/stupidedi/parser/states/transmission_state.rb +59 -0
  66. data/lib/stupidedi/parser/tokenization.rb +195 -0
  67. data/lib/stupidedi/parser.rb +32 -0
  68. data/lib/stupidedi/reader/input/abstract_input.rb +137 -0
  69. data/lib/stupidedi/reader/input/delegated_input.rb +112 -0
  70. data/lib/stupidedi/reader/input/file_input.rb +157 -0
  71. data/lib/stupidedi/reader/input.rb +31 -0
  72. data/lib/stupidedi/reader/position.rb +78 -0
  73. data/lib/stupidedi/reader/result.rb +172 -0
  74. data/lib/stupidedi/reader/segment_dict.rb +176 -0
  75. data/lib/stupidedi/reader/separators.rb +90 -0
  76. data/lib/stupidedi/reader/stream_reader.rb +173 -0
  77. data/lib/stupidedi/reader/token_reader.rb +465 -0
  78. data/lib/stupidedi/reader/tokens/component_element_tok.rb +71 -0
  79. data/lib/stupidedi/reader/tokens/composite_element_tok.rb +85 -0
  80. data/lib/stupidedi/reader/tokens/repeated_element_tok.rb +74 -0
  81. data/lib/stupidedi/reader/tokens/segment_tok.rb +74 -0
  82. data/lib/stupidedi/reader/tokens/simple_element_tok.rb +76 -0
  83. data/lib/stupidedi/reader.rb +121 -0
  84. data/lib/stupidedi/schema/abstract_def.rb +76 -0
  85. data/lib/stupidedi/schema/abstract_element_def.rb +35 -0
  86. data/lib/stupidedi/schema/abstract_element_use.rb +47 -0
  87. data/lib/stupidedi/schema/abstract_use.rb +79 -0
  88. data/lib/stupidedi/schema/code_list.rb +99 -0
  89. data/lib/stupidedi/schema/component_element_use.rb +76 -0
  90. data/lib/stupidedi/schema/composite_element_def.rb +103 -0
  91. data/lib/stupidedi/schema/composite_element_use.rb +78 -0
  92. data/lib/stupidedi/schema/element_req.rb +57 -0
  93. data/lib/stupidedi/schema/functional_group_def.rb +124 -0
  94. data/lib/stupidedi/schema/generation/definition_generator.rb +139 -0
  95. data/lib/stupidedi/schema/generation/element_generator.rb +221 -0
  96. data/lib/stupidedi/schema/generation/flat_file_reader.rb +551 -0
  97. data/lib/stupidedi/schema/generation/functional_group_generator.rb +64 -0
  98. data/lib/stupidedi/schema/generation/interchange_generator.rb +145 -0
  99. data/lib/stupidedi/schema/generation/master_loader_generator.rb +121 -0
  100. data/lib/stupidedi/schema/generation/models.rb +85 -0
  101. data/lib/stupidedi/schema/generation/module_loader_generator.rb +64 -0
  102. data/lib/stupidedi/schema/generation/registration_generator.rb +230 -0
  103. data/lib/stupidedi/schema/generation/runner.rb +161 -0
  104. data/lib/stupidedi/schema/generation/segment_generator.rb +130 -0
  105. data/lib/stupidedi/schema/generation/support.rb +78 -0
  106. data/lib/stupidedi/schema/generation/support_modules_generator.rb +126 -0
  107. data/lib/stupidedi/schema/generation/version_modules.rb +35 -0
  108. data/lib/stupidedi/schema/generation.rb +94 -0
  109. data/lib/stupidedi/schema/interchange_def.rb +103 -0
  110. data/lib/stupidedi/schema/loop_def.rb +156 -0
  111. data/lib/stupidedi/schema/repeat_count.rb +86 -0
  112. data/lib/stupidedi/schema/segment_def.rb +122 -0
  113. data/lib/stupidedi/schema/segment_req.rb +46 -0
  114. data/lib/stupidedi/schema/segment_use.rb +99 -0
  115. data/lib/stupidedi/schema/simple_element_def.rb +51 -0
  116. data/lib/stupidedi/schema/simple_element_use.rb +83 -0
  117. data/lib/stupidedi/schema/syntax_note.rb +52 -0
  118. data/lib/stupidedi/schema/table_def.rb +178 -0
  119. data/lib/stupidedi/schema/transaction_set_def.rb +125 -0
  120. data/lib/stupidedi/schema.rb +30 -0
  121. data/lib/stupidedi/sets.rb +42 -0
  122. data/lib/stupidedi/transaction_sets/builder/dsl.rb +192 -0
  123. data/lib/stupidedi/transaction_sets/builder.rb +188 -0
  124. data/lib/stupidedi/transaction_sets/common/implementations/element_reqs.rb +37 -0
  125. data/lib/stupidedi/transaction_sets/common/implementations/segment_reqs.rb +31 -0
  126. data/lib/stupidedi/transaction_sets/common/implementations.rb +11 -0
  127. data/lib/stupidedi/transaction_sets/common.rb +8 -0
  128. data/lib/stupidedi/transaction_sets/validation/ambiguity.rb +395 -0
  129. data/lib/stupidedi/transaction_sets/validation/implementation.rb +12 -0
  130. data/lib/stupidedi/transaction_sets/validation.rb +9 -0
  131. data/lib/stupidedi/transaction_sets.rb +25 -0
  132. data/lib/stupidedi/values/abstract_element_val.rb +19 -0
  133. data/lib/stupidedi/values/abstract_val.rb +133 -0
  134. data/lib/stupidedi/values/composite_element_val.rb +102 -0
  135. data/lib/stupidedi/values/functional_group_val.rb +105 -0
  136. data/lib/stupidedi/values/interchange_val.rb +102 -0
  137. data/lib/stupidedi/values/invalid_envelope_val.rb +61 -0
  138. data/lib/stupidedi/values/invalid_segment_val.rb +89 -0
  139. data/lib/stupidedi/values/loop_val.rb +73 -0
  140. data/lib/stupidedi/values/repeated_element_val.rb +113 -0
  141. data/lib/stupidedi/values/segment_val.rb +105 -0
  142. data/lib/stupidedi/values/segment_val_group.rb +20 -0
  143. data/lib/stupidedi/values/simple_element_val.rb +80 -0
  144. data/lib/stupidedi/values/table_val.rb +69 -0
  145. data/lib/stupidedi/values/transaction_set_val.rb +69 -0
  146. data/lib/stupidedi/values/transmission_val.rb +56 -0
  147. data/lib/stupidedi/values.rb +22 -0
  148. data/lib/stupidedi/version.rb +4 -0
  149. data/lib/stupidedi/versions/common/element_reqs.rb +13 -0
  150. data/lib/stupidedi/versions/common/element_types/an.rb +386 -0
  151. data/lib/stupidedi/versions/common/element_types/dt.rb +572 -0
  152. data/lib/stupidedi/versions/common/element_types/id.rb +304 -0
  153. data/lib/stupidedi/versions/common/element_types/nn.rb +312 -0
  154. data/lib/stupidedi/versions/common/element_types/operators.rb +128 -0
  155. data/lib/stupidedi/versions/common/element_types/r.rb +342 -0
  156. data/lib/stupidedi/versions/common/element_types/simple_element.rb +73 -0
  157. data/lib/stupidedi/versions/common/element_types/tm.rb +347 -0
  158. data/lib/stupidedi/versions/common/element_types.rb +29 -0
  159. data/lib/stupidedi/versions/common/segment_reqs.rb +15 -0
  160. data/lib/stupidedi/versions/common/syntax_notes.rb +172 -0
  161. data/lib/stupidedi/versions/common.rb +11 -0
  162. data/lib/stupidedi/versions.rb +25 -0
  163. data/lib/stupidedi/writer/claredi.rb +178 -0
  164. data/lib/stupidedi/writer/default.rb +119 -0
  165. data/lib/stupidedi/writer.rb +7 -0
  166. data/lib/stupidedi/zipper/abstract_cursor.rb +351 -0
  167. data/lib/stupidedi/zipper/dangling_cursor.rb +103 -0
  168. data/lib/stupidedi/zipper/edited_cursor.rb +157 -0
  169. data/lib/stupidedi/zipper/memoized_cursor.rb +133 -0
  170. data/lib/stupidedi/zipper/path.rb +132 -0
  171. data/lib/stupidedi/zipper/root_cursor.rb +120 -0
  172. data/lib/stupidedi/zipper/stack_cursor.rb +107 -0
  173. data/lib/stupidedi/zipper.rb +45 -0
  174. data/lib/stupidedi.rb +69 -0
  175. data/lib/tediparse.rb +1 -0
  176. metadata +249 -0
data/doc/Generating.md ADDED
@@ -0,0 +1,332 @@
1
+ Generating X12
2
+ ==============
3
+
4
+ > This page is about generating X12 *documents* (emitting EDI). To generate
5
+ > the *grammar* itself from ASC X12 Table Data, see
6
+ > [Generating-Grammars.md](Generating-Grammars.md).
7
+
8
+ Tediparse has a simple interface for generating X12 documents. Once you have
9
+ defined a transaction set or implementation guide (see [Defining](Defining.md))
10
+ — by hand, or by [generating it from X12 Table Data](Generating-Grammars.md) —
11
+ you can generate well-formed documents using [`BuilderDsl`][1].
12
+
13
+ [1]: Stupidedi/Parser/BuilderDsl.html
14
+
15
+ Tediparse ships the engine only — you supply the grammar definitions.
16
+ `spec/support/synthetic/` in the source tree is a worked authoring example;
17
+ the snippets below use `MyApp::*` placeholders for the application-provided
18
+ constants.
19
+
20
+ Configuration
21
+ -------------
22
+
23
+ Minimal configuration is needed so the parser can load the correct definitions
24
+ and ensure well-formedness. The configuration below links an interchange
25
+ version code to an instance of [`InterchangeDef`][2], a functional group
26
+ version code to an instance of [`FunctionalGroupDef`][3], and links a
27
+ transaction set (identified by three elements) to an instance of
28
+ [`TransactionSetDef`][4].
29
+
30
+ [2]: Stupidedi/Schema/InterchangeDef.html
31
+ [3]: Stupidedi/Schema/FunctionalGroupDef.html
32
+ [4]: Stupidedi/Schema/TransactionSetDef.html
33
+
34
+ config = Stupidedi::Config.new
35
+
36
+ # Link the ISA12 interchange version code to your interchange definition
37
+ config.interchange.register("00501") do
38
+ MyApp::Envelopes::FiveOhOne
39
+ end
40
+
41
+ # Link the GS08 functional-group version code to your functional group def
42
+ config.functional_group.register("005010") do
43
+ MyApp::FunctionalGroups::FiftyTen
44
+ end
45
+
46
+ # Link (GS08, GS01, ST01) to your transaction-set definition
47
+ config.transaction_set.register("005010X222", "HC", "837") do
48
+ MyApp::TransactionSets::X222A1_HC837
49
+ end
50
+
51
+ # Instantiate a new BuilderDsl
52
+ b = Stupidedi::Parser::BuilderDsl.build(config)
53
+
54
+ [`InterchangeDef`][2] specifies which segments can occur directly in the
55
+ interchange envelope (e.g. `ISA` and `ISE`), the order in which they can occur,
56
+ and the definition of those segments.
57
+
58
+ [`FunctionalGroupDef`][3] specifies which segments can occur directly in the
59
+ functional group envelope (e.g. `GS` and `GE`), the order in which they occur,
60
+ and the definition of _every_ segment that can occur _inside_ the envelope.
61
+
62
+ [`TransactionSetDef`][4] specifies the structure of an X12 message, including
63
+ groupings of segments (e.g. tables and loops), and the order in which segments
64
+ and groups of segments may occur. The definition of each segment is given by the
65
+ parent [`FunctionalGroupDef`][3].
66
+
67
+ Generating a Segment
68
+ --------------------
69
+
70
+ The [`BuilderDsl`][1] API uses `method_missing` to dynamically respond to method
71
+ calls. If the method name matches the format of a segment identifier, a segment
72
+ is constructed and added to the parse tree. The arguments to the method call
73
+ should be the elements of the segment.
74
+
75
+ b.ISA("00", "", # authorization information
76
+ "00", "", # authentication information
77
+ "ZZ", "SUBMITTER ID", # submitter identification
78
+ "ZZ", "RECEIVER ID", # recipient identification
79
+ Time.now.utc, # date
80
+ Time.now.utc, # time
81
+ "^", # repetition separator
82
+ "00501", # interchange version
83
+ "333666999", # control number
84
+ "1", # acknowledgement request
85
+ "T", # usage indicator
86
+ ":") # component separator
87
+
88
+ > _Note_: The repetition separator "ˆ" and component separator ":" have no
89
+ > special meaning when generating X12. These elements are only meaningful to
90
+ > Stupidedi when parsing X12 from an input stream (see
91
+ > [Parsing X12](Parsing.md)).
92
+
93
+ Alternatively, the [`#segment!`][5] method can be used to avoid the method
94
+ lookup overhead incurred by `method_missing`.
95
+
96
+ [5]: Stupidedi/Parser/BuilderDsl.html#segment!-instance_method
97
+
98
+ ### Simple Elements
99
+
100
+ Simple elements of _any_ type can be constructed from Strings (this is how X12
101
+ is parsed from a file), but certain element types can be constructed from other
102
+ types of Ruby values.
103
+
104
+ The description of each element type below uses the era-agnostic
105
+ `Versions::Common::ElementTypes` vocabulary that tediparse ships. The
106
+ [`SimpleElementDef`][7] and [`SimpleElementVal`][8] classes define the minimal
107
+ interfaces that are extended by subclasses like `AN` and `StringVal`. See the
108
+ [`Versions::Common::ElementTypes`][6] namespace for the primitives.
109
+
110
+ [6]: Stupidedi/Versions/Common/ElementTypes.html
111
+ [7]: Stupidedi/Schema/SimpleElementDef.html
112
+ [8]: Stupidedi/Values/SimpleElementVal.html
113
+
114
+ #### Strings
115
+
116
+ String elements (declared with type `AN`) can be constructed from any value that
117
+ responds to `#to_s`. The constructed element is a [`StringVal`][9].
118
+
119
+ [9]: Stupidedi/Versions/Common/ElementTypes/AN.html
120
+
121
+ #### Identifiers
122
+
123
+ Identifier elements (declared with type `ID`) can be constructed from any value
124
+ that responds to `#to_s`. The constructed element is an [`IdentifierVal`][10].
125
+
126
+ [10]: Stupidedi/Versions/Common/ElementTypes/ID.html
127
+
128
+ #### Dates
129
+
130
+ Date elements (declared with type `DT`) can be constructed from a `String` of
131
+ either six or eight characters, and from any value that responds to `#year`,
132
+ `#month`, and `#day`. This includes the `Date`, `Time`, and `DateTime` classes
133
+ included with the standard Ruby libraries. The constructed element is a
134
+ [`DateVal`][11].
135
+
136
+ [11]: Stupidedi/Versions/Common/ElementTypes/DT.html
137
+
138
+ #### Times
139
+
140
+ Time elements (declared with type `TM`) can be constructed from a `String` of
141
+ either two, four, six, or more than six characters, and from the `Time` and
142
+ `DateTime` value types. The constructed element is a [`TimeVal`][12].
143
+
144
+ [12]: Stupidedi/Versions/Common/ElementTypes/TM.html
145
+
146
+ #### Numbers
147
+
148
+ Numeric and decimal elements (declared with type `Nn` and `R`, respectively) can
149
+ be constructed from any value that responds to `#to_d`. The constructed element
150
+ is a [`FixnumVal`][13] or [`FloatVal`][14].
151
+
152
+ [13]: Stupidedi/Versions/Common/ElementTypes/Nn.html
153
+ [14]: Stupidedi/Versions/Common/ElementTypes/R.html
154
+
155
+ ### Composite Elements
156
+
157
+ Composite elements are constructed with the [`#composite`][24] method. The
158
+ constructed element is a [`CompositeElementVal`][15]. Arguments to the method
159
+ call should be the component elements. For instance, to generate an `HI` segment
160
+ with three composite elements:
161
+
162
+ b.HI(b.composite("ABK", "7868"),
163
+ b.composite("ABF", "052"),
164
+ b.composite("ABF", "E9283"))
165
+
166
+ [15]: Stupidedi/Values/CompositeElementVal.html
167
+
168
+ ### Repeated Elements
169
+
170
+ Repeated elements are constructed using the [`#repeated`][25] method. The
171
+ constructed element is a [`RepeatedElementVal`][16]. The arguments to the method
172
+ call should be either all simple elements or all composite elements, according
173
+ to the element definition.
174
+
175
+ b.AK9(b.repeated("R", "X", "E"), 1, 1, 0)
176
+
177
+ b.IK4(b.repeated(
178
+ b.composite(3, 1),
179
+ b.composite(4)),
180
+ 1068, 7, "B")
181
+
182
+ [16]: Stupidedi/Values/RepeatedElementVal.html
183
+
184
+ Element Placeholders
185
+ --------------------
186
+
187
+ Because [`BuilderDsl`][1] builds a parse tree as segments are generated, it can
188
+ infer and validate certain information about the elements in a segment.
189
+
190
+ ### Blank Elements
191
+
192
+ Blank elements (simple, composite, and repeated) can be generated from a `nil`
193
+ argument, but you can improve readability by using [`#blank`][26], and
194
+ no-argument calls to [`#composite`][24] and [`#repeated`][25]. These are
195
+ arguably more self-documenting.
196
+
197
+ b.AK9(nil, 1, 1, 0)
198
+ b.AK9(b.repeated, 1, 1, 0)
199
+
200
+ b.HI(nil, b.composite(b.blank, "052"))
201
+ b.HI(b.composite, b.composite(b.blank, "052"))
202
+
203
+ Also, if fewer than the defined number of elements are given as arguments, the
204
+ missing arguments generate blank elements. For example, the following statements
205
+ both generate the same segment.
206
+
207
+ b.ST("835", "1234")
208
+ b.ST("835", "1234", b.blank)
209
+
210
+ ### Default Elements
211
+
212
+ Certain elements are declared with a single value in [`#allowed_values`][18].
213
+ These are usually qualifier elements, like `NM103`, whose value adds little
214
+ readability. These values can be inferred by [`BuilderDsl`][1] when the
215
+ [`#default`][27] placeholder is used. For example, when generating the X222 837P
216
+ the following statements generate the same segment.
217
+
218
+ b.BHT("0019", "00", control_number, Time.now.utc, Time.now.utc, "CH")
219
+
220
+ b.BHT(b.default, "00", control_number, Time.now.utc, Time.now.utc, "CH")
221
+
222
+ When a default value cannot be inferred, a [`ParseError`][17] is thrown. Note
223
+ that repeated and composite elements can only be generated by [`#default`][27],
224
+ when they are declared `NOT USED`.
225
+
226
+ [17]: Stupidedi/Exceptions/ParseError.html
227
+ [18]: Stupidedi/Schema/SimpleElementUse.html#allowed_values-instance_method
228
+
229
+ ### Unused Elements
230
+
231
+ For elements that are declared to never be sent, `nil` or [`#blank`][26] will
232
+ generate the empty element; however using [`#not_used`][28] is arguably more
233
+ self-documenting. If [`BuilderDsl`][1] determines that the element is not
234
+ declared as such, it will raise a [`ParseError`][17]. For example the X221 835
235
+ document declares ST03 with `NOT USED`:
236
+
237
+ b.ST("835", "1234", b.not_used)
238
+
239
+ Syntax Validation
240
+ -----------------
241
+
242
+ The parse tree that [`BuilderDsl`][1] maintains is used to ensure only
243
+ well-formed X12 is generated. This means segments occur in the correct order
244
+ and have the correct number and type of elements.
245
+
246
+ ### Segment Order
247
+
248
+ The order in which segments may occur is defined by the active
249
+ [`TransactionSetDef`][4], [`FunctionalGroupDef`][3], and [`InterchangeDef`][2].
250
+ Internally, an instance of [`StateMachine`][19] is used to both
251
+ incrementally build the parse tree and keep track of which segments can occur
252
+ from the given state. The [`#successors`][21] method will return one or more
253
+ [`InstructionTable`][20] values which enumerate the segments that may occur in
254
+ the current state:
255
+
256
+ pp b.successors
257
+
258
+ [InstructionTable(
259
+ 1: Instruction[REF: Subscriber Secon..](pop: 0, drop: 0),
260
+ 2: Instruction[REF: Property and Cas..](pop: 0, drop: 0),
261
+ 3: Instruction[PER: Property and Cas..](pop: 0, drop: 3),
262
+ 4: Instruction[NM1: Subscriber Name ](pop: 1, drop: 0, push: LoopState),
263
+ 5: Instruction[NM1: Payer Name ](pop: 1, drop: 0, push: LoopState),
264
+ 6: Instruction[CLM: Claim Informatio..](pop: 1, drop: 2, push: LoopState),
265
+ 7: Instruction[ HL: Subscriber Hiera..](pop: 2, drop: 0, push: LoopState),
266
+ 8: Instruction[ HL: Billing Provider..](pop: 3, drop: 0, push: TableState),
267
+ 9: Instruction[ HL: Subscriber Hiera..](pop: 3, drop: 0, push: TableState),
268
+ 10: Instruction[ HL: Patient Hierachi..](pop: 3, drop: 0, push: TableState),
269
+ 11: Instruction[ SE: Transaction Set ..](pop: 3, drop: 4, push: TableState),
270
+ 12: Instruction[ ST](pop: 4, drop: 0, push: TransactionSetState),
271
+ 13: Instruction[ GE: Functional Group..](pop: 4, drop: 2),
272
+ 14: Instruction[ GS](pop: 5, drop: 0, push: FunctionalGroupState),
273
+ 15: Instruction[IEA: Interchange Cont..](pop: 5, drop: 2),
274
+ 16: Instruction[ISA](pop: 6, drop: 0, push: InterchangeState))]
275
+
276
+ [19]: Stupidedi/Parser/StateMachine.html
277
+ [20]: Stupidedi/Parser/InstructionTable.html
278
+ [21]: Stupidedi/Parser/BuilderDsl.html#successors-instance_method
279
+
280
+ The above output pertains to the X222 837 implementation guide. The output shows
281
+ a single active [`InstructionTable`][20] and the segments it is able to accept.
282
+ For more information about how the parser works, see [Parsing X12](Parsing.md).
283
+ Attempting to generate a segment that is not a member of at least one of the
284
+ instruction tables will cause a [`ParseError`][17] to be raised.
285
+
286
+ b.N3("SUITE 111", "1234 OCEAN BLVD")
287
+ #=> Segment N3 cannot occur here (Stupidedi::Exceptions::ParseError)
288
+
289
+ ### Element Types
290
+
291
+ The [`InterchangeDef`][2] and [`TransactionSetDef`][4] classes both respond to
292
+ `#segment_dict`, which allows looking up a [`SegmentDef`][23] by segment its
293
+ identifier. The [`SegmentDef`][23] indicates the number of elements and their
294
+ types (composite, repeated, simple). This information allows [`BuilderDsl`][1]
295
+ to raise a [`ParseError`][17] on the following conditions:
296
+
297
+ [23]: Stupidedi/Schema/SegmentDef.html
298
+
299
+ Generating a composite element where a simple or repeated element is defined:
300
+
301
+ b.NM1(b.composite(nil, "B"), nil)
302
+ #=> NM101 is a simple element (Stupidedi::Exceptions::ParseError)
303
+
304
+ Generating a simple element where a repeated or composite element is defined:
305
+
306
+ b.REF(nil, nil, nil, "D")
307
+ #=> REF04 is a composite element (Stupidedi::Exceptions::ParseError)
308
+
309
+ b.DMG(nil, nil, nil, nli, "E")
310
+ #=> DMG05 is a repeatable element (Stupiedi::Exceptions::ParseError)
311
+
312
+ b.DMG(nil, nil, nil, nil, b.repeated("E"))
313
+ #=> DMG05 is a composite element (Stupidedi::Exceptions::ParseError)
314
+
315
+ Generating a repeated element where a non-repeated element is defined:
316
+
317
+ b.NM1(b.repeated("A", "B"))
318
+ #=> NM101 is a simple element (Stupidedi::Exceptions::ParseError)
319
+
320
+ Generating more than the defined number of elements:
321
+
322
+ b.N3(nil, nil, nil)
323
+ #=> N3 has only 4 elements (Stupidedi::Exceptions::ParseError)
324
+
325
+ b.REF(nil, nil, nil, b.composite("A", "B", "C", "D", "E", "F", "G"))
326
+ #=> REF04 has only 6 components (Stupidedi::Exceptions::ParseError)
327
+
328
+ [24]: Stupidedi/Parser/BuilderDsl.html#composite-instance_method
329
+ [25]: Stupidedi/Parser/BuilderDsl.html#repeated-instance_method
330
+ [26]: Stupidedi/Parser/BuilderDsl.html#blank-instance_method
331
+ [27]: Stupidedi/Parser/BuilderDsl.html#default-instance_method
332
+ [28]: Stupidedi/Parser/BuilderDsl.html#not_used-instance_method