Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LT-21585: Hermit Crab Parsing Error yellow box crash #161

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
7 changes: 6 additions & 1 deletion Src/GenerateHCConfig/ConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.ComponentModel;
using SIL.LCModel;
using SIL.FieldWorks.WordWorks.Parser;
Expand Down Expand Up @@ -103,5 +103,10 @@ public void InvalidReduplicationForm(IMoForm form, string reason, IMoMorphSynAna
{
Console.WriteLine("The reduplication form \"{0}\" is invalid. Reason: {1}", form.Form.VernacularDefaultWritingSystem.Text, reason);
}

public void InvalidRewriteRule(IPhRegularRule rule, string reason)
{
Console.WriteLine("The rewrite rule \"{0}\" is invalid. Reason: {1}", rule.Name.BestAnalysisVernacularAlternative.Text, reason);
}
}
}
13 changes: 13 additions & 0 deletions Src/LexText/ParserCore/HCLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ private void LoadLanguage()
if (regRule.StrucDescOS.Count > 0 || regRule.RightHandSidesOS.Any(rhs => rhs.StrucChangeOS.Count > 0))
{
RewriteRule hcRegRule = LoadRewriteRule(regRule);
if (hcRegRule == null)
continue;
m_morphophonemic.PhonologicalRules.Add(hcRegRule);
if (!m_notOnClitics)
m_clitic.PhonologicalRules.Add(hcRegRule);
Expand Down Expand Up @@ -1698,6 +1700,11 @@ private RewriteRule LoadRewriteRule(IPhRegularRule prule)
}
hcPrule.Properties[HCParser.PRuleID] = prule.Hvo;

if (hcPrule.Lhs.Children.Count > 1)
{
m_logger.InvalidRewriteRule(prule, ParserCoreStrings.ksMaxElementsInRule);
return null;
}
foreach (IPhSegRuleRHS rhs in prule.RightHandSidesOS)
{
var psubrule = new RewriteSubrule();
Expand Down Expand Up @@ -1748,6 +1755,12 @@ private RewriteRule LoadRewriteRule(IPhRegularRule prule)
psubrule.RightEnvironment = rightPattern;
}

if (psubrule.Rhs.Children.Count > 1)
{
m_logger.InvalidRewriteRule(prule, ParserCoreStrings.ksMaxElementsInRule);
return null;
}

hcPrule.Subrules.Add(psubrule);
}

Expand Down
8 changes: 8 additions & 0 deletions Src/LexText/ParserCore/HCParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,14 @@ public void InvalidReduplicationForm(IMoForm form, string reason, IMoMorphSynAna
m_xmlWriter.WriteElementString("Reason", reason);
m_xmlWriter.WriteEndElement();
}
public void InvalidRewriteRule(IPhRegularRule rule, string reason)
{
m_xmlWriter.WriteStartElement("LoadError");
m_xmlWriter.WriteAttributeString("type", "invalid-rewrite-rule");
m_xmlWriter.WriteElementString("Rule", rule.Name.BestAnalysisVernacularAlternative.Text);
m_xmlWriter.WriteElementString("Reason", reason);
m_xmlWriter.WriteEndElement();
}
}
}
}
3 changes: 2 additions & 1 deletion Src/LexText/ParserCore/IHCLoadErrorLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using SIL.LCModel;
using SIL.LCModel;

namespace SIL.FieldWorks.WordWorks.Parser
{
Expand All @@ -10,5 +10,6 @@ public interface IHCLoadErrorLogger
void DuplicateGrapheme(IPhPhoneme phoneme);
void InvalidEnvironment(IMoForm form, IPhEnvironment env, string reason, IMoMorphSynAnalysis msa);
void InvalidReduplicationForm(IMoForm form, string reason, IMoMorphSynAnalysis msa);
void InvalidRewriteRule(IPhRegularRule prule, string reason);
}
}
9 changes: 9 additions & 0 deletions Src/LexText/ParserCore/ParserCoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Src/LexText/ParserCore/ParserCoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,7 @@
<data name="ksTestX" xml:space="preserve">
<value>Tested {0}</value>
</data>
<data name="ksMaxElementsInRule" xml:space="preserve">
<value>A rule can't have more than one element in its left-hand side or its right-hand side.</value>
</data>
</root>
13 changes: 8 additions & 5 deletions Src/LexText/ParserCore/ParserCoreTests/HCLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ private enum LoadErrorType
InvalidPhoneme,
DuplicateGrapheme,
InvalidEnvironment,
InvalidRedupForm
InvalidRedupForm,
InvalidRewriteRule
}

private class TestHCLoadErrorLogger : IHCLoadErrorLogger
Expand Down Expand Up @@ -82,6 +83,11 @@ public void InvalidReduplicationForm(IMoForm form, string reason, IMoMorphSynAna
{
m_loadErrors.Add(Tuple.Create(LoadErrorType.InvalidRedupForm, (ICmObject) msa));
}

public void InvalidRewriteRule(IPhRegularRule rule, string reason)
{
m_loadErrors.Add(Tuple.Create(LoadErrorType.InvalidRedupForm, (ICmObject) rule));
}
}

private readonly List<Tuple<LoadErrorType, ICmObject>> m_loadErrors = new List<Tuple<LoadErrorType, ICmObject>>();
Expand Down Expand Up @@ -988,9 +994,6 @@ public void PhonologicalRule()
Cache.LanguageProject.PhonologicalDataOA.PhonRulesOS.Add(prule);
prule.Name.SetAnalysisDefaultWritingSystem("prule");
prule.Direction = 2;
IPhSimpleContextSeg segCtxt = Cache.ServiceLocator.GetInstance<IPhSimpleContextSegFactory>().Create();
prule.StrucDescOS.Add(segCtxt);
segCtxt.FeatureStructureRA = GetPhoneme("a");
IPhSimpleContextNC ncCtxt = Cache.ServiceLocator.GetInstance<IPhSimpleContextNCFactory>().Create();
prule.StrucDescOS.Add(ncCtxt);
ncCtxt.FeatureStructureRA = m_vowel;
Expand Down Expand Up @@ -1021,7 +1024,7 @@ public void PhonologicalRule()

Assert.That(hcPrule.Direction, Is.EqualTo(Machine.DataStructures.Direction.LeftToRight));
Assert.That(hcPrule.ApplicationMode, Is.EqualTo(RewriteApplicationMode.Simultaneous));
Assert.That(hcPrule.Lhs.ToString(), Is.EqualTo(m_lang.Strata[0].CharacterDefinitionTable["a"].FeatureStruct + VowelFS));
Assert.That(hcPrule.Lhs.ToString(), Is.EqualTo(VowelFS));

Assert.That(hcPrule.Subrules.Count, Is.EqualTo(1));
RewriteSubrule subrule = hcPrule.Subrules[0];
Expand Down
9 changes: 9 additions & 0 deletions Src/Transforms/Presentation/FormatCommon.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
</span>
</li>
</xsl:when>
<xsl:when test="@type = 'invalid-rewrite-rule'">
<li>
<xsl:text>The rewrite rule "</xsl:text>
<xsl:value-of select="Rule" />
<xsl:text>" is invalid. Reason: </xsl:text>
<xsl:value-of select="Reason" />
<xsl:text> </xsl:text>
</li>
</xsl:when>
<xsl:otherwise>
<!-- Do not expect any others to happen, but just in case, we show them in all their HC glory -->
<li><xsl:value-of select="."/></li>
Expand Down
Loading