| 1 |
// Copyright 2019 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// protocol_gen (re)generates the cppdap .h and .cpp files that describe the |
| 16 |
// DAP protocol. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"encoding/json" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"io" |
| 25 |
"io/ioutil" |
| 26 |
"net/http" |
| 27 |
"os" |
| 28 |
"os/exec" |
| 29 |
"path" |
| 30 |
"reflect" |
| 31 |
"runtime" |
| 32 |
"sort" |
| 33 |
"strings" |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
protocolURL = "https://raw.githubusercontent.com/microsoft/vscode-debugadapter-node/master/debugProtocol.json" |
| 38 |
packageURL = "https://raw.githubusercontent.com/microsoft/vscode-debugadapter-node/master/protocol/package.json" |
| 39 |
|
| 40 |
versionTag = "${version}" |
| 41 |
commonPrologue = `// Copyright 2019 Google LLC |
| 42 |
// |
| 43 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 44 |
// you may not use this file except in compliance with the License. |
| 45 |
// You may obtain a copy of the License at |
| 46 |
// |
| 47 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 48 |
// |
| 49 |
// Unless required by applicable law or agreed to in writing, software |
| 50 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 51 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 52 |
// See the License for the specific language governing permissions and |
| 53 |
// limitations under the License. |
| 54 |
|
| 55 |
// Generated with protocol_gen.go -- do not edit this file. |
| 56 |
// go run scripts/protocol_gen/protocol_gen.go |
| 57 |
// |
| 58 |
// DAP version ${version} |
| 59 |
` |
| 60 |
|
| 61 |
headerPrologue = commonPrologue + ` |
| 62 |
#ifndef dap_protocol_h |
| 63 |
#define dap_protocol_h |
| 64 |
|
| 65 |
#include "optional.h" |
| 66 |
#include "typeinfo.h" |
| 67 |
#include "typeof.h" |
| 68 |
#include "variant.h" |
| 69 |
|
| 70 |
#include <string> |
| 71 |
#include <type_traits> |
| 72 |
#include <vector> |
| 73 |
|
| 74 |
namespace dap { |
| 75 |
|
| 76 |
struct Request {}; |
| 77 |
struct Response {}; |
| 78 |
struct Event {}; |
| 79 |
|
| 80 |
` |
| 81 |
|
| 82 |
headerEpilogue = `} // namespace dap |
| 83 |
|
| 84 |
#endif // dap_protocol_h |
| 85 |
` |
| 86 |
|
| 87 |
cppPrologue = commonPrologue + ` |
| 88 |
|
| 89 |
#include "dap/protocol.h" |
| 90 |
|
| 91 |
namespace dap { |
| 92 |
|
| 93 |
` |
| 94 |
|
| 95 |
cppEpilogue = `} // namespace dap |
| 96 |
` |
| 97 |
) |
| 98 |
|
| 99 |
func main() { |
| 100 |
flag.Parse() |
| 101 |
if err := run(); err != nil { |
| 102 |
fmt.Fprintf(os.Stderr, "%v\n", err) |
| 103 |
os.Exit(1) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
type root struct { |
| 108 |
Schema string `json:"$schema"` |
| 109 |
Title string `json:"title"` |
| 110 |
Description string `json:"description"` |
| 111 |
Ty string `json:"type"` |
| 112 |
Definitions map[string]definition `json:"definitions"` |
| 113 |
} |
| 114 |
|
| 115 |
func (r *root) definitions() []namedDefinition { |
| 116 |
sortedDefinitions := make([]namedDefinition, 0, len(r.Definitions)) |
| 117 |
for name, def := range r.Definitions { |
| 118 |
sortedDefinitions = append(sortedDefinitions, namedDefinition{name, def}) |
| 119 |
} |
| 120 |
sort.Slice(sortedDefinitions, func(i, j int) bool { return sortedDefinitions[i].name < sortedDefinitions[j].name }) |
| 121 |
return sortedDefinitions |
| 122 |
} |
| 123 |
|
| 124 |
func (r *root) getRef(ref string) (namedDefinition, error) { |
| 125 |
if !strings.HasPrefix(ref, "#/definitions/") { |
| 126 |
return namedDefinition{}, fmt.Errorf("Unknown $ref '%s'", ref) |
| 127 |
} |
| 128 |
name := strings.TrimPrefix(ref, "#/definitions/") |
| 129 |
def, ok := r.Definitions[name] |
| 130 |
if !ok { |
| 131 |
return namedDefinition{}, fmt.Errorf("Unknown $ref '%s'", ref) |
| 132 |
} |
| 133 |
return namedDefinition{name, def}, nil |
| 134 |
} |
| 135 |
|
| 136 |
type namedDefinition struct { |
| 137 |
name string |
| 138 |
def definition |
| 139 |
} |
| 140 |
|
| 141 |
type definition struct { |
| 142 |
Ty string `json:"type"` |
| 143 |
Title string `json:"title"` |
| 144 |
Description string `json:"description"` |
| 145 |
Properties properties `json:"properties"` |
| 146 |
Required []string `json:"required"` |
| 147 |
AllOf []definition `json:"allOf"` |
| 148 |
Ref string `json:"$ref"` |
| 149 |
ClosedEnum []string `json:"enum"` |
| 150 |
} |
| 151 |
|
| 152 |
type properties map[string]property |
| 153 |
|
| 154 |
func (p *properties) foreach(cb func(string, property) error) error { |
| 155 |
type namedProperty struct { |
| 156 |
name string |
| 157 |
property property |
| 158 |
} |
| 159 |
sorted := make([]namedProperty, 0, len(*p)) |
| 160 |
for name, property := range *p { |
| 161 |
sorted = append(sorted, namedProperty{name, property}) |
| 162 |
} |
| 163 |
sort.Slice(sorted, func(i, j int) bool { return sorted[i].name < sorted[j].name }) |
| 164 |
for _, entry := range sorted { |
| 165 |
if err := cb(entry.name, entry.property); err != nil { |
| 166 |
return err |
| 167 |
} |
| 168 |
} |
| 169 |
return nil |
| 170 |
} |
| 171 |
|
| 172 |
type property struct { |
| 173 |
typed |
| 174 |
Description string `json:"description"` |
| 175 |
} |
| 176 |
|
| 177 |
func (p *property) properties(r *root) (properties, []string, error) { |
| 178 |
if p.Ref == "" { |
| 179 |
return p.Properties, p.Required, nil |
| 180 |
} |
| 181 |
|
| 182 |
d, err := r.getRef(p.Ref) |
| 183 |
if err != nil { |
| 184 |
return nil, nil, err |
| 185 |
} |
| 186 |
return d.def.Properties, d.def.Required, nil |
| 187 |
} |
| 188 |
|
| 189 |
type typed struct { |
| 190 |
Ty interface{} `json:"type"` |
| 191 |
Items *typed `json:"items"` |
| 192 |
Ref string `json:"$ref"` |
| 193 |
Properties properties `json:"properties"` |
| 194 |
Required []string `json:"required"` |
| 195 |
ClosedEnum []string `json:"enum"` |
| 196 |
OpenEnum []string `json:"_enum"` |
| 197 |
} |
| 198 |
|
| 199 |
func (t typed) typename(r *root, refs *[]string) (string, error) { |
| 200 |
if t.Ref != "" { |
| 201 |
d, err := r.getRef(t.Ref) |
| 202 |
if err != nil { |
| 203 |
return "", err |
| 204 |
} |
| 205 |
*refs = append(*refs, d.name) |
| 206 |
return d.name, nil |
| 207 |
} |
| 208 |
|
| 209 |
if t.Ty == nil { |
| 210 |
return "", fmt.Errorf("No type specified") |
| 211 |
} |
| 212 |
|
| 213 |
var typeof func(v reflect.Value) (string, error) |
| 214 |
typeof = func(v reflect.Value) (string, error) { |
| 215 |
if v.Kind() == reflect.Interface { |
| 216 |
v = v.Elem() |
| 217 |
} |
| 218 |
switch v.Kind() { |
| 219 |
case reflect.String: |
| 220 |
ty := v.Interface().(string) |
| 221 |
switch ty { |
| 222 |
case "boolean", "string", "integer", "number", "object", "null": |
| 223 |
return ty, nil |
| 224 |
case "array": |
| 225 |
if t.Items != nil { |
| 226 |
el, err := t.Items.typename(r, refs) |
| 227 |
if err != nil { |
| 228 |
return "", err |
| 229 |
} |
| 230 |
return fmt.Sprintf("array<%s>", el), nil |
| 231 |
} |
| 232 |
return "array<any>", nil |
| 233 |
default: |
| 234 |
return "", fmt.Errorf("Unhandled property type '%v'", ty) |
| 235 |
} |
| 236 |
|
| 237 |
case reflect.Slice, reflect.Array: |
| 238 |
ty := "variant<" |
| 239 |
for i := 0; i < v.Len(); i++ { |
| 240 |
if i > 0 { |
| 241 |
ty += ", " |
| 242 |
} |
| 243 |
el, err := typeof(v.Index(i)) |
| 244 |
if err != nil { |
| 245 |
return "", err |
| 246 |
} |
| 247 |
ty += el |
| 248 |
} |
| 249 |
ty += ">" |
| 250 |
return ty, nil |
| 251 |
} |
| 252 |
|
| 253 |
return "", fmt.Errorf("Unsupported type '%v' kind: %v", v.Interface(), v.Kind()) |
| 254 |
} |
| 255 |
|
| 256 |
return typeof(reflect.ValueOf(t.Ty)) |
| 257 |
} |
| 258 |
|
| 259 |
type cppField struct { |
| 260 |
desc string |
| 261 |
ty string |
| 262 |
name string |
| 263 |
defaultValue string |
| 264 |
optional bool |
| 265 |
} |
| 266 |
|
| 267 |
type cppType interface { |
| 268 |
Name() string |
| 269 |
Dependencies() []string |
| 270 |
File() cppFile |
| 271 |
DefaultValue() string |
| 272 |
WriteHeader(w io.Writer) |
| 273 |
WriteCPP(w io.Writer) |
| 274 |
} |
| 275 |
|
| 276 |
type cppStruct struct { |
| 277 |
desc string |
| 278 |
name string |
| 279 |
typename string |
| 280 |
base string |
| 281 |
fields []cppField |
| 282 |
deps []string |
| 283 |
emit bool |
| 284 |
typedefs []cppTypedef |
| 285 |
file cppFile |
| 286 |
} |
| 287 |
|
| 288 |
func (s *cppStruct) Name() string { |
| 289 |
return s.name |
| 290 |
} |
| 291 |
|
| 292 |
func (s *cppStruct) Dependencies() []string { |
| 293 |
return s.deps |
| 294 |
} |
| 295 |
|
| 296 |
func (s *cppStruct) File() cppFile { |
| 297 |
return s.file |
| 298 |
} |
| 299 |
|
| 300 |
func (s *cppStruct) DefaultValue() string { return "" } |
| 301 |
|
| 302 |
func (s *cppStruct) WriteHeader(w io.Writer) { |
| 303 |
if s.desc != "" { |
| 304 |
io.WriteString(w, "// ") |
| 305 |
io.WriteString(w, strings.ReplaceAll(s.desc, "\n", "\n// ")) |
| 306 |
io.WriteString(w, "\n") |
| 307 |
} |
| 308 |
io.WriteString(w, "struct ") |
| 309 |
io.WriteString(w, s.name) |
| 310 |
if s.base != "" { |
| 311 |
io.WriteString(w, " : public ") |
| 312 |
io.WriteString(w, s.base) |
| 313 |
} |
| 314 |
io.WriteString(w, " {") |
| 315 |
|
| 316 |
// typedefs |
| 317 |
for _, t := range s.typedefs { |
| 318 |
io.WriteString(w, "\n using ") |
| 319 |
io.WriteString(w, t.from) |
| 320 |
io.WriteString(w, " = ") |
| 321 |
io.WriteString(w, t.to) |
| 322 |
io.WriteString(w, ";") |
| 323 |
} |
| 324 |
|
| 325 |
for _, f := range s.fields { |
| 326 |
if f.desc != "" { |
| 327 |
io.WriteString(w, "\n // ") |
| 328 |
io.WriteString(w, strings.ReplaceAll(f.desc, "\n", "\n // ")) |
| 329 |
} |
| 330 |
io.WriteString(w, "\n ") |
| 331 |
if f.optional { |
| 332 |
io.WriteString(w, "optional<") |
| 333 |
io.WriteString(w, f.ty) |
| 334 |
io.WriteString(w, ">") |
| 335 |
} else { |
| 336 |
io.WriteString(w, f.ty) |
| 337 |
} |
| 338 |
io.WriteString(w, " ") |
| 339 |
io.WriteString(w, sanitize(f.name)) |
| 340 |
if !f.optional && f.defaultValue != "" { |
| 341 |
io.WriteString(w, " = ") |
| 342 |
io.WriteString(w, f.defaultValue) |
| 343 |
} |
| 344 |
io.WriteString(w, ";") |
| 345 |
} |
| 346 |
|
| 347 |
io.WriteString(w, "\n};\n\n") |
| 348 |
|
| 349 |
io.WriteString(w, "DAP_DECLARE_STRUCT_TYPEINFO(") |
| 350 |
io.WriteString(w, s.name) |
| 351 |
io.WriteString(w, ");\n\n") |
| 352 |
} |
| 353 |
|
| 354 |
func (s *cppStruct) WriteCPP(w io.Writer) { |
| 355 |
// typeinfo |
| 356 |
io.WriteString(w, "DAP_IMPLEMENT_STRUCT_TYPEINFO(") |
| 357 |
io.WriteString(w, s.name) |
| 358 |
io.WriteString(w, ",\n \"") |
| 359 |
io.WriteString(w, s.typename) |
| 360 |
io.WriteString(w, "\"") |
| 361 |
for _, f := range s.fields { |
| 362 |
io.WriteString(w, ",\n ") |
| 363 |
io.WriteString(w, "DAP_FIELD(") |
| 364 |
io.WriteString(w, sanitize(f.name)) |
| 365 |
io.WriteString(w, ", \"") |
| 366 |
io.WriteString(w, f.name) |
| 367 |
io.WriteString(w, "\")") |
| 368 |
} |
| 369 |
io.WriteString(w, ");\n\n") |
| 370 |
} |
| 371 |
|
| 372 |
type cppTypedef struct { |
| 373 |
from string |
| 374 |
to string |
| 375 |
deps []string |
| 376 |
desc string |
| 377 |
defaultValue string |
| 378 |
} |
| 379 |
|
| 380 |
func (ty *cppTypedef) Name() string { |
| 381 |
return ty.from |
| 382 |
} |
| 383 |
|
| 384 |
func (ty *cppTypedef) Dependencies() []string { |
| 385 |
return ty.deps |
| 386 |
} |
| 387 |
|
| 388 |
func (ty *cppTypedef) File() cppFile { |
| 389 |
return types |
| 390 |
} |
| 391 |
|
| 392 |
func (ty *cppTypedef) DefaultValue() string { return ty.defaultValue } |
| 393 |
|
| 394 |
func (ty *cppTypedef) WriteHeader(w io.Writer) { |
| 395 |
if ty.desc != "" { |
| 396 |
io.WriteString(w, "// ") |
| 397 |
io.WriteString(w, strings.ReplaceAll(ty.desc, "\n", "\n// ")) |
| 398 |
io.WriteString(w, "\n") |
| 399 |
} |
| 400 |
|
| 401 |
io.WriteString(w, "using ") |
| 402 |
io.WriteString(w, ty.from) |
| 403 |
io.WriteString(w, " = ") |
| 404 |
io.WriteString(w, ty.to) |
| 405 |
io.WriteString(w, ";\n\n") |
| 406 |
} |
| 407 |
|
| 408 |
func (ty *cppTypedef) WriteCPP(w io.Writer) { |
| 409 |
} |
| 410 |
|
| 411 |
func sanitize(s string) string { |
| 412 |
s = strings.Trim(s, "_") |
| 413 |
switch s { |
| 414 |
case "default": |
| 415 |
return "def" |
| 416 |
default: |
| 417 |
return s |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
func appendEnumDetails(desc string, openEnum []string, closedEnum []string) string { |
| 422 |
if len(closedEnum) > 0 { |
| 423 |
desc += "\n\nMust be one of the following enumeration values:\n" |
| 424 |
for i, enum := range closedEnum { |
| 425 |
if i > 0 { |
| 426 |
desc += ", " |
| 427 |
} |
| 428 |
desc += "'" + enum + "'" |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
if len(openEnum) > 0 { |
| 433 |
desc += "\n\nMay be one of the following enumeration values:\n" |
| 434 |
for i, enum := range openEnum { |
| 435 |
if i > 0 { |
| 436 |
desc += ", " |
| 437 |
} |
| 438 |
desc += "'" + enum + "'" |
| 439 |
} |
| 440 |
} |
| 441 |
return desc |
| 442 |
} |
| 443 |
|
| 444 |
func (r *root) buildObject(entry namedDefinition) (*cppStruct, error) { |
| 445 |
defName := entry.name |
| 446 |
def := entry.def |
| 447 |
|
| 448 |
base := "" |
| 449 |
if len(def.AllOf) > 1 && def.AllOf[0].Ref != "" { |
| 450 |
ref, err := r.getRef(def.AllOf[0].Ref) |
| 451 |
if err != nil { |
| 452 |
return nil, err |
| 453 |
} |
| 454 |
base = ref.name |
| 455 |
if len(def.AllOf) > 2 { |
| 456 |
return nil, fmt.Errorf("Cannot handle allOf with more than 2 entries") |
| 457 |
} |
| 458 |
def = def.AllOf[1] |
| 459 |
} |
| 460 |
|
| 461 |
s := &cppStruct{ |
| 462 |
desc: def.Description, |
| 463 |
name: defName, |
| 464 |
base: base, |
| 465 |
} |
| 466 |
|
| 467 |
var props properties |
| 468 |
var required []string |
| 469 |
var err error |
| 470 |
switch base { |
| 471 |
case "Request": |
| 472 |
if arguments, ok := def.Properties["arguments"]; ok { |
| 473 |
props, required, err = arguments.properties(r) |
| 474 |
} |
| 475 |
if command, ok := def.Properties["command"]; ok { |
| 476 |
s.typename = command.ClosedEnum[0] |
| 477 |
} |
| 478 |
response := strings.TrimSuffix(s.name, "Request") + "Response" |
| 479 |
s.deps = append(s.deps, response) |
| 480 |
s.typedefs = append(s.typedefs, cppTypedef{from: "Response", to: response}) |
| 481 |
s.emit = true |
| 482 |
s.file = request |
| 483 |
case "Response": |
| 484 |
if body, ok := def.Properties["body"]; ok { |
| 485 |
props, required, err = body.properties(r) |
| 486 |
} |
| 487 |
s.emit = true |
| 488 |
s.file = response |
| 489 |
case "Event": |
| 490 |
if body, ok := def.Properties["body"]; ok { |
| 491 |
props, required, err = body.properties(r) |
| 492 |
} |
| 493 |
if command, ok := def.Properties["event"]; ok { |
| 494 |
s.typename = command.ClosedEnum[0] |
| 495 |
} |
| 496 |
s.emit = true |
| 497 |
s.file = event |
| 498 |
default: |
| 499 |
props = def.Properties |
| 500 |
required = def.Required |
| 501 |
s.file = types |
| 502 |
} |
| 503 |
if err != nil { |
| 504 |
return nil, err |
| 505 |
} |
| 506 |
|
| 507 |
if err = props.foreach(func(propName string, property property) error { |
| 508 |
ty, err := property.typename(r, &s.deps) |
| 509 |
if err != nil { |
| 510 |
return fmt.Errorf("While processing %v.%v: %v", defName, propName, err) |
| 511 |
} |
| 512 |
|
| 513 |
optional := true |
| 514 |
for _, r := range required { |
| 515 |
if propName == r { |
| 516 |
optional = false |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
desc := appendEnumDetails(property.Description, property.OpenEnum, property.ClosedEnum) |
| 521 |
|
| 522 |
defaultValue := "" |
| 523 |
if len(property.ClosedEnum) > 0 { |
| 524 |
defaultValue = `"` + property.ClosedEnum[0] + `"` |
| 525 |
} |
| 526 |
|
| 527 |
s.fields = append(s.fields, cppField{ |
| 528 |
desc: desc, |
| 529 |
defaultValue: defaultValue, |
| 530 |
ty: ty, |
| 531 |
name: propName, |
| 532 |
optional: optional, |
| 533 |
}) |
| 534 |
|
| 535 |
return nil |
| 536 |
}); err != nil { |
| 537 |
return nil, err |
| 538 |
} |
| 539 |
|
| 540 |
return s, nil |
| 541 |
} |
| 542 |
|
| 543 |
func (r *root) buildTypes() ([]cppType, error) { |
| 544 |
ignore := map[string]bool{ |
| 545 |
// These are handled internally. |
| 546 |
"ProtocolMessage": true, |
| 547 |
"Request": true, |
| 548 |
"Event": true, |
| 549 |
"Response": true, |
| 550 |
} |
| 551 |
|
| 552 |
out := []cppType{} |
| 553 |
for _, entry := range r.definitions() { |
| 554 |
if ignore[entry.name] { |
| 555 |
continue |
| 556 |
} |
| 557 |
switch entry.def.Ty { |
| 558 |
case "", "object": |
| 559 |
ty, err := r.buildObject(entry) |
| 560 |
if err != nil { |
| 561 |
return nil, err |
| 562 |
} |
| 563 |
out = append(out, ty) |
| 564 |
case "string": |
| 565 |
desc := appendEnumDetails(entry.def.Description, nil, entry.def.ClosedEnum) |
| 566 |
defaultValue := "" |
| 567 |
if len(entry.def.ClosedEnum) > 0 { |
| 568 |
defaultValue = entry.def.ClosedEnum[0] |
| 569 |
} |
| 570 |
ty := &cppTypedef{from: entry.name, to: "std::string", desc: desc, defaultValue: defaultValue} |
| 571 |
out = append(out, ty) |
| 572 |
default: |
| 573 |
return nil, fmt.Errorf("Unhandled type '%v' for '%v'", entry.def.Ty, entry.name) |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
return out, nil |
| 578 |
} |
| 579 |
|
| 580 |
type cppFile string |
| 581 |
|
| 582 |
const ( |
| 583 |
request = cppFile("request") |
| 584 |
response = cppFile("response") |
| 585 |
event = cppFile("event") |
| 586 |
types = cppFile("types") |
| 587 |
) |
| 588 |
|
| 589 |
type cppFilePaths map[cppFile]string |
| 590 |
|
| 591 |
type cppFiles map[cppFile]*os.File |
| 592 |
|
| 593 |
func run() error { |
| 594 |
pkg := struct { |
| 595 |
Version string `json:"version"` |
| 596 |
}{} |
| 597 |
if err := loadJSONFile(packageURL, &pkg); err != nil { |
| 598 |
return fmt.Errorf("Failed to load JSON file from '%v': %w", packageURL, err) |
| 599 |
} |
| 600 |
|
| 601 |
protocol := root{} |
| 602 |
if err := loadJSONFile(protocolURL, &protocol); err != nil { |
| 603 |
return fmt.Errorf("Failed to load JSON file from '%v': %w", protocolURL, err) |
| 604 |
} |
| 605 |
|
| 606 |
hPath, cppPaths := outputPaths() |
| 607 |
if err := emitFiles(&protocol, hPath, cppPaths, pkg.Version); err != nil { |
| 608 |
return fmt.Errorf("Failed to emit files: %w", err) |
| 609 |
} |
| 610 |
|
| 611 |
if clangfmt, err := exec.LookPath("clang-format"); err == nil { |
| 612 |
if out, err := exec.Command(clangfmt, "-i", hPath).CombinedOutput(); err != nil { |
| 613 |
return fmt.Errorf("Failed to run clang-format on '%v':\n%v\n%w", hPath, string(out), err) |
| 614 |
} |
| 615 |
for _, p := range cppPaths { |
| 616 |
if out, err := exec.Command(clangfmt, "-i", p).CombinedOutput(); err != nil { |
| 617 |
return fmt.Errorf("Failed to run clang-format on '%v':\n%v\n%w", p, string(out), err) |
| 618 |
} |
| 619 |
} |
| 620 |
} else { |
| 621 |
fmt.Printf("clang-format not found on PATH. Please format before committing.") |
| 622 |
} |
| 623 |
|
| 624 |
return nil |
| 625 |
} |
| 626 |
|
| 627 |
func emitFiles(r *root, hPath string, cppPaths map[cppFile]string, version string) error { |
| 628 |
h, err := os.Create(hPath) |
| 629 |
if err != nil { |
| 630 |
return err |
| 631 |
} |
| 632 |
defer h.Close() |
| 633 |
cppFiles := map[cppFile]*os.File{} |
| 634 |
for ty, p := range cppPaths { |
| 635 |
f, err := os.Create(p) |
| 636 |
if err != nil { |
| 637 |
return err |
| 638 |
} |
| 639 |
cppFiles[ty] = f |
| 640 |
defer f.Close() |
| 641 |
} |
| 642 |
|
| 643 |
h.WriteString(strings.ReplaceAll(headerPrologue, versionTag, version)) |
| 644 |
for _, f := range cppFiles { |
| 645 |
f.WriteString(strings.ReplaceAll(cppPrologue, versionTag, version)) |
| 646 |
} |
| 647 |
|
| 648 |
types, err := r.buildTypes() |
| 649 |
if err != nil { |
| 650 |
return err |
| 651 |
} |
| 652 |
|
| 653 |
typesByName := map[string]cppType{} |
| 654 |
for _, s := range types { |
| 655 |
typesByName[s.Name()] = s |
| 656 |
} |
| 657 |
|
| 658 |
seen := map[string]bool{} |
| 659 |
var emit func(cppType) error |
| 660 |
emit = func(ty cppType) error { |
| 661 |
name := ty.Name() |
| 662 |
if seen[name] { |
| 663 |
return nil |
| 664 |
} |
| 665 |
seen[name] = true |
| 666 |
for _, depName := range ty.Dependencies() { |
| 667 |
dep, ok := typesByName[depName] |
| 668 |
if !ok { |
| 669 |
return fmt.Errorf("'%v' depends on unknown type '%v'", name, depName) |
| 670 |
} |
| 671 |
if err := emit(dep); err != nil { |
| 672 |
return err |
| 673 |
} |
| 674 |
} |
| 675 |
ty.WriteHeader(h) |
| 676 |
ty.WriteCPP(cppFiles[ty.File()]) |
| 677 |
return nil |
| 678 |
} |
| 679 |
|
| 680 |
// emit message types. |
| 681 |
// Referenced types will be transitively emitted. |
| 682 |
for _, s := range types { |
| 683 |
switch s.File() { |
| 684 |
case request, response, event: |
| 685 |
if err := emit(s); err != nil { |
| 686 |
return err |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
h.WriteString(headerEpilogue) |
| 692 |
for _, f := range cppFiles { |
| 693 |
f.WriteString(cppEpilogue) |
| 694 |
} |
| 695 |
|
| 696 |
return nil |
| 697 |
} |
| 698 |
|
| 699 |
func loadJSONFile(url string, obj interface{}) error { |
| 700 |
resp, err := http.Get(url) |
| 701 |
if err != nil { |
| 702 |
return err |
| 703 |
} |
| 704 |
data, err := ioutil.ReadAll(resp.Body) |
| 705 |
if err != nil { |
| 706 |
return err |
| 707 |
} |
| 708 |
if err := json.NewDecoder(bytes.NewReader(data)).Decode(obj); err != nil { |
| 709 |
return err |
| 710 |
} |
| 711 |
return nil |
| 712 |
} |
| 713 |
|
| 714 |
func outputPaths() (string, cppFilePaths) { |
| 715 |
_, thisFile, _, _ := runtime.Caller(1) |
| 716 |
thisDir := path.Dir(thisFile) |
| 717 |
h := path.Join(thisDir, "../../include/dap/protocol.h") |
| 718 |
cpp := cppFilePaths{ |
| 719 |
request: path.Join(thisDir, "../../src/protocol_requests.cpp"), |
| 720 |
response: path.Join(thisDir, "../../src/protocol_response.cpp"), |
| 721 |
event: path.Join(thisDir, "../../src/protocol_events.cpp"), |
| 722 |
types: path.Join(thisDir, "../../src/protocol_types.cpp"), |
| 723 |
} |
| 724 |
return h, cpp |
| 725 |
} |