gpt4 book ai didi

c# - 这项技术的名称是什么?

转载 作者:行者123 更新时间:2023-11-29 10:54:24 26 4
gpt4 key购买 nike

我刚刚为我的 ECMAScript 实现编写了一个日期解析器。之前我写过一个正则表达式编译器,规范描述过程的方式给我留下了深刻的印象。本质上,输入是通过一系列测试字符串每个部分的延续来传递的。我的日期解析器大致基于这个想法,我真的很想知道它叫什么。

注意:我只留下解析器的核心来减少噪音。

public sealed class DateParser
{
public double Parse()
{
using (var tokens = Tokenize().GetEnumerator())
{
var previous = new Result(ResultType.Success, HandleFirst);
var next = default(Result);
while (true)
{
if (!tokens.MoveNext())
{
return previous.Type == ResultType.Optional ? Complete() : double.NaN;
}
next = previous.Continuation(tokens.Current);
if (next.Type == ResultType.Complete)
{
return Complete();
}
else if (next.Type == ResultType.MustFail)
{
return double.NaN;
}
else if (next.Type == ResultType.CanFail)
{
return previous.Type == ResultType.Optional ? Complete() : double.NaN;
}
previous = next;
}
}
}

private Result HandleFirst(DateToken token)
{
switch (token.Type)
{
case DateTokenType.Integer:
return HandleYear(token);
case DateTokenType.T:
return HandleT(token);
default:
return new Result(ResultType.MustFail, null);
}
}

private Result HandleYear(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 4)
{
_year = double.Parse(token.Value);
return new Result(ResultType.Optional, HandleMonthHyphen);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleMonthHyphen(DateToken token)
{
if (token.Type == DateTokenType.Hyphen)
{
return new Result(ResultType.Success, HandleMonth);
}
return new Result(ResultType.Complete, null);
}

private Result HandleMonth(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_month = double.Parse(token.Value);
if (_month < 1 || _month > 12)
{
_month = null;
return new Result(ResultType.MustFail, null);
}
return new Result(ResultType.Optional, HandleDayHyphen);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleDayHyphen(DateToken token)
{
if (token.Type == DateTokenType.Hyphen)
{
return new Result(ResultType.Success, HandleDay);
}
return new Result(ResultType.CanFail, null);
}

private Result HandleDay(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_day = double.Parse(token.Value);
if (_day < 1 || _day > 31)
{
_day = null;
return new Result(ResultType.MustFail, null);
}
return new Result(ResultType.Optional, HandleT);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleT(DateToken token)
{
if (token.Type == DateTokenType.T)
{
return new Result(ResultType.Success, HandleHour);
}
return new Result(ResultType.CanFail, null);
}

private Result HandleHour(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_hour = double.Parse(token.Value);
if (_hour >= DatePrototype.HoursPerDay)
{
_hour = null;
return new Result(ResultType.MustFail, null);
}
return new Result(ResultType.Success, HandleHourColon);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleHourColon(DateToken token)
{
if (token.Type == DateTokenType.Colon)
{
return new Result(ResultType.Success, HandleMinute);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleMinute(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_minute = double.Parse(token.Value);
if (_minute >= DatePrototype.MinutesPerHour)
{
_minute = null;
return new Result(ResultType.MustFail, null);
}
return new Result(ResultType.Optional, HandleSecondColonOrOffset);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleSecondColonOrOffset(DateToken token)
{
if (token.Type == DateTokenType.Colon)
{
return new Result(ResultType.Success, HandleSecond);
}
else
{
var result = HandleOffset(token);
if (result.Type == ResultType.CanFail)
{
return new Result(ResultType.MustFail, null);
}
return result;
}
}

private Result HandleSecond(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_second = double.Parse(token.Value);
if (_second >= DatePrototype.SecondsPerMinute)
{
_second = null;
return new Result(ResultType.MustFail, null);
}
return new Result(ResultType.Optional, HandleSecondDotOrOffset);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleSecondDotOrOffset(DateToken token)
{
if (token.Type == DateTokenType.Dot)
{
return new Result(ResultType.Success, HandleMillisecond);
}
else
{
var result = HandleOffset(token);
if (result.Type == ResultType.CanFail)
{
return new Result(ResultType.MustFail, null);
}
return result;
}
}

private Result HandleMillisecond(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 3)
{
_millisecond = double.Parse(token.Value);
if (_millisecond >= DatePrototype.MsPerMinute)
{
_millisecond = null;
return new Result(ResultType.MustFail, null);
}
return new Result(ResultType.Optional, HandleOffset);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleOffset(DateToken token)
{
switch (token.Type)
{
case DateTokenType.Z:
_offset = 0.0;
return new Result(ResultType.Success, null);
case DateTokenType.Plus:
_offset = 0.0;
return new Result(ResultType.Success, HandleOffsetHour);
case DateTokenType.Hyphen:
_offset = -0.0;
return new Result(ResultType.Success, HandleOffsetHour);
default:
return new Result(ResultType.CanFail, null);
}
}

private Result HandleOffsetHour(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_offset += double.Parse(token.Value) * DatePrototype.MsPerHour;
return new Result(ResultType.Success, HandleOffsetHourColon);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleOffsetHourColon(DateToken token)
{
if (token.Type == DateTokenType.Colon)
{
return new Result(ResultType.Success, HandleOffsetMinute);
}
return new Result(ResultType.MustFail, null);
}

private Result HandleOffsetMinute(DateToken token)
{
if (token.Type == DateTokenType.Integer && token.Value.Length == 2)
{
_offset += double.Parse(token.Value) * DatePrototype.MsPerMinute;
return new Result(ResultType.Complete, null);
}
return new Result(ResultType.MustFail, null);
}
}

最佳答案

我认为您要查找的术语是 Finite State Machine ,本质上是这样的:

while( tokensAvailable ){
// look at current token
// do something maybe relating to state
// loop
}

关于c# - 这项技术的名称是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3376439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com