1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
//
// Plan:
// GetProperties en el comando, asigna propiedades, invoca propiedades marcadas.
//
using System;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
namespace Mono.Debugger.Frontend
{
public class PropertyAttribute : Attribute
{
public readonly string Name;
public readonly string ShortName;
public PropertyAttribute (string name)
: this (name, null)
{ }
public PropertyAttribute (string name, string short_name)
{
this.Name = name;
this.ShortName = short_name;
}
}
public class Engine {
public readonly Interpreter Interpreter;
public readonly string[] CommandFamilyBlurbs;
public readonly ArrayList[] CommandsByFamily;
public readonly Hashtable Commands = new Hashtable ();
public readonly Hashtable Aliases = new Hashtable ();
public readonly Completer Completer;
public Engine (Interpreter interpreter)
{
this.Interpreter = interpreter;
Completer = new Completer (this);
CommandsByFamily = new ArrayList[Enum.GetValues(typeof (CommandFamily)).Length];
CommandFamilyBlurbs = new string [Enum.GetValues(typeof (CommandFamily)).Length];
CommandFamilyBlurbs[ (int)CommandFamily.Running ] = "Running the program";
CommandFamilyBlurbs[ (int)CommandFamily.Breakpoints ] = "Making program stops at certain points";
CommandFamilyBlurbs[ (int)CommandFamily.Catchpoints ] = "Dealing with exceptions";
CommandFamilyBlurbs[ (int)CommandFamily.Threads ] = "Dealing with multiple threads";
CommandFamilyBlurbs[ (int)CommandFamily.Stack ] = "Examining the stack";
CommandFamilyBlurbs[ (int)CommandFamily.Files ] = "Specifying and examining files";
CommandFamilyBlurbs[ (int)CommandFamily.Data ] = "Examining data";
CommandFamilyBlurbs[ (int)CommandFamily.Internal ] = "Maintenance commands";
CommandFamilyBlurbs[ (int)CommandFamily.Obscure ] = "Obscure features";
CommandFamilyBlurbs[ (int)CommandFamily.Support ] = "Support facilities";
}
public void RegisterCommand (string s, Type t)
{
if (!t.IsSubclassOf (typeof (Command)))
throw new Exception ("Need a type derived from CL.Command");
else if (t.IsAbstract)
throw new Exception (
"Some clown tried to register an abstract class");
IDocumentableCommand c = Activator.CreateInstance (t) as IDocumentableCommand;
if (c != null) {
ArrayList cmds;
cmds = CommandsByFamily[(int)c.Family] as ArrayList;
if (cmds == null)
CommandsByFamily[(int)c.Family] = cmds = new ArrayList();
cmds.Add (s);
}
Commands [s] = t;
}
public void RegisterAlias (string s, Type t)
{
if (!t.IsSubclassOf (typeof (Command)))
throw new Exception ("Need a type derived from CL.Command");
else if (t.IsAbstract)
throw new Exception (
"Some clown tried to register an abstract class");
Aliases [s] = t;
}
public Command Get (string s, ArrayList args)
{
Type t = (Type) Commands [s];
if (t == null) {
t = (Type) Aliases [s];
if (t == null)
return null;
}
Command c = (Command) Activator.CreateInstance (t);
return ParseArguments (c, args);
}
public Command ParseArguments (Command c, ArrayList args)
{
PropertyInfo [] pi = c.GetType ().GetProperties ();
int num_args = args != null ? args.Count : 0;
for (int i = 0; i < num_args; i++){
string arg = (string) args [i];
if (!arg.StartsWith ("-")){
if (c.Args == null)
c.Args = new ArrayList ();
c.Args.Add (arg);
for (int j = i+1; j < args.Count; j++)
c.Args.Add ((string) args [j]);
break;
}
arg = arg.Substring (1);
for (int j = 0; j < pi.Length; j++){
if (!pi [j].CanWrite)
continue;
object[] attrs = pi [j].GetCustomAttributes (
typeof (PropertyAttribute), false);
if ((attrs != null) && (attrs.Length > 0)) {
PropertyAttribute attr = (PropertyAttribute) attrs [0];
if ((arg != attr.Name) && (arg != attr.ShortName))
continue;
} else if (pi [j].Name.ToLower () != arg)
continue;
if (pi [j].PropertyType == typeof (bool)){
pi [j].SetValue (c, true, null);
goto next;
}
if (pi [j].PropertyType == typeof (string)){
i++;
if (i >= args.Count){
Error ("Missing argument to flag: " + arg);
return null;
}
pi [j].SetValue (c, args [i], null);
goto next;
}
if (pi [j].PropertyType == typeof (int)){
i++;
if (i >= args.Count){
Error ("Missing integer to flag: " + arg);
return null;
}
try {
pi [j].SetValue (c, Int32.Parse ((string) args [i]), null);
} catch {
Error ("Expected number, got: `{0}'", args [i]);
return null;
}
goto next;
}
}
if (c.Args == null)
c.Args = new ArrayList ();
c.Args.Add (args [i]);
next:
;
}
return c;
}
public void Error (string fmt, params object [] args)
{
Console.WriteLine (String.Format ("Error: " + fmt, args));
}
Command last_command;
public void Run (string s, ArrayList args)
{
Command c = Get (s, args);
last_command = c;
if (c == null) {
Interpreter.Error ("No such command `{0}'.", s);
return;
}
try {
c.Execute (this);
} catch (ThreadAbortException) {
} catch (ScriptingException ex) {
Interpreter.Error (ex);
} catch (TargetException ex) {
Interpreter.Error (ex);
} catch (Exception ex) {
Interpreter.Error (
"Caught exception while executing command {0}: {1}",
this, ex);
}
}
public void Repeat ()
{
if (last_command == null)
return;
try {
last_command.Repeat (this);
} catch (ThreadAbortException) {
} catch (ScriptingException ex) {
Interpreter.Error (ex);
} catch (TargetException ex) {
Interpreter.Error (ex);
} catch (Exception ex) {
Interpreter.Error (
"Caught exception while executing command {0}: {1}",
this, ex);
}
}
}
public class LineParser {
StringBuilder sb;
Engine engine;
int top;
string command;
ArrayList args;
//
// Command Operations
//
public LineParser (Engine e)
{
engine = e;
sb = new StringBuilder ();
}
public void Reset ()
{
sb.Length = 0;
}
public void Append (string s)
{
sb.Append (s);
}
//
// Parser
//
bool SkipSpace (ref int p)
{
while (p < top && Char.IsWhiteSpace (sb [p]))
p++;
return true;
}
bool GetCommand (ref int p)
{
SkipSpace (ref p);
if (p >= top)
return false;
int start = p++;
while (p < top && !Char.IsWhiteSpace (sb [p])) {
p++;
}
if (p-start > 0) {
command = sb.ToString (start, p-start);
return true;
}
return false;
}
void AddArg (string s)
{
if (args == null)
args = new ArrayList ();
args.Add (s);
}
// Parses a string, p points to the opening quote
bool GetString (ref int p)
{
p++;
if (p >= top)
return false;
StringBuilder helper = new StringBuilder ();
while (p < top){
char c = sb [p];
p++;
if (c == '"'){
AddArg ('"' + helper.ToString () + '"');
return true;
}
if (c == '\\'){
} else
helper.Append (c);
}
return false;
}
bool GetArgument (ref int p)
{
char c;
SkipSpace (ref p);
if (p >= top)
return true;
c = sb [p];
if (c == '"')
return GetString (ref p);
if (c == '{'){
//return GetBlock (ref p);
}
StringBuilder helper = new StringBuilder ();
while (p < top){
c = sb [p++];
if (Char.IsWhiteSpace (c))
break;
helper.Append (c);
}
AddArg (helper.ToString ());
return true;
}
bool Parse ()
{
top = sb.Length;
int p = 0;
if (args != null)
args.Clear ();
if (GetCommand (ref p)){
while (p < top){
if (!GetArgument (ref p))
return false;
}
} else
return false;
return true;
}
public bool IsComplete ()
{
return Parse ();
}
public void Execute ()
{
if (Parse ()){
engine.Run (command, args);
}
}
public Command GetCommand ()
{
if (Parse ())
return engine.Get (command, args);
else
return null;
}
}
}
|