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
@@ -64,6 +64,19 @@ namespace StgSharp.Script.Express
}
}
public override ExpToken Left => throw new NotImplementedException();
public override ExpToken Next
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override ExpToken Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
public void AddFalseOperation( ExpToken expressionRoot )
{
AssertSchemaInclude( expressionRoot );
@@ -82,16 +82,22 @@ namespace StgSharp.Script.Express
}
}
public override ExpToken Left => throw new NotImplementedException();
public override ExpToken Next
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override ExpToken Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
public override string TypeName => _type.Name;
public T value => _value;
public override bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
{
nextLevel = null!;
return false;
}
}
}
@@ -0,0 +1,58 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpOperandToken.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
public class ExpOperandToken : ExpToken
{
public ExpOperandToken( string source ) : base( source, null! ) { }
public override ExpToken Left => throw new NotImplementedException();
public override ExpToken Next
{
get;
set;
}
public override ExpToken Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
}
}
@@ -42,18 +42,31 @@ namespace StgSharp.Script.Express
public ExpRepeat( string name, ExpSchema context )
: base( name, context ) { }
public override ExpToken Left => throw new NotImplementedException();
public override ExpToken Next
{
get;
set;
}
public override ExpToken Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
}
public class ExpRepeatRule : ExpToken
{
private ExpElementInstanceBase _variable, _begin, _end, _increment;
private List<ExpToken> _token = new List<ExpToken>();
private ExpToken _token;
public ExpRepeatRule(
string name,
ExpSchema context,
IEnumerable<ExpToken> operation,
ExpToken firstOperation,
ExpElementInstanceBase variable,
ExpElementInstanceBase begin,
ExpElementInstanceBase end,
@@ -69,11 +82,11 @@ namespace StgSharp.Script.Express
switch( ( ExpTokenFlag )variable.TokenTypeCode | ExpTokenFlag.BuiltinType_Any ) {
case ExpTokenFlag.BuiltinType_Float:
increment = new ExpLiteralToken<float>(
$"{name}_defaultincrement", context, 1.0f );
$"{name}_defaultIncrement", context, 1.0f );
break;
case ExpTokenFlag.BuiltinType_Int:
increment = new ExpLiteralToken<int>(
$"{name}_defaultincrement", context, 1 );
$"{name}_defaultIncrement", context, 1 );
break;
default:
throw new ExpInvalidTypeException(
@@ -87,12 +100,18 @@ namespace StgSharp.Script.Express
_tokenType = ExpTokenFlag.Branch_Repeat;
}
public override bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
public override ExpToken Left => throw new NotImplementedException();
public override ExpToken Next
{
nextLevel = ( _token.ToList() as ICollection<IExpressionToken> )!;
return true;
get;
set;
}
public override ExpToken Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
}
}
@@ -37,45 +37,61 @@ namespace StgSharp.Script.Express
public class ExpSwitchToken : ExpToken
{
private Dictionary<ExpToken, List<ExpToken>> _cases;
private Dictionary<ExpToken, ExpToken> _cases;
private ExpToken _default;
private ExpToken _target;
private List<ExpToken> _default;
public ExpSwitchToken(
string name,
ExpSchema context,
ExpToken target,
IEnumerable<ExpToken> @default,
params (ExpToken label, IEnumerable<ExpToken> op)[] cases )
ExpToken defaultCase,
params (ExpToken label, ExpToken op)[] cases )
: base( name, context )
{
_target = target;
if( @default == null ) {
_default = new List<ExpToken>();
} else {
_default = new List<ExpToken>( @default );
}
_cases = new Dictionary<ExpToken, List<ExpToken>>();
foreach( (ExpToken label, IEnumerable<ExpToken> op) item in cases ) {
_default = defaultCase;
_cases = new Dictionary<ExpToken, ExpToken>();
foreach( (ExpToken label, ExpToken op ) item in cases ) {
if( item.op == null ) {
_cases.Add( item.label, new List<ExpToken>() );
_cases.Add( item.label, null! );
} else {
_cases.Add( item.label, new List<ExpToken>( item.op ) );
_cases.Add( item.label, item.op );
}
}
}
public List<ExpToken> this[ ExpToken index ]
public ExpToken this[ ExpToken index ]
{
get
{
if( _cases.TryGetValue( index, out List<ExpToken>? ret ) ) {
if( _cases.TryGetValue( index, out ExpToken? ret ) ) {
return ret;
}
return _default;
}
}
public override ExpToken Left => throw new NotImplementedException();
public override ExpToken Next
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override ExpToken Right => throw new NotImplementedException();
public override IExpElementSource EqualityTypeConvert => throw new NotImplementedException(
);
public void AddOperationToDefault( ExpToken expressionRoot )
{
if( expressionRoot != null ) {
_default.AppendToken( expressionRoot );
}
}
/// <summary>
/// Adds an expression to certain <see langword="case" />. The added expression will be run
/// at last.
@@ -83,25 +99,18 @@ namespace StgSharp.Script.Express
/// <param name="label"> Label of case to add operation </param>
/// <param name="expressionRoot"> Root token of the expression to be added. </param>
/// <exception cref="ExpCaseNotFoundException"> Cannot fine the case label in current witch expression. </exception>
public void AddOperationToCase(
public void AppendOperationToCase(
ExpToken label,
ExpToken expressionRoot )
{
AssertSchemaInclude( expressionRoot );
if( _cases.TryGetValue( label, out List<ExpToken>? op ) ) {
op.Add( expressionRoot );
if( _cases.TryGetValue( label, out ExpToken? op ) ) {
op.AppendToken( expressionRoot );
} else {
throw new ExpCaseNotFoundException( this, label );
}
}
public void AddOperationToDefault( ExpToken expressionRoot )
{
if( expressionRoot != null ) {
_default.Add( expressionRoot );
}
}
public bool ContainsCase( ExpToken token )
{
return _cases.ContainsKey( token );
@@ -36,90 +36,66 @@ using System.Text;
namespace StgSharp.Script.Express
{
[Flags]
public enum ExpTokenFlag : long
{
None = 0,
Name_Element = 1,
Name_Param = 2,
Name_Block = 4,
Name_Keyword = 8,
Name_Any = Name_Block | Name_Element | Name_Keyword | Name_Param,
Collection_Set = 16,
Collection_Bag = 32,
Collection_Array = 64,
Collection_List = 128,
Collection_Any = Collection_Set | Collection_Bag | Collection_Array | Collection_List,
Element_Const = 256,
Element_Entity = 512,
Element_type = 1024,
Element_Rule = 2048,
Element_Function = 4096,
Element_Any = Element_Entity | Element_Const | Element_Function | Element_Rule | Element_type,
BuiltinType_Int = 8192,
BuiltinType_Float = 16384,
BuiltinType_String = 32768,
BuiltinType_Boolean = 65536,
BuiltinType_Binary = 0x2000,
BuiltinType_Logic = 0x4000,
BuiltinType_Any = BuiltinType_Int | BuiltinType_Float | BuiltinType_String | BuiltinType_Boolean | BuiltinType_Binary | BuiltinType_Logic,
Branch_If = 0x8000,
Branch_Switch = 0x1_0000,
Branch_Repeat = 0x2_0000,
}
public ref struct ExpTokenSource
{
public ReadOnlySpan<char> Value;
public readonly int Line;
public readonly int Column;
public ExpTokenSource(ReadOnlySpan<char> chars, int lineNumber, int columnNumber)
{
Value = chars; Line = lineNumber; Column = columnNumber;
}
}
public abstract class ExpToken : IExpressionToken
public abstract class ExpToken : IExpressionToken<ExpToken, IExpElementSource>
{
protected internal ExpToken _previousToken;
protected internal ExpTokenFlag _tokenType;
protected internal List<ExpToken> _nextLevelTokens
= new List<ExpToken>();
protected internal string _name;
protected ExpToken( string name, ExpSchema context )
{
_name = name;
Context = context;
NameSpace = context;
}
public ExpSchema Context
public ExpSchema NameSpace
{
get;
private set;
}
public abstract ExpToken Left
{
get;
}
public abstract ExpToken Next
{
get;
set;
}
public abstract ExpToken Right
{
get;
}
public abstract IExpElementSource EqualityTypeConvert
{
get;
}
public long TokenTypeCode => ( long )_tokenType;
public string Name => _name;
public virtual bool TryGetNextLevel(
out ICollection<IExpressionToken> nextLevel )
public virtual void AppendToken( ExpToken nextToken )
{
nextLevel = ( _nextLevelTokens as ICollection<IExpressionToken> )!;
return true;
Next = nextToken;
}
public ExpTokenEnumerator GetEnumerator()
{
return new ExpTokenEnumerator( this );
}
protected void AssertSchemaInclude( ExpToken expressionRoot )
{
if( expressionRoot.Context != Context && Context.HasSchemaInclude(
expressionRoot.Context ) ) {
if( expressionRoot.NameSpace != NameSpace && NameSpace.HasSchemaInclude(
expressionRoot.NameSpace ) ) {
throw new ExpInvalidSchemaIncludeException(
Context, expressionRoot.Context );
NameSpace, expressionRoot.NameSpace );
}
}
@@ -0,0 +1,78 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ExpTokenEnumerator.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
public class ExpTokenEnumerator : IEnumerator<ExpToken>
{
private ExpToken _begin;
private ExpToken _current;
public ExpTokenEnumerator( ExpToken token )
{
_begin = token;
_current = token;
}
void IDisposable.Dispose()
{
return;
}
bool IEnumerator.MoveNext()
{
ExpToken next = _current.Next;
if( next == null ) {
return false;
}
_current = next;
return true;
}
void IEnumerator.Reset()
{
_current = _begin;
}
object IEnumerator.Current => throw new NotImplementedException();
ExpToken IEnumerator<ExpToken>.Current => throw new NotImplementedException(
);
}
}
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Script.Express
{
[Flags]
public enum ExpTokenFlag : long
{
//string as name of non-builtin variables
None = 0,
Name_Element = 1,
Name_Param = 2,
Name_Block = 4,
Name_Keyword = 8,
Name_Operator = 16,
Name_Any = Name_Block | Name_Element | Name_Keyword | Name_Param | Name_Operator,
//any type of collections, also has NAME flag
Collection_Set = 32,
Collection_Bag = 64,
Collection_Array = 128,
Collection_List = 256,
Collection_Any = Collection_Set | Collection_Bag | Collection_Array | Collection_List,
//the variable is type of element, both builtin and non-builtin has this flag
Element_Const = 512,
Element_Entity = 1024,
Element_Type = 2048,
Element_Rule = 4096,
Element_Function = 8192,
Element_Any = Element_Entity | Element_Const | Element_Function | Element_Rule | Element_Type,
//variable is one of these builtin type
BuiltinType_Int = 16384,
BuiltinType_Float = 32768,
BuiltinType_String = 65536,
BuiltinType_Boolean = 0x2000,
BuiltinType_Binary = 0x4000,
BuiltinType_Logic = 0x8000,
BuiltinType_Number = BuiltinType_Float | BuiltinType_Int,
BuiltinType_Any = BuiltinType_Int | BuiltinType_Float | BuiltinType_String | BuiltinType_Boolean | BuiltinType_Binary | BuiltinType_Logic,
//the token is root of a branch
Branch_If = 0x1_000,
Branch_Switch = 0x2_0000,
Branch_Repeat = 0x4_0000,
Branch_Any = Branch_If | Branch_Repeat | Branch_Switch,
//the token is one of separators
Separator_Inline = 0x8_0000, //, or sth else
Separator_Line = 0x10_0000, //; or eth else
Separator_Block = 0x20_0000, //{}or sth else
Separator_Range = 0x40_0000, //()or sth else
Separator_Any = Separator_Inline | Separator_Line | Separator_Block | Separator_Range,
}
}