/** * Parses a raw command line argument and stores it within the request. * EBNF: * arg ::= parameter | flag | cluster | argument * parameter ::= flag "=" argument * flag ::= "--" argument | "-" char * cluster ::= "-" char+ * argument ::= char+ * char ::= [A-Za-z0-9_] * * @param string $arg command line argument */ public function insertRaw($arg) { $i = new Parser\StringStream($arg); if ($i->LA(1) == '-') { // could be: parameter, flag, cluster $i->consume(); if ($i->LA(1) == '-') { $i->consume(); // could be: parameter, flag // look for "=" to decide; need's variable lookahead while ($i->LA(1) != Parser\StringStream::EOF) { if ($i->LA(1) == '=') { // FOUND: parameter $pos = $i->index(); $key = substr($arg, 2, $pos - 2); $value = substr($arg, $pos + 1); $this[$key] = $value; return; } $i->consume(); } // FOUND: flag $flag = substr($arg, 2); $this[$flag] = true; } elseif ($i->LA(2) == '=') { // FOUND: parameter $key = $i->LA(1); $value = substr($arg, 3); $this[$key] = $value; } else { // FOUND: flag or cluster // both can be handled exactly the same while ($i->LA(1) != Parser\StringStream::EOF) { $flag = $i->LA(1); $this[$flag] = true; $i->consume(); } } } else { // FOUND: argument $this->indexedStore[] = $arg; } }
private static function parseDigit(Parser\StringStream $in) { $digit = ''; $isNegative = false; // any digit might be negative if ($in->LA(1) == '-') { $digit .= '-'; $isNegative = true; $in->consume(); } // range(0..9) while ('0' <= ($d = $in->LA(1)) && $d <= '9') { $digit .= $d; $in->consume(); } // make sure the digit is longer than 0 characters... if (strlen($digit) < ($isNegative ? 2 : 1)) { throw new InvalidArgumentException("'{$digit}' does not appear to be a valid number", 500); } return intval($digit); }