Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SIL.Machine.Morphology.HermitCrab.Tool/HCContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class HCContext(Language language, TextWriter outWriter)

public void Compile()
{
_morpher = new Morpher(new TraceManager(), _language);
_morpher = new Morpher(new TraceManager { RetainWords = false }, _language);
}

public Language Language
Expand Down
47 changes: 4 additions & 43 deletions src/SIL.Machine.Morphology.HermitCrab.Tool/ParseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,19 @@ private void PrintTrace(Trace trace, int indent, HashSet<int> lineIndices)
_context.Out.Write("{0}: {1}", ruleLabel, trace.Source.Name);
first = false;
}
bool analysis = IsAnalysis(trace.Type);
if (trace.Input != null)
if (trace.InputText != null)
{
if (!first)
_context.Out.Write(", ");
first = false;
CharacterDefinitionTable table = trace.Input.Stratum.CharacterDefinitionTable;
_context.Out.Write(
"Input: {0}",
analysis ? trace.Input.Shape.ToRegexString(table, true) : trace.Input.Shape.ToString(table, true)
);
_context.Out.Write("Input: {0}", trace.InputText);
}
if (trace.Output != null)
if (trace.OutputText != null)
{
if (!first)
_context.Out.Write(", ");
first = false;
CharacterDefinitionTable table = trace.Output.Stratum.CharacterDefinitionTable;
_context.Out.Write(
"Output: {0}",
analysis ? trace.Output.Shape.ToRegexString(table, true) : trace.Output.Shape.ToString(table, true)
);
_context.Out.Write("Output: {0}", trace.OutputText);
}
if (trace.FailureReason != FailureReason.None)
{
Expand Down Expand Up @@ -130,36 +121,6 @@ private void PrintIndent(int indent, HashSet<int> lineIndices)
_context.Out.Write(lineIndices.Contains(i) ? "|" : " ");
}

private static bool IsAnalysis(TraceType type)
{
switch (type)
{
case TraceType.WordAnalysis:
case TraceType.LexicalLookup:
case TraceType.StratumAnalysisInput:
case TraceType.StratumAnalysisOutput:
case TraceType.TemplateAnalysisInput:
case TraceType.TemplateAnalysisOutput:
case TraceType.MorphologicalRuleAnalysis:
case TraceType.PhonologicalRuleAnalysis:
return true;

case TraceType.WordSynthesis:
case TraceType.Successful:
case TraceType.Failed:
case TraceType.Blocked:
case TraceType.StratumSynthesisInput:
case TraceType.StratumSynthesisOutput:
case TraceType.TemplateSynthesisInput:
case TraceType.TemplateSynthesisOutput:
case TraceType.MorphologicalRuleSynthesis:
case TraceType.PhonologicalRuleSynthesis:
return false;
}

return false;
}

private static string GetRuleLabelString(IHCRule rule)
{
if (rule is Stratum)
Expand Down
55 changes: 55 additions & 0 deletions src/SIL.Machine.Morphology.HermitCrab/Trace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,38 @@ internal Trace(TraceType type, IHCRule source)
SubruleIndex = -1;
}

private string _inputText;
private string _outputText;

internal void SetTexts(string inputText, string outputText)
{
_inputText = inputText;
_outputText = outputText;
}

private string FormatText(Word word)
{
return word?.GetTraceText(IsAnalysis(_type));
}

internal static bool IsAnalysis(TraceType type)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: F1. IsAnalysis omits CompoundingRuleAnalysis, so that node's public InputText is the plain form, while the InputText doc promises the regular-expression form for analysis node types. Keeping CLI output byte-identical is a fair reason; name the exception in the doc so a library caller is not surprised.

{
switch (type)
{
case TraceType.WordAnalysis:
case TraceType.LexicalLookup:
case TraceType.StratumAnalysisInput:
case TraceType.StratumAnalysisOutput:
case TraceType.TemplateAnalysisInput:
case TraceType.TemplateAnalysisOutput:
case TraceType.MorphologicalRuleAnalysis:
case TraceType.PhonologicalRuleAnalysis:
return true;
default:
return false;
}
}

public TraceType Type
{
get { return _type; }
Expand All @@ -147,10 +179,33 @@ public IHCRule Source

public int SubruleIndex { get; internal set; }

/// <summary>
/// Gets the word this step started from. It is <c>null</c> when the node has no input, and always
/// <c>null</c> when <see cref="TraceManager.RetainWords"/> is <c>false</c>, because a retained word keeps
/// its whole shape alive for as long as the trace lives. Use <see cref="InputText"/> in both modes.
/// </summary>
public Word Input { get; internal set; }

/// <summary>
/// Gets the word this step produced. It is <c>null</c> when the node has no output, and always
/// <c>null</c> when <see cref="TraceManager.RetainWords"/> is <c>false</c>. Use <see cref="OutputText"/>
/// in both modes.
/// </summary>
public Word Output { get; internal set; }

/// <summary>
/// Gets the shape text of the input in its display form: the regular-expression form for analysis node
/// types and the plain form otherwise. It is <c>null</c> when the node has no input. With retained words
/// it is computed from <see cref="Input"/> on each read.
/// </summary>
public string InputText => _inputText ?? FormatText(Input);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: F2. With RetainWords = true, reading InputText/OutputText on a frozen word fills Word._traceTextRegex/_traceTextPlain, so parse results returned to the caller carry those strings for their lifetime. Harmless for the CLI, which runs lean; worth knowing for a retained-mode consumer that renders the trace.


/// <summary>
/// Gets the shape text of the output, in the same form as <see cref="InputText"/>. It is <c>null</c> when
/// the node has no output.
/// </summary>
public string OutputText => _outputText ?? FormatText(Output);

public FailureReason FailureReason { get; internal set; }
}
}
Loading
Loading