public static Query Parse(Benumerator <char> en) { var s = en.Save(); en.MoveNext(); if (en.Current == ' ') { en.MoveNext(); } if (en.Current == 'C') { return(CreateTable.Parse(s)); } else if (en.Current == 'F') { return(Get.Parse(s)); } else if (en.Current == 'I') { return(Insert.Parse(s)); } else { throw new Exception("Unrecognized first command"); } }
public static new Get Parse(Benumerator <char> en) { // Skipping first whitespace and failing if empty if (!en.MoveNext() || en.Current == ' ' && !en.MoveNext()) { throw new Exception("failed to parse. No Input"); } var from = From.Parse(en); var select = Select.Parse(en); if (from != null && select.columns.Length > 0) { return(new Get { from = from, columns = select.columns, select = select }); } else { return(null); } }
public static void SkipWhitespace(Benumerator <char> en) { if (en.Current == ' ') { en.MoveNext(); } }
public static bool HasValue(Benumerator <char> en, string value) { foreach (Char c in value) { if (en.Current != c) { return(false); } en.MoveNext(); } return(true); }
public static From Parse(Benumerator <char> en) { if (en.Current != 'F' || !en.MoveNext() || en.Current != 'R' || !en.MoveNext() || en.Current != 'O' || !en.MoveNext() || en.Current != 'M' || !en.MoveNext() || en.Current != ' ' ) { throw new Exception("failed to parse FROM. Expected FROM keyword"); } List <char> tableName = new List <char>(); while (en.MoveNext() && en.Current != ' ') { tableName.Add(en.Current); } if (!en.MoveNext()) { throw new Exception("failed to parse FROM. Unexpected end up input"); } return(new From { table = new string(tableName.ToArray()) }); }
public static string ParseWord(Benumerator <char> en, bool possiblyAtEnd = false) { Parser.SkipWhitespace(en); List <char> work = new List <char>(); do { work.Add(en.Current); }while (en.MoveNext() && en.Current != ' ' && en.Current != ',' && en.Current != '(' && en.Current != ')'); return(new String(work.ToArray())); }
public static ColumnDefinition Parse(Benumerator <char> en) { ColumnDefinition colDef = new ColumnDefinition(); List <char> col; while (en.Current == ' ' || en.Current == ',') { en.MoveNext(); } // get name col = new List <char>(); while (en.Current != ' ' && en.Current != ',') { col.Add(en.Current); en.MoveNext(); } colDef.name = new string(col.ToArray()).Trim(); if (en.Current == ',') { throw new Exception("Failed parsing Column Definition. Missing type"); } en.MoveNext(); col = new List <char>(); while (en.Current != ' ' && en.Current != ',') { col.Add(en.Current); en.MoveNext(); } colDef.type = new string(col.ToArray()).Trim(); return(colDef); }
public static IEnumerable <SelectColumn> Parse(Benumerator <char> en, bool possiblyAtEnd = false) { do { Parser.SkipWhitespace(en); string columnName = Parser.ParseWord(en, possiblyAtEnd); if (!string.IsNullOrWhiteSpace(columnName)) { yield return(new SelectColumn { columnName = columnName }); } Parser.SkipWhitespace(en); }while (en.Current == ',' && en.MoveNext()); }
private static IEnumerable <SelectColumn> _Parse(Benumerator <char> en) { // Next value MUST be "SELECT " if (en.Current != 'S' || !en.MoveNext() || en.Current != 'E' || !en.MoveNext() || en.Current != 'L' || !en.MoveNext() || en.Current != 'E' || !en.MoveNext() || en.Current != 'C' || !en.MoveNext() || en.Current != 'T' || !en.MoveNext() || en.Current != ' ' || !en.MoveNext() ) { throw new Exception("failed to parse SELECT. Expecting SELECT keyword"); } // I am very lazily forcing the use of * for now foreach (var col in SelectColumn.Parse(en, true)) { yield return(col); } }
public static new CreateTable Parse(Benumerator <char> en) { if (!en.MoveNext() || en.Current == ' ' && !en.MoveNext()) { throw new Exception("failed to parse. No Input"); } if (en.Current != 'C' || !en.MoveNext() || en.Current != 'R' || !en.MoveNext() || en.Current != 'E' || !en.MoveNext() || en.Current != 'A' || !en.MoveNext() || en.Current != 'T' || !en.MoveNext() || en.Current != 'E' || !en.MoveNext() || en.Current != ' ' || !en.MoveNext() || en.Current != 'T' || !en.MoveNext() || en.Current != 'A' || !en.MoveNext() || en.Current != 'B' || !en.MoveNext() || en.Current != 'L' || !en.MoveNext() || en.Current != 'E' || !en.MoveNext() || en.Current != ' ' ) { throw new Exception("failed to parse CREATE. Expected 'CREATE TABLE '"); } List <char> tableName = new List <char>(); while (en.MoveNext() && en.Current != ' ') { tableName.Add(en.Current); } if (!en.MoveNext()) { throw new Exception("failed to parse CREATE. Unexpected end up input"); } if (en.Current != '(' || !en.MoveNext()) { throw new Exception("failed to parse CREATE. Ending ')' is missing"); } var colDefs = new List <ColumnDefinition>(); do { colDefs.Add(ColumnDefinition.Parse(en)); } while (en.Current == ','); if (en.Current == ' ') { en.MoveNext(); } if (en.Current != ')') { throw new Exception("failed to parse CREATE. Ending ')' is missing"); } return(new CreateTable { tableName = new string(tableName.ToArray()).Trim(), columns = colDefs.ToArray() }); }
public static new Insert Parse(Benumerator <char> en) { if (!en.MoveNext() || en.Current == ' ' && !en.MoveNext()) { throw new Exception("failed to parse. No Input"); } if (!Parser.HasValue(en, "INSERT INTO ")) { throw new Exception("failed to parse INSERT. Expecting 'INSERT INTO '"); } var tableName = Parser.ParseWord(en); Parser.SkipWhitespace(en); if (!Parser.HasValue(en, "(")) { throw new Exception("failed to parse INSERT. Expecting '('"); } var columns = SelectColumn.Parse(en).ToArray(); Parser.SkipWhitespace(en); if (!Parser.HasValue(en, ")")) { throw new Exception("failed to parse INSERT. Expecting ')'"); } Parser.SkipWhitespace(en); if (!Parser.HasValue(en, "VALUES")) { throw new Exception("failed to parse INSERT. Expecting ')'"); } List <SelectColumn[]> rows = new List <SelectColumn[]>(); do { Parser.SkipWhitespace(en); if (!Parser.HasValue(en, "(")) { throw new Exception("failed to parse INSERT. Expecting ')'"); } Parser.SkipWhitespace(en); var values = SelectColumn.Parse(en).ToArray(); if (values.Length != columns.Length) { throw new Exception("failed to parse INSERT. Number of values do not match number of columns"); } rows.Add(values); Parser.SkipWhitespace(en); if (!Parser.HasValue(en, ")")) { throw new Exception("failed to parse INSERT. Expecting ')'"); } Parser.SkipWhitespace(en); } while (Parser.HasValue(en, ",")); return(new Insert { tableName = tableName, columns = columns, rows = rows }); }