DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlNodeExtensions.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Runtime.InteropServices;
22 
23 namespace Deveel.Data.Sql.Parser {
27  public static class SqlNodeExtensions {
28  public static TNode FindNode<TNode>(this ISqlNode node) where TNode : class, ISqlNode {
29  var foundNode = node.FindNode(typeof(TNode));
30  if (foundNode == null)
31  return null;
32 
33  return (TNode)foundNode;
34  }
35 
36  public static ISqlNode FindNode(this ISqlNode node, Type nodeType) {
37  foreach (var childNode in node.ChildNodes) {
38  if (nodeType.IsInstanceOfType(childNode))
39  return childNode;
40 
41  var foundNode = childNode.FindNode(nodeType);
42  if (foundNode != null)
43  return foundNode;
44  }
45 
46  return null;
47  }
48 
49  public static TNode FindNodeOf<TNode>(this ISqlNode node, string nodeName) where TNode : class, ISqlNode {
50  var parent = node.FindByName(nodeName);
51  if (parent == null)
52  return null;
53 
54  return parent.FindNode<TNode>();
55  }
56 
57  public static ISqlNode FindByName(this ISqlNode node, string nodeName) {
58  foreach (var childNode in node.ChildNodes) {
59  if (childNode.NodeName.Equals(nodeName))
60  return childNode;
61 
62  var foundNode = childNode.FindByName(nodeName);
63  if (foundNode != null)
64  return foundNode;
65  }
66 
67  return null;
68  }
69 
70  public static bool HasOptionalNode(this ISqlNode node, string nodeName) {
71  var foundNode = node.FindByName(nodeName);
72  if (foundNode == null)
73  return false;
74 
75  return foundNode.ChildNodes.Any();
76  }
77 
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>();
81  }
82 
83  public static IEnumerable FindNodes(this ISqlNode node, Type nodeType) {
84  var nodes = new List<ISqlNode>();
85 
86  foreach (var childNode in node.ChildNodes) {
87  if (nodeType.IsInstanceOfType(childNode)) {
88  nodes.Add(childNode);
89  }
90 
91  nodes.AddRange(childNode.FindNodes(nodeType).Cast<ISqlNode>());
92  }
93 
94  return nodes.ToArray();
95  }
96 
97  public static int StartColumn(this ISqlNode node) {
98  if (node == null || node.Tokens == null)
99  return 0;
100 
101  var token = node.Tokens.FirstOrDefault();
102  return token == null ? 0 : token.Column;
103  }
104 
105  public static int EndColumn(this ISqlNode node) {
106  if (node == null || node.Tokens == null)
107  return 0;
108 
109  var token = node.Tokens.LastOrDefault();
110  return token == null ? 0 : token.Column;
111  }
112 
113  public static int StartLine(this ISqlNode node) {
114  if (node == null || node.Tokens == null)
115  return 0;
116 
117  var token = node.Tokens.FirstOrDefault();
118  return token == null ? 0 : token.Line;
119  }
120 
121  public static int EndLine(this ISqlNode node) {
122  if (node == null || node.Tokens == null)
123  return 0;
124 
125  var token = node.Tokens.LastOrDefault();
126  return token == null ? 0 : token.Line;
127  }
128  }
129 }
static int StartColumn(this ISqlNode node)
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
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.
Definition: ISqlNode.cs:39
IEnumerable< Token > Tokens
Gets an enumeration of the tokens composing the this node.
Definition: ISqlNode.cs:45
static IEnumerable FindNodes(this ISqlNode node, Type nodeType)
static ISqlNode FindNode(this ISqlNode node, Type nodeType)
static int EndLine(this ISqlNode node)