This commit is contained in:
Zel
2025-02-23 18:47:21 +08:00
parent eaaffeeccb
commit e46a7ca31c
104 changed files with 2630 additions and 2516 deletions

View File

@@ -14,6 +14,10 @@ namespace ZelWiki.Engine.Function
public WikiFunctionType FunctionTypes { get; private set; }
public List<PrototypeSet> Items { get; set; } = new();
/// <summary>
///
/// </summary>
/// <param name="functionTypes"></param>
public FunctionPrototypeCollection(WikiFunctionType functionTypes)
{
FunctionTypes = functionTypes;
@@ -36,40 +40,40 @@ namespace ZelWiki.Engine.Function
{
functionName = functionName.ToLower();
//$$ are scope functions and are not called by prefix, we only have prefixes to make it easier to parse
// the functions in the wikiText and scope functions are easy enough since they start with curly braces.
return Items.Any(o => (o.FunctionPrefix == functionPrefix || o.FunctionPrefix == "$$") && o.FunctionName == functionName);
return Items.Any(o =>
(o.FunctionPrefix == functionPrefix || o.FunctionPrefix == "$$") && o.FunctionName == functionName);
}
public FunctionPrototype Get(string functionPrefix, string functionName)
{
functionName = functionName.ToLower();
//$$ are scope functions and are not called by prefix, we only have prefixes to make it easier to parse
// the functions in the wikiText and scope functions are easy enough since they start with curly braces.
var functionPrototype = Items.Where(o => (o.FunctionPrefix == functionPrefix || o.FunctionPrefix == "$$") && o.FunctionName == functionName).FirstOrDefault()?.Value;
var functionPrototype = Items.FirstOrDefault(o =>
(o.FunctionPrefix == functionPrefix || o.FunctionPrefix == "$$") && o.FunctionName == functionName)
?.Value;
return functionPrototype
?? throw new WikiFunctionPrototypeNotDefinedException($"Function ({functionName}) does not have a defined prototype.");
?? throw new WikiFunctionPrototypeNotDefinedException(
$"函数 ({functionName}) 没有定义的原型.");
}
#region Private
private FunctionPrototype ParsePrototype(string prototypeString)
{
int nameStartIndex = prototypeString.TakeWhile(c => char.IsLetterOrDigit(c) == false).Count();
int nameEndIndex = prototypeString.IndexOf(':');
string properName = prototypeString.Substring(nameStartIndex, nameEndIndex - nameStartIndex).Trim();
string functionName = properName.ToLower();
string functionPrefix = prototypeString.Substring(0, nameStartIndex).Trim();
var nameStartIndex = prototypeString.TakeWhile(c => char.IsLetterOrDigit(c) == false).Count();
var nameEndIndex = prototypeString.IndexOf(':');
var properName = prototypeString.Substring(nameStartIndex, nameEndIndex - nameStartIndex).Trim();
var functionName = properName.ToLower();
var functionPrefix = prototypeString.Substring(0, nameStartIndex).Trim();
prototypeString = prototypeString.Substring(nameEndIndex + 1).Trim();
var prototype = new FunctionPrototype() { FunctionPrefix = functionPrefix, ProperName = properName, FunctionName = functionName };
var prototype = new FunctionPrototype()
{ FunctionPrefix = functionPrefix, ProperName = properName, FunctionName = functionName };
if (prototypeString.Length == 0)
{
//No parameters.
return prototype;
}
var segments = prototypeString.Trim().Split('|').Select(o => o.Trim());
@@ -77,13 +81,13 @@ namespace ZelWiki.Engine.Function
{
var prototypeSegment = new PrototypeParameter();
int index = 0;
var index = 0;
if (segment[index] == '<')
{
index++; //Skip the '<'
index++;
prototypeSegment.Type = Tok(segment, ref index);
index++; //Skip the '>'
index++;
if (prototypeSegment.Type.Contains(':'))
{
@@ -94,12 +98,14 @@ namespace ZelWiki.Engine.Function
prototypeSegment.IsInfinite = true;
if (prototype.Parameters.Any(o => o.IsInfinite))
{
throw new Exception($"Function [{functionName}], prototype error: cannot contain more than one [infinite] parameter.");
throw new Exception(
$"函数 [{functionName}], 原型错误: cannot contain more than one [infinite] parameter.");
}
}
else
{
throw new Exception($"Function [{functionName}], prototype error: expected [infinite] got [{splitSeg[1]}].");
throw new Exception(
$"函数 [{functionName}], 原型错误: expected [infinite] got [{splitSeg[1]}].");
}
}
@@ -112,41 +118,42 @@ namespace ZelWiki.Engine.Function
prototypeSegment.IsRequired = true;
}
index++; //Skip the '[' or '{'
index++;
prototypeSegment.Name = Tok(segment, ref index);
if (index < segment.Length && segment[index] == '(') //Parse allowed values.
if (index < segment.Length && segment[index] == '(')
{
int allowedValueEndIndex = segment.IndexOf(')', index);
string roteRequiredValues = segment.Substring(index + 1, allowedValueEndIndex - index - 1);
prototypeSegment.AllowedValues = roteRequiredValues.Trim().Split(',').Select(o => o.Trim().ToLower()).ToList();
prototypeSegment.AllowedValues = roteRequiredValues.Trim().Split(',')
.Select(o => o.Trim().ToLower()).ToList();
index = allowedValueEndIndex;
index++; //Skip the ')'
index++;
SkipWhiteSpace(segment, ref index);
}
index++; //Skip the ']' or '}'
index++;
}
else
{
throw new Exception($"Function [{functionName}], prototype error: expected [{{] or [[].");
throw new Exception($"函数 [{functionName}], 原型错误: expected [{{] or [[].");
}
SkipWhiteSpace(segment, ref index);
if (index < segment.Length && segment[index] == '=')
{
index++; //Skip the '='
index++;
SkipWhiteSpace(segment, ref index);
if (segment[index] != '\'')
{
throw new Exception($"Function [{functionName}], prototype error: expected [\'].");
throw new Exception($"函数 [{functionName}], 原型错误: expected [\'].");
}
index++; //Skip the '''
index++;
prototypeSegment.DefaultValue = segment.Substring(index, (segment.Length - index) - 1);
@@ -154,13 +161,13 @@ namespace ZelWiki.Engine.Function
if (index < segment.Length && segment[index] != '\'')
{
throw new Exception($"Function [{functionName}], prototype error: expected [\'].");
throw new Exception($"函数 [{functionName}], 原型错误: expected [\'].");
}
}
}
else
{
throw new Exception($"Function [{functionName}], prototype error: expected [<].");
throw new Exception($"函数 [{functionName}], 原型错误: expected [<].");
}
prototype.Parameters.Add(prototypeSegment);
@@ -168,9 +175,8 @@ namespace ZelWiki.Engine.Function
return prototype;
}
/// <summary>
/// Gets the next token in a string.
///
/// </summary>
/// <param name="str"></param>
/// <param name="index"></param>
@@ -203,5 +209,7 @@ namespace ZelWiki.Engine.Function
index++;
}
}
#endregion
}
}
}