| | | 1 | | using pva.Helpers.Extensions; |
| | | 2 | | using pva.SuperV.Engine.Exceptions; |
| | | 3 | | |
| | | 4 | | namespace 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) |
| | 171 | 17 | | { |
| | 171 | 18 | | var lines = new List<string>(); |
| | 171 | 19 | | using (var reader = new System.IO.StringReader(script)) |
| | 171 | 20 | | { |
| | | 21 | | string? line; |
| | 528 | 22 | | while ((line = reader.ReadLine()) != null) |
| | 357 | 23 | | { |
| | 357 | 24 | | line = line.Trim(); |
| | 357 | 25 | | lines.AddRange([.. line.Split('\n') |
| | 714 | 26 | | .Where(l => l.Length > 0 && !l.Trim().StartsWith("//"))]); |
| | 357 | 27 | | } |
| | 171 | 28 | | } |
| | 171 | 29 | | return lines; |
| | 171 | 30 | | } |
| | | 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) |
| | 176 | 38 | | { |
| | 176 | 39 | | List<FieldReference> fieldReferences = []; |
| | 176 | 40 | | int lineIndex = 0; |
| | 176 | 41 | | lines.ForEach(line => |
| | 176 | 42 | | { |
| | 176 | 43 | | int startReferenceIndex = 0; |
| | 176 | 44 | | int endReferenceIndex = -2; |
| | 507 | 45 | | while ((startReferenceIndex = line.IndexOf("{{", endReferenceIndex + 2)) != -1) |
| | 337 | 46 | | { |
| | 176 | 47 | | // Move past the "{{" |
| | 337 | 48 | | startReferenceIndex += 2; |
| | 337 | 49 | | endReferenceIndex = line.IndexOf("}}", startReferenceIndex); |
| | 337 | 50 | | if (endReferenceIndex == -1) |
| | 1 | 51 | | { |
| | 1 | 52 | | throw new ScriptSyntaxErrorException("Missing }}", line, startReferenceIndex); |
| | 176 | 53 | | } |
| | 336 | 54 | | string identifier = line.Substring(startReferenceIndex, endReferenceIndex - startReferenceIndex); |
| | 336 | 55 | | string[] parts = identifier.Split('.'); |
| | 336 | 56 | | if (parts.Length == 0 || parts[0].IsWhiteSpace()) |
| | 1 | 57 | | { |
| | 1 | 58 | | throw new ScriptSyntaxErrorException("Empty field reference", line, startReferenceIndex); |
| | 176 | 59 | | } |
| | 675 | 60 | | parts.ForEach(p => _ = IdentifierValidation.ValidateIdentifier("Field reference", p.Trim())); |
| | 176 | 61 | | FieldReference fieldReference; |
| | 331 | 62 | | if (parts.Length == 1) |
| | 327 | 63 | | { |
| | 327 | 64 | | fieldReference = new(null, parts[0].Trim()); |
| | 327 | 65 | | } |
| | 176 | 66 | | else |
| | 4 | 67 | | { |
| | 4 | 68 | | fieldReference = new(parts[0].Trim(), parts[1].Trim()); |
| | 4 | 69 | | } |
| | 331 | 70 | | fieldReferences.Add(fieldReference); |
| | 331 | 71 | | } |
| | 170 | 72 | | lineIndex++; |
| | 346 | 73 | | }); |
| | 170 | 74 | | return fieldReferences; |
| | 170 | 75 | | } |
| | | 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 |
| | 125 | 85 | | { |
| | 125 | 86 | | List<string> processedLines = []; |
| | 125 | 87 | | lines.ForEach(line => |
| | 127 | 88 | | { |
| | 127 | 89 | | string processedLine = line; |
| | 127 | 90 | | fieldReferences.ForEach(fieldReference => |
| | 254 | 91 | | { |
| | 254 | 92 | | string originalReference = fieldReference.GetOriginalString(); |
| | 254 | 93 | | string replacementString = fieldReference.GetReplacementString(ownInstance); |
| | 254 | 94 | | processedLine = processedLine.Replace(originalReference, replacementString); |
| | 381 | 95 | | }); |
| | 127 | 96 | | processedLines.Add(processedLine); |
| | 252 | 97 | | }); |
| | 125 | 98 | | return processedLines; |
| | 125 | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |