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 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 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()); }