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

Parser refactor (part 1) #143

Merged
merged 94 commits into from
Jul 22, 2020
Merged
Changes from 2 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
ca79f64
LaTeXParser now uses Result
Happypig375 Jun 24, 2020
0d2879a
CI passing?
Happypig375 Jun 24, 2020
c3368eb
nullables are now errors
Happypig375 Jun 24, 2020
2d6a587
Eliminate a nullability error
Happypig375 Jun 24, 2020
08c8983
WIP refactor in progress
Happypig375 Jun 25, 2020
efa5dd3
Finished porting AtomForCommand over
Happypig375 Jun 25, 2020
ea80b39
Stop Commands should be able to be ported now
Happypig375 Jun 25, 2020
d80ba89
Ported Stop Commands
Happypig375 Jun 26, 2020
cd4c2c3
Trie
Happypig375 Jun 26, 2020
413bc25
Moved commands up
Happypig375 Jun 26, 2020
6762fa0
Be more linent w.r.t. slow VMs
Happypig375 Jun 27, 2020
931c9c9
See the time
Happypig375 Jun 27, 2020
1f623f7
Now we optimize it
Happypig375 Jun 27, 2020
50500f7
Trie optimized
Happypig375 Jun 27, 2020
68c20f6
Optimizations again
Happypig375 Jun 27, 2020
afe2373
Trie removal
Happypig375 Jun 27, 2020
ff4519f
Generalized trie
Happypig375 Jun 28, 2020
0ef0e76
Trie iterate
Happypig375 Jun 28, 2020
71197c4
\atopwithdelims
Happypig375 Jun 28, 2020
6093f73
Correct implementation of \rm and friends
Happypig375 Jun 29, 2020
7cf4b7b
Fix test
Happypig375 Jun 29, 2020
9704272
Fixed \sqrt[3}
Happypig375 Jun 29, 2020
3ce6fe4
Fix \TeX
Happypig375 Jun 30, 2020
1515c1c
Using the trie
Happypig375 Jul 1, 2020
63c8e9e
Optimize trie lookup
Happypig375 Jul 1, 2020
4eaca0a
Update dotnet
Happypig375 Jul 1, 2020
879d013
Merge master
Happypig375 Jul 2, 2020
20c348c
Simplify Result
Happypig375 Jul 2, 2020
001521d
Fix comments
Happypig375 Jul 2, 2020
f8ba33a
Add comment range tests
Happypig375 Jul 2, 2020
0a9b2b7
Apply suggestions from code review
Happypig375 Jul 3, 2020
0fa6d1c
Document a bit
Happypig375 Jul 3, 2020
f539742
Rename kern -> skip
Happypig375 Jul 3, 2020
40ff691
Revert AngouriUpdate
Happypig375 Jul 3, 2020
e799529
Fix types
Happypig375 Jul 5, 2020
79d58b5
remove unused methods prior to reviewing ProxyAdder
charlesroddie Jul 12, 2020
572ed06
hygiene: remove "add" optional function inside BiDictionary (breaks b…
charlesroddie Jul 12, 2020
bac6c01
builds
charlesroddie Jul 12, 2020
efcb5d7
simplify
charlesroddie Jul 12, 2020
6d466e7
remove some unused BiDictionary methods and fix implementations of re…
charlesroddie Jul 12, 2020
c40e100
remove unused MultiDictionary
charlesroddie Jul 12, 2020
3e70cd7
remove commented code
charlesroddie Jul 12, 2020
c7500d4
Add doc and TODO about doc
charlesroddie Jul 12, 2020
e7aba48
further BiDictionary simplification
charlesroddie Jul 12, 2020
a9dce3c
finished reviewing bidictionary
charlesroddie Jul 12, 2020
33f524f
correct RemoveByFirst
charlesroddie Jul 12, 2020
23b73aa
correct RemoveByFirst
charlesroddie Jul 12, 2020
e2248dc
Fix BiDictionary initialization (messy; awaiting LaTeXCommandDictiona…
charlesroddie Jul 12, 2020
891959d
fix BiDictionary initialization
charlesroddie Jul 12, 2020
5ab0acf
fix bidicitonary initializastion
charlesroddie Jul 12, 2020
eacc060
remove class constraint on TFirst
charlesroddie Jul 12, 2020
036ac0f
Remove unused LaTeXCommandDictionary properties
charlesroddie Jul 12, 2020
4a15e4e
prune and document LaTeXCommandDictionary
charlesroddie Jul 12, 2020
10d7d7d
Remove Trie
charlesroddie Jul 12, 2020
33e33f0
remove Trie pt 2
charlesroddie Jul 12, 2020
ba026f2
Fix casing as ReadOnlySpan.StartsWithInvariant seems to be case insen…
charlesroddie Jul 12, 2020
11c38ad
document asymmetric RemoveByFirst/Second approach
charlesroddie Jul 13, 2020
137949f
Add dictionary remove tests
charlesroddie Jul 13, 2020
9eb8021
revert latexsettings.cs
charlesroddie Jul 16, 2020
f354c30
revert latexsettings
charlesroddie Jul 16, 2020
12619b5
rename added
charlesroddie Jul 16, 2020
894112f
Document LaTeXCommandDictionary
charlesroddie Jul 16, 2020
d7bffe7
restore a test
charlesroddie Jul 16, 2020
13d6159
tweaks
charlesroddie Jul 17, 2020
e308a15
tweaks
charlesroddie Jul 17, 2020
1837793
tweaks
charlesroddie Jul 17, 2020
999242f
tweaks
charlesroddie Jul 17, 2020
1e8c4c9
Remove CopyTo as it is not used and BiDictionary doesn't implement IC…
charlesroddie Jul 17, 2020
16b6229
Added an unused method but this is bad practice
charlesroddie Jul 17, 2020
7b97196
stringcomparison.ordinal to fix test
charlesroddie Jul 19, 2020
0e86ec0
another ordinal
charlesroddie Jul 19, 2020
568df7e
"AliasBiDictionary"
charlesroddie Jul 19, 2020
e1b9d85
Merge branch 'master' into ParserRefactor
Happypig375 Jul 19, 2020
7dad855
Fix a method reference
Happypig375 Jul 19, 2020
38fe7d8
Find longest non-command
charlesroddie Jul 19, 2020
e3f2d56
Merge branch 'ParserRefactor' of https://github.com/verybadcat/CSharp…
charlesroddie Jul 19, 2020
0dcd30c
tidy concatenation
charlesroddie Jul 19, 2020
75cfca9
Use (string Command, TValue Value)
charlesroddie Jul 19, 2020
8457458
apply automatic refactor
charlesroddie Jul 19, 2020
b3d22ae
remove i
charlesroddie Jul 19, 2020
3947d44
Simplify SortedSet
charlesroddie Jul 19, 2020
3ab9351
fix build
charlesroddie Jul 19, 2020
b8206d3
shorten
charlesroddie Jul 19, 2020
d56c7c4
local functions
charlesroddie Jul 19, 2020
56136f6
Update CSharpMath/Structures/Dictionary.cs
charlesroddie Jul 19, 2020
d2098ad
string
charlesroddie Jul 19, 2020
bdc3e25
Merge branch 'ParserRefactor' of https://github.com/verybadcat/CSharp…
charlesroddie Jul 19, 2020
ca00520
suppressmessage
charlesroddie Jul 19, 2020
53d89ee
csharp syntax
charlesroddie Jul 19, 2020
bd7b2c5
Update justification
Happypig375 Jul 22, 2020
ca3d759
Use more documentation tags
Happypig375 Jul 22, 2020
80a01ac
Update test description
Happypig375 Jul 22, 2020
3439754
Clarify words
Happypig375 Jul 22, 2020
202f7f1
Apply suggested change
Happypig375 Jul 22, 2020
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
29 changes: 19 additions & 10 deletions CSharpMath/Structures/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ public void Add<TCollection>(TCollection keys, Func<TKey, TValue> valueFunc) whe
foreach (var key in keys) Add(key, valueFunc(key));
}
}
class DescendingStringLengthIComparer<TValue> : IComparer<Tuple<string, TValue>> {
int IComparer<Tuple<string, TValue>>.Compare(Tuple<string, TValue> x, Tuple<string, TValue> y) {
if (x.Item1.Length > y.Item1.Length) { return -1; }
else if (x.Item1.Length < y.Item1.Length) { return 1; }
else { return string.CompareOrdinal(x.Item1, y.Item1); }
}
}

