这很棘手,主要是因为您需要保留空白,但我能够提出一个满足您需求的解析器。首先,我不得不Tokens
稍微改变你的枚举:
public enum Tokens
{
None,
String,
Number,
[Token(Example = "()")]
OpenCloseParen,
[Token(Example = "(")]
LParen,
[Token(Example = ")")]
RParen,
[Token(Example = "#")]
Hash,
[Token(Example = "$")]
Dollar,
[Token(Example = "|")]
Pipe,
[Token(Example = ".")]
Dot,
[Token(Example = " ")]
Whitespace,
}
接下来,我们可以构建以下内容Tokenizer
:
var tokenizer = new TokenizerBuilder<Tokens>()
.Match(Span.EqualTo("()"), Tokens.OpenCloseParen)
.Match(Character.EqualTo('('), Tokens.LParen)
.Match(Character.EqualTo(')'), Tokens.RParen)
.Match(Character.EqualTo('#'), Tokens.Hash)
.Match(Character.EqualTo('$'), Tokens.Dollar)
.Match(Character.EqualTo('.'), Tokens.Dot)
.Match(Character.EqualTo('|'), Tokens.Pipe)
.Match(Character.EqualTo(' '), Tokens.Whitespace)
.Match(Span.MatchedBy(Character.AnyChar), Tokens.String)
.Match(Numerics.Natural, Tokens.Number)
.Build();
接下来,创建模型类来保存输出(您可能会想到更好的名称,因为我不确定您正在解析的这些数据是什么):
public abstract class Node
{
}
public class TextNode : Node
{
public string Value { get; set; }
}
public class Expression : Node
{
public Node[] Nodes { get; set; }
}
然后我们创建解析器:
public static class MyParsers
{
/// <summary>
/// Parses any whitespace (if any) and returns a resulting string
/// </summary>
public readonly static TokenListParser<Tokens, string> OptionalWhitespace =
from chars in Token.EqualTo(Tokens.Whitespace).Many().OptionalOrDefault()
select chars == null ? "" : new string(' ', chars.Length);
/// <summary>
/// Parses a valid text expression
/// e.g. "abc", "a.c()", "$c", etc.
/// </summary>
public readonly static TokenListParser<Tokens, Node> TextExpression =
from tokens in
Token.EqualTo(Tokens.OpenCloseParen)
.Or(Token.EqualTo(Tokens.Hash))
.Or(Token.EqualTo(Tokens.Dollar))
.Or(Token.EqualTo(Tokens.Dot))
.Or(Token.EqualTo(Tokens.Number))
.Or(Token.EqualTo(Tokens.String))
.Or(Token.EqualTo(Tokens.Whitespace))
.Many()
// if this side of the pipe is all whitespace, return null
select (Node) (
tokens.All(x => x.ToStringValue() == " ")
? null
: new TextNode {
Value = string.Join("", tokens.Select(t => t.ToStringValue())).Trim()
}
);
/// <summary>
/// Parses a full expression that may contain text expressions or nested sub-expressions
/// e.g. "(a | b)", "( (a.c() | b) | (123 | c) )", etc.
/// </summary>
public readonly static TokenListParser<Tokens, Node> Expression =
from leadWs in OptionalWhitespace
from lp in Token.EqualTo(Tokens.LParen)
from nodes in TextExpression
.Or(Parse.Ref(() => Expression))
.ManyDelimitedBy(Token.EqualTo(Tokens.Pipe))
.OptionalOrDefault()
from rp in Token.EqualTo(Tokens.RParen)
from trailWs in OptionalWhitespace
where nodes.Length > 1 && nodes.Any(node => node != null) // has to have at least two sides and one has to be non-null
select (Node)new Expression {
Nodes = nodes.Select(node => node ?? new TextNode { Value = "" }).ToArray()
};
}
最后,我们可以使用分词器和解析器来解析您的输入:
string input = "(a b | c.())";
var tokens = tokenizer.Tokenize(input);
var result = MyParsers.Expression.TryParse(tokens);
if (result.HasValue)
{
// input is valid
var expression = (Expression)result.Value;
// do what you need with it here, i.e. loop through the nodes, output the text, etc.
}
else
{
// not valid
}
这适用于几乎所有的测试用例,除了像这样(()|())
的打开/关闭括号是管道两侧的值的测试用例。可能还有更好的方法来进行一些解析,因为我自己刚刚习惯了 Superpower,但我认为这是一个很好的开始,因此您可以优化它和/或将所有边缘案例集成到它。
编辑
是空白把一切都搞砸了。我必须在Expression
解析器中添加更多的空白检查,还必须添加一个条件来检查非空TextExpression
,然后还要检查一个可能为空的。这是为了处理管道一侧为空白的情况。这是工作解析器:
public readonly static TokenListParser<Tokens, Node> Expression =
from _1 in OptionalWhitespace
from lp in Token.EqualTo(Tokens.LParen)
from _2 in OptionalWhitespace
from nodes in
TextExpression.Where(node => node != null) // check for actual text node first
.Or(Expression)
.Or(TextExpression) // then check to see if it's empty
.ManyDelimitedBy(Token.EqualTo(Tokens.Pipe))
from _3 in OptionalWhitespace
from rp in Token.EqualTo(Tokens.RParen)
from _4 in OptionalWhitespace
where nodes.Length > 1 && nodes.Any(node => node != null) // has to have at least two sides and one has to be non-null
select (Node)new Expression {
Nodes = nodes.Select(node => node ?? new TextNode { Value = "" }).ToArray()
};