tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
refactor(registries): split ReadIndentation to keep cyclomatic complexity under 10
- Commit
851cc48b3d0e3df18dcc71104ec071ee1dceac98- Author
- Leitwolf11 ·
- Parents
d2235e0779b6
Changes
.../Dsl/Registries/TazelRegistries.cs | 25 ++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Progmasoft.Tazel/Dsl/Registries/TazelRegistries.cs b/src/Progmasoft.Tazel/Dsl/Registries/TazelRegistries.cs index ba342cb..6a15451 100644 --- a/src/Progmasoft.Tazel/Dsl/Registries/TazelRegistries.cs +++ b/src/Progmasoft.Tazel/Dsl/Registries/TazelRegistries.cs @@ -172,6 +172,18 @@ internal static class RegistryLexer } private static (bool Indented, int NextIndex, int NextLine) ReadIndentation(string text, int index, int line, string file) + { + var (spaces, tabs, nextIndex) = CountIndentation(text, index); + if (IsBlankOrComment(text, nextIndex)) + { + return (false, nextIndex, line); + } + + ValidateIndentation(spaces, tabs, text, file, line); + return (spaces > 0 || tabs > 0, nextIndex, line); + } + + private static (int Spaces, int Tabs, int NextIndex) CountIndentation(string text, int index) { var spaces = 0; var tabs = 0; @@ -190,11 +202,14 @@ internal static class RegistryLexer i++; } - if (i >= text.Length || IsLineBreak(text[i]) || text[i] == '#') - { - return (false, i, line); - } + return (spaces, tabs, i); + } + private static bool IsBlankOrComment(string text, int index) => + index >= text.Length || IsLineBreak(text[index]) || text[index] == '#'; + + private static void ValidateIndentation(int spaces, int tabs, string text, string file, int line) + { if (spaces > 0 && tabs > 0) { throw Fail(text, file, line, 1, "mixed tabs and spaces in indentation", @@ -206,8 +221,6 @@ internal static class RegistryLexer throw Fail(text, file, line, 1, $"indentation of {spaces} space(s) is not a multiple of 2", "indent with tabs or spaces in multiples of 2"); } - - return (spaces > 0 || tabs > 0, i, line); } private static (int NextIndex, int NextLine) ReadLineTokens(