/// <summary> /// Test method used during development /// </summary> /// <param name="scanner"></param> /// <param name="output"></param> private static void ReadAll(Scanner scanner, TextWriter output) { int token = None; while (token != Eof) { scanner.NextToken(); token = scanner.CurrentToken; #if DEBUG switch (token) { case Eol: output.WriteLine("[EOL]"); break; case Level: output.WriteLine("[LEVEL] {" + new string(scanner.Lexeme) + "}"); break; case Tag: output.WriteLine("[TAG] {" + new string(scanner.Lexeme) + "}"); break; case Xref: output.WriteLine("[XREF] {" + new string(scanner.Lexeme) + "}"); break; case Eof: output.WriteLine("[EOF]"); break; case Text: output.WriteLine("[TEXT] {" + new string(scanner.Lexeme) + "}"); break; default: output.WriteLine("[UNKNOWN] {" + new string(scanner.Lexeme) + "}"); break; } #endif } }
/// <summary> /// Test method used during development /// </summary> /// <param name="scanner"></param> /// <param name="output"></param> private static void ReadAll(Scanner scanner, TextWriter output) { int token = None; while (token != Eof) { scanner.NextToken(); token = scanner.CurrentToken; #if DEBUG switch(token) { case Eol: output.WriteLine("[EOL]"); break; case Level: output.WriteLine("[LEVEL] {" + new string(scanner.Lexeme) + "}"); break; case Tag: output.WriteLine("[TAG] {" + new string(scanner.Lexeme) + "}"); break; case Xref: output.WriteLine("[XREF] {" + new string(scanner.Lexeme) + "}"); break; case Eof: output.WriteLine("[EOF]" ); break; case Text: output.WriteLine("[TEXT] {" + new string(scanner.Lexeme) + "}"); break; default: output.WriteLine("[UNKNOWN] {" + new string(scanner.Lexeme) + "}"); break; } #endif } }
/// <summary> /// Reads a line of tokens and places the corresponding XML nodes into a queue /// /// Good candidate for performance improvements /// GEDCOMReader is actually faster than XmlReader. However, it allocates more strings /// and these are objects, so the GC has to do more work. - Since the char[] rewrite, this may /// no longer be true /// </summary> private void ReadLine() { _attributes.Clear(); while (true) { _scanner.NextToken(); switch (_scanner.CurrentToken) { case Scanner.Level: Debug.Assert(_nodeQueue.Count == 0, "Unexpected level token"); int depth = Convert.ToInt32(new string(_scanner.Lexeme)) + 2; while (depth <= _nodeStack.Count) { // Inject EndTag nodes into the queue _nodeQueue.Enqueue(new TokenInfo(XmlNodeType.EndElement, _nodeStack.Pop())); } break; case Scanner.Tag: // add node to queue // don't know whether next token is text or xref _nodeQueue.Enqueue(new TokenInfo(XmlNodeType.Element, _scanner.Lexeme)); //_scanner.LexemeNT)); break; case Scanner.Xref: // add node to attribute collection switch (SneakyPeek(_nodeQueue).Type) { case XmlNodeType.Element: _attributes.Add(new GedcomAttribute(GedcomAttributeType.Ref, _scanner.Lexeme)); break; case XmlNodeType.None: case XmlNodeType.EndElement: _attributes.Add(new GedcomAttribute(GedcomAttributeType.Id, _scanner.Lexeme)); break; } break; case Scanner.Text: // add node to queue _nodeQueue.Enqueue(new TokenInfo(XmlNodeType.Text, _scanner.Lexeme)); break; case Scanner.Escape: // add node to queue _nodeQueue.Enqueue(new TokenInfo(XmlNodeType.ProcessingInstruction, _scanner.Lexeme)); //_scanner.LexemeNT)); break; case Scanner.Eol: return; case Scanner.Eof: while (_nodeStack.Count != 0) { // Inject EndTag nodes into the queue _nodeQueue.Enqueue(new TokenInfo(XmlNodeType.EndElement, _nodeStack.Pop())); } return; default: throw new GedcomException("UNEXPECTED TOKEN '" + _scanner.Lexeme + "'", _scanner.Lexeme[0], LinePosition, LineNumber); } } }