def _validate_doc_refs_helper(self, env, doc, loc, type_context=None):
for match in doc_ref_re.finditer(doc):<EOL><INDENT>tag = match.group('<STR_LIT>')<EOL>val = match.group('<STR_LIT>')<EOL>if tag == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:.>' in val:<EOL><INDENT>type_name, field_name = val.split('<STR_LIT:.>', <NUM_LIT:1>)<EOL>if type_name not in env:<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % (field_name, quote(type_name)),<EOL>*loc)<EOL><DEDENT>elif isinstance(env[type_name], ApiRoutesByVersion):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>' %<EOL>(quote(field_name), quote(type_name)),<EOL>*loc)<EOL><DEDENT>if isinstance(env[type_name], Environment):<EOL><INDENT>namespace_name, type_name, field_name = val.split('<STR_LIT:.>', <NUM_LIT:2>)<EOL>data_type_to_check = env[namespace_name][type_name]<EOL><DEDENT>elif isinstance(env[type_name], Alias):<EOL><INDENT>data_type_to_check = env[type_name].data_type<EOL><DEDENT>else:<EOL><INDENT>data_type_to_check = env[type_name]<EOL><DEDENT>if not any(field.name == field_name<EOL>for field in data_type_to_check.all_fields):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>' % quote(val),<EOL>*loc)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>assert type_context is not None<EOL>if not any(field.name == val<EOL>for field in type_context.all_fields):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>' %<EOL>quote(val),<EOL>*loc)<EOL><DEDENT><DEDENT><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>if not (<NUM_LIT:1> < val.rfind('<STR_LIT:U+0020>') < len(val) - <NUM_LIT:1>):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % quote(val),<EOL>*loc)<EOL><DEDENT><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:.>' in val:<EOL><INDENT>namespace_name, val = val.split('<STR_LIT:.>', <NUM_LIT:1>)<EOL>if namespace_name not in env:<EOL><INDENT>raise InvalidSpec(<EOL>"<STR_LIT>" %<EOL>namespace_name, *loc)<EOL><DEDENT>env_to_check = env[namespace_name]<EOL><DEDENT>else:<EOL><INDENT>env_to_check = env<EOL><DEDENT>route_name, version = parse_route_name_and_version(val)<EOL>if route_name not in env_to_check:<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>'.format(quote(route_name)), *loc)<EOL><DEDENT>if not isinstance(env_to_check[route_name], ApiRoutesByVersion):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>'.format(quote(route_name)), *loc)<EOL><DEDENT>if version not in env_to_check[route_name].at_version:<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>'.format(<EOL>quote(route_name), version),<EOL>*loc)<EOL><DEDENT><DEDENT>elif tag == '<STR_LIT:type>':<EOL><INDENT>if '<STR_LIT:.>' in val:<EOL><INDENT>namespace_name, val = val.split('<STR_LIT:.>', <NUM_LIT:1>)<EOL>if namespace_name not in env:<EOL><INDENT>raise InvalidSpec(<EOL>"<STR_LIT>" %<EOL>namespace_name, *loc)<EOL><DEDENT>env_to_check = env[namespace_name]<EOL><DEDENT>else:<EOL><INDENT>env_to_check = env<EOL><DEDENT>if val not in env_to_check:<EOL><INDENT>raise InvalidSpec(<EOL>"<STR_LIT>" % val,<EOL>*loc)<EOL><DEDENT>elif not isinstance(env_to_check[val], (Struct, Union)):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>' %<EOL>quote(val), *loc)<EOL><DEDENT><DEDENT>elif tag == '<STR_LIT>':<EOL><INDENT>if not doc_ref_val_re.match(val):<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>' % quote(val),<EOL>*loc)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise InvalidSpec(<EOL>'<STR_LIT>' % quote(tag),<EOL>*loc)<EOL><DEDENT><DEDENT>
Validates that all the documentation references in a docstring are
formatted properly, have valid values, and make references to valid
symbols.

Args:
    env (dict): The environment of defined symbols.
    doc (str): The docstring to validate.
    lineno (int): The line number the docstring begins on in the spec.
    type_context (stone.data_type.UserDefined): If the docstring
        belongs to a user-defined type (Struct or Union) or one of its
        fields, set this to the type. This is needed for "field" doc
        refs that don't name a type to be validated.