/// <summary>Reads a tag from the given lexer. Note that this does not read it's children or closing tag.</summary> /// <param name="lexer">The lexer the tag should be read from.</param> protected void ReadTag(MLLexer lexer) { bool first = true; char peek = lexer.Peek(); if (peek == '<') { lexer.Read(); peek = lexer.Peek(); } while (peek != StringReader.NULL && peek != '>') { string property; string value; PropertyTextReader.Read(lexer, first || SelfClosing, out property, out value); property = property.ToLower(); if (first) { first = false; SetTag(property); } else if (property == "/") { SelfClosing = true; } else { this[property] = value; } peek = lexer.Peek(); } if (peek == '>') { lexer.Read(); } }
/// <summary>Reads the children for this tag from a lexer.</summary> /// <param name="lexer"></param> /// <param name="innerElement">True if we're looking for the closing tag of this element to exit. /// If its found, this method safely returns. Unbalanced tags will otherwise throw an exception.</param> /// <param name="literal">Literal is true if the content should be read 'as is', ignoring any tags.</param> protected void ReadContent(MLLexer lexer, bool innerElement, bool literal) { char Peek = lexer.Peek(); string variableString = ""; bool readingVariable = false; MLTextElement textElement = null; List <string> variableArguments = null; System.Text.StringBuilder builder = new System.Text.StringBuilder(); while (Peek != StringReader.NULL) { if (readingVariable) { lexer.Read(); if (Peek == ';') { readingVariable = false; // First, flush textElement if we're working on one (likely): if (textElement != null) { // Make sure it adds the last word: textElement.DoneWord(true); textElement = null; } if (builder.Length != 0) { // The name is in the builder: variableString = builder.ToString(); builder.Length = 0; } // Check if this string (e.g. &WelcomeMessage;) is provided by the variable set: // Generate a new variable element: MLVariableElement varElement = CreateVariableElement(); varElement.SetVariableName(variableString); if (variableArguments != null) { varElement.SetArguments(variableArguments.ToArray()); variableArguments = null; } varElement.LoadNow(innerElement); variableString = ""; } else if (Peek == '(') { // Read runtime argument set. &WelcomeMessage('heya!'); variableString = builder.ToString(); builder.Length = 0; Peek = lexer.Peek(); variableArguments = new List <string>(); while (Peek != StringReader.NULL) { if (Peek == ')') { // Add it: variableArguments.Add(builder.ToString()); builder.Length = 0; // Read it off: lexer.Read(); break; } else if (Peek == ',') { // Done one parameter - onto the next. variableArguments.Add(builder.ToString()); builder.Length = 0; } else if (Peek == '"' || Peek == '\'') { // One of our args is a "string". // Use the string reader of the PropertyTextReader to read it. PropertyTextReader.ReadString(lexer, builder); // We don't want to read a char off, so continue here. // Peek the next one: Peek = lexer.Peek(); continue; } else if (Peek != ' ') { // Generall numeric args will fall down here. // Disallowing spaces means the set can be spaced out like so: (14, 21) builder.Append(Peek); } // Read off the char: lexer.Read(); // Peek the next one: Peek = lexer.Peek(); } } else { builder.Append(Peek); } } else if (!literal && Peek == '<') { if (textElement != null) { // Make sure it adds the last word: textElement.DoneWord(true); textElement = null; } // Read off the <. lexer.Read(); if (lexer.Peek() == '/') { // Should be the closure (</tag>) for 'this' element. // Read off the /: lexer.Read(); char closePeek = lexer.Peek(); while (closePeek != '>' && closePeek != StringReader.NULL) { builder.Append(closePeek); lexer.Read(); closePeek = lexer.Peek(); } string tag = builder.ToString(); builder.Length = 0; if (closePeek == '>') { // Read it off: lexer.Read(); } if (innerElement && (tag == Tag || tag.ToLower() == Tag)) { // Closure for this element read off. // Time to get outta here! return; } else { int charNumber; int line = lexer.GetLineNumber(out charNumber); throw new Exception("Unbalanced tags detected: " + tag + " is closing " + Tag + " at line " + line + ", character " + charNumber + ". " + lexer.ReadLine(line) + "."); } } else { MLElement tag = CreateTagElement(lexer); TagHandler handler = tag.GetHandler(); if (tag.SelfClosing) { tag.OnChildrenLoaded(); handler.OnTagLoaded(); } else { handler.OnParseContent(lexer); // Read its kids and possibly its ending tag (the true means we're expecting an ending tag): tag.ReadContent(lexer, true, false); tag.OnChildrenLoaded(); handler.OnTagLoaded(); } } } else if (!literal && Peek == '&') { // E.g. > & etc. readingVariable = true; // Read off the &: lexer.Read(); } else { if (textElement == null) { textElement = CreateTextElement(); } textElement.AddCharacter(lexer.Read()); } Peek = lexer.Peek(); } if (textElement != null) { textElement.DoneWord(true); } }