/// <summary> /// 建物のサイズを構文解析する /// </summary> /// <param name="lexer">字句解析器</param> /// <returns>建物のサイズ</returns> private static BuildingSize ParseSize(TextLexer lexer) { // = Token token = lexer.GetToken(); if (token.Type != TokenType.Equal) { Log.InvalidToken(LogCategory, token, lexer); return null; } // 相対サイズ指定 token = lexer.GetToken(); if (token.Type == TokenType.Number) { return new BuildingSize { Size = (double) token.Value }; } // { if (token.Type != TokenType.OpenBrace) { Log.InvalidToken(LogCategory, token, lexer); return null; } BuildingSize size = new BuildingSize(); while (true) { token = lexer.GetToken(); // ファイルの終端 if (token == null) { break; } // } (セクション終端) if (token.Type == TokenType.CloseBrace) { break; } // 無効なトークン if (token.Type != TokenType.Identifier) { Log.InvalidToken(LogCategory, token, lexer); lexer.SkipLine(); continue; } string keyword = token.Value as string; if (string.IsNullOrEmpty(keyword)) { continue; } keyword = keyword.ToLower(); // size if (keyword.Equals("size")) { double? d = ParseDouble(lexer); if (!d.HasValue) { Log.InvalidClause(LogCategory, "size", lexer); continue; } if (d < 0) { Log.OutOfRange(LogCategory, "size", d, lexer); continue; } // 最大サイズ size.MaxSize = (double) d; continue; } // current_size if (keyword.Equals("current_size")) { double? d = ParseDouble(lexer); if (!d.HasValue) { Log.InvalidClause(LogCategory, "current_size", lexer); continue; } if (d < 0) { Log.OutOfRange(LogCategory, "current_size", d, lexer); continue; } // 現在サイズ size.CurrentSize = (double) d; continue; } // 無効なトークン Log.InvalidToken(LogCategory, token, lexer); lexer.SkipLine(); } return size; }
/// <summary> /// 建物のサイズを書き出す /// </summary> /// <param name="building">建物のサイズ</param> /// <param name="writer">ファイル書き込み用</param> private static void WriteBuilding(BuildingSize building, TextWriter writer) { if (DoubleHelper.IsZero(building.Size)) { writer.Write("{{ size = {0} current_size = {1} }}", ObjectHelper.ToString(building.MaxSize), ObjectHelper.ToString(building.CurrentSize)); } else { writer.Write(ObjectHelper.ToString(building.Size)); } }