class Sthx::Tree
- Sthx::Tree
- Reference
- Object
Defined in:
synthax/tree.crConstructors
Instance Method Summary
-
#attributes : AttributeView
Returns an
AttributeViewcontaining attributes defined on this tree. -
#begin : Int32
Returns the index of the first character (not byte!) of this tree in the source string.
-
#children
Returns a
ChildrenViewcontaining this tree's children. -
#dig(*steps) : Tree
Same as
#dig?, but raisesKeyErrorwhen it is impossible to follow one of the specified steps. -
#dig?(*steps) : Tree | Nil
Follows deeper into the tree guided by steps.
-
#end : Int32
Returns the index of the character (not byte!) immediately following the last character of this tree in the source string.
-
#getattr(name : String) : String
Same as
#getattrbut raisesKeyErrorif the attribute cannot be found. -
#getattr?(name : String) : String | Nil
Returns the value of an attribute with the given name, if one is defined on this tree.
-
#id : String
Returns the capture id of this tree (see
Sthx.capture). -
#inspect(io)
Appends a multiline string view of this tree to io.
-
#map(cls : T.class, &fn : Tree, Array(T) -> T) : T forall T
Converts tree objects to objects of type
Tusing fn. -
#span : Int32
Returns the amount of characters (not bytes!) that this tree matches in the source string.
-
#test?(object : String) : Bool
Returns
trueif this tree's capture id matches the given string object. -
#test?(object : Range) : Bool
Returns
trueif this tree's position in the source string matches the given range object. -
#test?(object : Array) : Bool
Returns
trueif 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. -
#test?(object : Symbol) : Bool
Returns
trueif this tree matches a predicate named by the given object symbol. -
#test?(object : Tuple) : Bool
Returns
trueif any child of this tree#test?s true for object. -
#test?(head, *rest) : Bool
Returns
trueif all of head, rest#test?true for this tree. -
#to_s(io)
Same as
#inspect.
Constructor Detail
Instance Method Detail
Returns an AttributeView containing attributes defined on this tree.
# tree : Tree
tree.attributes["foo"]?
tree.attributes.size
tree.attributes.each do |name, value|
# ...
end
# ...
Returns the index of the first character (not byte!) of this tree in the source string.
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
# ...
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
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?
Returns the index of the character (not byte!) immediately following the last character of this tree in the source string.
Same as #getattr but raises KeyError if the attribute cannot be found.
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.
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.
Returns the amount of characters (not bytes!) that this tree matches in the source string.
Returns true if this tree's capture id matches the given string object.
Returns true if this tree's position in the source string matches
the given range object.
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>]: returnstrueif this tree has an attribute with the given name, and its value is equal to the expected value.
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.
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}
)
# ...