-
Notifications
You must be signed in to change notification settings - Fork 0
/
gherkin.rb
4396 lines (3727 loc) · 116 KB
/
gherkin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Gherkin
module Formatter
class Hashable
def to_hash
instance_variables.inject({}) do |hash, ivar|
value = instance_variable_get(ivar)
value = value.to_hash if value.respond_to?(:to_hash)
if Array === value
value = value.map do |e|
e.respond_to?(:to_hash) ? e.to_hash : e
end
end
hash[ivar[1..-1]] = value unless [[], nil].index(value)
hash
end
end
end
end
end
require 'gherkin/native'
require 'gherkin/formatter/hashable'
module Gherkin
module Formatter
module Model
class BasicStatement < Hashable
attr_reader :comments, :keyword, :name, :line
def initialize(comments, keyword, name, line)
@comments, @keyword, @name, @line = comments, keyword, name, line
end
def line_range
first = @comments.any? ? @comments[0].line : first_non_comment_line
first..line
end
def first_non_comment_line
@line
end
end
class DescribedStatement < BasicStatement
attr_reader :description
def initialize(comments, keyword, name, description, line)
super(comments, keyword, name, line)
@description = description
end
end
class TagStatement < DescribedStatement
attr_reader :tags, :id
def initialize(comments, tags, keyword, name, description, line, id)
super(comments, keyword, name, description, line)
@tags = tags
@id = id
end
def first_non_comment_line
@tags.any? ? @tags[0].line : @line
end
end
class Feature < TagStatement
native_impl('gherkin')
def replay(formatter)
formatter.feature(self)
end
end
class Background < DescribedStatement
native_impl('gherkin')
def initialize(comments, keyword, name, description, line)
super(comments, keyword, name, description, line)
@type = "background"
end
def replay(formatter)
formatter.background(self)
end
end
class Scenario < TagStatement
native_impl('gherkin')
def initialize(comments, tags, keyword, name, description, line, id)
super(comments, tags, keyword, name, description, line, id)
@type = "scenario"
end
def replay(formatter)
formatter.scenario(self)
end
end
class ScenarioOutline < TagStatement
native_impl('gherkin')
def initialize(comments, tags, keyword, name, description, line, id)
super(comments, tags, keyword, name, description, line, id)
@type = "scenario_outline"
end
def replay(formatter)
formatter.scenario_outline(self)
end
end
class Examples < TagStatement
native_impl('gherkin')
attr_accessor :rows # needs to remain mutable for filters
def initialize(comments, tags, keyword, name, description, line, id, rows)
super(comments, tags, keyword, name, description, line, id)
@rows = rows
end
def replay(formatter)
formatter.examples(self)
end
class Builder
def initialize(*args)
@args = *args
@rows = nil
end
def row(comments, cells, line, id)
@rows ||= []
@rows << ExamplesTableRow.new(comments, cells, line, id)
end
def replay(formatter)
build.replay(formatter)
end
def build
Examples.new(*(@args << @rows))
end
end
end
class Step < BasicStatement
native_impl('gherkin')
attr_reader :rows, :doc_string
def initialize(comments, keyword, name, line, rows, doc_string)
super(comments, keyword, name, line)
@rows, @doc_string = rows, doc_string
end
def line_range
range = super
if(rows)
range = range.first..rows[-1].line
elsif(doc_string)
range = range.first..doc_string.line_range.last
end
range
end
def replay(formatter)
formatter.step(self)
end
def outline_args
offset = 0
name.scan(/<[^<]*>/).map do |val|
offset = name.index(val, offset)
Argument.new(offset, val)
end
end
class Builder
def initialize(*args)
@args = *args
@rows = nil
@doc_string = nil
end
def row(comments, cells, line, id)
@rows ||= []
@rows << DataTableRow.new(comments, cells, line)
end
def doc_string(string, content_type, line)
@doc_string = Formatter::Model::DocString.new(string, content_type, line)
end
def replay(formatter)
build.replay(formatter)
end
def build
Step.new(*(@args << @rows << @doc_string))
end
end
end
class Comment < Hashable
native_impl('gherkin')
attr_reader :value, :line
def initialize(value, line)
@value, @line = value, line
end
end
class Tag < Hashable
native_impl('gherkin')
attr_reader :name, :line
def initialize(name, line)
@name, @line = name, line
end
def eql?(tag)
@name.eql?(tag.name)
end
def hash
@name.hash
end
end
class DocString < Hashable
native_impl('gherkin')
attr_reader :value, :content_type, :line
def initialize(value, content_type, line)
@value, @content_type, @line = value, content_type, line
end
def line_range
line_count = value.split(/\r?\n/).length
line..(line+line_count+1)
end
end
class Row < Hashable
attr_reader :comments, :cells, :line
def initialize(comments, cells, line)
@comments, @cells, @line = comments, cells, line
end
end
class DataTableRow < Row
native_impl('gherkin')
end
class ExamplesTableRow < Row
native_impl('gherkin')
attr_reader :id
def initialize(comments, cells, line, id)
super(comments, cells, line)
@id = id
end
end
class Match < Hashable
native_impl('gherkin')
attr_reader :arguments, :location
def initialize(arguments, location)
@arguments, @location = arguments, location
end
def replay(formatter)
formatter.match(self)
end
end
class Result < Hashable
native_impl('gherkin')
attr_reader :status, :duration, :error_message
def initialize(status, duration, error_message)
@status, @duration, @error_message = status, duration, error_message
end
def replay(formatter)
formatter.result(self)
end
end
end
end
end
require 'gherkin/native'
require 'gherkin/formatter/model'
module Gherkin
module Listener
# Adapter from the "raw" Gherkin <tt>Listener</tt> API
# to the slightly more high-level <tt>Formatter</tt> API,
# which is easier to implement (less state to keep track of).
class FormatterListener
native_impl('gherkin')
def initialize(formatter)
@formatter = formatter
@stash = Stash.new
end
def comment(value, line)
@stash.comment Formatter::Model::Comment.new(value, line)
end
def tag(name, line)
@stash.tag Formatter::Model::Tag.new(name, line)
end
def feature(keyword, name, description, line)
@stash.feature(name) do |comments, tags, id|
replay Formatter::Model::Feature.new(comments, tags, keyword, name, description, line, id)
end
end
def background(keyword, name, description, line)
@stash.feature_element(name) do |comments, tags, id|
replay Formatter::Model::Background.new(comments, keyword, name, description, line)
end
end
def scenario(keyword, name, description, line)
replay_step_or_examples
@stash.feature_element(name) do |comments, tags, id|
replay Formatter::Model::Scenario.new(comments, tags, keyword, name, description, line, id)
end
end
def scenario_outline(keyword, name, description, line)
replay_step_or_examples
@stash.feature_element(name) do |comments, tags, id|
replay Formatter::Model::ScenarioOutline.new(comments, tags, keyword, name, description, line, id)
end
end
def examples(keyword, name, description, line)
replay_step_or_examples
@stash.examples(name) do |comments, tags, id|
@current_builder = Formatter::Model::Examples::Builder.new(comments, tags, keyword, name, description, line, id)
end
end
def step(keyword, name, line)
replay_step_or_examples
@stash.basic_statement do |comments, id|
@current_builder = Formatter::Model::Step::Builder.new(comments, keyword, name, line)
end
end
def row(cells, line)
@stash.basic_statement do |comments, id|
@current_builder.row(comments, cells, line, id)
end
end
def doc_string(content_type, value, line)
@current_builder.doc_string(value, content_type, line)
end
def eof
replay_step_or_examples
@formatter.eof
end
def syntax_error(state, ev, legal_events, uri, line)
@formatter.syntax_error(state, ev, legal_events, uri, line)
end
private
def replay(element)
element.replay(@formatter)
end
class Stash
attr_reader :comments, :tags, :ids
def initialize
@comments, @tags, @ids = [], [], []
@row_index = 0
end
def comment(comment)
@comments << comment
end
def feature(name)
@feature_id = id(name)
yield @comments, @tags, @feature_id
@comments, @tags = [], []
end
def feature_element(name)
@feature_element_id = "#{@feature_id};#{id(name)}"
yield @comments, @tags, @feature_element_id
@comments, @tags = [], []
end
def examples(name)
@examples_id = "#{@feature_element_id};#{id(name)}"
@row_index = 0
yield @comments, @tags, @examples_id
@comments, @tags = [], []
end
def basic_statement
@row_index += 1
yield @comments, "#{@examples_id};#{@row_index}"
@comments = []
end
def tag(tag)
@tags << tag
end
def id(name)
(name || '').gsub(/[\s_]/, '-').downcase
end
end
def replay_step_or_examples
return unless @current_builder
replay(@current_builder)
@current_builder = nil
end
end
end
end
require 'gherkin/i18n'
require 'gherkin/lexer/i18n_lexer'
require 'gherkin/native'
require 'gherkin/listener/formatter_listener'
module Gherkin
module Parser
class ParseError < StandardError
def initialize(state, new_state, expected_states, uri, line)
super("Parse error at #{uri}:#{line}. Found #{new_state} when expecting one of: #{expected_states.join(', ')}. (Current state: #{state}).")
end
end
class Parser
native_impl('gherkin')
# Initialize the parser. +machine_name+ refers to a state machine table.
def initialize(formatter, raise_on_error=true, machine_name='root', force_ruby=false)
@formatter = formatter
@listener = Listener::FormatterListener.new(@formatter)
@raise_on_error = raise_on_error
@machine_name = machine_name
@machines = []
push_machine(@machine_name)
@lexer = Gherkin::Lexer::I18nLexer.new(self, force_ruby)
end
def parse(gherkin, feature_uri, line_offset)
@formatter.uri(feature_uri)
@line_offset = line_offset
@lexer.scan(gherkin)
end
def i18n_language
@lexer.i18n_language
end
def errors
@lexer.errors
end
# Doesn't yet fall back to super
def method_missing(method, *args)
# TODO: Catch exception and call super
event(method.to_s, args[-1])
@listener.__send__(method, *args)
if method == :eof
pop_machine
push_machine(@machine_name)
end
end
def event(ev, line)
l = line ? @line_offset+line : nil
machine.event(ev, l) do |state, legal_events|
if @raise_on_error
raise ParseError.new(state, ev, legal_events, @feature_uri, l)
else
# Only used for testing
@listener.syntax_error(state, ev, legal_events, @feature_uri, l)
end
end
end
def push_machine(name)
@machines.push(Machine.new(self, name))
end
def pop_machine
@machines.pop
end
def machine
@machines[-1]
end
def expected
machine.expected
end
def force_state(state)
machine.instance_variable_set('@state', state)
end
class Machine
def initialize(parser, name)
@parser = parser
@name = name
@transition_map = transition_map(name)
@state = name
end
def event(ev, line)
states = @transition_map[@state]
raise "Unknown state: #{@state.inspect} for machine #{@name}" if states.nil?
new_state = states[ev]
case new_state
when "E"
yield @state, expected
when /push\((.+)\)/
@parser.push_machine($1)
@parser.event(ev, line)
when "pop()"
@parser.pop_machine()
@parser.event(ev, line)
else
raise "Unknown transition: #{ev.inspect} among #{states.inspect} for machine #{@name}" if new_state.nil?
@state = new_state
end
end
def expected
allowed = @transition_map[@state].find_all { |_, action| action != "E" }
allowed.collect { |state| state[0] }.sort - ['eof']
end
private
@@transition_maps = {}
def transition_map(name)
@@transition_maps[name] ||= build_transition_map(name)
end
def build_transition_map(name)
table = transition_table(name)
events = table.shift[1..-1]
table.inject({}) do |machine, actions|
state = actions.shift
machine[state] = Hash[*events.zip(actions).flatten]
machine
end
end
def transition_table(name)
state_machine_reader = StateMachineReader.new
lexer = Gherkin::I18n.new('en').lexer(state_machine_reader)
machine = File.dirname(__FILE__) + "/#{name}.txt"
lexer.scan(File.read(machine))
state_machine_reader.rows
end
class StateMachineReader
attr_reader :rows
def initialize
@rows = []
end
def uri(uri)
end
def row(row, line_number)
@rows << row
end
def eof
end
end
end
end
end
end
class Class
def native_impl(lib)
# no-op
end
end
if defined?(JRUBY_VERSION)
require 'gherkin/native/java'
elsif ENV['GHERKIN_JS_NATIVE']
require 'gherkin/native/therubyracer'
else
require 'gherkin/native/null'
end
module Gherkin
module Rubify
if defined?(JRUBY_VERSION)
# Translate Java objects to Ruby.
# This is especially important to convert java.util.List coming
# from Java and back to a Ruby Array.
def rubify(o)
case(o)
when Java.java.util.Collection, Array
o.map{|e| rubify(e)}
when Java.gherkin.formatter.model.DocString
require 'gherkin/formatter/model'
Formatter::Model::DocString.new(o.content_type, o.value, o.line)
else
o
end
end
else
def rubify(o)
o
end
end
end
end
require 'psych/tree_builder'
module Psych
module Handlers
class DocumentStream < Psych::TreeBuilder # :nodoc:
def initialize &block
super
@block = block
end
def start_document version, tag_directives, implicit
n = Nodes::Document.new version, tag_directives, implicit
push n
end
def end_document implicit_end = !streaming?
@last.implicit_end = implicit_end
@block.call pop
end
end
end
end
require 'psych/json/ruby_events'
require 'psych/json/yaml_events'
module Psych
module JSON
class Stream < Psych::Visitors::JSONTree
include Psych::JSON::RubyEvents
include Psych::Streaming
class Emitter < Psych::Stream::Emitter # :nodoc:
include Psych::JSON::YAMLEvents
end
end
end
end
module Psych
module JSON
module YAMLEvents # :nodoc:
def start_document version, tag_directives, implicit
super(version, tag_directives, !streaming?)
end
def end_document implicit_end = !streaming?
super(implicit_end)
end
def start_mapping anchor, tag, implicit, style
super(anchor, nil, implicit, Nodes::Mapping::FLOW)
end
def start_sequence anchor, tag, implicit, style
super(anchor, nil, implicit, Nodes::Sequence::FLOW)
end
def scalar value, anchor, tag, plain, quoted, style
if "tag:yaml.org,2002:null" == tag
super('null', nil, nil, true, false, Nodes::Scalar::PLAIN)
else
super
end
end
end
end
end
require 'psych/json/yaml_events'
module Psych
module JSON
###
# Psych::JSON::TreeBuilder is an event based AST builder. Events are sent
# to an instance of Psych::JSON::TreeBuilder and a JSON AST is constructed.
class TreeBuilder < Psych::TreeBuilder
include Psych::JSON::YAMLEvents
end
end
end
module Psych
###
# Psych::Stream is a streaming YAML emitter. It will not buffer your YAML,
# but send it straight to an IO.
#
# Here is an example use:
#
# stream = Psych::Stream.new($stdout)
# stream.start
# stream.push({:foo => 'bar'})
# stream.finish
#
# YAML will be immediately emitted to $stdout with no buffering.
#
# Psych::Stream#start will take a block and ensure that Psych::Stream#finish
# is called, so you can do this form:
#
# stream = Psych::Stream.new($stdout)
# stream.start do |em|
# em.push(:foo => 'bar')
# end
#
class Stream < Psych::Visitors::YAMLTree
class Emitter < Psych::Emitter # :nodoc:
def end_document implicit_end = !streaming?
super
end
def streaming?
true
end
end
include Psych::Streaming
end
end
# format.rb: Written by Tadayoshi Funaba 1999-2011
# date.rb: Written by Tadayoshi Funaba 1998-2011
require 'date_core'
require 'date/format'
class Date
class Infinity < Numeric # :nodoc:
include Comparable
def initialize(d=1) @d = d <=> 0 end
def d() @d end
protected :d
def zero? () false end
def finite? () false end
def infinite? () d.nonzero? end
def nan? () d.zero? end
def abs() self.class.new end
def -@ () self.class.new(-d) end
def +@ () self.class.new(+d) end
def <=> (other)
case other
when Infinity; return d <=> other.d
when Numeric; return d
else
begin
l, r = other.coerce(self)
return l <=> r
rescue NoMethodError
end
end
nil
end
def coerce(other)
case other
when Numeric; return -d, d
else
super
end
end
def to_f
return 0 if @d == 0
if @d > 0
Float::INFINITY
else
-Float::INFINITY
end
end
end
end
require 'date'
module Psych
DEPRECATED = __FILE__ # :nodoc:
module DeprecatedMethods # :nodoc:
attr_accessor :taguri
attr_accessor :to_yaml_style
end
def self.quick_emit thing, opts = {}, &block # :nodoc:
warn "#{caller[0]}: YAML.quick_emit is deprecated" if $VERBOSE && !caller[0].start_with?(File.dirname(__FILE__))
target = eval 'self', block.binding
target.extend DeprecatedMethods
metaclass = class << target; self; end
metaclass.send(:define_method, :encode_with) do |coder|
target.taguri = coder.tag
target.to_yaml_style = coder.style
block.call coder
end
target.psych_to_yaml unless opts[:nodump]
end
def self.load_documents yaml, &block
if $VERBOSE
warn "#{caller[0]}: load_documents is deprecated, use load_stream"
end
list = load_stream yaml
return list unless block_given?
list.each(&block)
end
def self.detect_implicit thing
warn "#{caller[0]}: detect_implicit is deprecated" if $VERBOSE
return '' unless String === thing
return 'null' if '' == thing
ScalarScanner.new.tokenize(thing).class.name.downcase
end
def self.add_ruby_type type_tag, &block
warn "#{caller[0]}: add_ruby_type is deprecated, use add_domain_type" if $VERBOSE
domain = 'ruby.yaml.org,2002'
key = ['tag', domain, type_tag].join ':'
@domain_types[key] = [key, block]
end
def self.add_private_type type_tag, &block
warn "#{caller[0]}: add_private_type is deprecated, use add_domain_type" if $VERBOSE
domain = 'x-private'
key = [domain, type_tag].join ':'
@domain_types[key] = [key, block]
end
def self.tagurize thing
warn "#{caller[0]}: add_private_type is deprecated, use add_domain_type" if $VERBOSE
return thing unless String === thing
"tag:yaml.org,2002:#{thing}"
end
def self.read_type_class type, reference
warn "#{caller[0]}: read_type_class is deprecated" if $VERBOSE
_, _, type, name = type.split ':', 4
reference = name.split('::').inject(reference) do |k,n|
k.const_get(n.to_sym)
end if name
[type, reference]
end
def self.object_maker klass, hash
warn "#{caller[0]}: object_maker is deprecated" if $VERBOSE
klass.allocate.tap do |obj|
hash.each { |k,v| obj.instance_variable_set(:"@#{k}", v) }
end
end
end
class Object
undef :to_yaml_properties rescue nil
def to_yaml_properties # :nodoc:
instance_variables
end
end
class Object
def self.yaml_tag url
Psych.add_tag(url, self)
end
# FIXME: rename this to "to_yaml" when syck is removed
###
# call-seq: to_yaml(options = {})
#
# Convert an object to YAML. See Psych.dump for more information on the
# available +options+.
def psych_to_yaml options = {}
Psych.dump self, options
end
remove_method :to_yaml rescue nil
alias :to_yaml :psych_to_yaml
end
class Module
def psych_yaml_as url
return if caller[0].end_with?('rubytypes.rb')
if $VERBOSE
warn "#{caller[0]}: yaml_as is deprecated, please use yaml_tag"
end
Psych.add_tag(url, self)
end
remove_method :yaml_as rescue nil
alias :yaml_as :psych_yaml_as
end
if defined?(::IRB)
module Kernel
def psych_y *objects
puts Psych.dump_stream(*objects)
end
remove_method :y rescue nil
alias y psych_y
private :y
end
end
module Psych
###
# If an object defines +encode_with+, then an instance of Psych::Coder will
# be passed to the method when the object is being serialized. The Coder
# automatically assumes a Psych::Nodes::Mapping is being emitted. Other
# objects like Sequence and Scalar may be emitted if +seq=+ or +scalar=+ are
# called, respectively.
class Coder
attr_accessor :tag, :style, :implicit, :object
attr_reader :type, :seq
def initialize tag
@map = {}
@seq = []
@implicit = false
@type = :map
@tag = tag
@style = Psych::Nodes::Mapping::BLOCK
@scalar = nil
@object = nil
end
def scalar *args
if args.length > 0
warn "#{caller[0]}: Coder#scalar(a,b,c) is deprecated" if $VERBOSE
@tag, @scalar, _ = args
@type = :scalar
end
@scalar
end
# Emit a map. The coder will be yielded to the block.
def map tag = @tag, style = @style
@tag = tag
@style = style
yield self if block_given?
@map
end
# Emit a scalar with +value+ and +tag+
def represent_scalar tag, value
self.tag = tag