class

Sthx::Tree

Inherits Reference < Object

Constructors

new(id : String, begin __arg0 : Int32, span : Int32, data : TreeData)
Source

Instance methods

attributes

Returns an AttributeView containing attributes defined on this tree.

# tree : Tree

tree.attributes["foo"]?
tree.attributes.size
tree.attributes.each do |name, value|
  # ...
end

# ...
Source
begin

Returns the index of the first character (not byte!) of this tree in the source string.

Source
children

Returns a ChildrenView containing this tree's children.

# tree : Tree

tree.children["foo"] # => Array(Tree)
tree.children.size   # => Int
tree.children.each do |child|
  pp child # => Tree
end

# ...
Source
dig(*steps) : Tree

Same as dig?, but raises KeyError when it is impossible to follow one of the specified steps.

# tree : Tree
tree.dig("range", ":begin:", 0) # => Tree
tree.dig("foo", 0, "bar")       # => Tree
Source
dig?(*steps) : Tree | Nil

Follows deeper into the tree guided by steps. Returns the final tree, or nil if could not follow one of the steps.

The following types of steps are available:

  • String: follow to a child whose id is equal to the given string.
  • Int: follow to a child whose index in the parent tree is equal to the given integer.
# tree : Tree
tree.dig?("range", ":begin:", 0) # => Tree?
tree.dig?("foo", 0, "bar")       # => Tree?
Source
end

Returns the index of the character (not byte!) immediately following the last character of this tree in the source string.

Source
getattr(name : String) : String

Same as getattr but raises KeyError if the attribute cannot be found.

Source
getattr?(name : String) : String | Nil

Returns the value of an attribute with the given name, if one is defined on this tree. Otherwise, returns nil. Attributes are usually defined using Rule.keep.

If you can't guarantee you'll need the read attribute's value prefer to use attributes as it's going to return a string view into the source code which uses no memory.

Source
id

Returns the capture id of this tree (see Sthx.capture).

Source
inspect(io)

Appends a multiline string view of this tree to io.

Source
map(cls : T.class, &fn : Tree, Array(T) -> T) : T forall T

Converts tree objects to objects of type T using fn.

  • Calls fn with leaf trees and an empty children array.
  • Calls fn with branch trees and a children array of converted leaves.
  • And so on, up to and including the root tree.
Source
span

Returns the amount of characters (not bytes!) that this tree matches in the source string.

Source
test?(object : String) : Bool

Returns true if this tree's capture id matches the given string object.

Source
test?(object : Range) : Bool

Returns true if this tree's position in the source string matches the given range object.

Source
test?(object : Array) : Bool

Returns true if this tree matches a predicate named by the first element of the given array object, whereas the predicate's arguments are specified by the rest of the elements in the array. Here is a list of possible predicates and their arguments:

  • [:hasattr, <attribute name>, <expected attribute value>]: returns true if this tree has an attribute with the given name, and its value is equal to the expected value.
Source
test?(object : Symbol) : Bool

Returns true if this tree matches a predicate named by the given object symbol. Here is a list of possible predicate names:

  • :empty: returns true if this tree is empty.
Source
test?(object : Tuple) : Bool

Returns true if any child of this tree test?s true for object.

Source
test?(head, *rest) : Bool

Returns true if all of head, rest test? true for this tree.

# tree : Tree

tree.test?(
  "root", 0...19,
  {"nlist", 0...8,
   {"n", 0...1, [:hasattr, "value", "1"]},
   {"n", 2...4, [:hasattr, "value", "23"]},
   {"n", 5...6, [:hasattr, "value", "3"]},
   {"n", 7...8, [:hasattr, "value", "4"]}},
  {"nlist", 9...15,
   {"n", 9...10, [:hasattr, "value", "5"]},
   {"n", 11...13, [:hasattr, "value", "69"]},
   {"n", 14...15, [:hasattr, "value", "7"]}},
  {"nlist", 16...16, :empty}
)

# ...
Source
to_s(io)

Same as inspect.

Source

Nested types