DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Properties | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
Deveel.Data.Sql.Parser.SqlDefaultParser Class Reference
Inheritance diagram for Deveel.Data.Sql.Parser.SqlDefaultParser:
Deveel.Data.Sql.Parser.ISqlParser

Classes

class  Timer
 

Public Member Functions

 SqlDefaultParser (SqlGrammarBase grammar)
 
void Dispose ()
 
SqlParseResult Parse (string input)
 Analyzes and parses the input and results an object that describes the parsed nodes in a tree that can be used to construct objects for interacting with the system. More...
 

Properties

string Dialect [get]
 
- Properties inherited from Deveel.Data.Sql.Parser.ISqlParser
string Dialect [get]
 Gets the string identifying the SQL dialect used to parse More...
 

Private Member Functions

void Dispose (bool disposing)
 
ISqlNode ParseNode (string sqlSource, ICollection< SqlParseError > errors, out long parseTime)
 

Static Private Member Functions

static void BuildErrors (ParseTree tree, ICollection< SqlParseError > errors, LogMessageList logMessages)
 
static string FormInfoMessage (ParseTree tree, int line, int column)
 
static IList< Irony.Parsing.Token > FindTokensFrom (ParseTree tree, int line, int column)
 
static IList< Irony.Parsing.Token > FindTokensTo (ParseTree tree, int line, int column)
 

Private Attributes

LanguageData languageData
 
Irony.Parsing.Parser parser
 

Detailed Description

Definition at line 28 of file SqlDefaultParser.cs.

Constructor & Destructor Documentation

Deveel.Data.Sql.Parser.SqlDefaultParser.SqlDefaultParser ( SqlGrammarBase  grammar)
inline

Definition at line 32 of file SqlDefaultParser.cs.

32  {
33  languageData = new LanguageData(grammar);
34  parser = new Irony.Parsing.Parser(languageData);
35 
36  if (!languageData.CanParse())
37  throw new InvalidOperationException();
38  }

Member Function Documentation

static void Deveel.Data.Sql.Parser.SqlDefaultParser.BuildErrors ( ParseTree  tree,
ICollection< SqlParseError errors,
LogMessageList  logMessages 
)
inlinestaticprivate

Definition at line 101 of file SqlDefaultParser.cs.

101  {
102  foreach (var logMessage in logMessages) {
103  if (logMessage.Level == ErrorLevel.Error) {
104  var line = logMessage.Location.Line;
105  var column = logMessage.Location.Column;
106  var locationMessage = FormInfoMessage(tree, line, column);
107  var expected = logMessage.ParserState.ReportedExpectedSet.ToArray();
108  var infoMessage = String.Format("A parse error occurred near '{0}' in the source", locationMessage);
109  if (expected.Length > 0)
110  infoMessage = String.Format("{0}. Expected {1}", infoMessage, String.Join(", ", expected));
111 
112  errors.Add(new SqlParseError(infoMessage, line, column));
113  }
114  }
115  }
A long string in the system.
static string FormInfoMessage(ParseTree tree, int line, int column)
ErrorLevel
In case of error messages, this enumerates the level of severity of the error.
Definition: ErrorLevel.cs:24
void Deveel.Data.Sql.Parser.SqlDefaultParser.Dispose ( bool  disposing)
inlineprivate

Definition at line 40 of file SqlDefaultParser.cs.

40  {
41  parser = null;
42  languageData = null;
43  }
void Deveel.Data.Sql.Parser.SqlDefaultParser.Dispose ( )
inline

Definition at line 45 of file SqlDefaultParser.cs.

45  {
46  Dispose(true);
47  GC.SuppressFinalize(this);
48  }
static IList<Irony.Parsing.Token> Deveel.Data.Sql.Parser.SqlDefaultParser.FindTokensFrom ( ParseTree  tree,
int  line,
int  column 
)
inlinestaticprivate

Definition at line 144 of file SqlDefaultParser.cs.

144  {
145  var tokens = tree.Tokens;
146  bool startCollect = false;
147 
148  var result = new List<Irony.Parsing.Token>();
149  foreach (var token in tokens) {
150  if (token.Location.Line == line &&
151  token.Location.Column == column) {
152  startCollect = true;
153  }
154 
155  if (startCollect)
156  result.Add(token);
157  }
158 
159  return result.ToList();
160  }
static IList<Irony.Parsing.Token> Deveel.Data.Sql.Parser.SqlDefaultParser.FindTokensTo ( ParseTree  tree,
int  line,
int  column 
)
inlinestaticprivate

Definition at line 162 of file SqlDefaultParser.cs.

