ShareX/ShareX.UploadersLib/CustomUploader/ShareXSyntaxParser.cs

137 lines
4.4 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
Copyright (c) 2007-2022 ShareX Team
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-01-31 20:22:56 +13:00
return ParseSyntax(text, false, 0, out _);
2022-01-31 15:10:10 +13:00
}
2022-01-31 20:22:56 +13:00
private string ParseSyntax(string text, bool isParameter, int startPosition, out int endPosition)
2022-01-31 15:10:10 +13:00
{
2022-01-30 22:42:18 +13:00
StringBuilder sbResult = new StringBuilder();
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
{
if (!escape)
{
if (text[i] == SyntaxStart)
{
2022-01-31 15:10:10 +13:00
string parsed = ParseFunction(text, i + 1, out i);
2022-01-30 22:42:18 +13:00
sbResult.Append(parsed);
continue;
}
2022-01-31 20:22:56 +13:00
else if (isParameter && (text[i] == SyntaxEnd || text[i] == 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
}
else if (text[i] == SyntaxEscape)
{
escape = true;
continue;
}
}
2022-01-31 15:10:10 +13:00
escape = false;
2022-01-30 22:42:18 +13:00
sbResult.Append(text[i]);
}
2022-01-31 15:10:10 +13:00
endPosition = i;
2022-01-30 22:42:18 +13:00
return sbResult.ToString();
}
2022-01-31 15:10:10 +13:00
private string ParseFunction(string text, int startPosition, out int endPosition)
2022-01-30 22:42:18 +13:00
{
2022-01-31 15:10:10 +13:00
StringBuilder sbFunctionName = new StringBuilder();
bool parsingFunctionName = true;
List<string> parameters = new List<string>();
bool escape = false;
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-01-31 15:10:10 +13:00
if (!escape)
{
if (text[i] == SyntaxEnd)
{
break;
}
else if (text[i] == SyntaxEscape)
{
escape = true;
continue;
}
if (parsingFunctionName)
{
if (text[i] == SyntaxParameterStart)
{
parsingFunctionName = false;
continue;
}
sbFunctionName.Append(text[i]);
}
else
{
2022-01-31 20:22:56 +13:00
string parsed = ParseSyntax(text, true, i, out i);
2022-01-31 15:10:10 +13:00
parameters.Add(parsed);
2022-04-09 12:15:48 +12:00
if (text[i] == SyntaxEnd)
{
break;
}
2022-01-31 15:10:10 +13:00
}
}
escape = false;
2022-01-30 22:42:18 +13:00
}
2022-01-31 15:10:10 +13:00
endPosition = i;
return CallFunction(sbFunctionName.ToString(), parameters.ToArray());
2022-01-30 22:42:18 +13:00
}
2022-01-31 20:39:43 +13:00
protected abstract string CallFunction(string functionName, string[] parameters);
2022-01-30 22:42:18 +13:00
}
}