Skip to content

Commit

Permalink
Merge branch 'LT-21888' of https://github.com/sillsdev/FieldWorks int…
Browse files Browse the repository at this point in the history
…o LT-21888
  • Loading branch information
jtmaxwell3 committed Sep 24, 2024
2 parents fc7ff25 + 34e3621 commit e664a1d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 9 deletions.
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);
}
}
}
6 changes: 4 additions & 2 deletions Src/LexText/Morphology/AffixRuleFormulaControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2015 SIL International
// Copyright (c) 2015 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)

Expand Down Expand Up @@ -599,6 +599,8 @@ protected override int RemoveItems(SelectionHelper sel, bool forward, out int ce
int prevCellId = GetPrevCell(seqCtxt.Hvo);
cellIndex = GetCellCount(prevCellId) - 1;
Rule.InputOS.Remove(seqCtxt);
// Unschedule the removal of the column.
m_removeCol = null;
return prevCellId;
}
bool reconstruct = RemoveContextsFrom(forward, sel, seqCtxt, false, out cellIndex);
Expand Down Expand Up @@ -716,7 +718,7 @@ protected bool RemoveFromOutput(bool forward, SelectionHelper sel, out int index
else
{
int idx = GetIndexToRemove(mappings, sel, forward);
if (idx > -1)
if (idx > -1 && idx < mappings.Count())
{
var mapping = (IMoRuleMapping) mappings[idx];
index = idx - 1;
Expand Down
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

0 comments on commit e664a1d

Please sign in to comment.