A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
emit_json.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
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 A11_FLOW_EMIT_JSON_H_
18#define A11_FLOW_EMIT_JSON_H_
19
20#include <string>
21#include <string_view>
22
23#include <absl/types/span.h>
24#include <nlohmann/json_fwd.hpp>
25
26#include "a11/flow/complete.h"
27#include "a11/flow/diagnostic.h"
28#include "a11/flow/format.h"
29#include "a11/flow/parser.h"
30#include "a11/flow/plan.h"
31#include "a11/flow/syntax.h"
32
33namespace a11::flow {
34
40inline constexpr std::string_view kDiagnosticsFormat = "flow.diagnostics/v1";
41inline constexpr std::string_view kCodesFormat = "flow.codes/v1";
42inline constexpr std::string_view kSyntaxFormat = "flow.syntax/v1";
43inline constexpr std::string_view kTokensFormat = "flow.tokens/v1";
44inline constexpr std::string_view kVocabularyFormat = "flow.vocabulary/v1";
45inline constexpr std::string_view kFormatFormat = "flow.format/v1";
46
48nlohmann::json DiagnosticToJsonValue(const Diagnostic& diagnostic);
49
50// A diagnostic read back from its JSON, for frontends that consume the envelope
51// rather than the library -- the IntelliJ plugin, a CI script.
57Diagnostic DiagnosticFromJsonValue(const nlohmann::json& value);
58
65nlohmann::json DiagnosticsToJsonValue(std::string_view source,
66 absl::Span<const Diagnostic> diagnostics);
67
69std::string DiagnosticsToJson(std::string_view source,
70 absl::Span<const Diagnostic> diagnostics);
71
73nlohmann::json CodesToJsonValue();
74
82nlohmann::json VocabularyToJsonValue();
83
92nlohmann::json DiagnosticsToSarifValue(
93 std::string_view source, absl::Span<const Diagnostic> diagnostics);
94
96std::string DiagnosticsToSarif(std::string_view source,
97 absl::Span<const Diagnostic> diagnostics);
98
108nlohmann::json NodeToJsonValue(const syntax::Node& node);
109
111nlohmann::json ConstantToJsonValue(const syntax::Constant& constant);
112
116nlohmann::json SyntaxToJsonValue(std::string_view source,
117 const ParseResult& result);
118
120std::string SyntaxToJson(std::string_view source, const ParseResult& result);
121
133nlohmann::json TokensToJsonValue(std::string_view source_name,
134 std::string_view source);
135
137std::string TokensToJson(std::string_view source_name, std::string_view source);
138
140nlohmann::json FormatToJsonValue(const FormatResult& result);
141
143nlohmann::json CompletionsToJsonValue(const CompleteResult& result);
144
151nlohmann::json PlanToJsonValue(std::string_view source_name,
152 const Program& program);
153
164nlohmann::json DtoToJsonValue(const DtoPlan& dto,
165 const Program* absl_nullable program = nullptr);
166
168std::string PlanToJson(std::string_view source_name, const Program& program);
169
170// One diagnostic on one line, in the shape editors and compilers have used for
171// decades: `path:line:column: severity: message [code]`.
176std::string DiagnosticToText(std::string_view source,
177 const Diagnostic& diagnostic);
178
179} // namespace a11::flow
180
181#endif // A11_FLOW_EMIT_JSON_H_
std::string value
Definition discover.cc:114
Definition boundary.cc:32
constexpr std::string_view kDiagnosticsFormat
The format field of each envelope: what a reader checks before parsing.
Definition emit_json.h:40
Diagnostic DiagnosticFromJsonValue(const nlohmann::json &value)
A diagnostic read back from its JSON, for frontends that consume the envelope rather than the library...
Definition emit_json.cc:594
nlohmann::json PlanToJsonValue(std::string_view source_name, const Program &program)
The flow.plan/v1 envelope: what each flow of a file resolved to.
Definition emit_json.cc:1127
nlohmann::json CodesToJsonValue()
The published code table, as flow.codes/v1.
Definition emit_json.cc:681
nlohmann::json TokensToJsonValue(std::string_view source_name, std::string_view source)
The flow.tokens/v1 envelope: every token, and what it means where it is.
Definition emit_json.cc:908
nlohmann::json NodeToJsonValue(const syntax::Node &node)
One syntax node, and everything under it.
Definition emit_json.cc:877
nlohmann::json DiagnosticToJsonValue(const Diagnostic &diagnostic)
One diagnostic, as it appears in the envelope.
Definition emit_json.cc:566
std::string PlanToJson(std::string_view source_name, const Program &program)
The envelope, serialised with a trailing newline and two-space indent.
Definition emit_json.cc:1200
std::string SyntaxToJson(std::string_view source, const ParseResult &result)
The envelope, serialised with a trailing newline and two-space indent.
Definition emit_json.cc:904
std::string DiagnosticToText(std::string_view source, const Diagnostic &diagnostic)
One diagnostic on one line, in the shape editors and compilers have used for decades: path:line:colum...
Definition emit_json.cc:1204
std::string DiagnosticsToJson(std::string_view source, absl::Span< const Diagnostic > diagnostics)
The envelope, serialised with a trailing newline and two-space indent.
Definition emit_json.cc:675
constexpr std::string_view kCodesFormat
Definition emit_json.h:41
nlohmann::json DiagnosticsToSarifValue(std::string_view source, absl::Span< const Diagnostic > diagnostics)
A SARIF 2.1.0 log for one file's diagnostics.
Definition emit_json.cc:774
nlohmann::json CompletionsToJsonValue(const CompleteResult &result)
The flow.completions/v1 envelope: what may be written at an offset.
Definition emit_json.cc:966
nlohmann::json SyntaxToJsonValue(std::string_view source, const ParseResult &result)
The full flow.syntax/v1 envelope: the flows a file declares, and what is wrong with it.
Definition emit_json.cc:881
constexpr std::string_view kVocabularyFormat
Definition emit_json.h:44
nlohmann::json FormatToJsonValue(const FormatResult &result)
The flow.format/v1 envelope: the formatted text, and what it took.
Definition emit_json.cc:949
nlohmann::json DiagnosticsToJsonValue(std::string_view source, absl::Span< const Diagnostic > diagnostics)
The full flow.diagnostics/v1 envelope.
Definition emit_json.cc:640
nlohmann::json DtoToJsonValue(const DtoPlan &dto, const Program *absl_nullable program)
One resolved shape, as the structs of a plan writes it.
Definition emit_json.cc:1167
nlohmann::json ConstantToJsonValue(const syntax::Constant &constant)
A constant as the syntax format writes it.
Definition emit_json.cc:841
std::string TokensToJson(std::string_view source_name, std::string_view source)
The envelope, serialised with a trailing newline and two-space indent.
Definition emit_json.cc:944
std::string DiagnosticsToSarif(std::string_view source, absl::Span< const Diagnostic > diagnostics)
The SARIF log, serialised with a trailing newline.
Definition emit_json.cc:835
constexpr std::string_view kFormatFormat
Definition emit_json.h:45
constexpr std::string_view kTokensFormat
Definition emit_json.h:43
nlohmann::json VocabularyToJsonValue()
Every word set the language gives meaning to, as flow.vocabulary/v1.
Definition emit_json.cc:692
constexpr std::string_view kSyntaxFormat
Definition emit_json.h:42
graph::RefId node
The graph ref this answer corresponds to, when a graph is being built.
Definition resolve.cc:698
What may be written at an offset, and the word already typed there.
Definition complete.h:108
One problem found in a flow.
Definition diagnostic.h:108
One compiled struct: a shape a port may be typed with.
Definition plan.h:80
The formatted text, and what it took to get there.
Definition format.h:39
The flows a file declares, and everything wrong with it.
Definition parser.h:32
Every flow and shape one source file declares.
Definition plan.h:185
A value the language can write out in full: what a literal is, and what folding a literal expression ...
Definition syntax.h:80
Base of every syntax node: what it is, and where it started.
Definition syntax.h:204
std::string dto
Which shape schema is asked about; every one of them when empty.
Definition main.cc:78