19 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
23 namespace Deveel.Data.Sql.Parser {
28 public static TNode FindNode<TNode>(
this ISqlNode node) where TNode :
class,
ISqlNode {
29 var foundNode = node.FindNode(typeof(TNode));
30 if (foundNode == null)
33 return (TNode)foundNode;
37 foreach (var childNode
in node.ChildNodes) {
38 if (nodeType.IsInstanceOfType(childNode))
41 var foundNode = childNode.FindNode(nodeType);
42 if (foundNode != null)
49 public static TNode FindNodeOf<TNode>(
this ISqlNode node,
string nodeName) where TNode :
class,
ISqlNode {
50 var parent = node.FindByName(nodeName);
54 return parent.FindNode<TNode>();
58 foreach (var childNode
in node.ChildNodes) {
59 if (childNode.NodeName.Equals(nodeName))
62 var foundNode = childNode.FindByName(nodeName);
63 if (foundNode != null)
71 var foundNode = node.FindByName(nodeName);
72 if (foundNode == null)
78 public static IEnumerable<TNode> FindNodes<TNode>(
this ISqlNode node) where TNode :
class,
ISqlNode {
79 var foundNodes = node.FindNodes(typeof(TNode));
80 return foundNodes.Cast<TNode>();
84 var nodes =
new List<ISqlNode>();
86 foreach (var childNode
in node.ChildNodes) {
87 if (nodeType.IsInstanceOfType(childNode)) {
91 nodes.AddRange(childNode.FindNodes(nodeType).Cast<
ISqlNode>());
94 return nodes.ToArray();
98 if (node == null || node.
Tokens == null)
101 var token = node.
Tokens.FirstOrDefault();
102 return token == null ? 0 : token.Column;
106 if (node == null || node.
Tokens == null)
109 var token = node.
Tokens.LastOrDefault();
110 return token == null ? 0 : token.Column;
114 if (node == null || node.
Tokens == null)
117 var token = node.
Tokens.FirstOrDefault();
118 return token == null ? 0 : token.Line;
122 if (node == null || node.
Tokens == null)
125 var token = node.
Tokens.LastOrDefault();
126 return token == null ? 0 : token.Line;
static int StartColumn(this ISqlNode node)
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
static bool HasOptionalNode(this ISqlNode node, string nodeName)
static int EndColumn(this ISqlNode node)
static ISqlNode FindByName(this ISqlNode node, string nodeName)
static int StartLine(this ISqlNode node)
Extension methods to ISqlNode for diagnostics and other purposes.
IEnumerable< ISqlNode > ChildNodes
Gets a read-only enumeration of the children nodes, if any.
IEnumerable< Token > Tokens
Gets an enumeration of the tokens composing the this node.
static IEnumerable FindNodes(this ISqlNode node, Type nodeType)
static ISqlNode FindNode(this ISqlNode node, Type nodeType)
static int EndLine(this ISqlNode node)