-
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.
Showing
9 changed files
with
173 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,59 @@ | ||
using Fiona.IDE.Tokenizer; | ||
|
||
namespace Fiona.IDE.ProjectManager.Models; | ||
|
||
public sealed class Class | ||
{ | ||
public IReadOnlyCollection<Dependency> Dependencies => _dependencies.AsReadOnly(); | ||
|
||
private List<Dependency> _dependencies = new(); | ||
public string Namespace => _namespace; | ||
public string Route => _route; | ||
public IReadOnlyCollection<Endpoint> Endpoints => _endpoints.AsReadOnly(); | ||
public IReadOnlyCollection<string> Usings => _usings.AsReadOnly(); | ||
public string Name => _name; | ||
|
||
private List<Dependency> _dependencies; | ||
private string _route; | ||
private List<Endpoint> _endpoints = new(); | ||
private List<Endpoint> _endpoints; | ||
private List<string> _usings; | ||
private string _name; | ||
private string _namespace; | ||
|
||
private Class(string name, string @namespace, List<Dependency> dependencies, string route, List<Endpoint> endpoints, List<string> usings) | ||
{ | ||
_name = name; | ||
_dependencies = dependencies; | ||
_route = route; | ||
_endpoints = endpoints; | ||
_usings = usings; | ||
_namespace = @namespace; | ||
} | ||
|
||
public static async Task<Class> Load(string path) | ||
{ | ||
await using FileStream file = File.Open(path, FileMode.Open); | ||
using StreamReader reader = new(file); | ||
IReadOnlyCollection<IToken> tokens = await Tokenizer.Tokenizer.GetTokensAsync(reader); | ||
|
||
List<Dependency> dependencies = []; | ||
List<Endpoint> endpoints = []; | ||
|
||
string @namespace = tokens.GetNamespaceToken()?.Value!; | ||
(List<IToken> usings, int indexOfUsingEndToken) = tokens.GetUsingTokens(); | ||
(IToken classToken, int indexOfClassToken) = tokens.GetClassToken(indexOfUsingEndToken); | ||
(IToken? classDependency, int indexOfDependencyToken) = tokens.GetClassDependency(indexOfClassToken); | ||
(IToken? classRoute, int indexOfClassRouteToken) = tokens.GetClassRoute(indexOfClassToken); | ||
int indexOfStartEndpointsSearch = indexOfClassToken; | ||
if (classDependency is not null) | ||
{ | ||
dependencies = Dependency.GetDependenciesFromToken(classDependency); | ||
indexOfStartEndpointsSearch = indexOfDependencyToken; | ||
} | ||
if(classRoute is not null && indexOfClassRouteToken > indexOfStartEndpointsSearch) | ||
{ | ||
indexOfStartEndpointsSearch = indexOfClassRouteToken; | ||
} | ||
|
||
|
||
return new Class(classToken.Value!, @namespace, dependencies, classRoute?.Value ?? string.Empty, endpoints, usings.Select(x => x.Value!).ToList()); | ||
} | ||
} |
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,8 +1,27 @@ | ||
using Fiona.IDE.Tokenizer; | ||
|
||
namespace Fiona.IDE.ProjectManager.Models; | ||
|
||
public sealed class Dependency(string name, string type) | ||
{ | ||
public string Name { get; } = name; | ||
public string Type { get; } = type; | ||
|
||
|
||
public static List<Dependency> GetDependenciesFromToken(IToken token) | ||
{ | ||
List<Dependency> result = []; | ||
foreach (string dependency in token.ArrayOfValues ?? []) | ||
{ | ||
(string name, string type) = dependency.Split(":") switch | ||
{ | ||
{ Length: 2 } array => (array[0], array[1]), | ||
_ => throw new Exception("Invalid dependency declaration") | ||
}; | ||
|
||
result.Add(new Dependency(name, type)); | ||
} | ||
|
||
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
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 |
---|---|---|
|
@@ -22,4 +22,4 @@ public Token(TokenType type, params string[] arrayOfValue) | |
ArrayOfValues = arrayOfValue; | ||
Type = type; | ||
} | ||
} | ||
} |
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,82 @@ | ||
namespace Fiona.IDE.Tokenizer; | ||
|
||
public static class TokenListHelper | ||
{ | ||
public static (List<IToken> tokens, int endSearchIndex) GetUsingTokens(this IReadOnlyCollection<IToken> tokens, int startSearchIndex = 0) | ||
{ | ||
List<IToken> result = []; | ||
bool isUsingPart = false; | ||
for (int i = startSearchIndex; i < tokens.Count; i++) | ||
{ | ||
IToken currentToken = tokens.ElementAt(i); | ||
switch (currentToken.Type) | ||
{ | ||
case TokenType.UsingBegin: | ||
isUsingPart = true; | ||
break; | ||
case TokenType.UsingEnd: | ||
isUsingPart = false; | ||
break; | ||
case TokenType.Using: | ||
if (isUsingPart) | ||
{ | ||
result.Add(currentToken); | ||
} | ||
return (result, i); | ||
case TokenType.Comment: | ||
continue; | ||
} | ||
} | ||
|
||
return (result, tokens.Count); | ||
} | ||
|
||
public static IToken? GetNamespaceToken(this IReadOnlyCollection<IToken> tokens) | ||
=> tokens.FirstOrDefault(x => x.Type == TokenType.Namespace); | ||
|
||
public static (IToken classToken, int findIndex) GetClassToken(this IReadOnlyCollection<IToken> tokens, int startSearchIndex = 0) | ||
{ | ||
for (int i = startSearchIndex; i < tokens.Count; i++) | ||
{ | ||
IToken currentToken = tokens.ElementAt(i); | ||
if (currentToken.Type == TokenType.Class) | ||
{ | ||
return (currentToken, i); | ||
} | ||
} | ||
throw new Exception("class token not found"); | ||
} | ||
|
||
public static (IToken? token, int findIndex) GetClassDependency(this IReadOnlyCollection<IToken> tokens, int startSearchIndex) | ||
{ | ||
for (int i = startSearchIndex; i < tokens.Count; i++) | ||
{ | ||
IToken currentToken = tokens.ElementAt(i); | ||
if (currentToken.Type == TokenType.Dependency) | ||
{ | ||
return (currentToken, i); | ||
} | ||
} | ||
|
||
return (null, tokens.Count); | ||
} | ||
|
||
public static (IToken? token, int findIndex) GetClassRoute(this IReadOnlyCollection<IToken> tokens, int startSearchIndex) | ||
{ | ||
for (int i = startSearchIndex; i < tokens.Count; i++) | ||
{ | ||
IToken currentToken = tokens.ElementAt(i); | ||
if (currentToken.Type == TokenType.Route) | ||
{ | ||
return (currentToken, i); | ||
} | ||
// it's mean class doesn't have route define | ||
if (currentToken.Type == TokenType.Endpoint) | ||
{ | ||
return (null, tokens.Count); | ||
} | ||
} | ||
|
||
return (null, tokens.Count); | ||
} | ||
} |
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