WPILibC++ 2024.3.2
SchemaParser.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
15
16/**
17 * A lexed raw struct schema token.
18 */
19struct Token {
20 /** A lexed raw struct schema token kind. */
21 enum Kind {
22 /// Unknown.
24 /// Integer.
26 /// Identifier.
28 /// Left square bracket.
30 /// Right square bracket.
32 /// Left curly brace.
34 /// Right curly brace.
36 /// Colon.
38 /// Semicolon.
40 /// Comma.
42 /// Equals.
44 /// End of input.
46 };
47
48 Token() = default;
50
51 bool Is(Kind k) const { return kind == k; }
52
55};
56
58
59/**
60 * Raw struct schema lexer.
61 */
62class Lexer {
63 public:
64 /**
65 * Construct a raw struct schema lexer.
66 *
67 * @param in schema
68 */
69 explicit Lexer(std::string_view in) : m_in{in} {}
70
71 /**
72 * Gets the next token.
73 *
74 * @return Token
75 */
76 [[nodiscard]]
78
79 /**
80 * Gets the starting position of the last lexed token.
81 *
82 * @return position (0 = first character)
83 */
84 size_t GetPosition() const { return m_tokenStart; }
85
86 private:
87 Token ScanInteger();
88 Token ScanIdentifier();
89
90 void Get() {
91 if (m_pos < m_in.size()) {
92 [[likely]] m_current = m_in[m_pos];
93 } else {
94 m_current = -1;
95 }
96 ++m_pos;
97 }
98
99 void Unget() {
100 if (m_pos > 0) {
101 [[likely]] m_pos--;
102 if (m_pos < m_in.size()) {
103 [[likely]] m_current = m_in[m_pos];
104 } else {
105 m_current = -1;
106 }
107 } else {
108 m_current = -1;
109 }
110 }
111
112 Token MakeToken(Token::Kind kind) {
113 return {kind, m_in.substr(m_tokenStart, m_pos - m_tokenStart)};
114 }
115
116 std::string_view m_in;
117 int m_current = -1;
118 size_t m_tokenStart = 0;
119 size_t m_pos = 0;
120};
121
122/**
123 * Raw struct set of enumerated values.
124 */
125using EnumValues = std::vector<std::pair<std::string, int64_t>>;
126
127/**
128 * Raw struct schema declaration.
129 */
131 std::string typeString;
132 std::string name;
134 size_t arraySize = 1;
135 unsigned int bitWidth = 0;
136};
137
138/**
139 * Raw struct schema.
140 */
142 std::vector<ParsedDeclaration> declarations;
143};
144
145/**
146 * Raw struct schema parser.
147 */
148class Parser {
149 public:
150 /**
151 * Construct a raw struct schema parser.
152 *
153 * @param in schema
154 */
155 explicit Parser(std::string_view in) : m_lexer{in} {}
156
157 /**
158 * Parses the schema.
159 *
160 * @param[out] out parsed schema object
161 * @return true on success, false on failure (use GetError() to get error)
162 */
163 [[nodiscard]]
164 bool Parse(ParsedSchema* out);
165
166 /**
167 * Gets the parser error if one occurred.
168 *
169 * @return parser error; blank if no error occurred
170 */
171 const std::string& GetError() const { return m_error; }
172
173 private:
174 [[nodiscard]]
175 bool ParseDeclaration(ParsedDeclaration* out);
176 [[nodiscard]]
177 bool ParseEnum(EnumValues* out);
178
179 Token::Kind GetNextToken() {
180 m_token = m_lexer.Scan();
181 return m_token.kind;
182 }
183 [[nodiscard]]
184 bool Expect(Token::Kind kind) {
185 if (m_token.Is(kind)) {
186 [[likely]] return true;
187 }
188 FailExpect(kind);
189 return false;
190 }
191 void FailExpect(Token::Kind desired);
192 void Fail(std::string_view msg);
193
194 Lexer m_lexer;
195 Token m_token;
196 std::string m_error;
197};
198
199} // namespace wpi::structparser
Raw struct schema lexer.
Definition: SchemaParser.h:62
Lexer(std::string_view in)
Construct a raw struct schema lexer.
Definition: SchemaParser.h:69
size_t GetPosition() const
Gets the starting position of the last lexed token.
Definition: SchemaParser.h:84
Token Scan()
Gets the next token.
Raw struct schema parser.
Definition: SchemaParser.h:148
Parser(std::string_view in)
Construct a raw struct schema parser.
Definition: SchemaParser.h:155
const std::string & GetError() const
Gets the parser error if one occurred.
Definition: SchemaParser.h:171
bool Parse(ParsedSchema *out)
Parses the schema.
basic_string_view< char > string_view
Definition: core.h:501
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
Definition: SchemaParser.h:14
std::vector< std::pair< std::string, int64_t > > EnumValues
Raw struct set of enumerated values.
Definition: SchemaParser.h:125
std::string_view ToString(Token::Kind kind)
Raw struct schema declaration.
Definition: SchemaParser.h:130
EnumValues enumValues
Definition: SchemaParser.h:133
unsigned int bitWidth
Definition: SchemaParser.h:135
std::string typeString
Definition: SchemaParser.h:131
std::string name
Definition: SchemaParser.h:132
size_t arraySize
Definition: SchemaParser.h:134
Raw struct schema.
Definition: SchemaParser.h:141
std::vector< ParsedDeclaration > declarations
Definition: SchemaParser.h:142
A lexed raw struct schema token.
Definition: SchemaParser.h:19
std::string_view text
Definition: SchemaParser.h:54
bool Is(Kind k) const
Definition: SchemaParser.h:51
Kind kind
Definition: SchemaParser.h:53
Kind
A lexed raw struct schema token kind.
Definition: SchemaParser.h:21
@ kInteger
Integer.
Definition: SchemaParser.h:25
@ kRightBrace
Right curly brace.
Definition: SchemaParser.h:35
@ kColon
Colon.
Definition: SchemaParser.h:37
@ kEquals
Equals.
Definition: SchemaParser.h:43
@ kLeftBrace
Left curly brace.
Definition: SchemaParser.h:33
@ kRightBracket
Right square bracket.
Definition: SchemaParser.h:31
@ kEndOfInput
End of input.
Definition: SchemaParser.h:45
@ kComma
Comma.
Definition: SchemaParser.h:41
@ kIdentifier
Identifier.
Definition: SchemaParser.h:27
@ kLeftBracket
Left square bracket.
Definition: SchemaParser.h:29
@ kUnknown
Unknown.
Definition: SchemaParser.h:23
@ kSemicolon
Semicolon.
Definition: SchemaParser.h:39
Token(Kind kind, std::string_view text)
Definition: SchemaParser.h:49