亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Sprache 解析文本時,我可以確定原始字符串中的當前索引嗎?

使用 Sprache 解析文本時,我可以確定原始字符串中的當前索引嗎?

C#
慕沐林林 2023-07-09 15:06:29
我設置了 Sprache 來解析一個方程,該方程中有許多不同的可能的方法調用。解析方法后,有沒有辦法確定原始字符串中的索引值?也許解析有一個可以以某種方式訪問的“當前索引”值和“長度”值?輸入字符串示例:IndexOf("fred", 2) + IndexOf("bob")使用這樣的解析器...Parser<Expression> FunctionCall = from namePart in Parse.Letter.Many().Text()                       from lparen in Parse.Char('(')                       from expr in Parameter.DelimitedBy(ListDelimiter)                       from rparen in Parse.Char(')')                       select CallMethod(namePart, Enumerable.Repeat(sourceData, 1)                                                             .Concat(expr)                                                             .ToArray());誰能想到一個“技巧”,讓我能夠確定第一個 CallMethod 處理SubString(0, 18),第二個 CallMethod 處理原始字符串中的SubString(21, 14) ?
查看完整描述

2 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

如果您使用通用類和擴展方法,您可以采用更通用的方法


public class PositionAware<T> : IPositionAware<PositionAware<T>>

{

    public PositionAware(T value)

    {

        Value = value;

    }


    public T Value { get; }

    public Position Start { get; private set; }

    public int Length { get; private set; }

    public PositionAware<T> SetPos(Position startPos, int length)

    {

        Start = startPos;

        Length = length;

        return this;

    }


}

public static Parser<PositionAware<T>> WithPosition<T>(this Parser<T> value)

{

    return value.Select(x => new PositionAware<T>(x)).Positioned();

}

使用它:


from c in Parse.Char('a').WithPosition()

select (c.Start, c.Value)


from c in Parameter.DelimitedBy(ListDelimiter).WithPosition()

select (c.Start, c.Value)


查看完整回答
反對 回復 2023-07-09
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

我已經設法回答我自己的問題。這是 Positioned() 解析器擴展調用,允許解析器跟蹤原始文本中的位置。


  Parser<Expression> FunctionCall = (from namePart in Parse.Letter.Many().Text()

                            from lparen in Parse.Char('(')

                            from expr in Parameter.DelimitedBy(ListDelimiter)

                            from rparen in Parse.Char(')')

                            select new MethodPosAware(namePart, expr)).Positioned()

                            .Select(x => CallMethod(x.Value, Enumerable.Repeat(sourceData, 1)

                                        .Concat(x.Params)

                                        .ToArray(),

                                        x.Pos.Pos, x.Length));

我必須創建一個新的MethodPosAware類來保留位置信息,該信息源自 Sprache 的IPositionAware:


class MethodPosAware : IPositionAware<MethodPosAware>

{

    public MethodPosAware(string methodName, IEnumerable<Expression> parameters)

    {

        Value = methodName;

        Params = parameters;

    }


    public MethodPosAware SetPos(Position startPos, int length)

    {

        Pos = startPos;

        Length = length;

        return this;

    }


    public Position Pos { get; set; }

    public int Length { get; set; }

    public string Value { get; set; }

    public IEnumerable<Expression> Params { get; set; }

}

我想我將進一步擴展它以不僅僅使用方法名稱,但這足以回答我現在的問題。我希望這可以幫助某人。


查看完整回答
反對 回復 2023-07-09
  • 2 回答
  • 0 關注
  • 135 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號