MongoExtensions / src / MongoCli / ParseHelper.cs
Code · 178 lines · 3595 bytes
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System.Text;

namespace MongoCli;

public class FunctionArguments
{
	public string Name { get; set; }
	public List<string> Arguments { get; set; }
	public bool IsFunction { get; set; }
	public bool Complete { get; set; }
	public FunctionArguments()
	{
		this.Name = string.Empty;
		this.Arguments = [];
	}
}
public static class ParseHelper
{
	private enum PartEnum
	{
		Unknown,
		FunctionName, // alphonsnumeric and _
		Arguments,
		Parenthesis, // (
		SingleQuote, // '
		DoubleQuote, // "
		SquareBracket, // [
		CurlyBracket // {
	}

	public static FunctionArguments ParseFunction(string s)
	{
		Stack<PartEnum> nivo = new();

		nivo.Push(PartEnum.Unknown);

		FunctionArguments fa = new();

		StringBuilder sb = new();

		for (int intI = 0; intI < s.Length; intI++)
		{
			var c = s[intI];

			switch (nivo.Peek())
			{
				default:
					break;
				case PartEnum.Unknown:
					if (char.IsWhiteSpace(c))
						continue;
					if (c == '(')
					{
						fa.IsFunction = true;
						nivo.Pop();
						nivo.Push(PartEnum.Parenthesis);
						continue; // function, so next char is exit
					}
					if (fa.Name.Length > 0) // already a name
					{
						nivo.Push(PartEnum.Arguments);
						sb.Append(c);
					}
					else
					{
						nivo.Push(PartEnum.FunctionName);
						fa.Name = $"{c}";
					}
					continue;
				case PartEnum.FunctionName:
					if (char.IsLetterOrDigit(c) || c == '_')
						fa.Name += c;
					else
					{
						nivo.Pop();
						if (c == '(')
						{
							fa.IsFunction = true;
							nivo.Push(PartEnum.Parenthesis);
						}
					}
					continue;
				case PartEnum.Arguments:
					if (char.IsWhiteSpace(c))
					{
						var aa = sb.ToString().Trim();
						if (aa.Length > 0)
						{
							fa.Arguments.Add(sb.ToString().Trim());
							sb = new();
						}
					}
					else
					{
						sb.Append(c);
					}
					continue;
			}

			switch (c)
			{
				default:
					break;
				case '\\':
					sb.Append(c);
					intI++;
					c = s[intI]; // todo check boundaries
					break;
				case '(':
					nivo.Push(PartEnum.Parenthesis);
					break; ;
				case ',':
					if (nivo.Peek() == PartEnum.Parenthesis)
						continue;
					break;
				case ')':
					if (nivo.Peek() == PartEnum.Parenthesis)
						nivo.Pop();
					if (nivo.Count <= 2)
						continue;
					break;
				case '{':
					nivo.Push(PartEnum.CurlyBracket);
					break;
				case '[':
					nivo.Push(PartEnum.SquareBracket);
					break;
				case '\'':
					if (nivo.Peek() == PartEnum.DoubleQuote)
						break;
					if (nivo.Peek() == PartEnum.SingleQuote)
						nivo.Pop();
					else
						nivo.Push(PartEnum.SingleQuote);
					break;
				case '\"':
					if (nivo.Peek() == PartEnum.SingleQuote)
						break;
					if (nivo.Peek() == PartEnum.DoubleQuote)
						nivo.Pop();
					else
						nivo.Push(PartEnum.DoubleQuote);
					break;
				case '}':
					if (nivo.Peek() == PartEnum.CurlyBracket)
						nivo.Pop();
					if (nivo.Peek() == PartEnum.Parenthesis)
					{
						sb.Append(c);
						fa.Arguments.Add(sb.ToString().Trim());
						sb = new();
						continue;
					}
					break;
				case ']':
					if (nivo.Peek() == PartEnum.SquareBracket)
						nivo.Pop();
					if (nivo.Peek() == PartEnum.Parenthesis)
					{
						sb.Append(c);
						fa.Arguments.Add(sb.ToString().Trim());
						sb = new();
						continue;
					}
					break;
			}
			sb.Append(c);
		}
		var rest = sb.ToString().Trim();
		if (!string.IsNullOrWhiteSpace(rest))
			fa.Arguments.Add(rest);

		fa.Complete = (nivo.Count < 2) || (nivo.Peek() == PartEnum.FunctionName) || (nivo.Peek() == PartEnum.Arguments);

		return fa;
	}
}