private OpResult TryParseEnum(ParserState state, LexerData data, ParsingIndex index) { if (!TryReadToken(data, TokenType.Identifier, out var namePos, out var enumName)) { return(OpResult.Fail($"Enum name is not defined at {namePos}")); } if (_mapper != null) { enumName = _mapper.RemapTypeName(TypeKind.Enum, enumName, state.RemappedTypes); } var validationResult = ValidateTypeName(enumName, namePos, index); if (validationResult.HasError) { return(validationResult); } if (!TryReadToken(data, TokenType.Colon, out var colonPos, out _)) { return(OpResult.Fail($"Missing : at {colonPos}")); } if (!TryReadToken(data, TokenType.Identifier, out var undTypePos, out var underlyingType)) { return(OpResult.Fail($"Missing enum `{enumName}` type definition at {undTypePos}")); } if (!ParsingHelper.IsInteger(underlyingType)) { return(OpResult.Fail($"Enum `{enumName}` has invalid underlying type `{underlyingType}`")); } var isFlags = false; if (TryReadToken(data, TokenType.Identifier, out var flagsPos, out var keyword)) { if (keyword != FlagsId) { return(OpResult.Fail($"Unknown enum `{enumName}` optional keyword `{keyword}` at {flagsPos}." + " Only `flags` keyword is supported.")); } isFlags = true; } if (!TryReadToken(data, TokenType.CurlyBraceLeft, out var bracePos, out _)) { return(OpResult.Fail($"Missing `{{` after enum `{enumName}` declaration at {bracePos}")); } state.StartBlock(ParsingBlockType.Enum, enumName); index.BeginEnum(enumName, underlyingType, isFlags); return(OpResult.Ok()); }
private static OpResult ParseEnumContent(ParserState state, LexerData data, ParsingIndex index) { var enumName = state.CurrentBlock.Name; if (TryReadToken(data, TokenType.CurlyBraceRight, out _, out _)) { state.EndBlock(); index.EndEnum(enumName); return(index.IsEnumNonEmpty(enumName) ? OpResult.Ok() : OpResult.Fail($"Enum `{enumName}` is empty. No default value is available")); } if (!TryReadToken(data, TokenType.Identifier, out var namePos, out var name)) { return(OpResult.Fail($"Failed to read enum name at {namePos}")); } if (!ParsingHelper.IsNameValid(name)) { return(OpResult.Fail($"Enum item `{enumName}.{name}` has invalid name at {namePos}")); } if (index.IsEnumContainsItem(enumName, name)) { return(OpResult.Fail($"Enum item `{enumName}.{name}` is defined second time at {namePos}")); } if (!TryReadToken(data, TokenType.Assignment, out var assignPos, out _)) { return(OpResult.Fail($"Missing `=` at {assignPos}")); } if (!TryReadToken(data, TokenType.Identifier, out var valPos, out var value)) { return(OpResult.Fail($"Missing enum item `{enumName}.{name}` default value at {valPos}")); } if (!IsEnumValueValid(enumName, value, index)) { return(OpResult.Fail($"Enum item `{enumName}.{name}` has invalid default value at {valPos}")); } if (!TryReadToken(data, TokenType.Semicolon, out var semicolonPos, out _)) { return(OpResult.Fail($"Missing ; at {semicolonPos}")); } index.PutEnumItem(enumName, name, value); return(OpResult.Ok()); }
private bool IsDefaultValueValid(string itemType, string value, ParsingIndex index) { if (ParsingHelper.IsPrimitive(itemType)) { return(ParsingHelper.IsPrimitiveValueValid(itemType, value)); } if (_externStructs.TryGetValue(itemType, out var externStructInfo)) { return(externStructInfo.Values.Contains(value)); } if (index.ContainsEnum(itemType)) { return(index.IsEnumContainsItem(itemType, value)); } return(false); }
private OpResult ValidateTypeName(string type, Position pos, ParsingIndex index) { if (!ParsingHelper.IsNameValid(type)) { return(OpResult.Fail($"Type `{type}` has invalid name")); } if (ParsingHelper.Primitives.Contains(type)) { return(OpResult.Fail($"Try to redefine built-in type `{type}` at {pos}")); } if (_externStructs.ContainsKey(type)) { return(OpResult.Fail($"Try to redefine extern struct `{type}` at {pos}")); } if (index.ContainsCompletedType(type)) { return(OpResult.Fail($"Try to define type `{type}` second time at {pos}")); } return(OpResult.Ok()); }
private static bool IsEnumValueValid(string enumName, string value, ParsingIndex index) { var underlyingType = index.GetEnumUnderlyingType(enumName); return(ParsingHelper.IsPrimitiveValueValid(underlyingType, value)); }
private OpResult ParseStructContent(ParserState state, LexerData data, ParsingIndex index) { var structName = state.CurrentBlock.Name; if (TryReadToken(data, TokenType.CurlyBraceRight, out _, out _)) { state.EndBlock(); index.EndStruct(structName); return(index.IsStructNonEmpty(structName) ? OpResult.Ok() : OpResult.Fail($"Struct `{structName}` is zero-sized")); } if (!TryReadToken(data, TokenType.Identifier, out var typePos, out var fieldType)) { return(OpResult.Fail($"Missing field type definition at {typePos}")); } if (_mapper != null) { fieldType = _mapper.RemapMemberType(fieldType, state.RemappedTypes); } if (!TryReadToken(data, TokenType.Identifier, out var namePos, out var fieldName)) { return(OpResult.Fail($"Missing field name definition at {namePos}")); } if (!ParsingHelper.IsNameValid(fieldName)) { return(OpResult.Fail($"Field `{structName}.{fieldName}` has invalid name at {namePos}")); } if (index.IsStructContainsField(structName, fieldName)) { return(OpResult.Fail($"Field `{structName}.{fieldName}` is defined second time at {namePos}")); } if (!IsTypeKnown(fieldType, index)) { return(OpResult.Fail($"Field `{structName}.{fieldName}` has unknown type `{fieldType}` at {typePos}")); } string fieldDefaultValue = null; if (TryReadToken(data, TokenType.Assignment, out _, out _)) { if (!TryReadToken(data, TokenType.Identifier, out var valuePos, out fieldDefaultValue)) { return(OpResult.Fail($"Missing default value of field `{structName}.{fieldName}` at {valuePos}")); } if (_mapper != null) { fieldDefaultValue = _mapper.RemapMemberDefaultValue(fieldType, fieldDefaultValue); } if (!IsDefaultValueValid(fieldType, fieldDefaultValue, index)) { return(OpResult.Fail($"Invalid default value `{fieldDefaultValue}` is defined for " + $"field `{structName}.{fieldName}` at {valuePos}")); } } if (!TryReadToken(data, TokenType.Semicolon, out var semicolonPos, out _)) { return(OpResult.Fail($"Missing ; after field `{structName}.{fieldName}` declaration at {semicolonPos}")); } index.PutStructField(structName, fieldType, fieldName, fieldDefaultValue); return(OpResult.Ok()); }