code: def querypath(self, block, path):
          
          class BadPath(Exception):
              """Bad path exception thrown when path cannot be found."""
              pass
          results = self.query(block)
          ROOT, SEP, WORD, FINAL = six.moves.range(4)               # pylint: disable=C0103
          state = ROOT
          lexer = RegexLexer(
              ("dotdot", r"\.\."),
              ("dot", r"\."),
              ("slashslash", r"//"),
              ("slash", r"/"),
              ("atword", r"@\w+"),
              ("word", r"\w+"),
              ("err", r"."),
          )
          for tokname, toktext in lexer.lex(path):
              if state == FINAL:
                  # Shouldn't be any tokens after a last token.
                  raise BadPath()
              if tokname == "dotdot":
                  # .. (parent)
                  if state == WORD:
                      raise BadPath()
                  results = results.parent()
                  state = WORD
              elif tokname == "dot":
                  # . (current node)
                  if state == WORD:
                      raise BadPath()
                  state = WORD
              elif tokname == "slashslash":
                  # // (descendants)
                  if state == SEP:
                      raise BadPath()
                  if state == ROOT:
                      raise NotImplementedError()
                  results = results.descendants()
                  state = SEP
              elif tokname == "slash":
                  # / (here)
                  if state == SEP:
                      raise BadPath()
                  if state == ROOT:
                      raise NotImplementedError()
                  state = SEP
              elif tokname == "atword":
                  # @xxx (attribute access)
                  if state != SEP:
                      raise BadPath()
                  results = results.attr(toktext[1:])
                  state = FINAL
              elif tokname == "word":
                  # xxx (tag selection)
                  if state != SEP:
                      raise BadPath()
                  results = results.children().tagged(toktext)
                  state = WORD
              else:
                  raise BadPath("Invalid thing: %r" % toktext)
          return results
docs: An XPath-like interface to `query`.