DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlNode.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.Generic;
19 using System.Collections.ObjectModel;
20 
22 
23 using Irony.Ast;
24 using Irony.Parsing;
25 
26 namespace Deveel.Data.Sql.Parser {
32  public class SqlNode : ISqlNode, IAstNodeInit {
33  public SqlNode() {
34  ChildNodes = new ReadOnlyCollection<ISqlNode>(new ISqlNode[0]);
35  Tokens = new ReadOnlyCollection<Token>(new Token[0]);
36  }
37 
42  protected ISqlNode Parent { get; private set; }
43 
48  protected string NodeName { get; private set; }
49 
55  protected IEnumerable<ISqlNode> ChildNodes { get; private set; }
56 
63  protected IEnumerable<Token> Tokens { get; private set; }
64 
65 
66  void IAstNodeInit.Init(AstContext context, ParseTreeNode parseNode) {
67  NodeName = parseNode.Term == null ? GetType().Name : parseNode.Term.Name;
68 
69  var tokens = new List<Token>();
70 
71  var iToken = parseNode.FindToken();
72  if (iToken != null) {
73  tokens.Add(new Token(iToken.Location.Column, iToken.Location.Line, iToken.Text, iToken.Value));
74  }
75 
76  var childNodes = new List<ISqlNode>();
77 
78  foreach (var childNode in parseNode.ChildNodes) {
79  ISqlNode child;
80  if (childNode.Term is KeyTerm) {
81  var childIToken = childNode.FindToken();
82  child = new SqlKeyNode(new Token(childIToken.Location.Column, childIToken.Location.Line, childIToken.Text, childIToken.Value));
83  } else {
84  child = (ISqlNode)childNode.AstNode;
85  }
86 
87  child = OnChildNode(child);
88 
89  if (child != null) {
90  if (child is ISqlChildNode)
91  (child as ISqlChildNode).SetParent(this);
92 
93  childNodes.Add(child);
94  tokens.AddRange(child.Tokens);
95  }
96  }
97 
98  ChildNodes = childNodes.ToArray();
99  Tokens = tokens.ToArray();
100 
101  OnNodeInit();
102  }
103 
105  get { return NodeName; }
106  }
107 
109  get { return Parent; }
110  }
111 
112  IEnumerable<ISqlNode> ISqlNode.ChildNodes {
113  get { return ChildNodes; }
114  }
115 
116  IEnumerable<Token> ISqlNode.Tokens {
117  get { return Tokens; }
118  }
119 
124  protected virtual void OnNodeInit() {
125  }
126 
135  protected virtual ISqlNode OnChildNode(ISqlNode node) {
136  return node;
137  }
138  }
139 }
Defines the contract for nodes in an AST model for a SQL grammar analysis and parsing.
Definition: ISqlNode.cs:25
Represents a keyword found during the compilation of a source text.
Definition: SqlKeyNode.cs:25
virtual ISqlNode OnChildNode(ISqlNode node)
During the initialization of the node from the parser, this method is called for every child node add...
Definition: SqlNode.cs:135
string NodeName
Gets the name of the node analyzed from the parser.
Definition: ISqlNode.cs:29
virtual void OnNodeInit()
After the initialization of the node from the parser, this method is invoked to let the specific init...
Definition: SqlNode.cs:124
This is a single token within a string parsed.
Definition: Token.cs:28
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
ISqlNode Parent
Gets a reference to the parent ISqlNode, if any.
Definition: ISqlNode.cs:34
The default implementation of ISqlNode, that is a node in the text analysis parsing of SQL commands...
Definition: SqlNode.cs:32