Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IXD] Compiler should generate also the enums including their values #180

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions src/AXSharp.compiler/src/ixd/Helpers/YamlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class YamlHelpers
{
public YamlHelpers()
{

}

// return all inherited members from class declaration
public string[] GetInheritedMembers(IClassDeclaration classDeclaration)
{
Expand All @@ -48,13 +48,29 @@ public bool CanBeFieldInherited(IFieldDeclaration field, ITypeDeclaration subCla
}

//acquire comments from source code
public Comments GetComments(Location location)
public Comments GetComments(Location location, bool isEnum = false)
{
string text = ((SourceLocation)location).SourceText.ToString();
int lineStart = location.GetFullLineSpan().StartLinePosition.Line;
var charStart = location.GetFullLineSpan().StartLinePosition.Character;

string commentsSection = "";
string[] lines = text.Split('\n');

if (isEnum)
{
if (lines[lineStart].Remove(0, charStart).Trim().Equals(","))
{
lineStart++;
while (lines[lineStart].Trim() == "" || lines[lineStart].Contains("///"))
{
lineStart++;
}
}
else if (!(string.IsNullOrEmpty(lines[lineStart].Remove(charStart).Trim())))
return new Comments();
}

for (int i = lineStart - 1; i >= 0 && (lines[i].Trim() == "" || lines[i].Contains("///")); i--)
{
if (lines[i].Trim() != "")
Expand Down Expand Up @@ -113,10 +129,11 @@ private string GetTextFromXml(XmlNode element, PropertyInfo? prop)
{
foreach (XmlNode node in element.ChildNodes)
{
if(node.Value != null)
if (node.Value != null)
{
text += node.Value;
} else
}
else
{
switch (node.Name)
{
Expand All @@ -136,7 +153,7 @@ private string GetTextFromXml(XmlNode element, PropertyInfo? prop)
}

text += GetTextFromXml(node, prop);

switch (node.Name)
{
case "code":
Expand All @@ -160,33 +177,33 @@ private string GetTextFromXml(XmlNode element, PropertyInfo? prop)
}

//creates parameter types for serialize purposes and creates declaration string
public (List<Parameter>,string) CreateParametersAndDeclarationString(IList<IVariableDeclaration> variableDeclarations, Comments comments)
{
public (List<Parameter>, string) CreateParametersAndDeclarationString(IList<IVariableDeclaration> variableDeclarations, Comments comments)
{
var inputParams = new List<Parameter>();
StringBuilder fullDeclaration = new StringBuilder();
foreach (var p in variableDeclarations)
{
var parameter = new Parameter
{
Id = p.Name,
Type = Helpers.GetBaseUid(p.Type)
};
Type = Helpers.GetBaseUid(p.Type)
};

string description;
comments.param.TryGetValue(p.Name, out description);
parameter.Description = description;

inputParams.Add(parameter);
fullDeclaration.Append($"in {parameter.Type} {parameter.Id}");
if(variableDeclarations.Last() != p)
{
if (variableDeclarations.Last() != p)
{
fullDeclaration.Append(",");
}
}

return(inputParams, fullDeclaration.ToString());
return (inputParams, fullDeclaration.ToString());
}

//create toc schema, grouped if namespace exists, or only global
public void AddToTocSchema(MyNodeVisitor visitor, TocSchema.Item tocSchemaItem, string? tocGroup)
{
Expand Down Expand Up @@ -237,7 +254,7 @@ public string GetAssembly(string projectFile)
return name.ToString();
}

//add references of inherited members
//add references of inherited members
public void AddReferences(string[] references, MyNodeVisitor v)
{
foreach (var member in references)
Expand All @@ -255,12 +272,12 @@ public void AddReferences(string[] references, MyNodeVisitor v)
}


//add references for namespace

//add references for namespace
public Reference CreateNamespaceReference(IFunctionDeclaration functionDeclaration)
{


return new Reference
{
Uid = Helpers.GetBaseUid(functionDeclaration),
Expand Down
27 changes: 27 additions & 0 deletions src/AXSharp.compiler/src/ixd/Interfaces/IYamlBuiderVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ public virtual void CreateNamedValueTypeYaml(
throw new NotImplementedException();
}

public virtual void CreateEnumTypeYaml(
IEnumTypeDeclaration enumTypeDeclaration,
MyNodeVisitor myNodeVisitor)
{
throw new NotImplementedException();
}

void CreateNamedValueYaml(
INamedValueDeclaration namedValueDeclaration,
MyNodeVisitor myNodeVisitor)
{
throw new NotImplementedException();
}

void CreateEnumValueYaml(
IEnumValueDeclaration enumValueDeclaration,
MyNodeVisitor myNodeVisitor)
{
throw new NotImplementedException();
}

public virtual void CreateInterfaceYaml(
IInterfaceDeclaration InterfaceDeclaration,
MyNodeVisitor visitor)
Expand All @@ -80,5 +101,11 @@ public virtual void CreateFunctionYaml(
throw new NotImplementedException();
}

void CreateStructuredTypeYaml(
IStructuredTypeDeclaration structuredTypeDeclaration,
MyNodeVisitor visitor)
{
throw new NotImplementedException();
}
}
}
62 changes: 61 additions & 1 deletion src/AXSharp.compiler/src/ixd/Mapper/CodeToYamlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,55 @@ public Item PopulateItem(IFunctionDeclaration functionDeclaration)

public Item PopulateItem(INamedValueTypeDeclaration namedValueTypeDeclaration)
{
var values = namedValueTypeDeclaration.Values.Select(p => Helpers.Helpers.GetBaseUid(p));

var item = PopulateItem((IDeclaration)namedValueTypeDeclaration);
item.Parent = Helpers.Helpers.GetBaseUid(namedValueTypeDeclaration.ContainingNamespace.FullyQualifiedName);
item.Children = values.ToList();
item.Type = "Enum";
item.Syntax = new Syntax { Content = $"{namedValueTypeDeclaration.Name} : {namedValueTypeDeclaration.Type.FullyQualifiedName}" };
item.Syntax = new Syntax { Content = $"{namedValueTypeDeclaration.Name} : {namedValueTypeDeclaration.Values.FirstOrDefault().Type.FullyQualifiedName}" };

return item;
}

public Item PopulateItem(IEnumTypeDeclaration enumTypeDeclaration)
{
var values = enumTypeDeclaration.Values.Select(p => Helpers.Helpers.GetBaseUid(p));

var item = PopulateItem((IDeclaration)enumTypeDeclaration);
item.Parent = Helpers.Helpers.GetBaseUid(enumTypeDeclaration.ContainingNamespace.FullyQualifiedName);
item.Children = values.ToList();
item.Type = "Enum";
item.Syntax = new Syntax { Content = $"{enumTypeDeclaration.Name}" };

return item;
}

public Item PopulateItem(INamedValueDeclaration namedValueDeclaration)
{
string text = ((SourceLocation)namedValueDeclaration.ValueExpression.Location).SourceText.ToString();
string[] lines = text.Split('\n');

var line = lines[namedValueDeclaration.ValueExpression.Location.GetFullLineSpan().StartLinePosition.Line];
var startChar = namedValueDeclaration.ValueExpression.Location.GetFullLineSpan().StartLinePosition.Character;
var endChar = namedValueDeclaration.ValueExpression.Location.GetFullLineSpan().EndLinePosition.Character;
var value = line.Substring(startChar, endChar - startChar);

var item = PopulateItem((IDeclaration)namedValueDeclaration);
item.Name = $"{namedValueDeclaration.Name} := {value}";
item.Parent = namedValueDeclaration.ContainingNamespace.FullyQualifiedName;
item.Type = "Field";
item.Summary = _yh.GetComments(namedValueDeclaration.Location, true).summary;

return item;
}

public Item PopulateItem(IEnumValueDeclaration enumValueDeclaration)
{
var item = PopulateItem((IDeclaration)enumValueDeclaration);
item.Parent = enumValueDeclaration.ContainingNamespace.FullyQualifiedName;
item.Type = "Field";
item.Summary = _yh.GetComments(enumValueDeclaration.Location, true).summary;

return item;
}
Expand Down Expand Up @@ -200,6 +245,21 @@ public Item PopulateItem(IMethodPrototypeDeclaration methodPrototypeDeclaration)
}
};

return item;
}

public Item PopulateItem(IStructuredTypeDeclaration structDeclaration)
{

var children = structDeclaration.Fields.Select(p => Helpers.Helpers.GetBaseUid(p));

var item = PopulateItem((IDeclaration)structDeclaration);
item.Parent = Helpers.Helpers.GetBaseUid(structDeclaration.ContainingNamespace.FullyQualifiedName);
item.Children = children.ToList();
item.Type = "Struct";
item.Syntax = new Syntax { Content = $"STRUCT {structDeclaration.Name}" };


return item;
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/AXSharp.compiler/src/ixd/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
"profiles": {
"ixd": {
"commandName": "Project",
"commandLineArgs": "ixd -x C:\\MTS\\ix-ax\\ix\\src\\AXSharp.compiler\\src\\ixd\\tests\\samples\\ax\\ -o C:\\MTS\\ix-ax\\ix\\src\\AXSharp.compiler\\src\\ixd\\tests\\docfx\\docfx_project\\api\\"
"commandLineArgs": "ixd -x C:\\MTS\\ix\\axsharp\\src\\AXSharp.compiler\\src\\ixd\\tests\\samples\\ax\\ -o C:\\MTS\\ix\\axsharp\\src\\AXSharp.compiler\\src\\ixd\\tests\\docfx\\docfx_project\\api\\"
},
"ixd-2": {
"commandName": "Project",
"commandLineArgs": "ixd-2 -x C:\\MTS\\AXSharp.framework\\src\\core\\ctrl -o C:\\MTS\\AXSharp.framework\\docfx\\apictrl"
},
"ixd-3": {
"commandName": "Project",
"commandLineArgs": "-x C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\messaging\\ctrl C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\abstractions\\ctrl C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\data\\ctrl C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\core\\ctrl -o C:\\W\\Develop\\gh\\ix-ax\\axopen\\docfx\\apictrl\\",
"workingDirectory": "C:\\W\\Develop\\gh\\ix-ax\\axopen\\src\\data\\ctrl"
"commandLineArgs": "-x C:\\MTS\\ix\\AXOpen\\src\\messaging\\ctrl C:\\MTS\\ix\\AXOpen\\src\\abstractions\\ctrl C:\\MTS\\ix\\AXOpen\\src\\data\\ctrl C:\\MTS\\ix\\AXOpen\\src\\core\\ctrl -o C:\\MTS\\ix\\AXOpen\\docfx\\apictrl\\",
"workingDirectory": "C:\\MTS\\ix\\AXOpen\\src\\data\\ctrl"
},
"ixd-david": {
"commandName": "Project",
"commandLineArgs": "ixd -x D:\\Inxton\\ax-framework\\axsharp\\src\\AXSharp.compiler\\src\\ixd\\tests\\samples\\ax\\ -o D:\\Inxton\\ax-framework\\axsharp\\src\\AXSharp.compiler\\src\\ixd\\tests\\docfx\\docfx_project\\api\\"
}
}
}
4 changes: 4 additions & 0 deletions src/AXSharp.compiler/src/ixd/Visitors/MyNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void Visit(IScalarTypeDeclaration scalarType, IYamlBuiderVisitor data)

public void Visit(IStructuredTypeDeclaration structuredTypeDeclaration, IYamlBuiderVisitor data)
{
data.CreateStructuredTypeYaml(structuredTypeDeclaration, this);
}

public void Visit(IArrayTypeDeclaration arrayTypeDeclaration, IYamlBuiderVisitor data)
Expand All @@ -131,6 +132,7 @@ public void Visit(IArrayTypeDeclaration arrayTypeDeclaration, IYamlBuiderVisitor

public void Visit(IEnumTypeDeclaration enumTypeDeclaration, IYamlBuiderVisitor data)
{
data.CreateEnumTypeYaml(enumTypeDeclaration, this);
}

public void Visit(INamedValueTypeDeclaration namedValueTypeDeclaration, IYamlBuiderVisitor data)
Expand Down Expand Up @@ -161,10 +163,12 @@ public void Visit(IVariableDeclaration variableDeclaration, IYamlBuiderVisitor d

public void Visit(IEnumValueDeclaration enumValueDeclaration, IYamlBuiderVisitor data)
{
data.CreateEnumValueYaml(enumValueDeclaration, this);
}

public void Visit(INamedValueDeclaration namedValueDeclaration, IYamlBuiderVisitor data)
{
data.CreateNamedValueYaml(namedValueDeclaration, this);
}

public void Visit(ISemanticInitializerExpression initializerExpression, IYamlBuiderVisitor data)
Expand Down
73 changes: 72 additions & 1 deletion src/AXSharp.compiler/src/ixd/Visitors/YamlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public virtual void CreateNamedValueTypeYaml(INamedValueTypeDeclaration namedVal

_yh.AddToTocSchema(visitor, tocSchemaItem, item.Namespace);

namedValueTypeDeclaration.ChildNodes.ToList().ForEach(p => p.Accept(visitor, this));

//map helpers list to schema lists
visitor.MapYamlHelperToSchema();

Expand All @@ -164,6 +166,42 @@ public virtual void CreateNamedValueTypeYaml(INamedValueTypeDeclaration namedVal
visitor.YamlHelper.References.Clear();
}

public virtual void CreateEnumTypeYaml(IEnumTypeDeclaration enumTypeDeclaration, MyNodeVisitor visitor)
{
var item = _mp.PopulateItem(enumTypeDeclaration);
visitor.YamlHelper.Items.Add(item);

var tocSchemaItem = new TocSchema.Item(item.Uid, item.FullName);

_yh.AddToTocSchema(visitor, tocSchemaItem, item.Namespace);

enumTypeDeclaration.ChildNodes.ToList().ForEach(p => p.Accept(visitor, this));

//map helpers list to schema lists
visitor.MapYamlHelperToSchema();

//serialize schema to yaml
_s.SchemaToYaml(visitor.YamlHelper.Schema, Helpers.Helpers.GetBaseUid(enumTypeDeclaration));
//clear schema for next use
visitor.YamlHelper.Schema = new YamlSchema();
visitor.YamlHelper.Items.Clear();
visitor.YamlHelper.References.Clear();
}

public virtual void CreateNamedValueYaml(INamedValueDeclaration namedValueDeclaration, MyNodeVisitor visitor)
{
var item = _mp.PopulateItem(namedValueDeclaration);
visitor.YamlHelper.Items.Add(item);
_yh.AddReference(namedValueDeclaration.Type, visitor);
}

public virtual void CreateEnumValueYaml(IEnumValueDeclaration enumValueDeclaration, MyNodeVisitor visitor)
{
var item = _mp.PopulateItem(enumValueDeclaration);
visitor.YamlHelper.Items.Add(item);
_yh.AddReference(enumValueDeclaration.Type, visitor);
}

public virtual void CreateInterfaceYaml(IInterfaceDeclaration interfaceDeclaration, MyNodeVisitor v)
{
var item = _mp.PopulateItem(interfaceDeclaration);
Expand Down Expand Up @@ -275,6 +313,39 @@ private void UpdateNamespaceChildrenAndReferences(INamespaceDeclaration namespac
wrapper.NamespaceItem.Children.AddRange(wrapper.NamespaceTemporaryChildren);
}


public virtual void CreateStructuredTypeYaml(IStructuredTypeDeclaration structuredTypeDeclaration, MyNodeVisitor v)
{
var item = _mp.PopulateItem(structuredTypeDeclaration);

//add to items
v.YamlHelper.Items.Add(item);

var tocSchemaItem = new TocSchema.Item(item.Uid, item.FullName);

if (item.Namespace != "$GLOBAL")
{
//class is grouped in namespace
_yh.AddToTocSchema(v, tocSchemaItem, item.Namespace);
}
else
{
// if global, class is not grouped
_yh.AddToTocSchema(v, tocSchemaItem, null);
}

structuredTypeDeclaration.ChildNodes.ToList().ForEach(p => p.Accept(v, this));

//map helpers list to schema lists
v.MapYamlHelperToSchema();

//serialize schema to yaml
_s.SchemaToYaml(v.YamlHelper.Schema, Helpers.Helpers.GetBaseUid(structuredTypeDeclaration));
//clear schema for next use
v.YamlHelper.Schema = new YamlSchema();
v.YamlHelper.Items.Clear();
v.YamlHelper.References.Clear();
}


}
}
Loading