/// <summary> /// Pascalizes the specified token. /// </summary> /// <param name="token">The token.</param> /// <param name="container">The container.</param> /// <returns></returns> private string Pascalize(ParserToken token, TemplateContainer container) { string value = this.EvaluateParameter(container, token.Parameters[0]); return StringUtil.Pascalize(value); }
/// <summary> /// Determines whether the specified token has the specified index. /// </summary> /// <param name="token">The token.</param> /// <param name="container">The container.</param> /// <returns></returns> private string IsIndex(ParserToken token, TemplateContainer container) { string value = this.EvaluateParameter(container, token.Parameters[0]); return (container.Attributes["Index"].ToString() == value).ToString(); }
/// <summary> /// Determines whether the specified token is last. /// </summary> /// <param name="token">The token.</param> /// <param name="container">The container.</param> /// <returns></returns> private string IsLast(ParserToken token, TemplateContainer container) { return (container.Attributes["Index"] == container.Attributes["MaxIndex"]).ToString(); }
private string GetLowGuid(ParserToken token, TemplateContainer container) { return GetGuid(token, container).ToLower(); }
/// <summary> /// Determines whether the specified token is first. /// </summary> /// <param name="token">The token.</param> /// <param name="container">The container.</param> /// <returns></returns> private string IsFirst(ParserToken token, TemplateContainer container) { return (container.Attributes["Index"].ToString() == "0").ToString(); }
/// <summary> /// Adds the parameter. /// </summary> /// <param name="token">The token.</param> public void AddParameter(ParserToken token) { token.Parent = this; this.Parameters.Add(token); }
private string GetGuid(ParserToken token, TemplateContainer container) { string value = this.EvaluateParameter(container, token.Parameters[0]); if (guidMap.ContainsKey(value)) { return guidMap[value]; } else { string guid = Guid.NewGuid().ToString().ToUpper(); guidMap.Add(value, guid); return guid; } }
/// <summary> /// Evaluates one identifier token. /// </summary> /// <param name="container">The container.</param> /// <param name="token">The token.</param> private void EvaluateIdentifier(TemplateContainer container, ParserToken token) { if (this.Commands.ContainsKey(token.Content)) { LanguageCommandType commandType = this.Commands[token.Content]; switch (commandType) { case LanguageCommandType.Foreach: if (token.Parameters.Count > 0) { object obj = container.GetByPath(token.Parameters[0].Content); if ((obj != null) && (obj is TemplateContainer)) { TemplateContainer sonContainer = (TemplateContainer)obj; if (sonContainer.IsList) { int index = 0; string maxIndex = (sonContainer.ArrayValues.Count - 1).ToString(); foreach (TemplateContainer gransonContainer in sonContainer.ArrayValues) { gransonContainer.AddAttribute("Index", index.ToString()); gransonContainer.AddAttribute("MaxIndex", maxIndex); foreach (ParserToken son in token.Sons) { this.Visit(gransonContainer, son); } index++; } } else { //foreach (ParserToken son in token.Sons) //{ // this.Visit(sonContainer, son); //} } } else if ((obj != null) && (obj is TemplateLink)) { TemplateLink link = obj as TemplateLink; if (link.IsList) { object objcontainer = container.GetByPath(link.Link); if ((objcontainer != null) && (objcontainer is TemplateContainer)) { TemplateContainer sonContainer = (TemplateContainer)objcontainer; if (sonContainer.IsList) { int index = 0; string maxIndex = (link.ListValues.Count - 1).ToString(); foreach (string listValue in link.ListValues) { object foundobj = sonContainer.GetByPath("[" + listValue + "]"); if ((foundobj != null) && (foundobj is TemplateContainer)) { TemplateContainer gransonContainer = foundobj as TemplateContainer; gransonContainer.AddAttribute("Index", index.ToString()); gransonContainer.AddAttribute("MaxIndex", maxIndex); foreach (ParserToken son in token.Sons) { this.Visit(gransonContainer, son); } index++; } } } } } } } break; case LanguageCommandType.If: if (token.Parameters.Count > 0) { if (EvaluateCondition(token.Parameters, container)) { foreach (ParserToken son in token.Sons) { this.Visit(container, son); } } } break; case LanguageCommandType.Else: break; } } else { object obj = container.GetByPath(token.Content); if (obj != null) { builder.Append(obj.ToString()); } else { if (this.Functions.ContainsKey(token.Content)) { builder.Append(this.Functions[token.Content](token, container)); } } } }
/// <summary> /// Evaluates one parameter, resolving to a string. /// </summary> /// <param name="container">The container.</param> /// <param name="parameter">The parameter.</param> /// <returns></returns> private string EvaluateParameter(TemplateContainer container, ParserToken parameter) { object obj = container.GetByPath(parameter.Content); if (obj != null) { return obj.ToString(); } else { if (this.Functions.ContainsKey(parameter.Content)) { return this.Functions[parameter.Content](parameter, container); } else { return parameter.Content; } } }
/// <summary> /// Builds the token tree. /// </summary> protected override void BuildTree() { ParserToken listToken = new ParserToken((int)LanguageTokenType.Root, ""); ParserToken currentToken = listToken; foreach (ParserToken token in this.tokens) { switch (token.TokenType) { case (int)LanguageTokenType.Root: break; case (int)LanguageTokenType.Separator: if (token.Content.Equals(this.ParametersBegin)) { currentToken = currentToken.Parameters[currentToken.Parameters.Count - 1]; } else if (token.Content.Equals(this.ParametersEnd)) { currentToken = currentToken.Parent; } else if (token.Content.Equals(".")) { } break; case (int)LanguageTokenType.Text: case (int)LanguageTokenType.Identifier: if (token.Content != string.Empty) { currentToken.AddParameter(token); } break; case (int)LanguageTokenType.TextNoBreak: if (token.Content != string.Empty) { currentToken.AddParameter(token); } break; case (int)LanguageTokenType.Literal: currentToken.AddParameter(token); break; } } Stack<ParserToken> tokenStack = new Stack<ParserToken>(); currentToken = this.root; foreach (ParserToken token in listToken.Parameters) { if (token.TokenType == (int)LanguageTokenType.Identifier) { if (this.Commands.ContainsKey(token.Content)) { LanguageCommandType commandType = this.Commands[token.Content]; switch (commandType) { case LanguageCommandType.Foreach: case LanguageCommandType.If: case LanguageCommandType.Else: currentToken.AddSon(token); tokenStack.Push(currentToken); currentToken = token; break; case LanguageCommandType.End: currentToken = tokenStack.Pop(); if (this.Commands.ContainsKey(currentToken.Content)) { commandType = this.Commands[currentToken.Content]; if (commandType == LanguageCommandType.Else) { currentToken = tokenStack.Pop(); } } break; } } else { currentToken.AddSon(token); } } else { currentToken.AddSon(token); } } }
private string Camelize2(ParserToken token, TemplateContainer container) { string result = ""; string value = this.EvaluateParameter(container, token.Parameters[0]); for (int i = 0; i < value.Length; i++) { char c = value[i]; if (i == 0) { result = result + char.ToLower(c); } else { result = result + c; } } return result; }
/// <summary> /// Parses the specified source. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> public ParserToken Parse(string source) { this.source = source; this.InitParsing(); while (this.GetChar()) { if (!ResolveAction()) { this.Append(); } } this.ResolveEndAction(); this.root = new ParserToken(rootTokenType, ""); this.BuildTree(); return root; }
/// <summary> /// Pushes one token. /// </summary> /// <param name="type">The type.</param> /// <returns></returns> private ParserToken PushToken(int type) { ParserToken result = new ParserToken(type, builder.ToString()); this.tokens.Add(result); return result; }
/// <summary> /// Pushes one token. /// </summary> /// <param name="type">The type.</param> /// <param name="content">The content.</param> /// <returns></returns> private ParserToken PushToken(int type, string content) { ParserToken result = new ParserToken(type, content); this.tokens.Add(result); return result; }
private string Upper(ParserToken token, TemplateContainer container) { string value = this.EvaluateParameter(container, token.Parameters[0]); return value.ToUpper(); }
private string FirstUpper(ParserToken token, TemplateContainer container) { string value = this.EvaluateParameter(container, token.Parameters[0]); if (string.IsNullOrEmpty(value)) { return value; } return char.ToUpper(value[0]) + value.Substring(1); }
/// <summary> /// Visits the specified token using the container. /// </summary> /// <param name="container">The container.</param> /// <param name="token">The token.</param> private void Visit(TemplateContainer container, ParserToken token) { if (token.TokenType == (int)LanguageTokenType.Root) { foreach (ParserToken nested in token.Sons) { this.Visit(container, nested); } } if (token.TokenType == (int)LanguageTokenType.Text) { this.builder.Append(token.Content); } else if (token.TokenType == (int)LanguageTokenType.TextNoBreak) { int i = token.Content.IndexOf('\n'); if (i > -1) { this.builder.Append(token.Content.Substring(i)); } else { this.builder.Append(token.Content); } } else if (token.TokenType == (int)LanguageTokenType.Identifier) { this.EvaluateIdentifier(container, token); } }
/// <summary> /// Adds the son. /// </summary> /// <param name="token">The token.</param> public void AddSon(ParserToken token) { token.Parent = this; this.Sons.Add(token); }