-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More proper implementation of 'leafref'
- Loading branch information
1 parent
10cc7aa
commit 6bdae8a
Showing
11 changed files
with
255 additions
and
71 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
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 |
---|---|---|
@@ -1,18 +1,54 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace YangParser.SemanticModel.Builtins; | ||
|
||
public class LeafReference() : BuiltinType("leafref", (statement) => | ||
{ | ||
var path = (Path)statement.Children.First(c => c is Path); | ||
var name = BuiltinTypeReference.TypeName(statement); | ||
var definition = $$""" | ||
{{statement.DescriptionString}}{{statement.AttributeString}} | ||
public class {{name}}() : InstanceIdentifier("{{path.Argument}}") | ||
{ | ||
public new static {{name}} Parse(string value) => new(); | ||
} | ||
"""; | ||
return (name, definition); | ||
var target = statement.Parent.Navigate(path.Argument); | ||
var prefix = path.Argument.Split('/').Last().Prefix(out _); | ||
var assignation = prefix.Contains('.') ? prefix : prefix + ":"; | ||
if (statement.FindSourceFor(prefix) == target.GetModule()) | ||
{ | ||
prefix = string.Empty; | ||
assignation = string.Empty; | ||
} | ||
|
||
var type = target!.GetChild<Type>(); | ||
var bname = target switch | ||
{ | ||
IXMLReadValue rw => rw.ClassName, | ||
IXMLParseable ps => ps.ClassName, | ||
_ => "string" | ||
}; | ||
if (bname.Contains(":") || bname.Contains(".")) | ||
{ | ||
var p = bname.Prefix(out _); | ||
if (bname.Contains(":") && !statement.GetModule()!.Usings.ContainsKey(p)) | ||
{ | ||
statement.GetModule()!.Usings[p] = target.GetModule()!.Usings[p]; | ||
} | ||
|
||
return (bname, null); | ||
} | ||
|
||
if (bname == "string") return (bname, null); | ||
if (type.Definition is null && !BuiltinTypeReference.IsBuiltinKeyword(type.Argument)) | ||
{ | ||
return (target.ModuleQualifiedClassName(), null); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(prefix) && !BuiltinTypeReference.IsBuiltinKeyword(type.Argument)) | ||
{ | ||
return (target.FullyQualifiedClassName(), null); | ||
} | ||
|
||
if (BuiltinTypeReference.IsBuiltin(type, out var name, out var def)) | ||
{ | ||
return def is null ? (name!, null) : (target.FullyQualifiedClassName(), null); | ||
} | ||
|
||
return (assignation + bname, null); | ||
}); |
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
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
Oops, something went wrong.