kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/indexing/KytheOutputStream.h (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #ifndef KYTHE_CXX_COMMON_INDEXING_KYTHE_OUTPUT_STREAM_H_
    18  #define KYTHE_CXX_COMMON_INDEXING_KYTHE_OUTPUT_STREAM_H_
    19  
    20  #include "absl/strings/str_cat.h"
    21  #include "absl/strings/str_format.h"
    22  #include "absl/strings/string_view.h"
    23  #include "kythe/proto/common.pb.h"
    24  #include "kythe/proto/storage.pb.h"
    25  
    26  namespace kythe {
    27  /// \brief Code marked with semantic spans.
    28  using MarkedSource = kythe::proto::common::MarkedSource;
    29  
    30  /// A collection of references to the components of a VName.
    31  class VNameRef {
    32   public:
    33    absl::string_view signature() const { return signature_; }
    34    absl::string_view corpus() const { return corpus_; }
    35    absl::string_view root() const { return root_; }
    36    absl::string_view path() const { return path_; }
    37    absl::string_view language() const { return language_; }
    38    void set_signature(absl::string_view s) { signature_ = s; }
    39    void set_corpus(absl::string_view s) { corpus_ = s; }
    40    void set_root(absl::string_view s) { root_ = s; }
    41    void set_path(absl::string_view s) { path_ = s; }
    42    void set_language(absl::string_view s) { language_ = s; }
    43  
    44    explicit VNameRef(const proto::VName& vname)
    45        : signature_(vname.signature().data(), vname.signature().size()),
    46          corpus_(vname.corpus().data(), vname.corpus().size()),
    47          root_(vname.root().data(), vname.root().size()),
    48          path_(vname.path().data(), vname.path().size()),
    49          language_(vname.language().data(), vname.language().size()) {}
    50    VNameRef() {}
    51    void Expand(proto::VName* vname) const {
    52      vname->mutable_signature()->assign(signature_.data(), signature_.size());
    53      vname->mutable_corpus()->assign(corpus_.data(), corpus_.size());
    54      vname->mutable_root()->assign(root_.data(), root_.size());
    55      vname->mutable_path()->assign(path_.data(), path_.size());
    56      vname->mutable_language()->assign(language_.data(), language_.size());
    57    }
    58    std::string DebugString() const {
    59      return absl::StrCat("{", corpus_, ",", root_, ",", path_, ",", signature_,
    60                          ",", language_, "}");
    61    }
    62  
    63   private:
    64    absl::string_view signature_;
    65    absl::string_view corpus_;
    66    absl::string_view root_;
    67    absl::string_view path_;
    68    absl::string_view language_;
    69  };
    70  /// A collection of references to the components of a single Kythe fact.
    71  struct FactRef {
    72    const VNameRef* source;
    73    absl::string_view fact_name;
    74    absl::string_view fact_value;
    75    /// Overwrites all of the fields in `entry` that can differ between single
    76    /// facts.
    77    void Expand(proto::Entry* entry) const {
    78      source->Expand(entry->mutable_source());
    79      entry->mutable_fact_name()->assign(fact_name.data(), fact_name.size());
    80      entry->mutable_fact_value()->assign(fact_value.data(), fact_value.size());
    81    }
    82  };
    83  /// A collection of references to the components of a single Kythe edge.
    84  struct EdgeRef {
    85    const VNameRef* source;
    86    absl::string_view edge_kind;
    87    const VNameRef* target;
    88    /// Overwrites all of the fields in `entry` that can differ between edges
    89    /// without ordinals.
    90    void Expand(proto::Entry* entry) const {
    91      source->Expand(entry->mutable_source());
    92      target->Expand(entry->mutable_target());
    93      entry->mutable_edge_kind()->assign(edge_kind.data(), edge_kind.size());
    94    }
    95  };
    96  /// A collection of references to the components of a single Kythe edge with an
    97  /// ordinal.
    98  struct OrdinalEdgeRef {
    99    const VNameRef* source;
   100    absl::string_view edge_kind;
   101    const VNameRef* target;
   102    uint32_t ordinal;
   103    /// Overwrites all of the fields in `entry` that can differ between edges with
   104    /// ordinals.
   105    void Expand(proto::Entry* entry) const {
   106      entry->set_edge_kind(absl::StrCat(edge_kind, ".", ordinal));
   107      source->Expand(entry->mutable_source());
   108      target->Expand(entry->mutable_target());
   109    }
   110  };
   111  
   112  // Interface for receiving Kythe data.
   113  class KytheOutputStream {
   114   public:
   115    virtual void Emit(const FactRef& fact) = 0;
   116    virtual void Emit(const EdgeRef& edge) = 0;
   117    virtual void Emit(const OrdinalEdgeRef& edge) = 0;
   118    /// Add a buffer to the buffer stack to group facts, edges, and buffers
   119    /// together.
   120    virtual void PushBuffer() {}
   121    /// Pop the last buffer from the buffer stack.
   122    virtual void PopBuffer() {}
   123    virtual ~KytheOutputStream() {}
   124  };
   125  
   126  }  // namespace kythe
   127  
   128  #endif  // KYTHE_CXX_COMMON_INDEXING_KYTHE_OUTPUT_STREAM_H_