Class: TreeHaver::Backends::Psych::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_haver/backends/psych.rb

Overview

Psych tree wrapper

Wraps a Psych::Nodes::Stream to provide TreeHaver-compatible tree interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, source) ⇒ Tree

Create a new tree wrapper

Parameters:

  • stream (::Psych::Nodes::Stream)

    Psych stream node

  • source (String)

    Original source



198
199
200
201
202
# File 'lib/tree_haver/backends/psych.rb', line 198

def initialize(stream, source)
  @inner_tree = stream
  @source = source
  @lines = source.lines
end

Instance Attribute Details

#inner_tree::Psych::Nodes::Stream (readonly)

Returns The underlying Psych stream.

Returns:

  • (::Psych::Nodes::Stream)

    The underlying Psych stream



189
190
191
# File 'lib/tree_haver/backends/psych.rb', line 189

def inner_tree
  @inner_tree
end

#sourceString (readonly)

Returns The original source.

Returns:

  • (String)

    The original source



192
193
194
# File 'lib/tree_haver/backends/psych.rb', line 192

def source
  @source
end

Instance Method Details

#commentsArray

Get comments from the document

Psych doesn’t preserve comments in the AST by default.

Returns:

  • (Array)

    Empty array



235
236
237
# File 'lib/tree_haver/backends/psych.rb', line 235

def comments
  []
end

#errorsArray

Get parse errors

Psych raises exceptions on parse errors rather than recording them, so this is always empty if we got a tree.

Returns:

  • (Array)

    Empty array (no errors if parsing succeeded)



219
220
221
# File 'lib/tree_haver/backends/psych.rb', line 219

def errors
  []
end

#inspectString

Returns human-readable representation.

Returns:

  • (String)

    human-readable representation



240
241
242
# File 'lib/tree_haver/backends/psych.rb', line 240

def inspect
  "#<TreeHaver::Backends::Psych::Tree documents=#{@inner_tree.children&.size || 0}>"
end

#root_nodeNode

Get the root node

For YAML, the stream is the root. We wrap it as a Node.

Returns:

  • (Node)

    Root node



209
210
211
# File 'lib/tree_haver/backends/psych.rb', line 209

def root_node
  Node.new(@inner_tree, @source, @lines)
end

#warningsArray

Get parse warnings

Returns:

  • (Array)

    Empty array (Psych doesn’t produce warnings)



226
227
228
# File 'lib/tree_haver/backends/psych.rb', line 226

def warnings
  []
end