3

我还没有找到一个例子 - 如何处理字符转义。我找到了一个代码示例:

static void Main(string[] args)
{
    string text = "'test \\\' text'";
    var result = Grammar.QuotedText.End().Parse(text);
}

public static class Grammar
{
    private static readonly Parser<char> QuoteEscape = Parse.Char('\\');
    private static Parser<T> Escaped<T>(Parser<T> following)
    {
        return from escape in QuoteEscape
               from f in following
               select f;
    }

    private static readonly Parser<char> QuotedTextDelimiter = Parse.Char('\'');

      private static readonly Parser<char> QuotedContent =
          Parse.AnyChar.Except(QuotedTextDelimiter).Or(Escaped(QuotedTextDelimiter));

    public static Parser<string> QuotedText = (
        from lquot in QuotedTextDelimiter
        from content in QuotedContent.Many().Text()
        from rquot in QuotedTextDelimiter
        select content
        ).Token();
}

如果文本不包含转义,它会成功解析文本,但不会解析带有转义字符的文本。

4

2 回答 2

6

我有一个类似的问题,使用"分隔符和\转义符解析字符串。我为此编写了一个简单的解析器(可能不是最优雅的解决方案),它似乎工作得很好。

您应该能够适应它,因为唯一的区别似乎是分隔符。

var escapedDelimiter = Parse.String("\\\"").Text().Named("Escaped delimiter");
var singleEscape = Parse.String("\\").Text().Named("Single escape character");
var doubleEscape = Parse.String("\\\\").Text().Named("Escaped escape character");
var delimiter = Parse.Char('"').Named("Delimiter");
var simpleLiteral = Parse.AnyChar.Except(singleEscape).Except(delimiter).Many().Text().Named("Literal without escape/delimiter character");

var stringLiteral = (from start in delimiter
            from v in escapedDelimiter.Or(doubleEscape).Or(singleEscape).Or(simpleLiteral).Many()
            from end in delimiter
            select string.Concat(start) + string.Concat(v) + string.Concat(end));

关键部分是from v in ...。它首先搜索转义分隔符,然后搜索双转义字符,然后搜索单个转义字符,然后尝试将其解析为没有任何转义或分隔符的“simpleLiteral”。在此处更改顺序会导致解析错误(例如,如果您尝试在转义分隔符之前解析单转义,您将永远找不到后者,对于双转义和单转义也是如此)。此步骤重复多次,直到出现未转义的定界符(from v in ...不处理未转义from end in delimiter的定界符,但当然可以)。

于 2015-11-01T16:47:44.917 回答
0

我需要解析可以用单引号或双引号表示的字符串文字,而且还支持对这些文字进行转义。

生成字符串文字解析器的方法:

private readonly StringBuilder _reusableStringBuilder = new StringBuilder();

private Parser<string> BuildStringLiteralParser(char delimiterChar)
{
    var escapeChar = '\\';

    var delimiter = Sprache.Parse.Char(delimiterChar);
    var escape = Sprache.Parse.Char(escapeChar);
    var escapedDelimiter = Sprache.Parse.String($"{escapeChar}{delimiterChar}");
    var splitByEscape = Sprache.Parse.AnyChar
        .Except(delimiter.Or(escape))
        .Many()
        .Text()
        .DelimitedBy(escapedDelimiter);

    string BuildStr(IEnumerable<IEnumerable<string>> splittedByEscape)
    {
        _reusableStringBuilder.Clear();

        var i = 0;

        foreach (var splittedByEscapedDelimiter in splittedByEscape)
        {
            if (i > 0)
            {
                _reusableStringBuilder.Append(escapeChar);
            }

            var j = 0;

            foreach (var str in splittedByEscapedDelimiter)
            {
                if (j > 0)
                {
                    _reusableStringBuilder.Append(delimiterChar);
                }

                _reusableStringBuilder.Append(str);

                j++;
            }

            i++;
        }

        return _reusableStringBuilder.ToString();
    }

    return (from ln in delimiter
            from splittedByEscape in splitByEscape.DelimitedBy(escape)
            from rn in delimiter
            select BuildStr(splittedByEscape)).Named("string");
}

用法:

var stringParser = BuildStringLiteralParser('\"').Or(BuildStringLiteralParser('\''));

var str1 = stringParser.Parse("\"'Hello' \\\"John\\\"\"");
Console.WriteLine(str1);

var str2 = stringParser.Parse("\'\\'Hello\\' \"John\"\'");
Console.WriteLine(str2);

输出:

'Hello' "John"
'Hello' "John"

检查工作演示: https ://dotnetfiddle.net/8wFNbj

于 2021-12-06T08:50:29.533 回答