/// <summary> /// Creates a template expression given a replacement token and parameters. /// </summary> /// <param name="context">Context about the expression being parsed</param> /// <returns>a template expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("template tags can't have a body", context.ReplacementKeyToken); } TokenReader <DocumentToken> reader = new TokenReader <DocumentToken>(context.Parameters); DocumentToken idToken; if (reader.TryAdvance(out idToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing template Id", context.ReplacementKeyToken); } DocumentToken evalToken; if (reader.TryAdvance(out evalToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing eval token", idToken); } var ret = new TemplateExpression(idToken, evalToken); return(ret); }
/// <summary> /// Creates a template expression given a replacement token and parameters. /// </summary> /// <param name="context">Context about the expression being parsed</param> /// <returns>a template expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("template tags can't have a body", context.ReplacementKeyToken); } TokenReader<DocumentToken> reader = new TokenReader<DocumentToken>(context.Parameters); DocumentToken idToken; if (reader.TryAdvance(out idToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing template Id", context.ReplacementKeyToken); } DocumentToken evalToken; if (reader.TryAdvance(out evalToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing eval token", idToken); } var ret = new TemplateExpression(idToken, evalToken); return ret; }
/// <summary> /// Creates an eval expression given template replacement info /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns></returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("eval tags can't have a body", context.ReplacementKeyToken); } TokenReader <DocumentToken> reader = new TokenReader <DocumentToken>(context.Parameters); DocumentToken fgToken, bgToken; var ret = new EvalExpression(context.ReplacementKeyToken); if (reader.TryAdvance(out fgToken, skipWhitespace: true) == false) { return(ret); } else { ret.ForegroundColorToken = fgToken; } if (reader.TryAdvance(out bgToken, skipWhitespace: true) == false) { return(ret); } else { ret.BackgroundColorToken = bgToken; } return(ret); }
/// <summary> /// Creates a variable expression given replacement info /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns>A variable expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("var tags can't have a body", context.Body.First()); } TokenReader <DocumentToken> reader = new TokenReader <DocumentToken>(context.Parameters); DocumentToken variableName; if (reader.TryAdvance(out variableName, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected variable name after var tag", context.ReplacementKeyToken); } DocumentToken variableValue; if (reader.TryAdvance(out variableValue, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected variable value expression", variableName); } return(new VarExpression(variableName, variableValue)); }
/// <summary> /// Creates a new table expression given a collection evaluation expression and a list of column tokens /// </summary> /// <param name="evalToken">A token containing an expression that should evaluate to an IEnumerable</param> /// <param name="columns">A list of tokens containing the names of columns to display in the table</param> /// <param name="context">Context that is used to determine the indentation of the table within the document</param> public TableExpression(DocumentToken evalToken, List<DocumentToken> columns, DocumentExpressionContext context) { this.EvalToken = evalToken; this.Columns = columns; this.ShowDefaultValuesForArguments = true; this.ShowPossibleValuesForArguments = true; this.indent = context.OpenToken.Column - 1; }
private void ParseReplacement(TokenReader <DocumentToken> reader, List <IDocumentExpression> ret) { var openToken = AdvanceAndExpectConstantType(reader, DocumentTokenType.BeginReplacementSegment); var replacementKeyToken = AdvanceAndExpect(reader, DocumentTokenType.ReplacementKey, "replacement key", skipWhitespace: true); List <DocumentToken> parameters = new List <DocumentToken>(); List <DocumentToken> body = new List <DocumentToken>(); while (reader.CanAdvance(skipWhitespace: true) && reader.Peek(skipWhitespace: true).TokenType == DocumentTokenType.ReplacementParameter) { var paramToken = reader.Advance(skipWhitespace: true); parameters.Add(paramToken); } DocumentToken closeReplacementToken; if (reader.TryAdvance(out closeReplacementToken, skipWhitespace: true) == false) { throw Unexpected(string.Format("'{0}' or '{1}'", DocumentToken.GetTokenTypeValue(DocumentTokenType.EndReplacementSegment), DocumentToken.GetTokenTypeValue(DocumentTokenType.QuickTerminateReplacementSegment))); } if (closeReplacementToken.TokenType == DocumentTokenType.EndReplacementSegment) { body.AddRange(ReadReplacementBody(reader, replacementKeyToken)); } else if (closeReplacementToken.TokenType == DocumentTokenType.QuickTerminateReplacementSegment) { // do nothing, there is no body when the quick termination replacement segment is used } else { throw Unexpected(string.Format("'{0}' or '{1}'", DocumentToken.GetTokenTypeValue(DocumentTokenType.EndReplacementSegment), DocumentToken.GetTokenTypeValue(DocumentTokenType.QuickTerminateReplacementSegment)), closeReplacementToken); } IDocumentExpressionProvider provider; if (this.expressionProviders.TryGetValue(replacementKeyToken.Value, out provider) == false) { provider = new EvalExpressionProvider(); } var context = new DocumentExpressionContext { OpenToken = openToken, CloseToken = closeReplacementToken, Parameters = parameters.AsReadOnly(), Body = body.AsReadOnly(), ReplacementKeyToken = replacementKeyToken, }; var expression = provider.CreateExpression(context); ret.Add(expression); }
/// <summary> /// Creates a table expression from the given document info. /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns>The created document expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("table tags can't have a body", context.ReplacementKeyToken); } TokenReader <DocumentToken> reader = new TokenReader <DocumentToken>(context.Parameters); DocumentToken variableExpressionToken; if (reader.TryAdvance(out variableExpressionToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing collection expression after table tag", context.ReplacementKeyToken); } List <DocumentToken> columns = new List <DocumentToken>(); bool showDefaults = true; bool showPossibilities = true; while (reader.CanAdvance(skipWhitespace: true)) { var nextToken = reader.Advance(skipWhitespace: true); if (nextToken.Value == "-HideDefaults") { showDefaults = false; } else if (nextToken.Value == "-HideEnumValues") { showPossibilities = false; } else { columns.Add(nextToken); } } if (columns.Count == 0) { throw new DocumentRenderException("table elements need to have at least one column parameter", context.ReplacementKeyToken); } return(new TableExpression(variableExpressionToken, columns, context) { ShowDefaultValuesForArguments = showDefaults, ShowPossibleValuesForArguments = showPossibilities }); }
/// <summary> /// Creates a clear variable expression given replacement info /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns>a clear variable expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("clearvar tags can't have a body", context.ReplacementKeyToken); } TokenReader<DocumentToken> reader = new TokenReader<DocumentToken>(context.Parameters); DocumentToken variableName; if (reader.TryAdvance(out variableName, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected variable name after clearvar tag", context.ReplacementKeyToken); } return new ClearVarExpression(variableName); }
/// <summary> /// Creates either an if or an ifnot expression, based on its configuration, using the given document info. /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns>The expression, either an if or an ifnot expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { TokenReader <DocumentToken> reader = new TokenReader <DocumentToken>(context.Parameters); DocumentToken ifExpressionToken; if (reader.TryAdvance(out ifExpressionToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing if expression", context.ReplacementKeyToken); } if (reader.CanAdvance(skipWhitespace: true)) { throw new DocumentRenderException("unexpected parameters after if expression", reader.Advance(skipWhitespace: true)); } return(not ? new IfNotExpression(ifExpressionToken, context.Body) : new IfExpression(ifExpressionToken, context.Body)); }
/// <summary> /// Takes in document replacement info and converts it into a document expression that represents an each loop. /// </summary> /// <param name="context">Context about the expression being parsed</param> /// <returns>The parsed each expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count == 0) { throw new DocumentRenderException("Each tag has no body", context.ReplacementKeyToken); } TokenReader <DocumentToken> reader = new TokenReader <DocumentToken>(context.Parameters); DocumentToken variableName; if (reader.TryAdvance(out variableName, skipWhitespace: true) == false) { throw new DocumentRenderException("missing variable name in each tag", context.ReplacementKeyToken); } DocumentToken inToken; if (reader.TryAdvance(out inToken, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected 'in' after iteration variable", variableName); } if (inToken.Value != "in") { throw new DocumentRenderException("Expected 'in', got '" + inToken.Value + "'", inToken); } DocumentToken collectionExpressionToken; if (reader.TryAdvance(out collectionExpressionToken, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected collection expression after 'in' ", inToken); } DocumentToken unexpected; if (reader.TryAdvance(out unexpected, skipWhitespace: true)) { throw new DocumentRenderException("Unexpected parameter '" + unexpected.Value + "' after collection", unexpected); } return(new EachExpression(variableName, collectionExpressionToken, context.Body)); }
/// <summary> /// Takes in document replacement info and converts it into a document expression that represents an each loop. /// </summary> /// <param name="context">Context about the expression being parsed</param> /// <returns>The parsed each expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if(context.Body.Count == 0) { throw new DocumentRenderException("Each tag has no body", context.ReplacementKeyToken); } TokenReader<DocumentToken> reader = new TokenReader<DocumentToken>(context.Parameters); DocumentToken variableName; if (reader.TryAdvance(out variableName, skipWhitespace: true) == false) { throw new DocumentRenderException("missing variable name in each tag", context.ReplacementKeyToken); } DocumentToken inToken; if (reader.TryAdvance(out inToken, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected 'in' after iteration variable", variableName); } if (inToken.Value != "in") { throw new DocumentRenderException("Expected 'in', got '" + inToken.Value + "'", inToken); } DocumentToken collectionExpressionToken; if(reader.TryAdvance(out collectionExpressionToken, skipWhitespace: true) == false) { throw new DocumentRenderException("Expected collection expression after 'in' ", inToken); } DocumentToken unexpected; if(reader.TryAdvance(out unexpected, skipWhitespace: true)) { throw new DocumentRenderException("Unexpected parameter '" + unexpected.Value + "' after collection", unexpected); } return new EachExpression(variableName, collectionExpressionToken, context.Body); }
public IDocumentExpression CreateExpression(DocumentExpressionContext c) { return new FuncDocExpression((context) => { return new ConsoleString("if override"); }); }
public IDocumentExpression CreateExpression(DocumentExpressionContext c) { return new FuncDocExpression((context) => { return new ConsoleString(DateTime.Now.Date.ToString()); }); }
/// <summary> /// Creates either an if or an ifnot expression, based on its configuration, using the given document info. /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns>The expression, either an if or an ifnot expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { TokenReader<DocumentToken> reader = new TokenReader<DocumentToken>(context.Parameters); DocumentToken ifExpressionToken; if(reader.TryAdvance(out ifExpressionToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing if expression", context.ReplacementKeyToken); } if (reader.CanAdvance(skipWhitespace: true)) { throw new DocumentRenderException("unexpected parameters after if expression", reader.Advance(skipWhitespace: true)); } return not ? new IfNotExpression(ifExpressionToken, context.Body) : new IfExpression(ifExpressionToken, context.Body); }
/// <summary> /// Creates a table expression from the given document info. /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns>The created document expression</returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("table tags can't have a body", context.ReplacementKeyToken); } TokenReader<DocumentToken> reader = new TokenReader<DocumentToken>(context.Parameters); DocumentToken variableExpressionToken; if (reader.TryAdvance(out variableExpressionToken, skipWhitespace: true) == false) { throw new DocumentRenderException("missing collection expression after table tag", context.ReplacementKeyToken); } List<DocumentToken> columns = new List<DocumentToken>(); bool showDefaults = true; bool showPossibilities = true; while(reader.CanAdvance(skipWhitespace: true)) { var nextToken = reader.Advance(skipWhitespace: true); if (nextToken.Value == "-HideDefaults") { showDefaults = false; } else if(nextToken.Value == "-HideEnumValues") { showPossibilities = false; } else { columns.Add(nextToken); } } if (columns.Count == 0) { throw new DocumentRenderException("table elements need to have at least one column parameter", context.ReplacementKeyToken); } return new TableExpression(variableExpressionToken, columns, context) { ShowDefaultValuesForArguments = showDefaults, ShowPossibleValuesForArguments = showPossibilities }; }
/// <summary> /// Creates a new table expression given a collection evaluation expression and a list of column tokens /// </summary> /// <param name="evalToken">A token containing an expression that should evaluate to an IEnumerable</param> /// <param name="columns">A list of tokens containing the names of columns to display in the table</param> /// <param name="context">Context that is used to determine the indentation of the table within the document</param> public TableExpression(DocumentToken evalToken, List <DocumentToken> columns, DocumentExpressionContext context) { this.EvalToken = evalToken; this.Columns = columns; this.ShowDefaultValuesForArguments = true; this.ShowPossibleValuesForArguments = true; this.indent = context.OpenToken.Column - 1; }
/// <summary> /// Creates an eval expression given template replacement info /// </summary> /// <param name="context">The context that contains information about the document being rendered</param> /// <returns></returns> public IDocumentExpression CreateExpression(DocumentExpressionContext context) { if (context.Body.Count > 0) { throw new DocumentRenderException("eval tags can't have a body", context.ReplacementKeyToken); } TokenReader<DocumentToken> reader = new TokenReader<DocumentToken>(context.Parameters); DocumentToken fgToken, bgToken; var ret = new EvalExpression(context.ReplacementKeyToken); if(reader.TryAdvance(out fgToken, skipWhitespace: true) == false) { return ret; } else { ret.ForegroundColorToken = fgToken; } if (reader.TryAdvance(out bgToken, skipWhitespace: true) == false) { return ret; } else { ret.BackgroundColorToken = bgToken; } return ret; }
private void ParseReplacement(TokenReader<DocumentToken> reader, List<IDocumentExpression> ret) { var openToken = AdvanceAndExpectConstantType(reader, DocumentTokenType.BeginReplacementSegment); var replacementKeyToken = AdvanceAndExpect(reader, DocumentTokenType.ReplacementKey, "replacement key", skipWhitespace: true); List<DocumentToken> parameters = new List<DocumentToken>(); List<DocumentToken> body = new List<DocumentToken>(); while (reader.CanAdvance(skipWhitespace: true) && reader.Peek(skipWhitespace: true).TokenType == DocumentTokenType.ReplacementParameter) { var paramToken = reader.Advance(skipWhitespace: true); parameters.Add(paramToken); } DocumentToken closeReplacementToken; if(reader.TryAdvance(out closeReplacementToken, skipWhitespace: true) == false) { throw Unexpected(string.Format("'{0}' or '{1}'", DocumentToken.GetTokenTypeValue(DocumentTokenType.EndReplacementSegment), DocumentToken.GetTokenTypeValue(DocumentTokenType.QuickTerminateReplacementSegment))); } if (closeReplacementToken.TokenType == DocumentTokenType.EndReplacementSegment) { body.AddRange(ReadReplacementBody(reader, replacementKeyToken)); } else if (closeReplacementToken.TokenType == DocumentTokenType.QuickTerminateReplacementSegment) { // do nothing, there is no body when the quick termination replacement segment is used } else { throw Unexpected(string.Format("'{0}' or '{1}'", DocumentToken.GetTokenTypeValue(DocumentTokenType.EndReplacementSegment), DocumentToken.GetTokenTypeValue(DocumentTokenType.QuickTerminateReplacementSegment)), closeReplacementToken); } IDocumentExpressionProvider provider; if (this.expressionProviders.TryGetValue(replacementKeyToken.Value, out provider) == false) { provider = new EvalExpressionProvider(); } var context = new DocumentExpressionContext { OpenToken = openToken, CloseToken = closeReplacementToken, Parameters = parameters.AsReadOnly(), Body = body.AsReadOnly(), ReplacementKeyToken = replacementKeyToken, }; var expression = provider.CreateExpression(context); ret.Add(expression); }