Class: Norikra::Query::ASTNode
- Inherits:
-
Object
- Object
- Norikra::Query::ASTNode
- Defined in:
- lib/norikra/query/ast.rb
Direct Known Subclasses
ASTEventPropNode, ASTExpression, ASTLibFunctionArgItemNode, ASTLibFunctionNode, ASTPatternNode, ASTSelectionElementNode, ASTStreamNode, ASTSubSelectNode
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
-
#tree ⇒ Object
Returns the value of attribute tree.
Instance Method Summary collapse
- #chain(*nodes) ⇒ Object
- #child ⇒ Object
- #fields(default_target = nil, known_targets_aliases = []) ⇒ Object
-
#find(type) ⇒ Object
only one, depth-first search.
- #has_a?(name) ⇒ Boolean
-
#initialize(name, children, tree) ⇒ ASTNode
constructor
A new instance of ASTNode.
-
#listup(*type) ⇒ Object
search all nodes that has ‘type’.
- #nodetype?(*sym) ⇒ Boolean
- #to_a ⇒ Object
Constructor Details
#initialize(name, children, tree) ⇒ ASTNode
Returns a new instance of ASTNode.
887 888 889 890 891 |
# File 'lib/norikra/query/ast.rb', line 887 def initialize(name, children, tree) @name = name @children = children @tree = tree end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
885 886 887 |
# File 'lib/norikra/query/ast.rb', line 885 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
885 886 887 |
# File 'lib/norikra/query/ast.rb', line 885 def name @name end |
#tree ⇒ Object
Returns the value of attribute tree.
885 886 887 |
# File 'lib/norikra/query/ast.rb', line 885 def tree @tree end |
Instance Method Details
#chain(*nodes) ⇒ Object
897 898 899 |
# File 'lib/norikra/query/ast.rb', line 897 def chain(*nodes) nodes.reduce(self){|n, next_node| n && n.has_a?(next_node) ? n.child : nil } end |
#child ⇒ Object
909 910 911 |
# File 'lib/norikra/query/ast.rb', line 909 def child @children.first end |
#fields(default_target = nil, known_targets_aliases = []) ⇒ Object
940 941 942 |
# File 'lib/norikra/query/ast.rb', line 940 def fields(default_target=nil, known_targets_aliases=[]) @children.map{|c| c.nodetype?(:subquery) ? [] : c.fields(default_target, known_targets_aliases)}.reduce(&:+) || [] end |
#find(type) ⇒ Object
only one, depth-first search
913 914 915 916 917 918 919 920 921 922 |
# File 'lib/norikra/query/ast.rb', line 913 def find(type) # only one, depth-first search return self if type.is_a?(String) && @name == type || nodetype?(type) @children.each do |c| next if type != :subquery && c.nodetype?(:subquery) r = c.find(type) return r if r end nil end |
#has_a?(name) ⇒ Boolean
893 894 895 |
# File 'lib/norikra/query/ast.rb', line 893 def has_a?(name) @children.size == 1 && (@children.first.name == name || @children.first.nodetype?(name)) end |
#listup(*type) ⇒ Object
search all nodes that has ‘type’
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
# File 'lib/norikra/query/ast.rb', line 924 def listup(*type) # search all nodes that has 'type' if type.size > 1 return type.map{|t| self.listup(t) }.reduce(&:+) end type = type.first result = [] result.push(self) if type.is_a?(String) && @name == type || nodetype?(type) @children.each do |c| next if type != :subquery && c.nodetype?(:subquery) result.push(*c.listup(type)) end result end |
#nodetype?(*sym) ⇒ Boolean
901 902 903 |
# File 'lib/norikra/query/ast.rb', line 901 def nodetype?(*sym) false end |
#to_a ⇒ Object
905 906 907 |
# File 'lib/norikra/query/ast.rb', line 905 def to_a [@name] + @children.map{|c| c.children.size > 0 ? c.to_a : c.name} end |