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 USFM parsing bugs #229

Merged
merged 18 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void Verse(
string pubNumber
)
{
if (state.VerseRef.Equals(_curVerseRef))
if (state.VerseRef.Equals(_curVerseRef) && !_duplicateVerse)
{
EndVerseText(state, CreateVerseRefs());
// ignore duplicate verses
Expand Down
18 changes: 16 additions & 2 deletions src/SIL.Machine/Corpora/UsfmParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using SIL.Scripture;

Expand Down Expand Up @@ -41,7 +43,19 @@ public static void Parse(
versification,
preserveWhitespace
);
parser.ProcessTokens();
try
{
parser.ProcessTokens();
}
catch (Exception ex)
{
var sb = new StringBuilder();
sb.Append(
$"An error occurred while parsing the USFM text in Verse: {parser.State.VerseRef}, line: {parser.State.LineNumber}, "
);
sb.Append($"column: {parser.State.ColumnNumber}, error: '{ex.Message}'");
throw new InvalidOperationException(sb.ToString(), ex);
}
}

private static readonly Regex OptBreakSplitter = new Regex("(//)", RegexOptions.Compiled);
Expand Down
2 changes: 1 addition & 1 deletion src/SIL.Machine/Corpora/UsfmTextUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private void SkipTokens(UsfmParserState state)
private bool ReplaceWithNewTokens(UsfmParserState state)
{
bool newText = _replace.Count > 0 && _replace.Peek();
int tokenEnd = state.Index + state.SpecialTokenCount + 1;
int tokenEnd = state.Index + state.SpecialTokenCount;
bool existingText = false;
for (int index = _tokenIndex; index <= tokenEnd; index++)
{
Expand Down
53 changes: 47 additions & 6 deletions tests/SIL.Machine.Tests/Corpora/UsfmMemoryTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ public void GetRows_VerseDescriptiveTitle()
{
Assert.That(rows, Has.Length.EqualTo(1));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:1")));
Assert.That(rows[0].Text, Is.EqualTo("Descriptive title"));
Assert.That(
rows[0].Ref,
Is.EqualTo(ScriptureRef.Parse("MAT 1:1")),
string.Join(",", rows.ToList().Select(tr => tr.Ref.ToString()))
);
Assert.That(
rows[0].Text,
Is.EqualTo("Descriptive title"),
string.Join(",", rows.ToList().Select(tr => tr.Text))
);
});
}

Expand All @@ -44,8 +52,16 @@ public void GetRows_LastSegment()
{
Assert.That(rows, Has.Length.EqualTo(1));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:1")));
Assert.That(rows[0].Text, Is.EqualTo("Last segment"));
Assert.That(
rows[0].Ref,
Is.EqualTo(ScriptureRef.Parse("MAT 1:1")),
string.Join(",", rows.ToList().Select(tr => tr.Ref.ToString()))
);
Assert.That(
rows[0].Text,
Is.EqualTo("Last segment"),
string.Join(",", rows.ToList().Select(tr => tr.Text))
);
});
}

Expand All @@ -67,7 +83,32 @@ public void GetRows_DuplicateVerseWithTable()
includeAllText: true
);

Assert.That(rows, Has.Length.EqualTo(5));
Assert.That(rows, Has.Length.EqualTo(5), string.Join(",", rows.ToList().Select(tr => tr.Text)));
}

[Test]
public void GetRows_TriplicateVerse()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\c 1
\v 1 First verse 1
\rem non verse 1
\v 1 First verse 2
\rem non verse 2
\v 1 First verse 3
\rem non verse 3
\v 2 Second verse
",
includeAllText: true
);
Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(5), string.Join(",", rows.ToList().Select(tr => tr.Text)));
Assert.That(rows[0].Text, Is.EqualTo("First verse 1"));
Assert.That(rows[3].Text, Is.EqualTo("non verse 3"));
Assert.That(rows[4].Text, Is.EqualTo("Second verse"));
});
}

[Test]
Expand All @@ -88,7 +129,7 @@ public void GetRows_VersePara_BeginningNonVerseSegment()
includeAllText: true
);

Assert.That(rows, Has.Length.EqualTo(4));
Assert.That(rows, Has.Length.EqualTo(4), string.Join(",", rows.ToList().Select(tr => tr.Text)));
}

private static TextRow[] GetRows(string usfm, bool includeMarkers = false, bool includeAllText = false)
Expand Down
Loading