Skip to content

Commit a4ee87c

Browse files
committed
support command arguments with parentheses.
1 parent fd3e97d commit a4ee87c

File tree

1 file changed

+80
-23
lines changed

1 file changed

+80
-23
lines changed

Assets/uREPL/Scripts/Core/Core.cs

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ public static class Core
2424
static private bool isInitialized = false;
2525
static private List<CompletionPlugin> completionPlugins = new List<CompletionPlugin>();
2626

27+
internal class ParseBlockResult
28+
{
29+
public List<string> matches = new List<string>();
30+
public string input;
31+
public string output;
32+
public string pattern;
33+
public string placeholder;
34+
35+
public ParseBlockResult(
36+
string input,
37+
string pattern,
38+
string placeholder)
39+
{
40+
this.input = this.output = input;
41+
this.pattern = pattern;
42+
this.placeholder = placeholder;
43+
}
44+
}
45+
2746
#if UNITY_EDITOR
2847
[UnityEditor.MenuItem("Assets/Create/uREPL")]
2948
[UnityEditor.MenuItem("GameObject/Create Other/uREPL")]
@@ -82,6 +101,36 @@ static public void UnregisterCompletionPlugins(CompletionPlugin plugin)
82101
}
83102
}
84103

104+
static private ParseBlockResult ConvertBlockToPlaceholder(
105+
string input,
106+
string pattern,
107+
string placeholder)
108+
{
109+
var result = new ParseBlockResult(input, pattern, placeholder);
110+
111+
var regex = new Regex(pattern);
112+
var n = 0;
113+
for (var m = regex.Match(input); m.Success; m = m.NextMatch(), ++n) {
114+
result.matches.Add(m.Value);
115+
result.output = regex.Replace(result.output, string.Format(placeholder, n), 1);
116+
}
117+
118+
return result;
119+
}
120+
121+
static private string ConvertPlaceholderToBlock(
122+
string input,
123+
ParseBlockResult result)
124+
{
125+
for (int i = 0; i < result.matches.Count; ++i) {
126+
var placeholder = string.Format(result.placeholder, i);
127+
input = input.Replace(placeholder, result.matches[i]);
128+
}
129+
130+
return input;
131+
}
132+
133+
85134
static private string ConvertIntoCodeIfCommand(string code)
86135
{
87136
// NOTE: If two or more commands that have a same name are registered,
@@ -96,31 +145,33 @@ static private string ConvertIntoCodeIfCommand(string code)
96145
return code;
97146
}
98147

99-
var argsTmp = code.Substring(commandInfo.command.Length).TrimEnd(';').Split(
100-
new string[] { " ", "\t" },
101-
System.StringSplitOptions.None).ToList();
102-
103-
// Check arguments with spaces inner semicolons (e.g. $ print "foo \"bar\"").
104-
var args = new List<string>();
105-
var semicolonCount = 0;
106-
for (int i = 0; i < argsTmp.Count; ++i) {
107-
bool isPartial = semicolonCount % 2 == 1;
108-
var arg = argsTmp[i];
109-
var regex = new Regex("(^|[^\\\\]+)\"");
110-
semicolonCount += regex.Matches(arg).Count;
111-
if (isPartial) {
112-
args[args.Count - 1] += " " + arg;
113-
} else if (!string.IsNullOrEmpty(arg)) {
114-
args.Add(arg);
115-
}
116-
}
148+
// Remove last semicolon.
149+
code = code.TrimEnd(';');
150+
151+
// Remove command and get only arguments.
152+
code = code.Substring(commandInfo.command.Length);
153+
154+
// Store parentheses.
155+
var parentheses = ConvertBlockToPlaceholder(code, "\\([^\\)]+\\)", "<%paren{0}%>");
156+
code = parentheses.output;
157+
158+
// Store quatation blocks.
159+
var quates = ConvertBlockToPlaceholder(code, "\"[^\"(\\\")]+\"", "<%quate{0}%>");
160+
code = quates.output;
161+
162+
// Split arguments with space.
163+
var args = code.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);
117164

118165
// Convert the command into the code.
119-
if (commandInfo != null) {
120-
code = string.Format("{0}.{1}(", commandInfo.className, commandInfo.methodName);
121-
code += string.Join(", ", args.ToArray());
122-
code += ");";
123-
}
166+
code = string.Format("{0}.{1}(", commandInfo.className, commandInfo.methodName);
167+
code += string.Join(", ", args);
168+
code += ");";
169+
170+
// Replace temporary quates placeholders to actual expressions.
171+
code = ConvertPlaceholderToBlock(code, quates);
172+
173+
// Replace temporary parentheses placeholders to actual expressions.
174+
code = ConvertPlaceholderToBlock(code, parentheses);
124175

125176
return code;
126177
}
@@ -206,6 +257,12 @@ static public void ShowUsing()
206257
{
207258
Log.Output(GetUsing());
208259
}
260+
261+
[Command(name = "test", description = "Show all using")]
262+
static public void Test(string hoge, float fuga)
263+
{
264+
Debug.Log(hoge + " " + fuga);
265+
}
209266
}
210267

211268
}

0 commit comments

Comments
 (0)