尝试新增命名空间的命名, 用于具有名称的块或是函数
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace Convention.RScript.Matcher
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Convention.RScript.Matcher
|
||||
{
|
||||
public class NamespaceMater : IRSentenceMatcher
|
||||
{
|
||||
@@ -14,6 +16,21 @@
|
||||
sentence.mode = RScriptSentence.Mode.ExitNamespace;
|
||||
return true;
|
||||
}
|
||||
else if (expression.StartsWith("namespace"))
|
||||
{
|
||||
sentence.mode = RScriptSentence.Mode.NamedSpace;
|
||||
Regex regex = new(@"namespace\s*\(([a-zA-Z_][a-zA-Z0-9_]*)\)");
|
||||
var match = regex.Match(expression);
|
||||
if (match.Success)
|
||||
{
|
||||
sentence.content = match.Groups[1].Value;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptRuntimeException("Invalid namespace declaration", -1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -3,9 +3,16 @@
|
||||
namespace Convention.RScript
|
||||
{
|
||||
[Serializable]
|
||||
public class RScriptException : Exception
|
||||
public class RScriptRuntimeException : Exception
|
||||
{
|
||||
public RScriptException(string message, int runtimePointer) : base($"when running {runtimePointer}, {message}") { }
|
||||
public RScriptException(string message, int runtimePointer, Exception inner) : base($"when running {runtimePointer}, {message}", inner) { }
|
||||
public RScriptRuntimeException(string message, int runtimePointer) : base($"when running {runtimePointer}, {message}") { }
|
||||
public RScriptRuntimeException(string message, int runtimePointer, Exception inner) : base($"when running {runtimePointer}, {message}", inner) { }
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class RScriptCompileException : Exception
|
||||
{
|
||||
public RScriptCompileException(string message, int line, int chIndex) : base($"when compile on line {line} char {chIndex}, {message}") { }
|
||||
public RScriptCompileException(string message, int line, int chIndex, Exception inner) : base($"when compile on line {line} char {chIndex}, {message}", inner) { }
|
||||
}
|
||||
}
|
||||
|
@@ -32,11 +32,11 @@ namespace Convention.RScript
|
||||
/// </summary>
|
||||
ExitNamespace,
|
||||
/// <summary>
|
||||
/// 标签, 格式: label(labelname)
|
||||
/// 标签, 格式: label(labelname);
|
||||
/// </summary>
|
||||
Label,
|
||||
/// <summary>
|
||||
/// 跳转到指定标签, 格式: goto(boolean,labelname)
|
||||
/// 跳转到指定标签, 格式: goto(boolean,labelname);
|
||||
/// <para>判断为真时跳转到labelname</para>
|
||||
/// </summary>
|
||||
Goto,
|
||||
@@ -48,6 +48,10 @@ namespace Convention.RScript
|
||||
/// 跳转到上次跳转的位置的后一个位置, 格式: back(boolean);
|
||||
/// </summary>
|
||||
Backpoint,
|
||||
/// <summary>
|
||||
/// 命名空间命名, 格式: namespace(labelname){}
|
||||
/// </summary>
|
||||
NamedSpace,
|
||||
}
|
||||
|
||||
public string content;
|
||||
@@ -66,7 +70,8 @@ namespace Convention.RScript
|
||||
public readonly RScriptVariables Variables;
|
||||
private readonly RScriptSentence[] Sentences;
|
||||
private readonly Dictionary<string, int> Labels;
|
||||
private readonly Dictionary<int, int> Namespace;
|
||||
private readonly Dictionary<int, int> NamespaceLayer;
|
||||
private readonly Dictionary<string, int> NamespaceLabels;
|
||||
|
||||
public List<IRSentenceMatcher> SentenceParser = new()
|
||||
{
|
||||
@@ -86,8 +91,8 @@ namespace Convention.RScript
|
||||
mode = RScriptSentence.Mode.Expression
|
||||
};
|
||||
expression = expression.Trim();
|
||||
expression.TrimEnd(';');
|
||||
SentenceParser.Any(matcher => matcher.Match(expression, ref result));
|
||||
expression = expression.TrimEnd(';');
|
||||
var _ = SentenceParser.Any(matcher => matcher.Match(expression, ref result));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -107,14 +112,14 @@ namespace Convention.RScript
|
||||
else if (Sentences[i].mode == RScriptSentence.Mode.ExitNamespace)
|
||||
{
|
||||
if (namespaceLayers.Count == 0)
|
||||
throw new RScriptException("Namespace exit without enter.", i);
|
||||
throw new RScriptRuntimeException("Namespace exit without enter.", i);
|
||||
var enterPointer = namespaceLayers.Pop();
|
||||
namespaceIndicator[enterPointer] = i;
|
||||
}
|
||||
}
|
||||
if (namespaceLayers.Count > 0)
|
||||
{
|
||||
throw new RScriptException("Namespace enter without exit.", namespaceLayers.Peek());
|
||||
throw new RScriptRuntimeException("Namespace enter without exit.", namespaceLayers.Peek());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,8 +129,9 @@ namespace Convention.RScript
|
||||
this.Variables = variables ?? new();
|
||||
this.Sentences = (from item in expressions select ParseToSentence(item)).ToArray();
|
||||
this.Labels = new();
|
||||
this.Namespace = new();
|
||||
BuildUpLabelsAndNamespace(ref this.Labels, ref this.Namespace);
|
||||
this.NamespaceLayer = new();
|
||||
this.NamespaceLabels = new();
|
||||
BuildUpLabelsAndNamespace(ref this.Labels, ref this.NamespaceLayer);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,7 +177,7 @@ namespace Convention.RScript
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptException($"Unsupported variable type '{varTypeName}'.", CurrentRuntimePointer);
|
||||
throw new RScriptRuntimeException($"Unsupported variable type '{varTypeName}'.", CurrentRuntimePointer);
|
||||
}
|
||||
}
|
||||
if (CurrentLocalSpaceVariableNames.Peek().Contains(varName) == false)
|
||||
@@ -182,7 +188,7 @@ namespace Convention.RScript
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptException($"Variable '{varName}' already defined on this namespace.", CurrentRuntimePointer);
|
||||
throw new RScriptRuntimeException($"Variable '{varName}' already defined on this namespace.", CurrentRuntimePointer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,7 +258,7 @@ namespace Convention.RScript
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptException($"Label '{sentence.content}' not found.", CurrentRuntimePointer);
|
||||
throw new RScriptRuntimeException($"Label '{sentence.content}' not found.", CurrentRuntimePointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,7 +272,7 @@ namespace Convention.RScript
|
||||
{
|
||||
CurrentRuntimePointer = Sentences.Length;
|
||||
}
|
||||
else if (Namespace.TryGetValue(RuntimePointerStack.Peek(), out var exitPointer))
|
||||
else if (NamespaceLayer.TryGetValue(RuntimePointerStack.Peek(), out var exitPointer))
|
||||
{
|
||||
CurrentRuntimePointer = exitPointer;
|
||||
DoExitNamespace(parser);
|
||||
|
@@ -19,10 +19,7 @@ namespace Convention.RScript
|
||||
StringBuilder builder = new();
|
||||
List<string> statements = new();
|
||||
|
||||
for (int i = 0, e = script.Length; i < e; i++)
|
||||
{
|
||||
char c = script[i];
|
||||
if (c == ';')
|
||||
void PushBuilder()
|
||||
{
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
@@ -30,6 +27,18 @@ namespace Convention.RScript
|
||||
builder.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
var lines = script.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
|
||||
for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
|
||||
{
|
||||
string line = lines[lineIndex];
|
||||
for (int i = 0, e = line.Length; i < e; i++)
|
||||
{
|
||||
char c = line[i];
|
||||
if (c == ';')
|
||||
{
|
||||
PushBuilder();
|
||||
}
|
||||
else if (c == '/' && i + 1 < e)
|
||||
{
|
||||
// Skip single-line comment
|
||||
@@ -72,29 +81,45 @@ namespace Convention.RScript
|
||||
if (i < e)
|
||||
builder.Append(script[i]);
|
||||
else
|
||||
throw new RScriptException("Invalid escape sequence in string literal", -1);
|
||||
throw new RScriptCompileException("Invalid escape sequence in string literal", lineIndex, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (c == '{' || c == '}')
|
||||
{
|
||||
// Treat braces as statement separators
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
statements.Add(builder.ToString().Trim());
|
||||
builder.Clear();
|
||||
}
|
||||
PushBuilder();
|
||||
statements.Add(c.ToString());
|
||||
}
|
||||
else if (string.Compare("namespace", 0, script, i, "namespace".Length) == 0)
|
||||
{
|
||||
builder.Append("namespace");
|
||||
i += "namespace".Length;
|
||||
if (i >= e)
|
||||
throw new RScriptCompileException("Invalid namespace declaration", lineIndex, i);
|
||||
Regex regex = new(@"^\s*\([a-zA-Z_][a-zA-Z0-9_]*\)");
|
||||
var match = regex.Match(script, i);
|
||||
if (match.Success)
|
||||
{
|
||||
builder.Append(match.Value);
|
||||
PushBuilder();
|
||||
i += match.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new RScriptCompileException("Invalid namespace declaration", lineIndex, i);
|
||||
}
|
||||
// 前面的操作中已经完成位移, 此处是抵消循环中的i++
|
||||
i -= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
statements.Add(builder.ToString().Trim());
|
||||
builder.Clear();
|
||||
PushBuilder();
|
||||
}
|
||||
|
||||
return statements.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||
|
Reference in New Issue
Block a user