| 401 | { |
| 402 | int nargs = MAXWORDS, curarg = -1; |
| 403 | loopi(MAXWORDS) |
| 404 | { |
| 405 | w[i] = NULL; wl[i] = 0; |
| 406 | if(i > nargs) continue; |
| 407 | w[i] = (p += strspn(p, " \t")); // skip whitespace |
| 408 | if(p[0]=='/' && p[1]=='/') p += strcspn(p, "\n\r\0"); // skip comment |
| 409 | if(*p == '"') |
| 410 | { // find matching " |
| 411 | do |
| 412 | { |
| 413 | ++p; |
| 414 | p += strcspn(p, "\"\n\r"); |
| 415 | } while(*p == '\"' && p[-1] == '\\'); |
| 416 | p++; |
| 417 | } |
| 418 | else if(*p == '[' || *p == '(') |
| 419 | { // find matching ) or ] |
| 420 | bool quot = false; |
| 421 | char right = *p == '[' ? ']' : ')', left = *p++; |
| 422 | brak.add(p); |
| 423 | while(brak.length()) |
| 424 | { |
| 425 | p += strcspn(p, "([\"])"); |
| 426 | int c = *p++; |
| 427 | if(c == left && !quot) brak.add(p); |
| 428 | else if(c == '"') quot = !quot; |
| 429 | else if(c == right && !quot) brak.drop(); |
| 430 | else if(!c) |
| 431 | { // unmatched braces, interpret from there (in first pass) |
| 432 | *unmatched = right; |
| 433 | if(w[0] == brak.last() || brak.last() == p - 1 || outer) break; |
| 434 | p = brak.last(); |
| 435 | brak.setsize(0); |
| 436 | i = -1; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | else p += strcspn(p, "; \t\n\r\0"); // skip regular word |
| 441 | if(i < 0) continue; |
| 442 | if(!(wl[i] = (int) (p - w[i]))) w[i] = NULL, nargs = i; |
| 443 | else if(w[i] <= pos) curarg = i - 1; |
| 444 | } |
| 445 | p += strcspn(p, ";\n\r\0"); // skip statement delimiter |
| 446 | if(!*p++ || pos < p) return curarg; // was last statement, or the one we wanted |
| 447 | } |