forked from kontur-school-2014/01-mark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
36 lines (34 loc) · 872 Bytes
/
Parser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mark.Readers;
using MoreLinq;
namespace Mark
{
public static class Parser
{
private static readonly List<ITokenReader> Readers = new List<ITokenReader>
{
new LineEndReader(),
new WhitespaceReader(),
new WordReader(),
new UnderscoreReader(),
new EscapeReader(),
new CodeReader(),
new SeparatorReader()
};
public static List<Token> Parse(string text)
{
var result = new List<Token>();
while (text.Length > 0)
{
var best = Readers.Select(x => x.ReadToken(text)).Where(token => token != null)
.Concat(new Token(text.Substring(0, 1), HttpUtility.HtmlEncode(text.Substring(0, 1)), TokenType.Unknown))
.MaxBy(token => token.Source.Length);
result.Add(best);
text = text.Substring(best.Source.Length);
}
return result;
}
}
}