[wpiutil] Rename struct constants to all caps

This commit is contained in:
Peter Johnson
2026-03-15 22:37:36 -07:00
parent c913b27a27
commit aa88fa0fcf
48 changed files with 286 additions and 288 deletions

View File

@@ -125,7 +125,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public boolean getBoolField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kBool) {
if (field.getType() != StructFieldType.BOOL) {
throw new UnsupportedOperationException("field is not bool type");
}
return getFieldImpl(field, arrIndex) != 0;
@@ -157,7 +157,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setBoolField(StructFieldDescriptor field, boolean value, int arrIndex) {
if (field.getType() != StructFieldType.kBool) {
if (field.getType() != StructFieldType.BOOL) {
throw new UnsupportedOperationException("field is not bool type");
}
setFieldImpl(field, value ? 1 : 0, arrIndex);
@@ -253,7 +253,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public float getFloatField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kFloat) {
if (field.getType() != StructFieldType.FLOAT) {
throw new UnsupportedOperationException("field is not float type");
}
return Float.intBitsToFloat((int) getFieldImpl(field, arrIndex));
@@ -285,7 +285,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setFloatField(StructFieldDescriptor field, float value, int arrIndex) {
if (field.getType() != StructFieldType.kFloat) {
if (field.getType() != StructFieldType.FLOAT) {
throw new UnsupportedOperationException("field is not float type");
}
setFieldImpl(field, Float.floatToIntBits(value), arrIndex);
@@ -317,7 +317,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public double getDoubleField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kDouble) {
if (field.getType() != StructFieldType.DOUBLE) {
throw new UnsupportedOperationException("field is not double type");
}
return Double.longBitsToDouble(getFieldImpl(field, arrIndex));
@@ -349,7 +349,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setDoubleField(StructFieldDescriptor field, double value, int arrIndex) {
if (field.getType() != StructFieldType.kDouble) {
if (field.getType() != StructFieldType.DOUBLE) {
throw new UnsupportedOperationException("field is not double type");
}
setFieldImpl(field, Double.doubleToLongBits(value), arrIndex);
@@ -380,7 +380,7 @@ public final class DynamicStruct {
*/
@SuppressWarnings({"PMD.CollapsibleIfStatements", "PMD.AvoidDeeplyNestedIfStmts"})
public String getStringField(StructFieldDescriptor field) {
if (field.getType() != StructFieldType.kChar) {
if (field.getType() != StructFieldType.CHAR) {
throw new UnsupportedOperationException("field is not char type");
}
if (!field.getParent().equals(m_desc)) {
@@ -454,7 +454,7 @@ public final class DynamicStruct {
* @throws IllegalStateException if struct descriptor is invalid
*/
public boolean setStringField(StructFieldDescriptor field, String value) {
if (field.getType() != StructFieldType.kChar) {
if (field.getType() != StructFieldType.CHAR) {
throw new UnsupportedOperationException("field is not char type");
}
if (!field.getParent().equals(m_desc)) {
@@ -485,7 +485,7 @@ public final class DynamicStruct {
* @throws ArrayIndexOutOfBoundsException if array index is out of bounds
*/
public DynamicStruct getStructField(StructFieldDescriptor field, int arrIndex) {
if (field.getType() != StructFieldType.kStruct) {
if (field.getType() != StructFieldType.STRUCT) {
throw new UnsupportedOperationException("field is not struct type");
}
if (!field.getParent().equals(m_desc)) {
@@ -528,7 +528,7 @@ public final class DynamicStruct {
* @throws ReadOnlyBufferException if the underlying buffer is read-only
*/
public void setStructField(StructFieldDescriptor field, DynamicStruct value, int arrIndex) {
if (field.getType() != StructFieldType.kStruct) {
if (field.getType() != StructFieldType.STRUCT) {
throw new UnsupportedOperationException("field is not struct type");
}
if (!field.getParent().equals(m_desc)) {
@@ -588,7 +588,7 @@ public final class DynamicStruct {
default -> throw new IllegalStateException("invalid field size");
};
if (field.isUint() || field.getType() == StructFieldType.kBool) {
if (field.isUint() || field.getType() == StructFieldType.BOOL) {
// for unsigned fields, we can simply logical shift and mask
return (val >>> field.m_bitShift) & field.getBitMask();
} else {

View File

@@ -21,25 +21,25 @@ import java.nio.ByteBuffer;
*/
public interface Struct<T> {
/** Serialized size of a "bool" value. */
int kSizeBool = 1;
int BOOL_SIZE = 1;
/** Serialized size of an "int8" or "uint8" value. */
int kSizeInt8 = 1;
int INT8_SIZE = 1;
/** Serialized size of an "int16" or "uint16" value. */
int kSizeInt16 = 2;
int INT16_SIZE = 2;
/** Serialized size of an "int32" or "uint32" value. */
int kSizeInt32 = 4;
int INT32_SIZE = 4;
/** Serialized size of an "int64" or "uint64" value. */
int kSizeInt64 = 8;
int INT64_SIZE = 8;
/** Serialized size of an "float" or "float32" value. */
int kSizeFloat = 4;
int FLOAT_SIZE = 4;
/** Serialized size of an "double" or "float64" value. */
int kSizeDouble = 8;
int DOUBLE_SIZE = 8;
/**
* Gets the Class object for the stored value.

View File

@@ -113,7 +113,7 @@ public class StructDescriptor {
offset += field.m_size * field.m_arraySize;
} else {
int bitWidth = field.getBitWidth();
if (field.getType() == StructFieldType.kBool
if (field.getType() == StructFieldType.BOOL
&& prevBitfieldSize != 0
&& (shift + 1) <= (prevBitfieldSize * 8)) {
// bool takes on size of preceding bitfield type (if it fits)

View File

@@ -47,7 +47,7 @@ public class StructDescriptorDatabase {
// bitfield checks
if (decl.bitWidth != 0) {
// only integer or boolean types are allowed
if (!type.isInt && !type.isUint && type != StructFieldType.kBool) {
if (!type.isInt && !type.isUint && type != StructFieldType.BOOL) {
throw new BadSchemaException(
decl.name, "type " + decl.typeString + " cannot be bitfield");
}
@@ -59,7 +59,7 @@ public class StructDescriptorDatabase {
}
// bit width must be 1 for booleans
if (type == StructFieldType.kBool && decl.bitWidth != 1) {
if (type == StructFieldType.BOOL && decl.bitWidth != 1) {
throw new BadSchemaException(decl.name, "bit width must be 1 for bool type");
}
@@ -71,7 +71,7 @@ public class StructDescriptorDatabase {
// struct handling
StructDescriptor structDesc = null;
if (type == StructFieldType.kStruct) {
if (type == StructFieldType.STRUCT) {
// recursive definitions are not allowed
if (decl.typeString.equals(name)) {
throw new BadSchemaException(decl.name, "recursive struct reference");

View File

@@ -7,31 +7,31 @@ package org.wpilib.util.struct;
/** Known data types for raw struct dynamic fields (see StructFieldDescriptor). */
public enum StructFieldType {
/** bool. */
kBool("bool", false, false, 1),
BOOL("bool", false, false, 1),
/** char. */
kChar("char", false, false, 1),
CHAR("char", false, false, 1),
/** int8. */
kInt8("int8", true, false, 1),
INT8("int8", true, false, 1),
/** int16. */
kInt16("int16", true, false, 2),
INT16("int16", true, false, 2),
/** int32. */
kInt32("int32", true, false, 4),
INT32("int32", true, false, 4),
/** int64. */
kInt64("int64", true, false, 8),
INT64("int64", true, false, 8),
/** uint8. */
kUint8("uint8", false, true, 1),
UINT8("uint8", false, true, 1),
/** uint16. */
kUint16("uint16", false, true, 2),
UINT16("uint16", false, true, 2),
/** uint32. */
kUint32("uint32", false, true, 4),
UINT32("uint32", false, true, 4),
/** uint64. */
kUint64("uint64", false, true, 8),
UINT64("uint64", false, true, 8),
/** float. */
kFloat("float", false, false, 4),
FLOAT("float", false, false, 4),
/** double. */
kDouble("double", false, false, 8),
DOUBLE("double", false, false, 8),
/** struct. */
kStruct("struct", false, false, 0);
STRUCT("struct", false, false, 0);
/** The name of the data type. */
@SuppressWarnings("MemberName")
@@ -74,11 +74,11 @@ public enum StructFieldType {
}
}
if ("float32".equals(str)) {
return kFloat;
return FLOAT;
} else if ("float64".equals(str)) {
return kDouble;
return DOUBLE;
} else {
return kStruct;
return STRUCT;
}
}
}

View File

@@ -28,21 +28,21 @@ public class Lexer {
m_tokenStart = m_pos - 1;
return switch (m_current) {
case '[' -> TokenKind.kLeftBracket;
case ']' -> TokenKind.kRightBracket;
case '{' -> TokenKind.kLeftBrace;
case '}' -> TokenKind.kRightBrace;
case ':' -> TokenKind.kColon;
case ';' -> TokenKind.kSemicolon;
case ',' -> TokenKind.kComma;
case '=' -> TokenKind.kEquals;
case '[' -> TokenKind.LEFT_BRACKET;
case ']' -> TokenKind.RIGHT_BRACKET;
case '{' -> TokenKind.LEFT_BRACE;
case '}' -> TokenKind.RIGHT_BRACE;
case ':' -> TokenKind.COLON;
case ';' -> TokenKind.SEMICOLON;
case ',' -> TokenKind.COMMA;
case '=' -> TokenKind.EQUALS;
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' -> scanInteger();
case '\0' -> TokenKind.kEndOfInput;
case '\0' -> TokenKind.END_OF_INPUT;
default -> {
if (Character.isLetter(m_current) || m_current == '_') {
yield scanIdentifier();
}
yield TokenKind.kUnknown;
yield TokenKind.UNKNOWN;
}
};
}
@@ -73,7 +73,7 @@ public class Lexer {
get();
} while (Character.isDigit(m_current));
unget();
return TokenKind.kInteger;
return TokenKind.INTEGER;
}
private TokenKind scanIdentifier() {
@@ -81,7 +81,7 @@ public class Lexer {
get();
} while (Character.isLetterOrDigit(m_current) || m_current == '_');
unget();
return TokenKind.kIdentifier;
return TokenKind.IDENTIFIER;
}
private void get() {

View File

@@ -28,14 +28,14 @@ public class Parser {
ParsedSchema schema = new ParsedSchema();
do {
getNextToken();
if (m_token == TokenKind.kSemicolon) {
if (m_token == TokenKind.SEMICOLON) {
continue;
}
if (m_token == TokenKind.kEndOfInput) {
if (m_token == TokenKind.END_OF_INPUT) {
break;
}
schema.declarations.add(parseDeclaration());
} while (m_token != TokenKind.kEndOfInput);
} while (m_token != TokenKind.END_OF_INPUT);
return schema;
}
@@ -43,30 +43,30 @@ public class Parser {
ParsedDeclaration decl = new ParsedDeclaration();
// optional enum specification
if (m_token == TokenKind.kIdentifier && "enum".equals(m_lexer.getTokenText())) {
if (m_token == TokenKind.IDENTIFIER && "enum".equals(m_lexer.getTokenText())) {
getNextToken();
expect(TokenKind.kLeftBrace);
expect(TokenKind.LEFT_BRACE);
decl.enumValues = parseEnum();
getNextToken();
} else if (m_token == TokenKind.kLeftBrace) {
} else if (m_token == TokenKind.LEFT_BRACE) {
decl.enumValues = parseEnum();
getNextToken();
}
// type name
expect(TokenKind.kIdentifier);
expect(TokenKind.IDENTIFIER);
decl.typeString = m_lexer.getTokenText();
getNextToken();
// identifier name
expect(TokenKind.kIdentifier);
expect(TokenKind.IDENTIFIER);
decl.name = m_lexer.getTokenText();
getNextToken();
// array or bit field
if (m_token == TokenKind.kLeftBracket) {
if (m_token == TokenKind.LEFT_BRACKET) {
getNextToken();
expect(TokenKind.kInteger);
expect(TokenKind.INTEGER);
String valueStr = m_lexer.getTokenText();
int value;
try {
@@ -81,11 +81,11 @@ public class Parser {
m_lexer.m_pos, "array size '" + valueStr + "' is not a positive integer");
}
getNextToken();
expect(TokenKind.kRightBracket);
expect(TokenKind.RIGHT_BRACKET);
getNextToken();
} else if (m_token == TokenKind.kColon) {
} else if (m_token == TokenKind.COLON) {
getNextToken();
expect(TokenKind.kInteger);
expect(TokenKind.INTEGER);
String valueStr = m_lexer.getTokenText();
int value;
try {
@@ -103,8 +103,8 @@ public class Parser {
}
// declaration must end with EOF or semicolon
if (m_token != TokenKind.kEndOfInput) {
expect(TokenKind.kSemicolon);
if (m_token != TokenKind.END_OF_INPUT) {
expect(TokenKind.SEMICOLON);
}
return decl;
@@ -115,13 +115,13 @@ public class Parser {
// we start with current = '{'
getNextToken();
while (m_token != TokenKind.kRightBrace) {
expect(TokenKind.kIdentifier);
while (m_token != TokenKind.RIGHT_BRACE) {
expect(TokenKind.IDENTIFIER);
final String name = m_lexer.getTokenText();
getNextToken();
expect(TokenKind.kEquals);
expect(TokenKind.EQUALS);
getNextToken();
expect(TokenKind.kInteger);
expect(TokenKind.INTEGER);
String valueStr = m_lexer.getTokenText();
long value;
try {
@@ -131,10 +131,10 @@ public class Parser {
}
map.put(name, value);
getNextToken();
if (m_token == TokenKind.kRightBrace) {
if (m_token == TokenKind.RIGHT_BRACE) {
break;
}
expect(TokenKind.kComma);
expect(TokenKind.COMMA);
getNextToken();
}
return map;

View File

@@ -7,40 +7,40 @@ package org.wpilib.util.struct.parser;
/** A lexed raw struct schema token. */
public enum TokenKind {
/** Unknown. */
kUnknown("unknown"),
UNKNOWN("unknown"),
/** Integer. */
kInteger("integer"),
INTEGER("integer"),
/** Identifier. */
kIdentifier("identifier"),
IDENTIFIER("identifier"),
/** Left square bracket. */
kLeftBracket("'['"),
LEFT_BRACKET("'['"),
/** Right square bracket. */
kRightBracket("']'"),
RIGHT_BRACKET("']'"),
/** Left curly brace. */
kLeftBrace("'{'"),
LEFT_BRACE("'{'"),
/** Right curly brace. */
kRightBrace("'}'"),
RIGHT_BRACE("'}'"),
/** Colon. */
kColon("':'"),
COLON("':'"),
/** Semicolon. */
kSemicolon("';'"),
SEMICOLON("';'"),
/** Comma. */
kComma("','"),
COMMA("','"),
/** Equals. */
kEquals("'='"),
EQUALS("'='"),
/** End of input. */
kEndOfInput("<EOF>");
END_OF_INPUT("<EOF>");
private final String m_name;

View File

@@ -20,21 +20,21 @@ using namespace wpi::util;
static size_t TypeToSize(StructFieldType type) {
switch (type) {
case StructFieldType::kBool:
case StructFieldType::kChar:
case StructFieldType::kInt8:
case StructFieldType::kUint8:
case StructFieldType::BOOL:
case StructFieldType::CHAR:
case StructFieldType::INT8:
case StructFieldType::UINT8:
return 1;
case StructFieldType::kInt16:
case StructFieldType::kUint16:
case StructFieldType::INT16:
case StructFieldType::UINT16:
return 2;
case StructFieldType::kInt32:
case StructFieldType::kUint32:
case StructFieldType::kFloat:
case StructFieldType::INT32:
case StructFieldType::UINT32:
case StructFieldType::FLOAT:
return 4;
case StructFieldType::kInt64:
case StructFieldType::kUint64:
case StructFieldType::kDouble:
case StructFieldType::INT64:
case StructFieldType::UINT64:
case StructFieldType::DOUBLE:
return 8;
default:
return 0;
@@ -43,31 +43,31 @@ static size_t TypeToSize(StructFieldType type) {
static StructFieldType TypeStringToType(std::string_view str) {
if (str == "bool") {
return StructFieldType::kBool;
return StructFieldType::BOOL;
} else if (str == "char") {
return StructFieldType::kChar;
return StructFieldType::CHAR;
} else if (str == "int8") {
return StructFieldType::kInt8;
return StructFieldType::INT8;
} else if (str == "int16") {
return StructFieldType::kInt16;
return StructFieldType::INT16;
} else if (str == "int32") {
return StructFieldType::kInt32;
return StructFieldType::INT32;
} else if (str == "int64") {
return StructFieldType::kInt64;
return StructFieldType::INT64;
} else if (str == "uint8") {
return StructFieldType::kUint8;
return StructFieldType::UINT8;
} else if (str == "uint16") {
return StructFieldType::kUint16;
return StructFieldType::UINT16;
} else if (str == "uint32") {
return StructFieldType::kUint32;
return StructFieldType::UINT32;
} else if (str == "uint64") {
return StructFieldType::kUint64;
return StructFieldType::UINT64;
} else if (str == "float" || str == "float32") {
return StructFieldType::kFloat;
return StructFieldType::FLOAT;
} else if (str == "double" || str == "float64") {
return StructFieldType::kDouble;
return StructFieldType::DOUBLE;
} else {
return StructFieldType::kStruct;
return StructFieldType::STRUCT;
}
}
@@ -145,7 +145,7 @@ std::string StructDescriptor::CalculateOffsets(
}
offset += field.m_size * field.m_arraySize;
} else {
if (field.m_type == StructFieldType::kBool && prevBitfieldSize != 0 &&
if (field.m_type == StructFieldType::BOOL && prevBitfieldSize != 0 &&
(shift + 1) <= (prevBitfieldSize * 8)) {
// bool takes on size of preceding bitfield type (if it fits)
field.m_size = prevBitfieldSize;
@@ -209,9 +209,8 @@ const StructDescriptor* StructDescriptorDatabase::Add(std::string_view name,
// bitfield checks
if (decl.bitWidth != 0) {
// only integer or boolean types are allowed
if (type == StructFieldType::kChar || type == StructFieldType::kFloat ||
type == StructFieldType::kDouble ||
type == StructFieldType::kStruct) {
if (type == StructFieldType::CHAR || type == StructFieldType::FLOAT ||
type == StructFieldType::DOUBLE || type == StructFieldType::STRUCT) {
*err = fmt::format("field {}: type {} cannot be bitfield", decl.name,
decl.typeString);
[[unlikely]] return nullptr;
@@ -225,7 +224,7 @@ const StructDescriptor* StructDescriptorDatabase::Add(std::string_view name,
}
// bit width must be 1 for booleans
if (type == StructFieldType::kBool && decl.bitWidth != 1) {
if (type == StructFieldType::BOOL && decl.bitWidth != 1) {
*err = fmt::format("field {}: bit width must be 1 for bool type",
decl.name);
[[unlikely]] return nullptr;
@@ -241,7 +240,7 @@ const StructDescriptor* StructDescriptorDatabase::Add(std::string_view name,
// struct handling
const StructDescriptor* structDesc = nullptr;
if (type == StructFieldType::kStruct) {
if (type == StructFieldType::STRUCT) {
// recursive definitions are not allowed
if (decl.typeString == name) {
*err = fmt::format("field {}: recursive struct reference", decl.name);
@@ -350,7 +349,7 @@ void MutableDynamicStruct::SetData(std::span<const uint8_t> data) {
std::string_view DynamicStruct::GetStringField(
const StructFieldDescriptor* field) const {
assert(field->m_type == StructFieldType::kChar);
assert(field->m_type == StructFieldType::CHAR);
assert(field->m_parent == m_desc);
assert(m_desc->IsValid());
// Find last non zero character
@@ -407,7 +406,7 @@ std::string_view DynamicStruct::GetStringField(
bool MutableDynamicStruct::SetStringField(const StructFieldDescriptor* field,
std::string_view value) {
assert(field->m_type == StructFieldType::kChar);
assert(field->m_type == StructFieldType::CHAR);
assert(field->m_parent == m_desc);
assert(m_desc->IsValid());
size_t len = (std::min)(field->m_arraySize, value.size());
@@ -422,7 +421,7 @@ bool MutableDynamicStruct::SetStringField(const StructFieldDescriptor* field,
void MutableDynamicStruct::SetStructField(const StructFieldDescriptor* field,
const DynamicStruct& value,
size_t arrIndex) {
assert(field->m_type == StructFieldType::kStruct);
assert(field->m_type == StructFieldType::STRUCT);
assert(field->m_parent == m_desc);
assert(m_desc->IsValid());
assert(value.GetDescriptor() == field->m_struct);

View File

@@ -15,27 +15,27 @@ using namespace wpi::util::structparser;
std::string_view wpi::util::structparser::ToString(Token::Kind kind) {
switch (kind) {
case Token::kInteger:
case Token::INTEGER:
return "integer";
case Token::kIdentifier:
case Token::IDENTIFIER:
return "identifier";
case Token::kLeftBracket:
case Token::LEFT_BRACKET:
return "'['";
case Token::kRightBracket:
case Token::RIGHT_BRACKET:
return "']'";
case Token::kLeftBrace:
case Token::LEFT_BRACE:
return "'{'";
case Token::kRightBrace:
case Token::RIGHT_BRACE:
return "'}'";
case Token::kColon:
case Token::COLON:
return "':'";
case Token::kSemicolon:
case Token::SEMICOLON:
return "';'";
case Token::kComma:
case Token::COMMA:
return "','";
case Token::kEquals:
case Token::EQUALS:
return "'='";
case Token::kEndOfInput:
case Token::END_OF_INPUT:
return "<EOF>";
default:
return "unknown";
@@ -52,21 +52,21 @@ Token Lexer::Scan() {
switch (m_current) {
case '[':
return MakeToken(Token::kLeftBracket);
return MakeToken(Token::LEFT_BRACKET);
case ']':
return MakeToken(Token::kRightBracket);
return MakeToken(Token::RIGHT_BRACKET);
case '{':
return MakeToken(Token::kLeftBrace);
return MakeToken(Token::LEFT_BRACE);
case '}':
return MakeToken(Token::kRightBrace);
return MakeToken(Token::RIGHT_BRACE);
case ':':
return MakeToken(Token::kColon);
return MakeToken(Token::COLON);
case ';':
return MakeToken(Token::kSemicolon);
return MakeToken(Token::SEMICOLON);
case ',':
return MakeToken(Token::kComma);
return MakeToken(Token::COMMA);
case '=':
return MakeToken(Token::kEquals);
return MakeToken(Token::EQUALS);
case '-':
case '0':
case '1':
@@ -80,12 +80,12 @@ Token Lexer::Scan() {
case '9':
return ScanInteger();
case -1:
return {Token::kEndOfInput, {}};
return {Token::END_OF_INPUT, {}};
default:
if (isAlpha(m_current) || m_current == '_') {
[[likely]] return ScanIdentifier();
}
return MakeToken(Token::kUnknown);
return MakeToken(Token::UNKNOWN);
}
}
@@ -94,7 +94,7 @@ Token Lexer::ScanInteger() {
Get();
} while (isDigit(m_current));
Unget();
return MakeToken(Token::kInteger);
return MakeToken(Token::INTEGER);
}
Token Lexer::ScanIdentifier() {
@@ -102,7 +102,7 @@ Token Lexer::ScanIdentifier() {
Get();
} while (isAlnum(m_current) || m_current == '_');
Unget();
return MakeToken(Token::kIdentifier);
return MakeToken(Token::IDENTIFIER);
}
void Parser::FailExpect(Token::Kind desired) {
@@ -116,31 +116,31 @@ void Parser::Fail(std::string_view msg) {
bool Parser::Parse(ParsedSchema* out) {
do {
GetNextToken();
if (m_token.Is(Token::kSemicolon)) {
if (m_token.Is(Token::SEMICOLON)) {
continue;
}
if (m_token.Is(Token::kEndOfInput)) {
if (m_token.Is(Token::END_OF_INPUT)) {
break;
}
if (!ParseDeclaration(&out->declarations.emplace_back())) {
[[unlikely]] return false;
}
} while (m_token.kind != Token::kEndOfInput);
} while (m_token.kind != Token::END_OF_INPUT);
return true;
}
bool Parser::ParseDeclaration(ParsedDeclaration* out) {
// optional enum specification
if (m_token.Is(Token::kIdentifier) && m_token.text == "enum") {
if (m_token.Is(Token::IDENTIFIER) && m_token.text == "enum") {
GetNextToken();
if (!Expect(Token::kLeftBrace)) {
if (!Expect(Token::LEFT_BRACE)) {
[[unlikely]] return false;
}
if (!ParseEnum(&out->enumValues)) {
[[unlikely]] return false;
}
GetNextToken();
} else if (m_token.Is(Token::kLeftBrace)) {
} else if (m_token.Is(Token::LEFT_BRACE)) {
if (!ParseEnum(&out->enumValues)) {
[[unlikely]] return false;
}
@@ -148,23 +148,23 @@ bool Parser::ParseDeclaration(ParsedDeclaration* out) {
}
// type name
if (!Expect(Token::kIdentifier)) {
if (!Expect(Token::IDENTIFIER)) {
[[unlikely]] return false;
}
out->typeString = m_token.text;
GetNextToken();
// identifier name
if (!Expect(Token::kIdentifier)) {
if (!Expect(Token::IDENTIFIER)) {
[[unlikely]] return false;
}
out->name = m_token.text;
GetNextToken();
// array or bit field
if (m_token.Is(Token::kLeftBracket)) {
if (m_token.Is(Token::LEFT_BRACKET)) {
GetNextToken();
if (!Expect(Token::kInteger)) {
if (!Expect(Token::INTEGER)) {
[[unlikely]] return false;
}
auto val = parse_integer<uint64_t>(m_token.text, 10);
@@ -176,13 +176,13 @@ bool Parser::ParseDeclaration(ParsedDeclaration* out) {
[[unlikely]] return false;
}
GetNextToken();
if (!Expect(Token::kRightBracket)) {
if (!Expect(Token::RIGHT_BRACKET)) {
[[unlikely]] return false;
}
GetNextToken();
} else if (m_token.Is(Token::kColon)) {
} else if (m_token.Is(Token::COLON)) {
GetNextToken();
if (!Expect(Token::kInteger)) {
if (!Expect(Token::INTEGER)) {
[[unlikely]] return false;
}
auto val = parse_integer<unsigned int>(m_token.text, 10);
@@ -197,27 +197,27 @@ bool Parser::ParseDeclaration(ParsedDeclaration* out) {
}
// declaration must end with EOF or semicolon
if (m_token.Is(Token::kEndOfInput)) {
if (m_token.Is(Token::END_OF_INPUT)) {
return true;
}
return Expect(Token::kSemicolon);
return Expect(Token::SEMICOLON);
}
bool Parser::ParseEnum(EnumValues* out) {
// we start with current = '{'
GetNextToken();
while (!m_token.Is(Token::kRightBrace)) {
if (!Expect(Token::kIdentifier)) {
while (!m_token.Is(Token::RIGHT_BRACE)) {
if (!Expect(Token::IDENTIFIER)) {
[[unlikely]] return false;
}
std::string name;
name = m_token.text;
GetNextToken();
if (!Expect(Token::kEquals)) {
if (!Expect(Token::EQUALS)) {
[[unlikely]] return false;
}
GetNextToken();
if (!Expect(Token::kInteger)) {
if (!Expect(Token::INTEGER)) {
[[unlikely]] return false;
}
int64_t value;
@@ -229,10 +229,10 @@ bool Parser::ParseEnum(EnumValues* out) {
}
out->emplace_back(std::move(name), value);
GetNextToken();
if (m_token.Is(Token::kRightBrace)) {
if (m_token.Is(Token::RIGHT_BRACE)) {
break;
}
if (!Expect(Token::kComma)) {
if (!Expect(Token::COMMA)) {
[[unlikely]] return false;
}
GetNextToken();

View File

@@ -31,31 +31,31 @@ class StructDescriptorDatabase;
*/
enum class StructFieldType {
/// bool.
kBool,
BOOL,
/// char.
kChar,
CHAR,
/// int8.
kInt8,
INT8,
/// int16.
kInt16,
INT16,
/// int32.
kInt32,
INT32,
/// int64.
kInt64,
INT64,
/// uint8.
kUint8,
UINT8,
/// uint16.
kUint16,
UINT16,
/// uint32.
kUint32,
UINT32,
/// uint64.
kUint64,
UINT64,
/// float.
kFloat,
FLOAT,
/// double.
kDouble,
DOUBLE,
/// struct.
kStruct
STRUCT
};
/**
@@ -107,10 +107,9 @@ class StructFieldDescriptor {
* @return true if signed integer, false otherwise
*/
bool IsInt() const {
return m_type == StructFieldType::kInt8 ||
m_type == StructFieldType::kInt16 ||
m_type == StructFieldType::kInt32 ||
m_type == StructFieldType::kInt64;
return m_type == StructFieldType::INT8 ||
m_type == StructFieldType::INT16 ||
m_type == StructFieldType::INT32 || m_type == StructFieldType::INT64;
}
/**
@@ -119,10 +118,10 @@ class StructFieldDescriptor {
* @return true if unsigned integer, false otherwise
*/
bool IsUint() const {
return m_type == StructFieldType::kUint8 ||
m_type == StructFieldType::kUint16 ||
m_type == StructFieldType::kUint32 ||
m_type == StructFieldType::kUint64;
return m_type == StructFieldType::UINT8 ||
m_type == StructFieldType::UINT16 ||
m_type == StructFieldType::UINT32 ||
m_type == StructFieldType::UINT64;
}
/**
@@ -408,7 +407,7 @@ class DynamicStruct {
*/
bool GetBoolField(const StructFieldDescriptor* field,
size_t arrIndex = 0) const {
assert(field->m_type == StructFieldType::kBool);
assert(field->m_type == StructFieldType::BOOL);
return GetFieldImpl(field, arrIndex);
}
@@ -457,7 +456,7 @@ class DynamicStruct {
*/
float GetFloatField(const StructFieldDescriptor* field,
size_t arrIndex = 0) const {
assert(field->m_type == StructFieldType::kFloat);
assert(field->m_type == StructFieldType::FLOAT);
return bit_cast<float>(
static_cast<uint32_t>(GetFieldImpl(field, arrIndex)));
}
@@ -471,7 +470,7 @@ class DynamicStruct {
*/
double GetDoubleField(const StructFieldDescriptor* field,
size_t arrIndex = 0) const {
assert(field->m_type == StructFieldType::kDouble);
assert(field->m_type == StructFieldType::DOUBLE);
return bit_cast<double>(GetFieldImpl(field, arrIndex));
}
@@ -492,7 +491,7 @@ class DynamicStruct {
*/
DynamicStruct GetStructField(const StructFieldDescriptor* field,
size_t arrIndex = 0) const {
assert(field->m_type == StructFieldType::kStruct);
assert(field->m_type == StructFieldType::STRUCT);
assert(field->m_parent == m_desc);
assert(m_desc->IsValid());
assert(arrIndex < field->m_arraySize);
@@ -552,7 +551,7 @@ class MutableDynamicStruct : public DynamicStruct {
*/
void SetBoolField(const StructFieldDescriptor* field, bool value,
size_t arrIndex = 0) {
assert(field->m_type == StructFieldType::kBool);
assert(field->m_type == StructFieldType::BOOL);
SetFieldImpl(field, value ? 1 : 0, arrIndex);
}
@@ -591,7 +590,7 @@ class MutableDynamicStruct : public DynamicStruct {
*/
void SetFloatField(const StructFieldDescriptor* field, float value,
size_t arrIndex = 0) {
assert(field->m_type == StructFieldType::kFloat);
assert(field->m_type == StructFieldType::FLOAT);
SetFieldImpl(field, bit_cast<uint32_t>(value), arrIndex);
}
@@ -604,7 +603,7 @@ class MutableDynamicStruct : public DynamicStruct {
*/
void SetDoubleField(const StructFieldDescriptor* field, double value,
size_t arrIndex = 0) {
assert(field->m_type == StructFieldType::kDouble);
assert(field->m_type == StructFieldType::DOUBLE);
SetFieldImpl(field, bit_cast<uint64_t>(value), arrIndex);
}
@@ -637,7 +636,7 @@ class MutableDynamicStruct : public DynamicStruct {
*/
MutableDynamicStruct GetStructField(const StructFieldDescriptor* field,
size_t arrIndex = 0) {
assert(field->m_type == StructFieldType::kStruct);
assert(field->m_type == StructFieldType::STRUCT);
assert(field->m_parent == m_desc);
assert(m_desc->IsValid());
assert(arrIndex < field->m_arraySize);

View File

@@ -20,29 +20,29 @@ struct Token {
/** A lexed raw struct schema token kind. */
enum Kind {
/// Unknown.
kUnknown,
UNKNOWN,
/// Integer.
kInteger,
INTEGER,
/// Identifier.
kIdentifier,
IDENTIFIER,
/// Left square bracket.
kLeftBracket,
LEFT_BRACKET,
/// Right square bracket.
kRightBracket,
RIGHT_BRACKET,
/// Left curly brace.
kLeftBrace,
LEFT_BRACE,
/// Right curly brace.
kRightBrace,
RIGHT_BRACE,
/// Colon.
kColon,
COLON,
/// Semicolon.
kSemicolon,
SEMICOLON,
/// Comma.
kComma,
COMMA,
/// Equals.
kEquals,
EQUALS,
/// End of input.
kEndOfInput,
END_OF_INPUT,
};
Token() = default;
@@ -50,7 +50,7 @@ struct Token {
bool Is(Kind k) const { return kind == k; }
Kind kind = kUnknown;
Kind kind = UNKNOWN;
std::string_view text;
};