尝试新增命名空间的命名, 用于具有名称的块或是函数
This commit is contained in:
141
RScriptEngine.cs
141
RScriptEngine.cs
@@ -19,82 +19,107 @@ namespace Convention.RScript
|
||||
StringBuilder builder = new();
|
||||
List<string> statements = new();
|
||||
|
||||
for (int i = 0, e = script.Length; i < e; i++)
|
||||
void PushBuilder()
|
||||
{
|
||||
char c = script[i];
|
||||
if (c == ';')
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
statements.Add(builder.ToString().Trim());
|
||||
builder.Clear();
|
||||
}
|
||||
statements.Add(builder.ToString().Trim());
|
||||
builder.Clear();
|
||||
}
|
||||
else if (c == '/' && i + 1 < e)
|
||||
}
|
||||
|
||||
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++)
|
||||
{
|
||||
// Skip single-line comment
|
||||
if (script[i + 1] == '/')
|
||||
char c = line[i];
|
||||
if (c == ';')
|
||||
{
|
||||
PushBuilder();
|
||||
}
|
||||
else if (c == '/' && i + 1 < e)
|
||||
{
|
||||
// Skip single-line comment
|
||||
if (script[i + 1] == '/')
|
||||
{
|
||||
while (i < script.Length && script[i] != '\n')
|
||||
i++;
|
||||
}
|
||||
// Skip multi-line comment
|
||||
else if (script[i + 1] == '*')
|
||||
{
|
||||
i += 2;
|
||||
while (i + 1 < script.Length && !(script[i] == '*' && script[i + 1] == '/'))
|
||||
i++;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append(c);
|
||||
}
|
||||
}
|
||||
else if (c == '#')
|
||||
{
|
||||
// Skip single-line comment
|
||||
while (i < script.Length && script[i] != '\n')
|
||||
i++;
|
||||
}
|
||||
// Skip multi-line comment
|
||||
else if (script[i + 1] == '*')
|
||||
else if (c == '\"')
|
||||
{
|
||||
i += 2;
|
||||
while (i + 1 < script.Length && !(script[i] == '*' && script[i + 1] == '/'))
|
||||
i++;
|
||||
i++;
|
||||
for (i++; i < e; i++)
|
||||
{
|
||||
builder.Append(script[i]);
|
||||
if (script[i] == '\"')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (script[i] == '\\')
|
||||
{
|
||||
i++;
|
||||
if (i < e)
|
||||
builder.Append(script[i]);
|
||||
else
|
||||
throw new RScriptCompileException("Invalid escape sequence in string literal", lineIndex, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (c == '{' || c == '}')
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
else if (c == '#')
|
||||
{
|
||||
// Skip single-line comment
|
||||
while (i < script.Length && script[i] != '\n')
|
||||
i++;
|
||||
}
|
||||
else if (c == '\"')
|
||||
{
|
||||
for (i++; i < e; i++)
|
||||
{
|
||||
builder.Append(script[i]);
|
||||
if (script[i] == '\"')
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if (script[i] == '\\')
|
||||
{
|
||||
i++;
|
||||
if (i < e)
|
||||
builder.Append(script[i]);
|
||||
else
|
||||
throw new RScriptException("Invalid escape sequence in string literal", -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (c == '{' || c == '}')
|
||||
{
|
||||
// Treat braces as statement separators
|
||||
if (builder.Length > 0)
|
||||
{
|
||||
statements.Add(builder.ToString().Trim());
|
||||
builder.Clear();
|
||||
}
|
||||
statements.Add(c.ToString());
|
||||
}
|
||||
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