-
Notifications
You must be signed in to change notification settings - Fork 3
/
YeSqlParser.HelperMethods.cs
80 lines (74 loc) · 2.8 KB
/
YeSqlParser.HelperMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Text;
using static YeSql.Net.FormattingMessage;
namespace YeSql.Net;
// This class defines the private (or internal) helper methods.
public partial class YeSqlParser
{
/// <summary>
/// Checks if the line of text is a comment without a tag.
/// </summary>
/// <param name="line">The line of text to validate.</param>
/// <returns><c>true</c> if the line is a comment without a tag, otherwise <c>false</c>.</returns>
/// <example>
/// -- This is a comment.
/// </example>
private bool IsCommentWithoutTag(ref Line line)
=> Regex.IsMatch(line.Text, @"^\s*--");
/// <summary>
/// Checks if the line of text is a comment with tag.
/// </summary>
/// <param name="line">The line of text to validate.</param>
/// <returns><c>true</c> if the line is a comment with tag, otherwise <c>false</c>.</returns>
/// <example>
/// -- name: This is a comment with tag.
/// </example>
private bool IsCommentWithTag(ref Line line)
=> Regex.IsMatch(line.Text, @"^\s*--\s*name\s*:");
/// <summary>
/// Extracts the tag name from a comment.
/// </summary>
/// <param name="line">The line with the tag name.</param>
/// <returns>
/// The tag name extracted; otherwise, <see cref="string.Empty"/> if the tag is empty.
/// </returns>
private string ExtractTagName(ref Line line)
{
var extractedTag = line.Text.Split(new[] { ':' }, MaxCount)[1];
if (string.IsNullOrWhiteSpace(extractedTag))
{
ValidationResult.Add(errorMessage: FormatParserExceptionMessage(
ExceptionMessages.TagIsEmptyOrWhitespace,
actualValue: line.Text,
lineNumber: line.Number,
column: line.Text.IndexOf(NamePrefix) + 6,
sqlFileName: _sqlFileName
));
return string.Empty;
}
return extractedTag.Trim();
}
/// <summary>
/// Adds the extracted tag from a comment to the dictionary.
/// </summary>
/// <param name="tagName">The tag name obtained from a comment.</param>
/// <param name="line">The current line.</param>
private void AddExtractedTagToDictionary(string tagName, ref Line line)
{
if (string.IsNullOrEmpty(tagName))
return;
bool isDuplicated = SqlStatements.TryAdd(tagName, string.Empty) is false;
if (isDuplicated)
{
ValidationResult.Add(errorMessage: FormatParserExceptionMessage(
ExceptionMessages.DuplicateTagName,
actualValue: tagName,
lineNumber: line.Number,
column: line.Text.IndexOf(NamePrefix) + 6,
sqlFileName: _sqlFileName
));
}
}
}