/// <summary>
/// A dictionary-based helper where the keys are classes of LaTeX <see cref="string"/>s, with special treatment
/// for commands (starting "\"). The start of an inputted <see cref="Span{Char}"/> is parsed, and an arbitrary object
/// <typeparamref name="TValue"/> is returned, along with the number of matching characters. Processing is based on dictionary lookup
/// with fallack to specified default functions for command and non-commands when lookup fails.
/// <typeparamref name="TValue"/> is returned, along with the number of matching characters. Processing is based on
/// dictionary lookup with fallack to specified default functions for command and non-commands when lookup fails.
/// For non-commands, dictionary lookup finds the longest matching non-command.
/// </summary>
[SuppressMessage("Naming", "CA1710:Identifiers should have correct suffix",
Justification = "This is conceptually a dictionary but has different lookup behavior")]
Expand All @@ -60,17 +69,18 @@ public LaTeXCommandDictionary(DefaultDelegate defaultParser,
if (SplitCommand(key.AsSpan()) != key.Length - 1)
commands.Add(key, value);
else throw new ArgumentException("Key is unreachable: " + key, nameof(key));
else nonCommands.Add(key, value);
else nonCommands.Add(new Tuple<string, TValue>(key, value));
};
}
readonly DefaultDelegate defaultParser;
readonly DefaultDelegate defaultParserForCommands;

readonly Dictionary<string, TValue> nonCommands = new Dictionary<string, TValue>();
readonly SortedSet<Tuple<string, TValue>> nonCommands =
new SortedSet<Tuple<string, TValue>>(new DescendingStringLengthIComparer<TValue>());
readonly Dictionary<string, TValue> commands = new Dictionary<string, TValue>();

public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator() =>
nonCommands.Select(kvp => new KeyValuePair<string, TValue>(kvp.Key, kvp.Value))
nonCommands.Select(t => new KeyValuePair<string, TValue>(t.Item1, t.Item2))
.Concat(commands.Select(kvp => new KeyValuePair<string, TValue>(kvp.Key, kvp.Value)))
.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down Expand Up @@ -111,12 +121,11 @@ static int SplitCommand(ReadOnlySpan<char> chars) {
return TryLookupNonCommand(chars);
}
Result<(TValue Result, int SplitIndex)> TryLookupNonCommand(ReadOnlySpan<char> chars) {
string? commandFound = null; // TODO:short-circuit when found
foreach (string command in nonCommands.Keys) {
if (chars.StartsWith(command.AsSpan(), StringComparison.Ordinal)) {
commandFound = command; }
foreach (Tuple<string,TValue> t in nonCommands) {
if (chars.StartsWith(t.Item1.AsSpan(), StringComparison.Ordinal)) {
return Result.Ok((t.Item2, t.Item1.Length)); }
}
return commandFound == null ? defaultParser(chars) : Result.Ok((nonCommands[commandFound],commandFound.Length));
return defaultParser(chars);
}
}

Expand Down