-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
5 changed files
with
248 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
namespace Fabulous.AST | ||
|
||
open System.Runtime.CompilerServices | ||
open Fantomas.FCS.Text | ||
open Fabulous.AST.StackAllocatedCollections | ||
open Fantomas.Core.SyntaxOak | ||
open Fabulous.AST.StackAllocatedCollections.StackList | ||
open Microsoft.FSharp.Collections | ||
|
||
module ClassEnd = | ||
let Name = Attributes.defineWidget "Name" | ||
let Parameters = Attributes.defineScalar<SimplePatNode list> "Parameters" | ||
let Members = Attributes.defineWidgetCollection "Members" | ||
let MultipleAttributes = Attributes.defineScalar<string list> "MultipleAttributes" | ||
|
||
let WidgetKey = | ||
Widgets.register "ClassEnd" (fun widget -> | ||
let name = Helpers.getNodeFromWidget<SingleTextNode> widget Name | ||
let parameters = Helpers.tryGetScalarValue widget Parameters | ||
let attributes = Helpers.tryGetScalarValue widget MultipleAttributes | ||
|
||
let multipleAttributes = | ||
match attributes with | ||
| ValueSome values -> MultipleAttributeListNode.Create values |> Some | ||
| ValueNone -> None | ||
|
||
let implicitConstructor = | ||
match parameters with | ||
| ValueNone -> None | ||
| ValueSome(parameters) when parameters.IsEmpty -> | ||
Some( | ||
ImplicitConstructorNode( | ||
None, | ||
None, | ||
None, | ||
SingleTextNode.leftParenthesis, | ||
[], | ||
SingleTextNode.rightParenthesis, | ||
None, | ||
Range.Zero | ||
) | ||
) | ||
| ValueSome(simplePatNodes) -> | ||
let simplePats = | ||
match simplePatNodes with | ||
| [] -> [] | ||
| head :: tail -> | ||
[ yield Choice1Of2 head | ||
for p in tail do | ||
yield Choice2Of2 SingleTextNode.comma | ||
yield Choice1Of2 p ] | ||
|
||
Some( | ||
ImplicitConstructorNode( | ||
None, | ||
None, | ||
None, | ||
SingleTextNode.leftParenthesis, | ||
simplePats, | ||
SingleTextNode.rightParenthesis, | ||
None, | ||
Range.Zero | ||
) | ||
) | ||
|
||
TypeDefnExplicitNode( | ||
TypeNameNode( | ||
None, | ||
multipleAttributes, | ||
SingleTextNode.``type``, | ||
Some(name), | ||
IdentListNode([], Range.Zero), | ||
None, | ||
[], | ||
implicitConstructor, | ||
Some(SingleTextNode.equals), | ||
None, | ||
Range.Zero | ||
), | ||
TypeDefnExplicitBodyNode(SingleTextNode.``class``, [], SingleTextNode.``end``, Range.Zero), | ||
[], | ||
Range.Zero | ||
)) | ||
|
||
[<AutoOpen>] | ||
module ClassEndBuilders = | ||
type Ast with | ||
|
||
static member inline ClassEnd(name: WidgetBuilder<#SingleTextNode>) = | ||
WidgetBuilder<TypeDefnExplicitNode>( | ||
ClassEnd.WidgetKey, | ||
AttributesBundle(StackList.empty(), ValueSome [| ClassEnd.Name.WithValue(name.Compile()) |], ValueNone) | ||
) | ||
|
||
static member inline ClassEnd(node: SingleTextNode) = Ast.ClassEnd(Ast.EscapeHatch(node)) | ||
|
||
static member inline ClassEnd(name: string) = | ||
Ast.ClassEnd(SingleTextNode.Create(name)) | ||
|
||
[<Extension>] | ||
type ClassEndModifiers = | ||
[<Extension>] | ||
static member inline isStruct(this: WidgetBuilder<TypeDefnExplicitNode>) = | ||
this.AddScalar(ClassEnd.MultipleAttributes.WithValue([ "Struct" ])) | ||
|
||
[<Extension>] | ||
static member inline attributes(this: WidgetBuilder<TypeDefnExplicitNode>, attributes: string list) = | ||
this.AddScalar(ClassEnd.MultipleAttributes.WithValue(attributes)) | ||
|
||
[<Extension>] | ||
static member inline implicitConstructorParameters | ||
( | ||
this: WidgetBuilder<TypeDefnExplicitNode>, | ||
parameters: SimplePatNode list | ||
) = | ||
this.AddScalar(ClassEnd.Parameters.WithValue(parameters)) | ||
|
||
[<Extension>] | ||
type ClassEndYieldExtensions = | ||
[<Extension>] | ||
static member inline Yield | ||
( | ||
_: CollectionBuilder<'parent, ModuleDecl>, | ||
x: WidgetBuilder<TypeDefnExplicitNode> | ||
) : CollectionContent = | ||
let node = Tree.compile x | ||
let typeDefn = TypeDefn.Explicit(node) | ||
let typeDefn = ModuleDecl.TypeDefn(typeDefn) | ||
let widget = Ast.EscapeHatch(typeDefn).Compile() | ||
{ Widgets = MutStackArray1.One(widget) } | ||
|
||
[<Extension>] | ||
static member inline Yield | ||
( | ||
_: CollectionBuilder<TypeDefnExplicitNode, MemberDefn>, | ||
x: MemberDefn | ||
) : CollectionContent = | ||
let widget = Ast.EscapeHatch(x).Compile() | ||
{ Widgets = MutStackArray1.One(widget) } |