{"record":{"id":"13c22098beec0338","repo":"phacility/phabricator","slug":"unable-to-parse-function-and-arguments-for-token","errorCode":null,"errorMessage":"Unable to parse function and arguments for token \"%s\".","messagePattern":"Unable to parse function and arguments for token \"(.+?)\"\\.","errorType":"exception","errorClass":"PhabricatorTypeaheadInvalidTokenException","httpStatus":null,"severity":"error","filePath":"src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php","lineNumber":488,"sourceCode":"    return ($token !== null && strpos($token, '(') !== false);\n  }\n\n\n  /**\n   * @task functions\n   */\n  protected function parseFunction($token, $allow_partial = false) {\n    $matches = null;\n\n    if ($allow_partial) {\n      $ok = preg_match('/^([^(]+)\\((.*?)\\)?\\z/', $token, $matches);\n    } else {\n      $ok = preg_match('/^([^(]+)\\((.*)\\)\\z/', $token, $matches);\n    }\n\n    if (!$ok) {\n      if (!$allow_partial) {\n        throw new PhabricatorTypeaheadInvalidTokenException(\n          pht(\n            'Unable to parse function and arguments for token \"%s\".',\n            $token));\n      }\n      return null;\n    }\n\n    $function = trim($matches[1]);\n\n    if (!$this->canEvaluateFunction($function)) {\n      if (!$allow_partial) {\n        throw new PhabricatorTypeaheadInvalidTokenException(\n          pht(\n            'This datasource (\"%s\") can not evaluate the function \"%s(...)\".',\n            get_class($this),\n            $function));\n      }\n","sourceCodeStart":470,"sourceCodeEnd":506,"githubUrl":"https://github.com/phacility/phabricator/blob/5720a38cfe95b00ca4be5016dd0d2f3195f4fa04/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php#L470-L506","documentation":"Typeahead datasources tokenize function expressions like 'viewer()' or 'user(alice)'. parseFunction() in strict mode (allow_partial=false) requires a fully-formed 'name(args)' string with balanced parentheses; anything else throws PhabricatorTypeaheadInvalidTokenException with this message.","triggerScenarios":"Passing a malformed function token to a datasource's tokenize/evaluate path: 'viewer(' (missing close paren), 'func)(', empty name '()', or stray text after the closing parenthesis; typically from stored queries, bookmarked tokens, or Conduit callers submitting raw tokenizer values.","commonSituations":"Hand-crafted Conduit search constraints with function tokens; URL parameters copied from a form and truncated; tokens built by string concatenation that drops the '()' suffix; older saved queries surviving a format change.","solutions":["Send well-formed function tokens: name plus balanced parentheses, e.g. 'viewer()' or 'user(alice)'.","For plain values, submit the raw string without parentheses so it is treated as a literal token.","Catch PhabricatorTypeaheadInvalidTokenException and drop or re-prompt for the offending token instead of failing the whole query.","Validate tokens with the same regex ('/^([^(]+)\\((.*)\\)\\z/') before storing or forwarding them."],"exampleFix":"// before\n$token = 'viewer(';\n\n// after\n$token = 'viewer()';","handlingStrategy":"try-catch","validationCode":"// Pre-validate function tokens before storing/forwarding them\nif (preg_match('/\\(.*\\)/', $token)) { // looks like a function\n  if (!preg_match('/^([^(]+)\\((.*)\\)\\z/', $token)) {\n    // reject or repair the token before it reaches the datasource\n    $token = null;\n  }\n}","typeGuard":"function isWellFormedFunctionToken($token) {\n  return is_string($token)\n    && preg_match('/^([^(]+)\\((.*)\\)\\z/s', $token) === 1;\n}","tryCatchPattern":"try {\n  $function = $datasource->parseFunction($token);\n} catch (PhabricatorTypeaheadInvalidTokenException $ex) {\n  // drop the malformed token and continue with the rest of the query\n}","preventionTips":["Never build function tokens by concatenation without guaranteeing balanced parentheses.","Validate tokenizer strings from URLs/Conduit with the datasource regex before saving queries.","Treat tokens without '()' as literals; do not append partial parentheses."],"tags":["phabricator","typeahead","tokenizer","validation"],"backgroundTag":"invalid-token-format","analyzedSha":"5720a38cfe95b00ca4be5016dd0d2f3195f4fa04","analyzedAt":"2026-08-21T05:07:25.672Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}