Compare commits
3
Commits
f372f83171
...
57587bec16
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57587bec16 | ||
|
|
9e9078ced1 | ||
|
|
5a3286dc21 |
@@ -16,12 +16,15 @@ namespace Nitload.RegularAnalysis.Text
|
||||
internal partial class TextRegexSourceGen
|
||||
{
|
||||
|
||||
internal enum ExitMode
|
||||
internal enum ExitKind
|
||||
{
|
||||
|
||||
Break,
|
||||
Continue,
|
||||
Goto,
|
||||
MatchFailed,
|
||||
MatchEnd,
|
||||
CountRetry,
|
||||
CountResume,
|
||||
AltNext,
|
||||
AltMatched,
|
||||
|
||||
}
|
||||
|
||||
@@ -29,25 +32,28 @@ namespace Nitload.RegularAnalysis.Text
|
||||
{
|
||||
|
||||
public ExitLabel(
|
||||
ExitMode mode,
|
||||
ExitKind kind,
|
||||
int order
|
||||
)
|
||||
{
|
||||
Mode = mode;
|
||||
Kind = kind;
|
||||
LabelOrder = order;
|
||||
}
|
||||
|
||||
public ExitMode Mode { get; init; }
|
||||
public ExitKind Kind { get; init; }
|
||||
|
||||
public int LabelOrder { get; init; }
|
||||
|
||||
public string GetLabelName()
|
||||
{
|
||||
return Mode switch
|
||||
return Kind switch
|
||||
{
|
||||
ExitMode.Break => $@"__break_{LabelOrder}",
|
||||
ExitMode.Continue => $@"__continue_{LabelOrder}",
|
||||
ExitMode.Goto => $@"__goto_{LabelOrder}",
|
||||
ExitKind.MatchFailed => $@"__match_failed_{LabelOrder}",
|
||||
ExitKind.MatchEnd => $@"__match_end_{LabelOrder}",
|
||||
ExitKind.CountRetry => $@"__count_retry_{LabelOrder}",
|
||||
ExitKind.CountResume => $@"__count_resume_{LabelOrder}",
|
||||
ExitKind.AltNext => $@"__alt_next_{LabelOrder}",
|
||||
ExitKind.AltMatched => $@"__alt_matched_{LabelOrder}",
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,20 +14,31 @@ namespace Nitload.RegularAnalysis.Text
|
||||
internal partial class TextRegexSourceGen
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Emits a subtree and updates the retry target for the rest of the current path.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// local_fail is the owning region's fallback. backtrack starts at that fallback and
|
||||
/// advances when a COUNT registers a retry point. Returning from recursion does not
|
||||
/// discard it. Mutually exclusive branches must use separate backtrack variables.
|
||||
/// </remarks>
|
||||
internal static void GenerateMethodSource(
|
||||
RegexAstNode ast,
|
||||
in SequenceEmitter<string> sc,
|
||||
in List<SequenceEmitter<string>> func_list,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit_label = default,
|
||||
in ExitLabel local_fail,
|
||||
ref ExitLabel backtrack,
|
||||
bool is_find = false
|
||||
)
|
||||
{
|
||||
// local_fail belongs to this region. COUNT updates only the current path's
|
||||
// backtrack target; CONCAT passes that update on to every later sibling.
|
||||
RegexAstNode root = ast;
|
||||
if (root.Label == RegexElementLabel.EPSILON) {
|
||||
return;
|
||||
}
|
||||
if (TryGenerateSpecialSource(root, sc, func_list, context, exit_label)) {
|
||||
if (TryGenerateSpecialSource(root, sc, func_list, context, local_fail, ref backtrack)) {
|
||||
return;
|
||||
}
|
||||
RegexElementLabel op = root.Label & (RegexElementLabel.VAST_OPERATOR);
|
||||
@@ -36,16 +47,16 @@ namespace Nitload.RegularAnalysis.Text
|
||||
switch (op)
|
||||
{
|
||||
case RegexElementLabel.CONCAT:
|
||||
GenerateConcat(root, sc, func_list, context, exit_label);
|
||||
GenerateConcat(root, sc, func_list, context, local_fail, ref backtrack);
|
||||
break;
|
||||
case RegexElementLabel.COUNT:
|
||||
GenerateCount(root, sc, func_list, context, exit_label);
|
||||
GenerateCount(root, sc, func_list, context, local_fail, ref backtrack);
|
||||
break;
|
||||
case RegexElementLabel.GROUP_BEGIN:
|
||||
GenerateGroup(root, sc, func_list, context, exit_label);
|
||||
GenerateGroup(root, sc, func_list, context, local_fail, ref backtrack);
|
||||
break;
|
||||
case RegexElementLabel.ALT:
|
||||
GenerateAlt(root, sc, func_list, context, exit_label);
|
||||
GenerateAlt(root, sc, func_list, context, backtrack);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -59,13 +70,13 @@ namespace Nitload.RegularAnalysis.Text
|
||||
switch (op)
|
||||
{
|
||||
case RegexElementLabel.UNIT:
|
||||
GenerateUnit(root, sc, context, exit_label);
|
||||
GenerateUnit(root, sc, context, backtrack);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
GenerateUnitSet(root, sc, context, exit_label);
|
||||
GenerateUnitSet(root, sc, context, backtrack);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
GenerateUnitSpan(root, sc, context, exit_label);
|
||||
GenerateUnitSpan(root, sc, context, backtrack);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -221,7 +232,8 @@ namespace Nitload.RegularAnalysis.Text
|
||||
bool is_greedy,
|
||||
[NotNull] in SequenceEmitter<string> sc,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit
|
||||
in ExitLabel exit,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
if (min < 0 || (max >= 0 && max < min)) {
|
||||
@@ -300,23 +312,54 @@ namespace Nitload.RegularAnalysis.Text
|
||||
}
|
||||
|
||||
int additional_limit = max < 0 ? -1 : max - min;
|
||||
if (is_greedy && additional_limit != 0)
|
||||
{
|
||||
_ = additional_limit < 0 ?
|
||||
sc.AppendLine($"while (!{remain_span}.IsEmpty && {remain_span}[0] == {expected_literal})")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {remain_span}.Slice(1);")
|
||||
.AppendLine("}") :
|
||||
sc.AppendLine($"for (int {additional_count} = 0; {additional_count} < {additional_limit} && !{remain_span}.IsEmpty && {remain_span}[0] == {expected_literal}; {additional_count}++)")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {remain_span}.Slice(1);")
|
||||
.AppendLine("}");
|
||||
} else if (!is_greedy && additional_limit != 0)
|
||||
{
|
||||
// Lazy COUNT deliberately stops at Min. Its optional tail is consumed only when
|
||||
// a future continuation attempt fails and requests another repetition.
|
||||
_ = sc.AppendLine("// TODO(NGRA): Retry the lazy COUNT tail from its continuation.");
|
||||
if (additional_limit == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the optional portion once. Its counter is shared by scanning and retries.
|
||||
string additional_entry = $"__additional_entry_{scratch_scope_index}";
|
||||
ExitLabel retry = context.RequestExitLabel(ExitKind.CountRetry);
|
||||
ExitLabel resume = context.RequestExitLabel(ExitKind.CountResume);
|
||||
_ = sc.AppendLine($"{ROS_char} {additional_entry} = {remain_span};")
|
||||
.AppendLine($"int {additional_count} = 0;");
|
||||
if (is_greedy)
|
||||
{
|
||||
string limit = additional_limit < 0 ? string.Empty :
|
||||
$"{additional_count} < {additional_limit} && ";
|
||||
_ = sc.AppendLine($"while ({limit}!{remain_span}.IsEmpty && {remain_span}[0] == {expected_literal})")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {remain_span}.Slice(1);")
|
||||
.AppendLine($"{additional_count}++;")
|
||||
.AppendLine("}");
|
||||
}
|
||||
|
||||
// Initial success skips retry handling. Later failures jump back here from anywhere
|
||||
// in this path; exhaustion forwards to the target inherited before this COUNT.
|
||||
_ = sc.AppendLine($"goto {resume.GetLabelName()};")
|
||||
.AppendLine($"{retry.GetLabelName()}: {{}}");
|
||||
if (is_greedy)
|
||||
{
|
||||
_ = sc.AppendLine($"if ({additional_count} == 0)")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {additional_entry};")
|
||||
.AppendLine($"goto {exit_label_node};")
|
||||
.AppendLine("}")
|
||||
.AppendLine($"{additional_count}--;");
|
||||
} else
|
||||
{
|
||||
string limit = additional_limit < 0 ? string.Empty :
|
||||
$"{additional_count} == {additional_limit} || ";
|
||||
_ = sc.AppendLine($"{remain_span} = {additional_entry}.Slice({additional_count});")
|
||||
.AppendLine($"if ({limit}{remain_span}.IsEmpty || {remain_span}[0] != {expected_literal})")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {additional_entry};")
|
||||
.AppendLine($"goto {exit_label_node};")
|
||||
.AppendLine("}")
|
||||
.AppendLine($"{additional_count}++;");
|
||||
}
|
||||
_ = sc.AppendLine($"{remain_span} = {additional_entry}.Slice({additional_count});")
|
||||
.AppendLine($"{resume.GetLabelName()}: {{}}");
|
||||
backtrack = retry;
|
||||
}
|
||||
|
||||
private static void GenerateUnitSetCount(
|
||||
@@ -400,7 +443,8 @@ namespace Nitload.RegularAnalysis.Text
|
||||
bool is_greedy,
|
||||
[NotNull] in SequenceEmitter<string> sc,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit
|
||||
in ExitLabel exit,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
if (min < 0 || (max >= 0 && max < min)) {
|
||||
@@ -510,27 +554,54 @@ namespace Nitload.RegularAnalysis.Text
|
||||
}
|
||||
|
||||
int additional_limit = max < 0 ? -1 : max - min;
|
||||
if (is_greedy && additional_limit != 0)
|
||||
{
|
||||
if (additional_limit < 0)
|
||||
{
|
||||
_ = sc.AppendLine($"while ({remain_span}.{starts_with}({expected_literal}))")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {remain_span}.Slice({literal.Length});")
|
||||
.AppendLine("}");
|
||||
} else
|
||||
{
|
||||
_ = sc.AppendLine($"for (int {additional_count} = 0; {additional_count} < {additional_limit} && {remain_span}.{starts_with}({expected_literal}); {additional_count}++)")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {remain_span}.Slice({literal.Length});")
|
||||
.AppendLine("}");
|
||||
}
|
||||
} else if (!is_greedy && additional_limit != 0)
|
||||
{
|
||||
// Lazy COUNT deliberately stops at Min. Its optional tail is consumed only when
|
||||
// a future continuation attempt fails and requests another repetition.
|
||||
_ = sc.AppendLine("// TODO(NGRA): Retry the lazy COUNT tail from its continuation.");
|
||||
if (additional_limit == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the optional portion once. Its counter is shared by scanning and retries.
|
||||
string additional_entry = $"__additional_entry_{scratch_scope_index}";
|
||||
ExitLabel retry = context.RequestExitLabel(ExitKind.CountRetry);
|
||||
ExitLabel resume = context.RequestExitLabel(ExitKind.CountResume);
|
||||
_ = sc.AppendLine($"{ROS_char} {additional_entry} = {remain_span};")
|
||||
.AppendLine($"int {additional_count} = 0;");
|
||||
if (is_greedy)
|
||||
{
|
||||
string limit = additional_limit < 0 ? string.Empty :
|
||||
$"{additional_count} < {additional_limit} && ";
|
||||
_ = sc.AppendLine($"while ({limit}{remain_span}.{starts_with}({expected_literal}))")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {remain_span}.Slice({literal.Length});")
|
||||
.AppendLine($"{additional_count}++;")
|
||||
.AppendLine("}");
|
||||
}
|
||||
|
||||
// Initial success skips retry handling. Later failures jump back here from anywhere
|
||||
// in this path; exhaustion forwards to the target inherited before this COUNT.
|
||||
_ = sc.AppendLine($"goto {resume.GetLabelName()};")
|
||||
.AppendLine($"{retry.GetLabelName()}: {{}}");
|
||||
if (is_greedy)
|
||||
{
|
||||
_ = sc.AppendLine($"if ({additional_count} == 0)")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {additional_entry};")
|
||||
.AppendLine($"goto {exit_label_node};")
|
||||
.AppendLine("}")
|
||||
.AppendLine($"{additional_count}--;");
|
||||
} else
|
||||
{
|
||||
string limit = additional_limit < 0 ? string.Empty :
|
||||
$"{additional_count} == {additional_limit} || ";
|
||||
_ = sc.AppendLine($"{remain_span} = {additional_entry}.Slice({additional_count} * {literal.Length});")
|
||||
.AppendLine($"if ({limit}!{remain_span}.{starts_with}({expected_literal}))")
|
||||
.AppendLine("{")
|
||||
.AppendLine($"{remain_span} = {additional_entry};")
|
||||
.AppendLine($"goto {exit_label_node};")
|
||||
.AppendLine("}")
|
||||
.AppendLine($"{additional_count}++;");
|
||||
}
|
||||
_ = sc.AppendLine($"{remain_span} = {additional_entry}.Slice({additional_count} * {literal.Length});")
|
||||
.AppendLine($"{resume.GetLabelName()}: {{}}");
|
||||
backtrack = retry;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -552,7 +623,7 @@ namespace Nitload.RegularAnalysis.Text
|
||||
in ExitLabel exit_label
|
||||
)
|
||||
{
|
||||
ExitLabel success = context.RequestExitLabel(ExitMode.Goto);
|
||||
ExitLabel success = context.RequestExitLabel(ExitKind.AltMatched);
|
||||
RegexAstNode current = node.Right;
|
||||
while (!RegexAstNode.IsNullOrEmpty(current))
|
||||
{
|
||||
@@ -561,7 +632,7 @@ namespace Nitload.RegularAnalysis.Text
|
||||
}
|
||||
|
||||
_ = sc.AppendLine($"goto {exit_label.GetLabelName()};")
|
||||
.AppendLine($"{success.GetLabelName()}: ;");
|
||||
.AppendLine($"{success.GetLabelName()}: {{}}");
|
||||
}
|
||||
|
||||
private static void GenerateGroup(
|
||||
@@ -569,7 +640,8 @@ namespace Nitload.RegularAnalysis.Text
|
||||
[NotNull] in SequenceEmitter<string> sc,
|
||||
in List<SequenceEmitter<string>> func_list,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit_label
|
||||
in ExitLabel local_fail,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
RegexAstNode left = node.Left;
|
||||
@@ -583,10 +655,10 @@ namespace Nitload.RegularAnalysis.Text
|
||||
// TODO(NGRA): Snapshot the group start cursor and restore it when the child fails.
|
||||
if (!RegexAstNode.IsNullOrEmpty(left))
|
||||
{
|
||||
GenerateMethodSource(left, sc, func_list, context, exit_label);
|
||||
GenerateMethodSource(left, sc, func_list, context, local_fail, ref backtrack);
|
||||
} else
|
||||
if (!RegexAstNode.IsNullOrEmpty(right)) {
|
||||
GenerateMethodSource(right, sc, func_list, context, exit_label);
|
||||
GenerateMethodSource(right, sc, func_list, context, local_fail, ref backtrack);
|
||||
}
|
||||
|
||||
string group_name = config.Name;
|
||||
@@ -601,14 +673,15 @@ namespace Nitload.RegularAnalysis.Text
|
||||
[NotNull] in SequenceEmitter<string> sc,
|
||||
in List<SequenceEmitter<string>> func_list,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit_label = default
|
||||
in ExitLabel local_fail,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
if (!RegexAstNode.IsNullOrEmpty(node.Left)) {
|
||||
GenerateMethodSource(node.Left, sc, func_list, context, exit_label);
|
||||
GenerateMethodSource(node.Left, sc, func_list, context, local_fail, ref backtrack);
|
||||
}
|
||||
if (!RegexAstNode.IsNullOrEmpty(node.Right)) {
|
||||
GenerateMethodSource(node.Right, sc, func_list, context, exit_label);
|
||||
GenerateMethodSource(node.Right, sc, func_list, context, local_fail, ref backtrack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -617,17 +690,29 @@ namespace Nitload.RegularAnalysis.Text
|
||||
[NotNull] in SequenceEmitter<string> sc,
|
||||
in List<SequenceEmitter<string>> func_list,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit_label
|
||||
in ExitLabel local_fail,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
RegexAstNode value = RegexAstNode.IsNullOrEmpty(node.Left) ? node.Right : node.Left;
|
||||
if (RegexAstNode.IsNullOrEmpty(value)) {
|
||||
throw new InvalidOperationException("COUNT requires an operand.");
|
||||
}
|
||||
// Inspect transparent CONCAT wrappers without rewriting the repetition body.
|
||||
while (value.Label == RegexElementLabel.CONCAT)
|
||||
{
|
||||
bool left_empty = RegexAstNode.IsNullOrEmpty(value.Left);
|
||||
bool right_empty = RegexAstNode.IsNullOrEmpty(value.Right);
|
||||
if (left_empty == right_empty) {
|
||||
break;
|
||||
}
|
||||
value = left_empty ? value.Right : value.Left;
|
||||
}
|
||||
RegexCountPayload payload = node.PayloadAs<RegexCountPayload>();
|
||||
int min = payload.Min;
|
||||
int max = payload.Max;
|
||||
bool is_greedy = payload.IsGreedy;
|
||||
ExitLabel failure = backtrack;
|
||||
|
||||
// TODO(NGRA-NAMING): Reserve exclusive names for future repetition/rollback state
|
||||
// across complex COUNT retries; recursive children must use the same method pool.
|
||||
@@ -635,20 +720,20 @@ namespace Nitload.RegularAnalysis.Text
|
||||
{
|
||||
// TODO(NGRA): Emit min/max repetition, zero-width progress protection, greedy vs.
|
||||
// lazy ordering, and per-attempt rollback for a complex COUNT body.
|
||||
GenerateMethodSource(value, sc, func_list, context, exit_label);
|
||||
GenerateMethodSource(value, sc, func_list, context, local_fail, ref backtrack);
|
||||
} else
|
||||
{
|
||||
switch (value.Label & RegexElementLabel.SEQUENCE)
|
||||
{
|
||||
case RegexElementLabel.UNIT:
|
||||
GenerateUnitCount(value, min, max, is_greedy, sc, context, exit_label);
|
||||
break;
|
||||
GenerateUnitCount(value, min, max, is_greedy, sc, context, failure, ref backtrack);
|
||||
return;
|
||||
case RegexElementLabel.UNIT_SET:
|
||||
GenerateUnitSetCount(value, min, max, is_greedy, sc, context, exit_label);
|
||||
GenerateUnitSetCount(value, min, max, is_greedy, sc, context, failure);
|
||||
break;
|
||||
case RegexElementLabel.UNIT_SPAN:
|
||||
GenerateUnitSpanCount(value, min, max, is_greedy, sc, context, exit_label);
|
||||
break;
|
||||
GenerateUnitSpanCount(value, min, max, is_greedy, sc, context, failure, ref backtrack);
|
||||
return;
|
||||
default:
|
||||
|
||||
// TODO(NGRA): Diagnose malformed COUNT children instead of emitting an
|
||||
@@ -656,7 +741,6 @@ namespace Nitload.RegularAnalysis.Text
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Nitload.RegularAnalysis.Text
|
||||
|
||||
public const string _remain_span = "__remain";
|
||||
|
||||
private int _max_exit_lable;
|
||||
private int _exit_label_count;
|
||||
|
||||
private int _scratch_scope_count;
|
||||
|
||||
@@ -54,12 +54,12 @@ namespace Nitload.RegularAnalysis.Text
|
||||
}
|
||||
|
||||
public ExitLabel RequestExitLabel(
|
||||
ExitMode exitMode
|
||||
ExitKind kind
|
||||
)
|
||||
{
|
||||
int index = _max_exit_lable;
|
||||
_max_exit_lable++;
|
||||
return new(exitMode, index);
|
||||
int index = _exit_label_count;
|
||||
_exit_label_count++;
|
||||
return new(kind, index);
|
||||
}
|
||||
|
||||
public int RequestScratchScopeIndex()
|
||||
|
||||
@@ -17,7 +17,8 @@ namespace Nitload.RegularAnalysis.Text
|
||||
in SequenceEmitter<string> sc,
|
||||
in List<SequenceEmitter<string>> func_list,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit_label
|
||||
in ExitLabel local_fail,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
// A special rule owns the entire matched subtree. Returning false leaves emission
|
||||
@@ -25,7 +26,7 @@ namespace Nitload.RegularAnalysis.Text
|
||||
return node.Label switch
|
||||
{
|
||||
RegexElementLabel.CONCAT => TryGenerateFindConcat(node, sc, func_list, context,
|
||||
exit_label),
|
||||
local_fail, ref backtrack),
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
@@ -166,10 +167,12 @@ namespace Nitload.RegularAnalysis.Text
|
||||
in ExitLabel success
|
||||
)
|
||||
{
|
||||
ExitLabel failure = context.RequestExitLabel(ExitMode.Goto);
|
||||
ExitLabel failure = context.RequestExitLabel(ExitKind.AltNext);
|
||||
string entry = $"__alt_entry_{context.RequestScratchScopeIndex()}";
|
||||
_ = sc.AppendLine($"{ROS_char} {entry} = {context.RemainSpan};");
|
||||
GenerateMethodSource(node, sc, func_list, context, failure);
|
||||
// Keep branch-local retries isolated from both siblings and the caller's path.
|
||||
ExitLabel branch_backtrack = failure;
|
||||
GenerateMethodSource(node, sc, func_list, context, failure, ref branch_backtrack);
|
||||
_ = sc.AppendLine($"goto {success.GetLabelName()};")
|
||||
.AppendLine($"{failure.GetLabelName()}:")
|
||||
.AppendLine($"{context.RemainSpan} = {entry};");
|
||||
@@ -187,7 +190,8 @@ namespace Nitload.RegularAnalysis.Text
|
||||
in SequenceEmitter<string> sc,
|
||||
in List<SequenceEmitter<string>> func_list,
|
||||
in SourceGenContext context,
|
||||
in ExitLabel exit_label
|
||||
in ExitLabel local_fail,
|
||||
ref ExitLabel backtrack
|
||||
)
|
||||
{
|
||||
RegexAstNode left = node.Left;
|
||||
@@ -202,7 +206,7 @@ namespace Nitload.RegularAnalysis.Text
|
||||
{
|
||||
// TODO(NGRA): Emit the unnamed FIND candidate loop and retry the right subtree
|
||||
// until it succeeds or no anchor candidate remains.
|
||||
GenerateMethodSource(right, sc, func_list, context, exit_label, true);
|
||||
GenerateMethodSource(right, sc, func_list, context, local_fail, ref backtrack, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -212,7 +216,7 @@ namespace Nitload.RegularAnalysis.Text
|
||||
{
|
||||
// TODO(NGRA): Emit named FIND retry and capture the text preceding the
|
||||
// successful anchor candidate.
|
||||
GenerateMethodSource(right, sc, func_list, context, exit_label, true);
|
||||
GenerateMethodSource(right, sc, func_list, context, local_fail, ref backtrack, true);
|
||||
} else
|
||||
{
|
||||
// TODO(NGRA): Capture the remaining input for a terminal named dot-star.
|
||||
|
||||
@@ -172,7 +172,15 @@ namespace Nitload.RegularAnalysis.Text
|
||||
SequenceEmitter<string> methodSource = new();
|
||||
List<SequenceEmitter<string>> localFunctions = [];
|
||||
|
||||
GenerateMethodSource(root, methodSource, localFunctions, generationContext);
|
||||
ExitLabel local_fail = generationContext.RequestExitLabel(ExitKind.MatchFailed);
|
||||
ExitLabel backtrack = local_fail;
|
||||
ExitLabel end = generationContext.RequestExitLabel(ExitKind.MatchEnd);
|
||||
GenerateMethodSource(root, methodSource, localFunctions, generationContext,
|
||||
local_fail, ref backtrack);
|
||||
_ = methodSource.AppendLine($"goto {end.GetLabelName()};")
|
||||
.AppendLine($"{local_fail.GetLabelName()}: {{}}")
|
||||
.AppendLine("// TODO(NGRA): Produce the root failure result here.")
|
||||
.AppendLine($"{end.GetLabelName()}: {{}}");
|
||||
foreach (SequenceEmitter<string> localFunction in localFunctions) {
|
||||
_ = methodSource.AppendLine()
|
||||
.AppendLine()
|
||||
|
||||
@@ -24,17 +24,8 @@ namespace Nitload
|
||||
|
||||
private static string testExpPath = @"D:\Programmer\stepcode\data\ap203\ap203.exp";
|
||||
|
||||
internal static unsafe void Main(
|
||||
string[] args
|
||||
)
|
||||
internal static unsafe void Main()
|
||||
{
|
||||
// Select the managed-only emitter preview before initializing the normal runtime.
|
||||
if (args.Length > 0 && args[0] == "--regex-preview")
|
||||
{
|
||||
Environment.ExitCode = TestRegexGenerate.Run(args[1..]);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
World.Initialize(new()
|
||||
@@ -46,8 +37,8 @@ namespace Nitload
|
||||
// .UseModule<GraphicModule>()
|
||||
.InitializeModule();
|
||||
|
||||
// TestRegexAnalyse.Run();
|
||||
_ = TestRegexGenerate.Run("abc|123");
|
||||
TestRegexAnalyse.Run();
|
||||
_ = TestRegexGenerate.Run("abc1{1,2}23");
|
||||
|
||||
// TestHlsf();
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace Nitload
|
||||
}
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"123.*\w123", RegexOptions.ExplicitCapture)]
|
||||
[GeneratedRegex(@"(a{2,4}bc)d", RegexOptions.ExplicitCapture)]
|
||||
internal static partial Regex GetRegex();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// file="TestRegexGenerate"
|
||||
// Project: Nitload
|
||||
// Project: StgSharp
|
||||
// Copyright (c) Nitload.
|
||||
// SPDX-License-Identifier: MIT
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -16,29 +16,19 @@ namespace Nitload
|
||||
{
|
||||
internal static class TestRegexGenerate
|
||||
{
|
||||
public static int Run(params string[] args)
|
||||
{
|
||||
if (args.Length > 1) {
|
||||
throw new ArgumentException("Expected at most one regex pattern.", nameof(args));
|
||||
}
|
||||
if (args.Length == 1)
|
||||
{
|
||||
Print(args[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
Console.Write("regex> ");
|
||||
string? pattern = Console.ReadLine();
|
||||
if (pattern is null or ":exit") {
|
||||
return 0;
|
||||
}
|
||||
Print(pattern);
|
||||
}
|
||||
public static int Run(
|
||||
string arg
|
||||
)
|
||||
{
|
||||
Console.WriteLine($"Text regex to be tested: {arg} :\n\n\n");
|
||||
Print(arg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static void Print(string pattern)
|
||||
private static void Print(
|
||||
string pattern
|
||||
)
|
||||
{
|
||||
TextRegexSource analysis = RegexAnalyzer.Analyze(pattern);
|
||||
if (analysis.AnalyseException is not null) {
|
||||
@@ -48,8 +38,14 @@ namespace Nitload
|
||||
SequenceEmitter<string> body = new();
|
||||
List<SequenceEmitter<string>> functions = [];
|
||||
SourceGenContext context = new();
|
||||
ExitLabel exit = context.RequestExitLabel(ExitMode.Goto);
|
||||
GenerateMethodSource(analysis.Ast.Root, body, functions, context, exit);
|
||||
ExitLabel local_fail = context.RequestExitLabel(ExitKind.MatchFailed);
|
||||
ExitLabel backtrack = local_fail;
|
||||
ExitLabel end = context.RequestExitLabel(ExitKind.MatchEnd);
|
||||
GenerateMethodSource(analysis.Ast.Root, body, functions, context, local_fail, ref backtrack);
|
||||
_ = body.AppendLine($"goto {end.GetLabelName()};")
|
||||
.AppendLine($"{local_fail.GetLabelName()}: {{}}")
|
||||
.AppendLine("// TODO(NGRA): Produce the root failure result here.")
|
||||
.AppendLine($"{end.GetLabelName()}: {{}}");
|
||||
|
||||
foreach (string line in body.ToList()) {
|
||||
Console.WriteLine(line);
|
||||
@@ -61,5 +57,6 @@ namespace Nitload
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user