162  {
163  var tokens = tree.Tokens;
164 
165  var result = new List<Irony.Parsing.Token>();
166  foreach (var token in tokens) {
167  if (token.Location.Line == line &&
168  token.Location.Column == column)
169  break;
170 
171  result.Add(token);
172  }
173 
174  return result.ToList();
175  }
static string Deveel.Data.Sql.Parser.SqlDefaultParser.FormInfoMessage ( ParseTree  tree,
int  line,
int  column 
)
inlinestaticprivate

Definition at line 117 of file SqlDefaultParser.cs.

117  {
118  const int tokensBeforeCount = 10;
119  const int tokensAfterCount = 10;
120 
121  var tokensBefore = FindTokensTo(tree, line, column).Reverse().ToList();
122  var tokensAfter = FindTokensFrom(tree, line, column);
123 
124  var countTokensBefore = System.Math.Min(tokensBefore.Count, tokensBeforeCount);
125  var countTokensAfter = System.Math.Min(tokensAfterCount, tokensAfter.Count);
126 
127  var takeTokensBefore = tokensBefore.Take(countTokensBefore).Reverse();
128  var takeTokensAfter = tokensAfter.Take(countTokensAfter);
129 
130  var sb = new StringBuilder();
131  foreach (var token in takeTokensBefore) {
132  sb.Append(token.Text);
133  sb.Append(" ");
134  }
135 
136  foreach (var token in takeTokensAfter) {
137  sb.Append(token.Text);
138  sb.Append(" ");
139  }
140 
141  return sb.ToString();
142  }
static IList< Irony.Parsing.Token > FindTokensFrom(ParseTree tree, int line, int column)
static IList< Irony.Parsing.Token > FindTokensTo(ParseTree tree, int line, int column)
SqlParseResult Deveel.Data.Sql.Parser.SqlDefaultParser.Parse ( string  input)
inline

Analyzes and parses the input and results an object that describes the parsed nodes in a tree that can be used to construct objects for interacting with the system.

Parameters
inputThe input string to be parsed.
Returns
Returns an instance of SqlParseResult that includes information about the analyzed input.
Exceptions
ArgumentNullExceptionIf the provided input is null or empty.
See also
SqlParseResult

Implements Deveel.Data.Sql.Parser.ISqlParser.

Definition at line 54 of file SqlDefaultParser.cs.

54  {
55  var result = new SqlParseResult(Dialect);
56 
57  var timer = new Timer();
58 
59  try {
60  long time;
61  var node = ParseNode(input, result.Errors, out time);
62  result.RootNode = node;
63  } catch (Exception ex) {
64  // TODO: form a better exception
65  result.Errors.Add(new SqlParseError(ex.Message, 0, 0));
66  } finally {
67  timer.Dispose();
68  result.ParseTime = timer.Elapsed;
69  }
70 
71  return result;
72  }
ISqlNode ParseNode(string sqlSource, ICollection< SqlParseError > errors, out long parseTime)
ISqlNode Deveel.Data.Sql.Parser.SqlDefaultParser.ParseNode ( string  sqlSource,
ICollection< SqlParseError errors,
out long  parseTime 
)
inlineprivate

Definition at line 74 of file SqlDefaultParser.cs.

74  {
75  var tree = parser.Parse(sqlSource);
76  parseTime = tree.ParseTimeMilliseconds;
77 
78  if (tree.Status == ParseTreeStatus.Error) {
79  BuildErrors(tree, errors, tree.ParserMessages);
80  return null;
81  }
82 
83  var astContext = new AstContext(languageData) {
84  DefaultNodeType = typeof (SqlNode),
85  DefaultIdentifierNodeType = typeof (IdentifierNode)
86  };
87 
88  var astCompiler = new AstBuilder(astContext);
89  astCompiler.BuildAst(tree);
90 
91  if (tree.HasErrors())
92  BuildErrors(tree, errors, tree.ParserMessages);
93 
94  var node = (ISqlNode) tree.Root.AstNode;
95  if (node.NodeName == "root")
96  node = node.ChildNodes.FirstOrDefault();
97 
98  return node;
99  }
static void BuildErrors(ParseTree tree, ICollection< SqlParseError > errors, LogMessageList logMessages)

Member Data Documentation

LanguageData Deveel.Data.Sql.Parser.SqlDefaultParser.languageData
private

Definition at line 29 of file SqlDefaultParser.cs.

Irony.Parsing.Parser Deveel.Data.Sql.Parser.SqlDefaultParser.parser
private

Definition at line 30 of file SqlDefaultParser.cs.

Property Documentation

string Deveel.Data.Sql.Parser.SqlDefaultParser.Dialect
get

Definition at line 50 of file SqlDefaultParser.cs.


The documentation for this class was generated from the following file: