ShareX/ShareX.UploadersLib/CustomUploader/ShareXSyntaxParser.cs

108 lines
3.5 KiB
C#
Raw Normal View History

2022-01-30 22:42:18 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2022-01-30 22:42:18 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System.Collections.Generic;
using System.Text;
namespace ShareX.UploadersLib
{
2022-01-31 20:39:43 +13:00
public abstract class ShareXSyntaxParser
2022-01-30 22:42:18 +13:00
{
2022-01-31 20:39:43 +13:00
public virtual char SyntaxStart { get; } = '{';
public virtual char SyntaxEnd { get; } = '}';
public virtual char SyntaxParameterStart { get; } = ':';
public virtual char SyntaxParameterDelimiter { get; } = '|';
public virtual char SyntaxEscape { get; } = '\\';
2022-01-30 22:42:18 +13:00
2022-01-31 21:09:40 +13:00
public virtual string Parse(string text)
2022-01-30 22:42:18 +13:00
{
if (string.IsNullOrEmpty(text))
{
return "";
}
2022-04-13 17:47:57 +12:00
return Parse(text, false, 0, out _);
2022-01-31 15:10:10 +13:00
}
2022-04-13 17:47:57 +12:00
private string Parse(string text, bool isFunction, int startPosition, out int endPosition)
2022-01-31 15:10:10 +13:00
{
2022-04-13 17:47:57 +12:00
StringBuilder sbOutput = new StringBuilder();
2022-01-30 22:42:18 +13:00
bool escape = false;
2022-01-31 15:10:10 +13:00
int i;
2022-01-30 22:42:18 +13:00
2022-01-31 15:10:10 +13:00
for (i = startPosition; i < text.Length; i++)
2022-01-30 22:42:18 +13:00
{
2022-04-09 13:33:48 +12:00
char c = text[i];
2022-01-30 22:42:18 +13:00
if (!escape)
{
2022-04-09 13:33:48 +12:00
if (c == SyntaxStart)
2022-01-30 22:42:18 +13:00
{
2022-04-13 17:47:57 +12:00
string parsed = Parse(text, true, i + 1, out i);
sbOutput.Append(parsed);
2022-01-30 22:42:18 +13:00
continue;
}
2022-04-13 17:15:16 +12:00
else if (c == SyntaxEnd || c == SyntaxParameterDelimiter)
2022-01-30 22:42:18 +13:00
{
2022-01-31 15:10:10 +13:00
break;
2022-01-30 22:42:18 +13:00
}
2022-04-09 13:33:48 +12:00
else if (c == SyntaxEscape)
2022-01-30 22:42:18 +13:00
{
escape = true;
continue;
}
2022-04-13 17:15:16 +12:00
else if (isFunction && c == SyntaxParameterStart)
2022-04-09 13:33:48 +12:00
{
2022-04-13 17:47:57 +12:00
List<string> parameters = new List<string>();
2022-04-13 17:15:16 +12:00
do
{
2022-04-13 17:47:57 +12:00
string parsed = Parse(text, false, i + 1, out i);
2022-04-13 17:15:16 +12:00
parameters.Add(parsed);
} while (i < text.Length && text[i] == SyntaxParameterDelimiter);
2022-04-13 17:47:57 +12:00
endPosition = i;
return CallFunction(sbOutput.ToString(), parameters.ToArray());
2022-04-09 13:33:48 +12:00
}
2022-01-30 22:42:18 +13:00
}
2022-01-31 15:10:10 +13:00
escape = false;
2022-04-13 17:47:57 +12:00
sbOutput.Append(c);
2022-01-30 22:42:18 +13:00
}
2022-01-31 15:10:10 +13:00
endPosition = i;
2022-01-30 22:42:18 +13:00
2022-04-09 13:33:48 +12:00
if (isFunction)
2022-01-30 22:42:18 +13:00
{
2022-04-13 17:47:57 +12:00
return CallFunction(sbOutput.ToString());
2022-01-30 22:42:18 +13:00
}
2022-04-13 17:47:57 +12:00
return sbOutput.ToString();
2022-01-30 22:42:18 +13:00
}
2022-04-13 17:47:57 +12:00
protected abstract string CallFunction(string functionName, string[] parameters = null);
2022-01-30 22:42:18 +13:00
}
}