number parsing and control characters skipping

This commit is contained in:
Timerix 2025-03-22 18:33:28 +05:00
parent f69b498caf
commit 17981347f4

View File

@ -19,7 +19,7 @@ public class Parser
protected enum TokenType : byte protected enum TokenType : byte
{ {
Invalid, Invalid,
String, StringOrNumber,
Equals, Equals,
BracketOpen, BracketOpen,
BracketClose, BracketClose,
@ -40,7 +40,7 @@ public class Parser
case TokenType.Invalid: case TokenType.Invalid:
s = "INVALID_TOKEN"; s = "INVALID_TOKEN";
break; break;
case TokenType.String: case TokenType.StringOrNumber:
s = value ?? "NULL"; s = value ?? "NULL";
break; break;
case TokenType.Equals: case TokenType.Equals:
@ -86,7 +86,7 @@ public class Parser
return; return;
_tokens.Add(new Token _tokens.Add(new Token
{ {
type = TokenType.String, type = TokenType.StringOrNumber,
column = (short)(column - str.Length), column = (short)(column - str.Length),
line = line, line = line,
value = str.ToString() value = str.ToString()
@ -143,7 +143,10 @@ public class Parser
}); });
break; break;
default: default:
str.Append((char)c); // Skip control characters, which are invisible and causing frontend bugs.
// I dont know why there are so many of them in strings.
if(c >= 0x20)
str.Append((char)c);
break; break;
} }
} }
@ -162,8 +165,16 @@ public class Parser
Token tok = _tokens[_tokenIndex++]; Token tok = _tokens[_tokenIndex++];
switch (tok.type) switch (tok.type)
{ {
case TokenType.String: case TokenType.StringOrNumber:
return tok.value!; if(string.IsNullOrEmpty(tok.value))
return string.Empty;
if (tok.value[0] != '-' && !char.IsDigit(tok.value[0]))
return tok.value;
if(tok.value.Contains('.') && Double.TryParse(tok.value, out double d))
return d;
if (Int64.TryParse(tok.value, out long l))
return l;
return tok.value;
case TokenType.BracketOpen: case TokenType.BracketOpen:
return ParseListOrDict(); return ParseListOrDict();
case TokenType.BracketClose: case TokenType.BracketClose:
@ -177,7 +188,7 @@ public class Parser
{ {
Token first = _tokens[_tokenIndex]; Token first = _tokens[_tokenIndex];
Token second = _tokens[_tokenIndex + 1]; Token second = _tokens[_tokenIndex + 1];
if (first.type == TokenType.String && second.type == TokenType.Equals) if (first.type == TokenType.StringOrNumber && second.type == TokenType.Equals)
return ParseDict(); return ParseDict();
return ParseList(); return ParseList();
@ -227,7 +238,7 @@ public class Parser
continue; continue;
} }
if(tok.type != TokenType.String) if(tok.type != TokenType.StringOrNumber)
throw new UnexpectedTokenException(tok, _tokenIndex - 1); throw new UnexpectedTokenException(tok, _tokenIndex - 1);
string key = tok.value!; string key = tok.value!;