BlockLexer.cs 753 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Larkdown.Parser.Lexer
  5. {
  6. class BlockLexer
  7. {
  8. private IList<string> _lines;
  9. private IEnumerator<string> _enumerator;
  10. public BlockLexer(string source)
  11. {
  12. _lines = source.Split(new[] { "\n" }, 0);
  13. _enumerator = _lines.GetEnumerator();
  14. }
  15. public BlockToken NextToken()
  16. {
  17. if (!_enumerator.MoveNext())
  18. {
  19. return null;
  20. }
  21. var line = _enumerator.Current;
  22. var indent = 0;
  23. while (indent < line.Length && IsIndent(line[indent++])) { }
  24. if (indent > 0)
  25. {
  26. line = line.Substring(indent);
  27. }
  28. return new BlockToken(BlockTokenType.Line, indent, line);
  29. }
  30. private bool IsIndent(char c)
  31. {
  32. return c == ' ' || c == '\t';
  33. }
  34. }
  35. }