public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new ElseKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); kw.Body = engine.Advance(Token.Length).ParseStatement(); if (engine.LastStatement == null) { engine.AddError(new YAMPIfRequiredError(engine), kw); } else if (engine.LastStatement.IsKeyword<IfKeyword>()) { engine.LastStatement.GetKeyword<IfKeyword>().Else = kw; } else if (engine.LastStatement.IsKeyword<ElseKeyword>()) { var otherwise = engine.LastStatement.GetKeyword<ElseKeyword>(); if (otherwise.IsElseIf) { otherwise.ElseIf.Else = kw; } else { engine.AddError(new YAMPSingleElseError(engine), kw); } } else { engine.AddError(new YAMPIfRequiredError(engine), kw); } kw.Length = engine.Pointer - start; return kw; }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new IfKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); var index = engine.Advance(Token.Length).Skip().Pointer; var chars = engine.Characters; if (index == chars.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPIfArgumentsMissing(engine), kw); return(kw); } if (chars[index] == '(') { var ln = engine.CurrentLine; var col = engine.CurrentColumn; kw.Condition = engine.Advance().ParseStatement(')', e => new YAMPBracketNotClosedError(ln, col)); if (kw.Condition.Container == null || !kw.Condition.Container.HasContent) { engine.AddError(new YAMPIfArgumentsMissing(engine), kw); } kw.Body = engine.ParseStatement(); } else { engine.AddError(new YAMPIfArgumentsMissing(engine), kw); } kw.Length = engine.Pointer - start; return(kw); }
public override Expression Scan(ParseEngine engine) { var kw = new ForKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); var start = engine.Pointer; var index = engine.Advance(Token.Length).Skip().Pointer; var chars = engine.Characters; if (index == chars.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPForArgumentsMissing(engine)); return(kw); } if (chars[index] == '(') { var ln = engine.CurrentLine; var col = engine.CurrentColumn; kw.Initialization = engine.Advance().ParseStatement(); kw.Condition = engine.ParseStatement(); kw.Condition.IsMuted = false; kw.End = engine.ParseStatement(')', e => new YAMPBracketNotClosedError(ln, col)); SetMarker(engine); kw.Body = engine.ParseStatement(); UnsetMarker(engine); } else { engine.AddError(new YAMPForArgumentsMissing(engine)); } kw.Length = engine.Pointer - start; return(kw); }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var chars = engine.Characters; if (chars[start] == '{') { var index = start; var line = engine.CurrentLine; var column = engine.CurrentColumn; engine.Advance(); index++; var query = engine.Query; var scope = new QueryContext(query, query.Input.Substring(index)); var eng = scope.Parser.SetOffset(line, column + 1).Parse(); if (!eng.IsTerminated) { engine.AddError(new YAMPScopeNotClosedError(line, column)); } foreach (var error in eng.Errors) { engine.AddError(error); } engine.Advance(eng.Pointer); return(new GroupExpression(line, column, engine.Pointer - start, scope)); } return(null); }
/// <summary> /// Finalizes the statement by analyzing the contained objects and creating /// the container. /// </summary> /// <param name="engine">The current parse engine.</param> /// <returns>The current (finalized) instance.</returns> internal Statement Finalize(ParseEngine engine) { if (finalized) { return(this); } if (errors.Count != 0) { foreach (var error in errors) { engine.AddError(error); } return(this); } if (_expressions.Count == 0 && _operators.Count > 0) { engine.AddError(new YAMPExpressionMissingError(engine)); return(this); } while (_operators.Count > 0) { var op = _operators.Pop(); var exp = new Expression[op.Expressions]; if (_expressions.Count < op.Expressions) { engine.AddError(new YAMPExpressionMissingError(engine, op, _expressions.Count)); return(this); } for (var i = op.Expressions - 1; i >= 0; i--) { exp[i] = _expressions.Pop(); } var container = new ContainerExpression(exp, op); _expressions.Push(container); ReduceUnary(container); } if (_expressions.Count == 1) { _container = new ContainerExpression(_expressions.Pop()); } else { _container = new ContainerExpression(); } finalized = true; return(this); }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new WhileKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); var index = engine.Advance(Token.Length).Skip().Pointer; var chars = engine.Characters; if (index == chars.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPWhileArgumentsMissing(engine), kw); return(kw); } if (chars[index] == '(') { var ln = engine.CurrentLine; var col = engine.CurrentColumn; kw.Condition = engine.Advance().ParseStatement(')', e => new YAMPBracketNotClosedError(ln, col)); if (kw.Condition.Container == null || !kw.Condition.Container.HasContent) { engine.AddError(new YAMPWhileArgumentsMissing(engine), kw); } SetMarker(engine); kw.Body = engine.ParseStatement(); UnsetMarker(engine); if (engine.LastStatement != null && engine.LastStatement.IsKeyword <DoKeyword>()) { if (kw.Body.IsEmpty) { IsDoWhile = true; engine.LastStatement.GetKeyword <DoKeyword>().While = kw; } else { engine.AddError(new YAMPDoWhileNotEmptyError(engine), kw); } } } else { engine.AddError(new YAMPWhileArgumentsMissing(engine), kw); } kw.Length = engine.Pointer - start; return(kw); }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var chars = engine.Characters; if (chars[start] == '(') { var index = start; var line = engine.CurrentLine; var col = engine.CurrentColumn; var container = engine.Advance().ParseStatement(')', e => new YAMPBracketNotClosedError(line, col), (ch, statement) => { if (ch == ',') { var op = new CommaOperator(engine); engine.Advance(); statement.Push(engine, op); return true; } return false; }).Container; var exp = new BracketExpression(line, col, engine.Pointer - start, engine.Query, container ?? new ContainerExpression()); if (container == null) { engine.AddError(new YAMPBracketEmptyError(line, col), exp); } return exp; } return null; }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var chars = engine.Characters; if (chars[start] == '{') { var index = start; var line = engine.CurrentLine; var column = engine.CurrentColumn; engine.Advance(); index++; var query = engine.Query; var scope = new QueryContext(query, query.Input.Substring(index)); var eng = scope.Parser.SetOffset(line, column + 1).Parse(); if (!eng.IsTerminated) { engine.AddError(new YAMPScopeNotClosedError(line, column)); } foreach (var error in eng.Errors) { engine.AddError(error); } engine.Advance(eng.Pointer); return new GroupExpression(line, column, engine.Pointer - start, scope); } return null; }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new IfKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); var index = engine.Advance(Token.Length).Skip().Pointer; var chars = engine.Characters; if (index == chars.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPIfArgumentsMissing(engine), kw); return kw; } if (chars[index] == '(') { var ln = engine.CurrentLine; var col = engine.CurrentColumn; kw.Condition = engine.Advance().ParseStatement(')', e => new YAMPBracketNotClosedError(ln, col)); if (kw.Condition.Container == null || !kw.Condition.Container.HasContent) { engine.AddError(new YAMPIfArgumentsMissing(engine), kw); } kw.Body = engine.ParseStatement(); } else { engine.AddError(new YAMPIfArgumentsMissing(engine), kw); } kw.Length = engine.Pointer - start; return kw; }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var chars = engine.Characters; if (chars[start] == '(') { var index = start; var line = engine.CurrentLine; var col = engine.CurrentColumn; var container = engine.Advance().ParseStatement(')', e => new YAMPBracketNotClosedError(line, col), (ch, statement) => { if (ch == ',') { var op = new CommaOperator(engine); engine.Advance(); statement.Push(engine, op); return(true); } return(false); }).Container; var exp = new BracketExpression(line, col, engine.Pointer - start, engine.Query, container ?? new ContainerExpression()); if (container == null) { engine.AddError(new YAMPBracketEmptyError(line, col), exp); } return(exp); } return(null); }
public override Expression Scan(ParseEngine engine) { var kw = new BreakKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); engine.Advance(Token.Length); if (!IsBreakable(engine)) { engine.AddError(new YAMPKeywordNotPossible(engine, Token), kw); } return kw; }
public override Expression Scan(ParseEngine engine) { var kw = new BreakKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); engine.Advance(Token.Length); if (!IsBreakable(engine)) { engine.AddError(new YAMPKeywordNotPossible(engine, Token), kw); } return(kw); }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new ElseKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); kw.Body = engine.Advance(Token.Length).ParseStatement(); if (engine.LastStatement == null) { engine.AddError(new YAMPIfRequiredError(engine), kw); } else if (engine.LastStatement.IsKeyword <IfKeyword>()) { engine.LastStatement.GetKeyword <IfKeyword>().Else = kw; } else if (engine.LastStatement.IsKeyword <ElseKeyword>()) { var otherwise = engine.LastStatement.GetKeyword <ElseKeyword>(); if (otherwise.IsElseIf) { otherwise.ElseIf.Else = kw; } else { engine.AddError(new YAMPSingleElseError(engine), kw); } } else { engine.AddError(new YAMPIfRequiredError(engine), kw); } kw.Length = engine.Pointer - start; return(kw); }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new LetKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); engine.Advance(Token.Length); kw.Length = engine.Pointer - start; engine.Skip(); start = engine.Pointer; kw.name = engine.Elements.FindExpression<SymbolExpression>().Scan(engine) as SymbolExpression; if (kw.name == null) { engine.AddError(new YAMPVariableNameMissing(engine), kw); return kw; } engine.SetPointer(start); return kw; }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new LetKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); engine.Advance(Token.Length); kw.Length = engine.Pointer - start; engine.Skip(); start = engine.Pointer; kw.name = Elements.Instance.FindExpression <SymbolExpression>().Scan(engine) as SymbolExpression; if (kw.name == null) { engine.AddError(new YAMPVariableNameMissing(engine), kw); return(kw); } engine.SetPointer(start); return(kw); }
/// <summary> /// Scans for a function entry. /// </summary> /// <param name="engine">The current parse engine.</param> /// <returns>The created expression.</returns> public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new FunctionKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); engine.Advance(Token.Length).Skip(); if (engine.Pointer == engine.Characters.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPFunctionNameMissing(engine), kw); return kw; } kw.name = engine.Elements.FindExpression<SymbolExpression>().Scan(engine) as SymbolExpression; if (kw.name == null) { engine.AddError(new YAMPFunctionNameMissing(engine), kw); return kw; } engine.Skip(); if (engine.Pointer == engine.Characters.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPFunctionArgumentsMissing(engine), kw); return kw; } kw.arguments = engine.Elements.FindExpression<BracketExpression>().Scan(engine) as BracketExpression; if (engine.Pointer == engine.Characters.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPFunctionBodyMissing(engine), kw.arguments); return kw; } kw.Body = engine.ParseStatement(); kw.Length = engine.Pointer - start; if (kw.Body.Container.Expressions.Length == 1 && kw.Body.Container.Expressions[0] is GroupExpression) { var container = (GroupExpression)kw.Body.Container.Expressions[0]; var context = new ParseContext(engine.Context.Parent); var input = container.Scope.Input; container.Scope = new QueryContext(context, input); } else { engine.AddError(new YAMPFunctionBodyMissing(engine), kw.arguments); return kw; } if (kw.arguments == null) { engine.AddError(new YAMPFunctionArgumentsMissing(engine), kw); return kw; } else if (kw.arguments.HasContent && !kw.arguments.IsSymbolList) { engine.AddError(new YAMPFunctionArgumentsSymbols(engine), kw.arguments); return kw; } return kw; }
public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new WhileKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); var index = engine.Advance(Token.Length).Skip().Pointer; var chars = engine.Characters; if (index == chars.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPWhileArgumentsMissing(engine), kw); return kw; } if (chars[index] == '(') { var ln = engine.CurrentLine; var col = engine.CurrentColumn; kw.Condition = engine.Advance().ParseStatement(')', e => new YAMPBracketNotClosedError(ln, col)); if (kw.Condition.Container == null || !kw.Condition.Container.HasContent) { engine.AddError(new YAMPWhileArgumentsMissing(engine), kw); } SetMarker(engine); kw.Body = engine.ParseStatement(); UnsetMarker(engine); if (engine.LastStatement != null && engine.LastStatement.IsKeyword<DoKeyword>()) { if (kw.Body.IsEmpty) { IsDoWhile = true; engine.LastStatement.GetKeyword<DoKeyword>().While = kw; } else { engine.AddError(new YAMPDoWhileNotEmptyError(engine), kw); } } } else { engine.AddError(new YAMPWhileArgumentsMissing(engine), kw); } kw.Length = engine.Pointer - start; return kw; }
public override Expression Scan(ParseEngine engine) { var kw = new ForKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); var start = engine.Pointer; var index = engine.Advance(Token.Length).Skip().Pointer; var chars = engine.Characters; if (index == chars.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPForArgumentsMissing(engine)); return kw; } else if (chars[index] == '(') { var ln = engine.CurrentLine; var col = engine.CurrentColumn; kw.Initialization = engine.Advance().ParseStatement(); kw.Condition = engine.ParseStatement(); kw.Condition.IsMuted = false; kw.End = engine.ParseStatement(')', e => new YAMPBracketNotClosedError(ln, col)); SetMarker(engine); kw.Body = engine.ParseStatement(); UnsetMarker(engine); } else { engine.AddError(new YAMPForArgumentsMissing(engine)); } kw.Length = engine.Pointer - start; return kw; }
/// <summary> /// Scans the current parse engine for a matrix expression. /// </summary> /// <param name="engine">The parse engine to use.</param> /// <returns>The found expression or NULL.</returns> public override Expression Scan(ParseEngine engine) { var column = engine.CurrentColumn; var line = engine.CurrentLine; var chars = engine.Characters; var start = engine.Pointer; if (chars[start] == '[') { engine.Advance(); var terminated = false; var statement = new Statement(); bool ws = false, nl = false; while (engine.Pointer < chars.Length && engine.IsParsing) { if (ParseEngine.IsWhiteSpace(chars[engine.Pointer])) { ws = true; engine.Advance(); } else if (ParseEngine.IsNewLine(chars[engine.Pointer])) { nl = true; engine.Advance(); } else if (chars[engine.Pointer] == ']') { terminated = true; engine.Advance(); break; } else if (chars[engine.Pointer] == ',') { ws = false; nl = false; statement.Push(engine, new ColumnOperator(engine)); engine.Advance(); } else if (chars[engine.Pointer] == ';') { ws = false; nl = false; statement.Push(engine, new RowOperator(engine)); engine.Advance(); } else if (engine.Pointer < chars.Length - 1 && ParseEngine.IsComment(chars[engine.Pointer], chars[engine.Pointer + 1])) { if (ParseEngine.IsLineComment(chars[engine.Pointer], chars[engine.Pointer + 1])) { engine.AdvanceToNextLine(); } else { engine.AdvanceTo("*/"); } } else { engine.ParseBlock(statement, nl ? (Operator) new RowOperator(engine) : (ws ? new ColumnOperator(engine) : null)); ws = false; nl = false; } } if (!terminated) { var err = new YAMPMatrixNotClosedError(line, column); engine.AddError(err); } var container = statement.Finalize(engine).Container; return(new MatrixExpression(line, column, engine.Pointer - start, engine.Query, container ?? new ContainerExpression())); } return(null); }
public override Expression Scan(ParseEngine engine) { var chars = engine.Characters; var start = engine.Pointer; if (chars[start] == '"' || (chars[start] == '@' && start + 1 < chars.Length && chars[start + 1] == '"')) { var index = start; var exp = new StringExpression(engine); var escape = false; var terminated = false; var sb = new StringBuilder(); if (chars[index] == '@') { index += 2; exp.literal = true; } else { index++; } while (index < chars.Length) { if (!literal && !escape && chars[index] == '\\') { escape = true; } else if (!escape && chars[index] == '"') { terminated = true; index++; break; } else if (escape) { switch (chars[index]) { case 't': sb.Append("\t"); break; case 'n': sb.AppendLine(); break; case '\\': case '"': sb.Append(chars[index]); break; default: engine.SetPointer(index); engine.AddError(new YAMPEscapeSequenceNotFoundError(engine, chars[index]), exp); break; } escape = false; } else { sb.Append(chars[index]); } index++; } if (!terminated) { engine.AddError(new YAMPStringNotTerminatedError(engine), exp); } exp.value = sb.ToString(); exp.Length = index - start; engine.SetPointer(index); return exp; } return null; }
/// <summary> /// Scans the current parse engine for a matrix expression. /// </summary> /// <param name="engine">The parse engine to use.</param> /// <returns>The found expression or NULL.</returns> public override Expression Scan(ParseEngine engine) { var column = engine.CurrentColumn; var line = engine.CurrentLine; var chars = engine.Characters; var start = engine.Pointer; if (chars[start] == '[') { engine.Advance(); var terminated = false; var statement = new Statement(); var ws = false; var nl = false; while (engine.Pointer < chars.Length && engine.IsParsing) { if (ParseEngine.IsWhiteSpace(chars[engine.Pointer])) { ws = true; engine.Advance(); } else if (ParseEngine.IsNewLine(chars[engine.Pointer])) { nl = true; engine.Advance(); } else if (chars[engine.Pointer] == ']') { terminated = true; engine.Advance(); break; } else if (chars[engine.Pointer] == ',') { ws = false; nl = false; statement.Push(engine, new ColumnOperator(engine)); engine.Advance(); } else if (chars[engine.Pointer] == ';') { ws = false; nl = false; statement.Push(engine, new RowOperator(engine)); engine.Advance(); } else if (engine.Pointer < chars.Length - 1 && ParseEngine.IsComment(chars[engine.Pointer], chars[engine.Pointer + 1])) { if (ParseEngine.IsLineComment(chars[engine.Pointer], chars[engine.Pointer + 1])) engine.AdvanceToNextLine(); else engine.AdvanceTo("*/"); } else { engine.ParseBlock(statement, nl ? (Operator)new RowOperator(engine) : (ws ? new ColumnOperator(engine) : null)); ws = false; nl = false; } } if (!terminated) { var err = new YAMPMatrixNotClosedError(line, column); engine.AddError(err); } var container = statement.Finalize(engine).Container; return new MatrixExpression(line, column, engine.Pointer - start, engine.Query, container ?? new ContainerExpression()); } return null; }
public override Expression Scan(ParseEngine engine) { var chars = engine.Characters; var start = engine.Pointer; if (chars[start] == '"' || (chars[start] == '@' && start + 1 < chars.Length && chars[start + 1] == '"')) { var index = start; var exp = new StringExpression(engine); var escape = false; var terminated = false; var sb = new StringBuilder(); if (chars[index] == '@') { index += 2; exp.literal = true; } else { index++; } while (index < chars.Length) { if (!literal && !escape && chars[index] == '\\') { escape = true; } else if (!escape && chars[index] == '"') { terminated = true; index++; break; } else if (escape) { switch (chars[index]) { case 't': sb.Append("\t"); break; case 'n': sb.AppendLine(); break; case '\\': case '"': sb.Append(chars[index]); break; default: engine.SetPointer(index); engine.AddError(new YAMPEscapeSequenceNotFoundError(engine, chars[index]), exp); break; } escape = false; } else { sb.Append(chars[index]); } index++; } if (!terminated) { engine.AddError(new YAMPStringNotTerminatedError(engine), exp); } exp.value = sb.ToString(); exp.Length = index - start; engine.SetPointer(index); return(exp); } return(null); }
/// <summary> /// Scans for a function entry. /// </summary> /// <param name="engine">The current parse engine.</param> /// <returns>The created expression.</returns> public override Expression Scan(ParseEngine engine) { var start = engine.Pointer; var kw = new FunctionKeyword(engine.CurrentLine, engine.CurrentColumn, engine.Query); engine.Advance(Token.Length).Skip(); if (engine.Pointer == engine.Characters.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPFunctionNameMissing(engine), kw); return(kw); } kw.name = engine.Elements.FindExpression <SymbolExpression>().Scan(engine) as SymbolExpression; if (kw.name == null) { engine.AddError(new YAMPFunctionNameMissing(engine), kw); return(kw); } engine.Skip(); if (engine.Pointer == engine.Characters.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPFunctionArgumentsMissing(engine), kw); return(kw); } kw.arguments = engine.Elements.FindExpression <BracketExpression>().Scan(engine) as BracketExpression; if (engine.Pointer == engine.Characters.Length) { kw.Length = engine.Pointer - start; engine.AddError(new YAMPFunctionBodyMissing(engine), kw.arguments); return(kw); } kw.Body = engine.ParseStatement(); kw.Length = engine.Pointer - start; if (kw.Body.Container.Expressions.Length == 1 && kw.Body.Container.Expressions[0] is GroupExpression) { var container = (GroupExpression)kw.Body.Container.Expressions[0]; var context = new ParseContext(engine.Context.Parent); var input = container.Scope.Input; container.Scope = new QueryContext(context, input); } else { engine.AddError(new YAMPFunctionBodyMissing(engine), kw.arguments); return(kw); } if (kw.arguments == null) { engine.AddError(new YAMPFunctionArgumentsMissing(engine), kw); return(kw); } else if (kw.arguments.HasContent && !kw.arguments.IsSymbolList) { engine.AddError(new YAMPFunctionArgumentsSymbols(engine), kw.arguments); return(kw); } return(kw); }