class Sthx::Tree

Defined in:

synthax/tree.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(id : String, begin __arg0 : Int32, span : Int32, data : TreeData) #

[View source]

Instance Method Detail

def attributes : AttributeView #

Returns an AttributeView containing attributes defined on this tree.

# tree : Tree

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

# ...

[View source]
def begin : Int32 #

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


[View source]
def 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

# ...

[View source]
def 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

[View source]
def 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?

[View source]
def end : Int32 #

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


[View source]
def getattr(name : String) : String #

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


[View source]
def 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.


[View source]
def id : String #

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


[View source]
def inspect(io) #

Appends a multiline string view of this tree to io.


[View source]
def 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.

[View source]
def span : Int32 #

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


[View source]
def test?(object : String) : Bool #

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


[View source]
def test?(object : Range) : Bool #

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


[View source]
def 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.

[View source]
def 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.

[View source]
def test?(object : Tuple) : Bool #

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


[View source]
def 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}
)

# ...

[View source]
def to_s(io) #

Same as #inspect.


[View source]