aboutsummaryrefslogtreecommitdiff
path: root/tools/cddl/parse.h
blob: d029c8517516d0f8a1a6d66010331de644c1f9b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef TOOLS_CDDL_PARSE_H_
#define TOOLS_CDDL_PARSE_H_

#include <stddef.h>

#include <memory>
#include <string>
#include <vector>

#include "absl/strings/string_view.h"

struct AstNode {
  // These types all correspond to types in the grammar, which can be found in
  // grammar.abnf.
  enum class Type {
    kRule,
    kTypename,
    kGroupname,
    kAssign,
    kAssignT,
    kAssignG,
    kType,
    kGrpent,
    kType1,
    kType2,
    kValue,
    kGroup,
    kUint,
    kDigit,
    kRangeop,
    kCtlop,
    kGrpchoice,
    kOccur,
    kMemberKey,
    kId,
    kNumber,
    kText,
    kBytes,
    kOther,
  };

  // A node that (along with its' sublings) represents some sub-section of the
  // text represented by this node.
  AstNode* children;

  // Pointer to the next sibling of this node in the siblings linked list.
  AstNode* sibling;

  // Type of node being represented.
  Type type;

  // Text parsed from the CDDL spec to create this node.
  std::string text;

  // Text parsed from another source but used when serializing this node.
  std::string integer_member_key_text;
};

struct ParseResult {
  AstNode* root;
  std::vector<std::unique_ptr<AstNode>> nodes;
};

ParseResult ParseCddl(absl::string_view data);
void DumpAst(AstNode* node, int indent_level = 0);

#endif  // TOOLS_CDDL_PARSE_H_