-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from vein-lang/feature/type-aliases
Type Aliases
- Loading branch information
Showing
20 changed files
with
450 additions
and
267 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace vein.syntax; | ||
|
||
using Sprache; | ||
|
||
public partial class VeinSyntax | ||
{ | ||
internal virtual Parser<AliasSyntax> AliasDeclaration => | ||
from global in KeywordExpression("global").Token().Optional() | ||
from keyword in KeywordExpression("alias").Token() | ||
from aliasName in IdentifierExpression.Token() | ||
from s in Parse.String("<|").Token() | ||
from body in MethodParametersAndBody.Token().Select(x => new TypeOrMethod(null, x)) | ||
.Or(TypeExpression.Token().Then(_ => Parse.Char(';').Token().Return(_)).Select(x => new TypeOrMethod(x, null))) | ||
select new AliasSyntax(global.IsDefined, aliasName, body); | ||
} |
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,27 +1,26 @@ | ||
namespace vein.syntax | ||
{ | ||
using Sprache; | ||
namespace vein.syntax; | ||
|
||
using Sprache; | ||
|
||
public partial class VeinSyntax | ||
{ | ||
internal virtual Parser<DirectiveType> DirectiveDeclarator(DirectiveType type) => | ||
from start in Parse.Char('#') | ||
from keyword in Parse.String(type.ToString().ToLowerInvariant()) | ||
select type; | ||
public partial class VeinSyntax | ||
{ | ||
internal virtual Parser<DirectiveType> DirectiveDeclarator(DirectiveType type) => | ||
from start in Parse.Char('#') | ||
from keyword in Parse.String(type.ToString().ToLowerInvariant()) | ||
select type; | ||
|
||
internal virtual Parser<DirectiveSyntax> UseSyntax => | ||
(from start in DirectiveDeclarator(DirectiveType.Use) | ||
from str in StringLiteralExpression.Token() | ||
select new UseSyntax | ||
{ | ||
Value = str | ||
}).Token().Named("use directive").Positioned(); | ||
internal virtual Parser<DirectiveSyntax> SpaceSyntax => | ||
(from start in DirectiveDeclarator(DirectiveType.Space) | ||
from str in StringLiteralExpression.Token() | ||
select new SpaceSyntax | ||
{ | ||
Value = str | ||
}).Token().Named("space directive").Positioned(); | ||
} | ||
internal virtual Parser<DirectiveSyntax> UseSyntax => | ||
(from start in DirectiveDeclarator(DirectiveType.Use) | ||
from str in StringLiteralExpression.Token() | ||
select new UseSyntax | ||
{ | ||
Value = str | ||
}).Token().Named("use directive").Positioned(); | ||
internal virtual Parser<DirectiveSyntax> SpaceSyntax => | ||
(from start in DirectiveDeclarator(DirectiveType.Space) | ||
from str in StringLiteralExpression.Token() | ||
select new SpaceSyntax | ||
{ | ||
Value = str | ||
}).Token().Named("space directive").Positioned(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace vein.syntax; | ||
|
||
using Sprache; | ||
|
||
|
||
public record TypeOrMethod(TypeExpression type, MethodDeclarationSyntax method); | ||
|
||
public class AliasSyntax(bool isGlobal, IdentifierExpression aliasName, TypeOrMethod body) : BaseSyntax, IPositionAware<AliasSyntax> | ||
{ | ||
public bool IsGlobal { get; } = isGlobal; | ||
public IdentifierExpression AliasName { get; } = aliasName; | ||
public TypeExpression? Type { get; } = body.type; | ||
public MethodDeclarationSyntax? MethodDeclaration { get; } = body.method; | ||
|
||
public bool IsMethod => MethodDeclaration != null; | ||
public bool IsType => Type != null; | ||
|
||
public override SyntaxType Kind => SyntaxType.Alias; | ||
public override IEnumerable<BaseSyntax> ChildNodes | ||
=> GetNodes([AliasName, Type, MethodDeclaration]); | ||
|
||
public AliasSyntax SetPos(Position startPos, int length) | ||
{ | ||
base.SetPos(startPos, length); | ||
return this; | ||
} | ||
} |
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.