daily update on 20250214 by nitload youmu, building syntax analysor, tough!

This commit is contained in:
Nitload
2025-02-14 13:30:17 +08:00
parent c23b640cae
commit ccef0de6f6
68 changed files with 2594 additions and 913 deletions
@@ -51,7 +51,7 @@ namespace StgSharp.Script.Express
private const string Token = "token", End = "end", Rest = "rest";
private bool _isReadingMultiLineComment;
private int _current;
private int _current, _lineNum;
private IScriptSourceProvider _provider;
private string _lineCache;
@@ -63,17 +63,17 @@ namespace StgSharp.Script.Express
_isReadingMultiLineComment = false;
}
public int LineNumber
{
get => _provider.Location;
}
public bool IsEmpty
{
get => ( ( string.IsNullOrEmpty( _lineCache ) || _current >=
_lineCache.Length ) ) && _provider.IsEmpty;
}
public int LineNumber
{
get => _provider.Location;
}
/// <summary>
/// <para> Read code until end of a line. Lines in multi-line comment will be ignored. Line
/// begins with part of multi-line comment will automatically remove the comment part, but
@@ -88,7 +88,7 @@ namespace StgSharp.Script.Express
while( lineOver || _isReadingMultiLineComment ) {
if( lineOver ) {
do {
_lineCache = _provider.ReadLine();
_lineCache = _provider.ReadLine( out _lineNum );
} while (string.IsNullOrEmpty( _lineCache ));
_current = 0;
}
@@ -111,7 +111,7 @@ namespace StgSharp.Script.Express
return _lineCache.AsSpan().Slice( _current );
}
public unsafe ReadOnlySpan<char> ReadToken()
public unsafe TokenSource ReadToken()
{
int begin, end, rest;
readLine:
@@ -126,13 +126,24 @@ namespace StgSharp.Script.Express
end = match.Groups[ End ].Index;
rest = match.Groups[ Rest ].Index;
_current = rest;
return source.Slice( 0, end - begin );
return new(
source[ .. ( end - begin ) ], _lineNum, begin,
PreprocessingMask.Names );
} else {
throw new ExpInvalidTokenException( _current );
}
} else if( char.IsPunctuation( c ) || char.IsSymbol( c ) ) {
switch( c ) {
case '"':
char cNext = source[ 1 ];
if( cNext == '\"' ) //empty string
{
begin = _current;
_current += 2;
return new(
ReadOnlySpan<char>.Empty, _lineNum, begin,
PreprocessingMask.String );
}
Match match = _nextStringLiteralPattern.Match(
_lineCache, _current );
if( match.Success ) {
@@ -140,18 +151,24 @@ namespace StgSharp.Script.Express
end = match.Groups[ "end" ].Index;
rest = match.Groups[ "rest" ].Index;
_current = rest;
return source[ ..( end - begin ) ];
return new(
source[ 1..( end - begin ) ], _lineNum, begin,
PreprocessingMask.String );
} else {
throw new ExpInvalidCharException( '\"' );
}
case '(':
char cNext = source[ 1 ];
cNext = source[ 1 ];
if( cNext == '*' ) {
_isReadingMultiLineComment = true;
_current += 2;
goto readLine;
} else {
return source[ ..1 ];
begin = _current;
_current++;
return new(
source[ ..1 ], _lineNum, begin,
PreprocessingMask.Symbol );
}
case '-':
@@ -167,7 +184,9 @@ namespace StgSharp.Script.Express
end = match.Groups[ End ].Index;
rest = match.Groups[ Rest ].Index;
_current = rest;
return source[ ..( end - begin ) ];
return new(
source[ ..( end - begin ) ], _lineNum,
begin, PreprocessingMask.Number );
} else {
throw new ExpInvalidTokenException( _current );
}
@@ -176,13 +195,19 @@ namespace StgSharp.Script.Express
_current = 0;
goto readLine;
} else {
begin = _current;
_current++;
return source[ ..1 ];
return new(
source[ ..1 ], _lineNum, begin,
PreprocessingMask.Symbol );
}
case ')' or '[' or ']' or '{' or '}' or ':' or ';' or
'+' or '/' or '\\' or '*' or '.' or ',':
begin = _current;
_current++;
return source[ ..1 ];
return new(
source[ ..1 ], _lineNum, begin,
PreprocessingMask.Symbol );
default:
throw new ExpInvalidCharException( c );
}
@@ -194,7 +219,9 @@ namespace StgSharp.Script.Express
end = match.Groups[ End ].Index;
rest = match.Groups[ Rest ].Index;
_current = rest;
return source[ ..( end - begin ) ];
return new(
source[ ..( end - begin ) ], _lineNum, begin,
PreprocessingMask.Number );
} else {
throw new ExpInvalidTokenException( _current );
}
@@ -226,7 +253,7 @@ namespace StgSharp.Script.Express
private static partial Regex GetSkipBlankPattern();
[GeneratedRegex(
@"(?<token>"")(?:.*?[^""\\](\\\\)*)?(?<end>"")\s*?(?<next>\S|$)",
@"(?<token>"")(?:.*?[^\\](?:\\\\)*?)(?<end>"")\s*?(?<rest>\S|$)",
RegexOptions.Singleline )]
private static partial Regex GetStringLiteralPattern();