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

[Proposal] Quotations (Not Macros) #8065

Closed
AdamSpeight2008 opened this issue Jan 21, 2016 · 6 comments
Closed

[Proposal] Quotations (Not Macros) #8065

AdamSpeight2008 opened this issue Jan 21, 2016 · 6 comments

Comments

@AdamSpeight2008
Copy link
Contributor

Looking the through the issues, it seem that we could benefit from having some mechanism to quote code (aka Quotations).

template ComplilationUnit foo ( SymbolContext context ) 
{
  partial class <#= context.Symbol.name #>
  {
    public const string ClassName = "<#= context.Symbol.Name #>" ; 
  }
}

Everything within the { ... } of the template is considered text, except if it a parameter hole <#= #>

Not Macros

Within the parameter holes, it contents can one refer to parameters of the template. The surrounding context does not exist, each exist independent of each other. Scope is restriction to only parameters of the template.


The target return type (in above example CompilationUnit) could be used to help validate the body on the template, within the IDE.

During compilation, invocation of a template would produce an instance of Template

class Template()
{
  TemplateParameters Parameters;
}
class Template(Of T)() : Template
{
  TemplateTarget(Of T) rT ;
}

.ToString would produce a string version of the body of the template (quotation). Just as if it was just a interpolation string.


Similar to String Interpolation the argument supplied in a parameter hole would be checked to see if it is in scope. Scope is restriction to only parameters of the template. If it isn't valid then it is compile-time error.

The Compilers could have factory method similar to:=
SyntaxFactory.ParseExpression(string expr)
for example

SyntaxFactory.ParseTemplate(template temp)
SyntaxFactory.ParseTemplate(Of T)(template(of T) temp)
SyntaxFactory.ParseTemplate (  new Template.Foo(  sc) )
@gafter
Copy link
Member

gafter commented Jan 21, 2016

Dup of #1911 with a different syntax.

@AdamSpeight2008
Copy link
Contributor Author

@gafter Just to be clear #8065 is not a macro proposal.
It about just a way of producing the "code / expression" string.

Think of this like how T4 templates work, except with the executable part of T4 removed.

@AdamSpeight2008 AdamSpeight2008 changed the title [Proposal] Quotations [Proposal] Quotations (Not Macros) Jan 21, 2016
@AdamSpeight2008
Copy link
Contributor Author

Roughing out the structures.

class template
  .Header     : Template_Header
  .Body       : Template_Body

class Template_Header
  .Name       : Template_Identifier
  .Parameters : Template_Parameters

class Template_Identifier { }

class Template_Parameters
  .Count       : Integer
  .Parameters  : IEnumerable< Template_Parameter >

class Template_Parameter
  .Name       : Parameter_Identifier

class Template_Body
  .Parts      : Templates_BodyParts
  .Holes      : Templates_Holes

base Template_BodyPart
  .Index      : Integer
  .Length     : Integer

class Template_Text <: Template_BodyPart { }

class Template_Hole <: Template_BodyPart
  .Edge_Left  : Hole_EdgeLeft
  .Edge_Right : Hole_EdgeRight
  .Parameter  : Hole_Parameter

class Hole_BodyPart  <: Template_BodyPart { }
class Hole_EdgeRight <: Template_BodyPart { }
class Hole_Parameter <: Template_BodyPart
  .Name        : Parameter_Identifier

@AdamSpeight2008
Copy link
Contributor Author

Wrote a little parser and engine for the templating methods.

Grammar

template         ::= template_opening template_body template_closing
template_opening ::= "<#"
template_body    ::= Quoted | Hole | Text
template_closing ::= "#>"
quoted           ::= '@' ( '@' | template_opening | template_closing | hole_L | hole_R )
hole             ::= hole_L ws* identifier ws hole_R
hole_L           ::= "<{"
hole_r           ::= "}>"
ws               ::= ' '
identifier       ::= identifierFirst IdentifierTail*
IdentifierFirst  ::= Underscode | Letter
IdentifierTail   ::= IdentifierFirst | Digit

colorisation
Template Engine
applied

Things left to do.

  • Method Header
    template ComplilationUnit foo ( SymbolContext context )
  • Method Footer
    end template
  • Extend the Identifier parser to handle dotted identifiers context.Symbol.Name
  • Implement a version within the language compiler for C# or VB.net

@alrz
Copy link
Member

alrz commented Jan 31, 2016

@AdamSpeight2008 👍

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants