-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Аннотации как значения параметров аннотаций.
- Loading branch information
Showing
8 changed files
with
242 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/*---------------------------------------------------------- | ||
This Source Code Form is subject to the terms of the | ||
Mozilla Public License, v.2.0. If a copy of the MPL | ||
was not distributed with this file, You can obtain one | ||
at http://mozilla.org/MPL/2.0/. | ||
----------------------------------------------------------*/ | ||
using System; | ||
using System.Text; | ||
|
||
namespace ScriptEngine.Machine | ||
{ | ||
[Serializable] | ||
public struct AnnotationDefinition | ||
{ | ||
public string Name; | ||
public AnnotationParameter[] Parameters; | ||
|
||
public int ParamCount => Parameters?.Length ?? 0; | ||
|
||
public string ToConstValue() | ||
{ | ||
var builder = new StringBuilder("&"); | ||
builder.Append(Name); | ||
builder.Append("("); | ||
|
||
foreach (var parameter in Parameters) | ||
{ | ||
builder.Append(parameter); | ||
} | ||
builder.Append(")"); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
/// <summary> | ||
/// Разбирает объект из сериализованного представления значения аннотации. | ||
/// </summary> | ||
/// <param name="constValuePresentation">Сериализованная аннотация. Строка вида `&ИмяАннотации(ИмяПараметра=ЗначениеПараметра,...)`</param> | ||
/// <returns></returns> | ||
public static AnnotationDefinition FromConstValue(string constValuePresentation) | ||
{ | ||
var result = new AnnotationDefinition(); | ||
|
||
var nameEnd = constValuePresentation.IndexOf("(", StringComparison.Ordinal); | ||
const int prefixLength = 1; // Длина строки & | ||
result.Name = constValuePresentation.Substring(prefixLength, nameEnd - prefixLength); | ||
|
||
var paramsEnd = constValuePresentation.LastIndexOf(")", StringComparison.Ordinal); | ||
var paramsPresentation = constValuePresentation.Substring(nameEnd + 1, paramsEnd - nameEnd - 1); | ||
|
||
var paramsSplitted = paramsPresentation.Split(','); | ||
result.Parameters = new AnnotationParameter[paramsSplitted.Length]; | ||
for (var i = 0; i < paramsSplitted.Length; i++) | ||
{ | ||
result.Parameters[i] = AnnotationParameter.FromString(paramsSplitted[i]); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*---------------------------------------------------------- | ||
This Source Code Form is subject to the terms of the | ||
Mozilla Public License, v.2.0. If a copy of the MPL | ||
was not distributed with this file, You can obtain one | ||
at http://mozilla.org/MPL/2.0/. | ||
----------------------------------------------------------*/ | ||
|
||
using System; | ||
|
||
namespace ScriptEngine.Machine | ||
{ | ||
[Serializable] | ||
public struct AnnotationParameter | ||
{ | ||
public string Name; | ||
public int ValueIndex; | ||
|
||
[NonSerialized] | ||
public IValue RuntimeValue; | ||
|
||
public const int UNDEFINED_VALUE_INDEX = -1; | ||
|
||
public override string ToString() | ||
{ | ||
if (string.IsNullOrEmpty(Name)) | ||
{ | ||
return $"[{ValueIndex}]"; | ||
} | ||
if (ValueIndex == UNDEFINED_VALUE_INDEX) | ||
{ | ||
return Name; | ||
} | ||
return $"{Name}=[{ValueIndex}]"; | ||
} | ||
|
||
public static AnnotationParameter FromString(string presentation) | ||
{ | ||
var result = new AnnotationParameter(); | ||
var parts = presentation.Split(','); | ||
if (parts.Length == 1) | ||
{ | ||
if (parts[0].StartsWith("[")) | ||
{ | ||
result.ValueIndex = AnnotationValueConstIndex(parts[0]); | ||
} | ||
else | ||
{ | ||
result.Name = parts[0]; | ||
} | ||
} | ||
else | ||
{ | ||
result.Name = parts[0]; | ||
result.ValueIndex = AnnotationValueConstIndex(parts[1]); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static int AnnotationValueConstIndex(string valuePresentation) | ||
{ | ||
// убираем [] по краям | ||
var intPresentation = valuePresentation.Substring(1, valuePresentation.Length - 2); | ||
return int.Parse(intPresentation); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/ScriptEngine/Machine/InternalAnnotationDefinitionWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/*---------------------------------------------------------- | ||
This Source Code Form is subject to the terms of the | ||
Mozilla Public License, v.2.0. If a copy of the MPL | ||
was not distributed with this file, You can obtain one | ||
at http://mozilla.org/MPL/2.0/. | ||
----------------------------------------------------------*/ | ||
|
||
using ScriptEngine.Machine.Contexts; | ||
|
||
namespace ScriptEngine.Machine | ||
{ | ||
[ContextClass("ОпределениеАннотацииИсходногоКода", "AnnotationDefinitionWrapper")] | ||
public class InternalAnnotationDefinitionWrapper: AutoContext<InternalAnnotationDefinitionWrapper> | ||
{ | ||
public InternalAnnotationDefinitionWrapper(AnnotationDefinition annotationDefinition) | ||
{ | ||
AnnotationDefinition = annotationDefinition; | ||
} | ||
|
||
public AnnotationDefinition AnnotationDefinition { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters