public WhileStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); _cond = ExpressionNode.Read(p, null); if (_cond == null) { ReportError(keywordSpan, CAError.CA0018, "if"); // Expected condition after '{0}'. return; } if (!p.Code.ReadExact("{")) { ReportError(keywordSpan, CAError.CA0019); // Expected '{'. return; } while (!p.Code.EndOfFile && !p.Code.ReadExact("}")) { var stmt = Statement.Read(p); if (stmt == null) { break; } _body.Add(stmt); } }
public ExtractStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; code.ReadExactWholeWord("permanent"); // Optional if (!code.ReadWord()) { ReportError(keywordSpan, CAError.CA0044); // Expected temp table name to follow 'extract'. return; } var tableName = code.Text; var def = p.CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromFile <ExtractTableDefinition>(tableName).FirstOrDefault(); if (def == null) { ReportError(code.Span, CAError.CA0045, tableName); // Extract table '{0}' does not exist. return; } while (!code.EndOfFile) { if (!code.ReadWord()) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0046); // Expected extract column name. return; } var colDef = def.GetChildDefinitions(code.Text).FirstOrDefault(); if (colDef == null) { ReportError(code.Span, CAError.CA0046); // Expected extract column name. return; } if (!code.ReadExact('=')) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0047); // Expected '=' to follow extract column name. return; } var exp = ExpressionNode.Read(p, null, true); if (exp == null) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0048); // Expected extract column expression. return; } _colExps.Add(exp); if (code.ReadExact(';')) { return; } } }
public ColStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; if (!code.ReadExact('+')) { code.ReadExact('-'); } code.ReadNumber(); code.ReadExact(';'); // Optional }
public HeaderStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; if (!code.ReadExact('{')) { ReportError(keywordSpan, CAError.CA0019); // Expected '{'. return; } while (!code.EndOfFile && !code.ReadExact("}")) { var stmt = Statement.Read(p); if (stmt == null) break; _body.Add(stmt); } }
public ReturnStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var retDataType = p.FuncDef.DataType; if (retDataType != null && !retDataType.IsVoid) { _exp = ExpressionNode.Read(p, retDataType); if (_exp == null) { ReportError(keywordSpan, CAError.CA0014); // Expected value after 'return'. } } if (!p.Code.ReadExact(';')) { ReportError(p.Code.Span, CAError.CA0015); // Expected ';'. } }
public ForStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; if (!code.ReadExact('(')) { ReportError(keywordSpan, CAError.CA0025); // Expected '('. return; } var errSpan = code.Span; _initExp = ExpressionNode.Read(p, null, ")"); if (_initExp != null) { errSpan = _initExp.Span; } if (!code.ReadExact(';')) { ReportError(errSpan, CAError.CA0026); // Expected ';'. return; } errSpan = code.Span; _condExp = ExpressionNode.Read(p, null, ")"); if (_condExp != null) { errSpan = _condExp.Span; } if (!code.ReadExact(';')) { ReportError(errSpan, CAError.CA0026); // Expected ';'. return; } errSpan = code.Span; _incExp = ExpressionNode.Read(p, null, ")"); if (_incExp != null) { errSpan = _incExp.Span; } if (!code.ReadExact(')')) { ReportError(errSpan, CAError.CA0027); // Expected ')'. return; } errSpan = code.Span; if (!code.ReadExact('{')) { ReportError(errSpan, CAError.CA0019); // Expected '{'. return; } errSpan = code.Span; while (!code.EndOfFile && !code.ReadExact("}")) { var stmt = Statement.Read(p); if (stmt == null) { break; } _body.Add(stmt); errSpan = stmt.Span; } }
public SelectStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; code.ReadStringLiteral(); if (!code.ReadExact('*')) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "*"); // Expected '{0}'. return; } var bodyStarted = false; while (!code.EndOfFile && !bodyStarted) { if (code.ReadExact('{')) { bodyStarted = true; break; } #region from if (code.ReadExact("from")) { while (!code.EndOfFile) { if (code.ReadExact('{')) { bodyStarted = true; break; } if (code.ReadWord()) { if (code.Text == "where" || code.Text == "order") { code.Position = code.TokenStartPostion; break; } if (code.Text == "of") { if (!code.ReadWord()) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0036); // Expected table name after 'of'. return; } } var def = (from d in CodeAnalyzer.PreprocessorModel.DefinitionProvider.GetGlobalFromAnywhere(code.Text) where d is TableDefinition || d is RelIndDefinition || d is ExtractTableDefinition select d).FirstOrDefault(); if (def == null) { ReportError(code.Span, CAError.CA0035, code.Text); // Table or relationship '{0}' does not exist. return; } else { _tables.Add(def); } } else if (code.ReadExact(',')) { } else { ReportError(code.Span, CAError.CA0034, "{"); // Expected '{0}'. return; } } } #endregion #region where else if (code.ReadExact("where")) { _whereExp = ExpressionNode.Read(p, null, "from", "where", "order"); if (code.ReadExact(';')) { bodyStarted = true; break; } } #endregion #region order by else if (code.ReadExact("order")) { if (!code.ReadExactWholeWord("by")) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "by"); // Expected '{0}'. return; } while (!code.EndOfFile) { if (code.ReadExact('{')) { bodyStarted = true; break; } if (code.ReadExact(',')) { continue; } if (code.ReadExactWholeWord("from") || code.ReadExactWholeWord("where")) { code.Position = code.TokenStartPostion; break; } if (!ReadTableColOrRelInd(p, true)) { return; } if (!code.ReadExactWholeWord("asc")) { code.ReadExactWholeWord("desc"); } } } #endregion } while (!code.EndOfFile && !code.ReadExact('}')) { if (code.ReadExactWholeWord("before") || code.ReadExactWholeWord("after")) { if (!code.ReadExactWholeWord("group")) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "group"); // Expected '{0}'. return; } if (code.ReadExactWholeWord("all")) { } else if (!ReadTableColOrRelInd(p, false)) { return; } if (!code.ReadExact(':')) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ":"); // Expected '{0}'. return; } if (!ReadGroupStatements(p, false)) { return; } } else if (code.ReadExactWholeWord("for")) { if (!code.ReadExactWholeWord("each")) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, "each"); // Expected '{0}'. return; } if (!code.ReadExact(':')) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ":"); // Expected '{0}'. return; } ReadGroupStatements(p, false); } else if (code.ReadExactWholeWord("default")) { if (!code.ReadExact(':')) { ReportError(new Span(code.Position, code.Position + 1), CAError.CA0034, ":"); // Expected '{0}'. return; } ReadGroupStatements(p, true); } else { break; } } }
public static Statement Read(ReadParams p) { p.Code.SkipWhiteSpace(); if (p.Code.EndOfFile) { return(null); } var word = p.Code.PeekWordR(); if (!string.IsNullOrEmpty(word)) { switch (word) { case "break": return(new BreakStatement(p, p.Code.MovePeekedSpan())); case "center": return(new CenterStatement(p, p.Code.MovePeekedSpan())); case "col": case "colff": return(new ColStatement(p, p.Code.MovePeekedSpan())); case "continue": return(new ContinueStatement(p, p.Code.MovePeekedSpan())); case "extract": return(new ExtractStatement(p, p.Code.MovePeekedSpan())); case "footer": return(new FooterStatement(p, p.Code.MovePeekedSpan())); case "for": return(new ForStatement(p, p.Code.MovePeekedSpan())); case "format": return(new FormatStatement(p, p.Code.MovePeekedSpan())); case "header": return(new HeaderStatement(p, p.Code.MovePeekedSpan())); case "if": return(new IfStatement(p, p.Code.MovePeekedSpan())); case "page": return(new PageStatement(p, p.Code.MovePeekedSpan())); case "return": return(new ReturnStatement(p, p.Code.MovePeekedSpan())); case "row": return(new RowStatement(p, p.Code.MovePeekedSpan())); case "select": return(new SelectStatement(p, p.Code.MovePeekedSpan())); case "switch": return(new SwitchStatement(p, p.Code.MovePeekedSpan())); case "while": return(new WhileStatement(p, p.Code.MovePeekedSpan())); } } var stmt = new SimpleStatement(p.CodeAnalyzer); p = p.Clone(stmt); while (!p.Code.EndOfFile) { if (p.Code.ReadExact(';')) { return(stmt); } var node = ExpressionNode.Read(p, null); if (node == null) { break; } stmt.AddNode(node); } if (stmt.NumChildren == 0) { return(null); } return(stmt); }
public FormatStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; var errorSpan = keywordSpan; while (!code.EndOfFile) { if (code.ReadExact(';')) { break; } switch (code.PeekWordR()) { case "rows": errorSpan = code.MovePeekedSpan(); if (!code.ReadExact('=')) { ReportError(errorSpan, CAError.CA0033); // Expected '='. return; } else { errorSpan = code.Span; } _rowsExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile"); if (_rowsExp != null) { errorSpan = _rowsExp.Span; } break; case "cols": errorSpan = code.MovePeekedSpan(); if (!code.ReadExact('=')) { ReportError(errorSpan, CAError.CA0033); // Expected '='. return; } else { errorSpan = code.Span; } _colsExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile"); if (_colsExp != null) { errorSpan = _colsExp.Span; } break; case "genpages": errorSpan = code.MovePeekedSpan(); if (!code.ReadExact('=')) { ReportError(errorSpan, CAError.CA0033); // Expected '='. return; } else { errorSpan = code.Span; } _genpagesExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile"); if (_genpagesExp != null) { errorSpan = _genpagesExp.Span; } break; case "outfile": errorSpan = code.MovePeekedSpan(); if (!code.ReadExact('=')) { ReportError(errorSpan, CAError.CA0033); // Expected '='. return; } else { errorSpan = code.Span; } _outfileExp = ExpressionNode.Read(p, null, "rows", "cols", "genpages", "outfile"); if (_outfileExp != null) { errorSpan = _outfileExp.Span; } break; default: ReportError(errorSpan, CAError.CA0015); // Expected ';'. return; } } }
public IfStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; var stmtSpan = keywordSpan; while (!code.EndOfFile) { var condition = ExpressionNode.Read(p, null); if (condition == null) { ReportError(stmtSpan, CAError.CA0018, "if"); // Expected condition after '{0}'. break; } _conditions.Add(condition); if (!code.ReadExact("{")) { ReportError(stmtSpan, CAError.CA0019); // Expected '{'. break; } var trueBody = new List <Statement>(); while (!code.EndOfFile && !code.ReadExact("}")) { var stmt = Statement.Read(p); if (stmt == null) { break; } trueBody.Add(stmt); } _trueBodies.Add(trueBody); if (code.ReadExactWholeWord("else")) { stmtSpan = code.Span; if (code.ReadExactWholeWord("if")) { stmtSpan = code.Span; continue; } if (!code.ReadExact("{")) { ReportError(stmtSpan, CAError.CA0019); // Expected '{'. break; } _falseBody = new List <Statement>(); while (!code.EndOfFile && !code.ReadExact("}")) { var stmt = Statement.Read(p); if (stmt == null) { break; } _falseBody.Add(stmt); } } break; } }
public SwitchStatement(ReadParams p, Span keywordSpan) : base(p.CodeAnalyzer, keywordSpan) { p = p.Clone(this); var code = p.Code; _condExp = ExpressionNode.Read(p, null); if (_condExp == null) { ReportError(keywordSpan, CAError.CA0018, "switch"); // Expected condition after '{0}'. return; } var errorSpan = keywordSpan; if (_condExp.NumChildren > 1 && _condExp.LastChild is OperatorNode) { _condExp.RemoveChild(_compareOp = _condExp.LastChild as OperatorNode); errorSpan = _compareOp.Span; } if (!code.ReadExact('{')) { ReportError(errorSpan, CAError.CA0019); // Expected '{'. return; } errorSpan = code.Span; var insideDefault = false; while (!code.EndOfFile) { if (code.ReadExact('}')) { break; } if (code.ReadExactWholeWord("case")) { errorSpan = code.Span; if (_cases.Count > 0) { var lastCase = _cases[_cases.Count - 1]; if (lastCase != null && lastCase.body.Count == 0) { lastCase.safeFallThrough = true; } } _cases.Add(new Case { caseSpan = code.Span }); insideDefault = false; var exp = ExpressionNode.Read(p, _condExp.DataType, ":"); if (exp == null) { ReportError(errorSpan, CAError.CA0028); // Expected case value. } else { _cases.Last().exp = exp; errorSpan = exp.Span; } if (!code.ReadExact(':')) { ReportError(errorSpan, CAError.CA0029); // Expected ':'. } } else if (code.ReadExactWholeWord("default")) { if (_default != null) { ReportError(code.Span, CAError.CA0032); // Duplicate default case. } insideDefault = true; _default = new List <Statement>(); if (!code.ReadExact(':')) { ReportError(errorSpan, CAError.CA0029); // Expected ':'. } } else if (code.ReadExactWholeWord("break")) { if (insideDefault) { _default.Add(new BreakStatement(p, code.Span)); } else if (_cases.Any()) { _cases.Last().body.Add(new BreakStatement(p, code.Span)); } else { ReportError(code.Span, CAError.CA0023); // 'break' is not valid here. } } else { var stmt = Statement.Read(p); if (stmt == null) { break; } if (insideDefault) { _default.Add(stmt); } else if (_cases.Any()) { _cases.Last().body.Add(stmt); } else { ReportError(stmt.Span, CAError.CA0030); // Statement is not valid here. } } } }