1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef TOOLS_CDDL_PARSE_H_
6 #define TOOLS_CDDL_PARSE_H_
7 
8 #include <stddef.h>
9 
10 #include <iostream>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "absl/strings/string_view.h"
16 #include "absl/types/optional.h"
17 
18 struct AstNode {
19   // These types all correspond to types in the grammar, which can be found in
20   // grammar.abnf.
21   enum class Type {
22     kRule,
23     kTypename,
24     kGroupname,
25     kAssign,
26     kAssignT,
27     kAssignG,
28     kType,
29     kGrpent,
30     kType1,
31     kType2,
32     kValue,
33     kGroup,
34     kUint,
35     kDigit,
36     kRangeop,
37     kCtlop,
38     kGrpchoice,
39     kOccur,
40     kMemberKey,
41     kId,
42     kNumber,
43     kText,
44     kBytes,
45     kOther,
46   };
47 
48   // A node that (along with its' sublings) represents some sub-section of the
49   // text represented by this node.
50   AstNode* children;
51 
52   // Pointer to the next sibling of this node in the siblings linked list.
53   AstNode* sibling;
54 
55   // Type of node being represented.
56   Type type;
57 
58   // Text parsed from the CDDL spec to create this node.
59   std::string text;
60 
61   // Text parsed from another source but used when serializing this node.
62   std::string integer_member_key_text;
63 
64   // Text parsed from the CDDL spec for the type key.
65   absl::optional<std::string> type_key;
66 };
67 
68 // Override for << operator to simplify logging.
69 // NOTE: If a new enum value is added without modifying this operator, it will
70 // lead to a compilation failure because an enum class is used.
71 inline std::ostream& operator<<(std::ostream& os, const AstNode::Type& which) {
72   switch (which) {
73     case AstNode::Type::kRule:
74       os << "kRule";
75       break;
76     case AstNode::Type::kTypename:
77       os << "kTypename";
78       break;
79     case AstNode::Type::kGroupname:
80       os << "kGroupname";
81       break;
82     case AstNode::Type::kAssign:
83       os << "kAssign";
84       break;
85     case AstNode::Type::kAssignT:
86       os << "kAssignT";
87       break;
88     case AstNode::Type::kAssignG:
89       os << "kAssignG";
90       break;
91     case AstNode::Type::kType:
92       os << "kType";
93       break;
94     case AstNode::Type::kGrpent:
95       os << "kGrpent";
96       break;
97     case AstNode::Type::kType1:
98       os << "kType1";
99       break;
100     case AstNode::Type::kType2:
101       os << "kType2";
102       break;
103     case AstNode::Type::kValue:
104       os << "kValue";
105       break;
106     case AstNode::Type::kGroup:
107       os << "kGroup";
108       break;
109     case AstNode::Type::kUint:
110       os << "kUint";
111       break;
112     case AstNode::Type::kDigit:
113       os << "kDigit";
114       break;
115     case AstNode::Type::kRangeop:
116       os << "kRangeop";
117       break;
118     case AstNode::Type::kCtlop:
119       os << "kCtlop";
120       break;
121     case AstNode::Type::kGrpchoice:
122       os << "kGrpchoice";
123       break;
124     case AstNode::Type::kOccur:
125       os << "kOccur";
126       break;
127     case AstNode::Type::kMemberKey:
128       os << "kMemberKey";
129       break;
130     case AstNode::Type::kId:
131       os << "kId";
132       break;
133     case AstNode::Type::kNumber:
134       os << "kNumber";
135       break;
136     case AstNode::Type::kText:
137       os << "kText";
138       break;
139     case AstNode::Type::kBytes:
140       os << "kBytes";
141       break;
142     case AstNode::Type::kOther:
143       os << "kOther";
144       break;
145   }
146   return os;
147 }
148 
149 struct ParseResult {
150   AstNode* root;
151   std::vector<std::unique_ptr<AstNode>> nodes;
152 };
153 
154 ParseResult ParseCddl(absl::string_view data);
155 void DumpAst(AstNode* node, int indent_level = 0);
156 
157 #endif  // TOOLS_CDDL_PARSE_H_
158