fixed many bugs in parser

This commit is contained in:
2025-03-22 18:14:32 +05:00
parent 05972fa40f
commit f69b498caf
4 changed files with 134 additions and 37 deletions
@@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
+99 -19
View File
@@ -1,11 +1,14 @@
using System.Text;
global using System;
global using System.IO;
global using System.Text;
global using System.Collections.Generic;
namespace ParadoxSaveParser.Lib;
public class Parser
{
protected Stream _saveFile;
private List<Token> _tokens = new();
private List<Token> _tokens = new(4_194_304);
private int _tokenIndex;
public Parser(Stream savefile)
@@ -13,7 +16,7 @@ public class Parser
_saveFile = savefile;
}
protected enum TokenType
protected enum TokenType : byte
{
Invalid,
String,
@@ -25,67 +28,119 @@ public class Parser
protected struct Token
{
public TokenType type;
public short column;
public int line;
public string? value;
public override string ToString()
{
string s;
switch (type)
{
case TokenType.Invalid:
return "INVALID_TOKEN";
s = "INVALID_TOKEN";
break;
case TokenType.String:
return value ?? "NULL";
s = value ?? "NULL";
break;
case TokenType.Equals:
return "=";
s = "=";
break;
case TokenType.BracketOpen:
return "{";
s = "{";
break;
case TokenType.BracketClose:
return "}";
s = "}";
break;
default:
throw new ArgumentOutOfRangeException(type.ToString());
}
return $"{line}:{column} '{s}'";
}
}
protected void Lex()
{
_tokens.Clear();
string expectedHeader = "EU4txt";
byte[] headBytes = new byte[expectedHeader.Length];
_saveFile.ReadExactly(headBytes);
string headStr = Encoding.UTF8.GetString(headBytes);
if (headStr != expectedHeader)
throw new Exception($"Invalid gamestate header: '{headStr}'");
StringBuilder str = new();
int line = 2;
int column = 0;
bool isQuoteOpen = false;
bool isStrInQuotes = false;
void CompleteStringToken()
{
if (str.Length > 0 && str[0] != '#')
if (isQuoteOpen)
return;
// strings in quotes can be empty
if (!isStrInQuotes && (str.Length <= 0 || str[0] == '#'))
return;
_tokens.Add(new Token
{
_tokens.Add(new Token { type = TokenType.String, value = str.ToString() });
str.Clear();
}
type = TokenType.String,
column = (short)(column - str.Length),
line = line,
value = str.ToString()
});
str.Clear();
isStrInQuotes = false;
}
while (_saveFile.CanRead)
{
int c = _saveFile.ReadByte();
column++;
switch (c)
{
case -1:
CompleteStringToken();
return;
case '\"':
isQuoteOpen = !isQuoteOpen;
isStrInQuotes = true;
break;
case ' ':
case '\t':
case '\n':
case '\r':
CompleteStringToken();
break;
case '\n':
CompleteStringToken();
line++;
column = 0;
break;
case '=':
CompleteStringToken();
_tokens.Add(new Token { type = TokenType.Equals });
_tokens.Add(new Token
{
type = TokenType.Equals,
line = line, column = (short)column
});
break;
case '{':
CompleteStringToken();
_tokens.Add(new Token { type = TokenType.BracketOpen });
_tokens.Add(new Token
{
type = TokenType.BracketOpen,
line = line, column = (short)column
});
break;
case '}':
CompleteStringToken();
_tokens.Add(new Token { type = TokenType.BracketClose });
_tokens.Add(new Token
{
type = TokenType.BracketClose,
line = line, column = (short)column
});
break;
default:
str.Append((char)c);
@@ -131,8 +186,7 @@ public class Parser
private List<object> ParseList()
{
List<object> list = new();
Token tok = _tokens[_tokenIndex];
while (tok.type != TokenType.BracketClose)
while(true)
{
object? value = ParseValue();
if (value == null)
@@ -150,16 +204,42 @@ public class Parser
while (_tokenIndex < _tokens.Count)
{
Token tok = _tokens[_tokenIndex++];
// end of dictionary
if (tok.type == TokenType.BracketClose)
break;
// Saves may contain some blocks without key.
// Such blocks are skipped because idk where to put them.
// Example: `technology_group=tech_cannorian{ }
// { } { } { }`
if (tok.type == TokenType.BracketOpen)
{
int bracketBalance = 1;
while (bracketBalance != 0)
{
tok = _tokens[_tokenIndex++];
if (tok.type == TokenType.BracketOpen)
bracketBalance++;
else if (tok.type == TokenType.BracketClose)
bracketBalance--;
}
continue;
}
if(tok.type != TokenType.String)
throw new UnexpectedTokenException(tok, _tokenIndex - 1);
string key = tok.value!;
tok = _tokens[_tokenIndex++];
if(tok.type != TokenType.Equals)
if (tok.type == TokenType.BracketOpen)
{
// Saves may contain key-value definition without `=`.
// Example: `map_area_data{` instead of `map_area_data = {`
_tokenIndex--;
}
else if(tok.type != TokenType.Equals)
throw new UnexpectedTokenException(tok, _tokenIndex - 1);