< Summary - pva.SuperV

Information
Class: pva.SuperV.Engine.Processing.ScriptParser
Assembly: pva.SuperV.Engine
File(s): /home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/Processing/ScriptParser.cs
Tag: dotnet-ubuntu_22190969454
Line coverage
100%
Covered lines: 66
Uncovered lines: 0
Coverable lines: 66
Total lines: 101
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ParseLine(...)100%44100%
ParseFieldReferences(...)90%1010100%
ReplaceFieldReferences(...)100%11100%

File(s)

/home/runner/work/pva.SuperV/pva.SuperV/pva.SuperV.Engine/Processing/ScriptParser.cs

#LineLine coverage
 1using pva.Helpers.Extensions;
 2using pva.SuperV.Engine.Exceptions;
 3
 4namespace pva.SuperV.Engine.Processing
 5{
 6    /// <summary>
 7    /// Parser of script lines to identify field references.
 8    /// </summary>
 9    public static class ScriptParser
 10    {
 11        /// <summary>
 12        /// Parses the line, generating an array of lines and removing the blank and commented lines.
 13        /// </summary>
 14        /// <param name="script">The script.</param>
 15        /// <returns>List of lines.</returns>
 16        public static List<string> ParseLine(string script)
 17117        {
 17118            var lines = new List<string>();
 17119            using (var reader = new System.IO.StringReader(script))
 17120            {
 21                string? line;
 52822                while ((line = reader.ReadLine()) != null)
 35723                {
 35724                    line = line.Trim();
 35725                    lines.AddRange([.. line.Split('\n')
 71426                        .Where(l => l.Length > 0 && !l.Trim().StartsWith("//"))]);
 35727                }
 17128            }
 17129            return lines;
 17130        }
 31
 32        /// <summary>
 33        /// Parses the field references from the lines. a field reference is delimited with {{ and }}
 34        /// </summary>
 35        /// <param name="lines">The lines.</param>
 36        /// <returns>List of references.</returns>
 37        public static List<FieldReference> ParseFieldReferences(List<string> lines)
 17638        {
 17639            List<FieldReference> fieldReferences = [];
 17640            int lineIndex = 0;
 17641            lines.ForEach(line =>
 17642            {
 17643                int startReferenceIndex = 0;
 17644                int endReferenceIndex = -2;
 50745                while ((startReferenceIndex = line.IndexOf("{{", endReferenceIndex + 2)) != -1)
 33746                {
 17647                    // Move past the "{{"
 33748                    startReferenceIndex += 2;
 33749                    endReferenceIndex = line.IndexOf("}}", startReferenceIndex);
 33750                    if (endReferenceIndex == -1)
 151                    {
 152                        throw new ScriptSyntaxErrorException("Missing }}", line, startReferenceIndex);
 17653                    }
 33654                    string identifier = line.Substring(startReferenceIndex, endReferenceIndex - startReferenceIndex);
 33655                    string[] parts = identifier.Split('.');
 33656                    if (parts.Length == 0 || parts[0].IsWhiteSpace())
 157                    {
 158                        throw new ScriptSyntaxErrorException("Empty field reference", line, startReferenceIndex);
 17659                    }
 67560                    parts.ForEach(p => _ = IdentifierValidation.ValidateIdentifier("Field reference", p.Trim()));
 17661                    FieldReference fieldReference;
 33162                    if (parts.Length == 1)
 32763                    {
 32764                        fieldReference = new(null, parts[0].Trim());
 32765                    }
 17666                    else
 467                    {
 468                        fieldReference = new(parts[0].Trim(), parts[1].Trim());
 469                    }
 33170                    fieldReferences.Add(fieldReference);
 33171                }
 17072                lineIndex++;
 34673            });
 17074            return fieldReferences;
 17075        }
 76
 77        /// <summary>
 78        /// Replaces the field references with actual variable names.
 79        /// </summary>
 80        /// <param name="ownInstance">The own instance for when the references don't have an instance.</param>
 81        /// <param name="lines">The lines.</param>
 82        /// <param name="fieldReferences">The field references.</param>
 83        /// <returns>The lines of code with the references replaced with the variables></returns>
 84        public static List<string> ReplaceFieldReferences(string ownInstance, List<string> lines, List<FieldReference> f
 12585        {
 12586            List<string> processedLines = [];
 12587            lines.ForEach(line =>
 12788            {
 12789                string processedLine = line;
 12790                fieldReferences.ForEach(fieldReference =>
 25491                {
 25492                    string originalReference = fieldReference.GetOriginalString();
 25493                    string replacementString = fieldReference.GetReplacementString(ownInstance);
 25494                    processedLine = processedLine.Replace(originalReference, replacementString);
 38195                });
 12796                processedLines.Add(processedLine);
 25297            });
 12598            return processedLines;
 12599        }
 100    }
 101}