Ivan Shen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- RFC: RFC0017 Author: Robert Holt Status: Experimental Area: Domain Specific Languages, Object Schemas Comments Due: --- # Domain-Specific Language Specifications PowerShell provides rich metaprogramming features with a natural-language-oriented interface, but currently lacks a true, simple mechanism for language extensions and keyword addition. Users have already created their own domain-specific languages (DSLs) in PowerShell to suit their needs using a variety of language features, however a canonical DSL definition mechanism could standardize this, in addition to providing more natural operability within PowerShell, such as: * Syntax highlighting * Autocompletion * Semantic checking * Better readability * Reproducible, schema-based formats * Informative error messages * Less XML (in the case of `.ps1xml` files) * Specification of parse-time functionality While not allowing arbitrary changes to the PowerShell language, a DSL mechanism would seek to let users define their own language functionality, especially with respect to types, with a well-defined and standardized syntax. ## Motivation As a PowerShell user, I can create a domain-specific language to specify a (print format | xml document | object-type resource configuration | testing setup) schema using a familiar PowerShell syntax, so that I can repeatably instantiate that schema and leverage PowerShell's semantic functionality for field checking and autocompletion. ### Examples of Motivating DSLs in PowerShell #### Examples pre-existing in PowerShell * [PowerShell `configuration` modules](https://blogs.msdn.microsoft.com/powershell/2013/11/05/understanding-configuration-keyword-in-desired-state-configuration/) &mdash; DSC configuration modules (Note: this already uses the `DynamicKeyword` mechanisms) * [Pester](https://github.com/pester/pester) &mdash; testing framework * [psake](https://github.com/JamesKovacs/psake) &mdash; build automation * [simplex](https://github.com/beefarino/simplex) &mdash; PowerShell providers * [ShowUI](https://showui.codeplex.com/) &mdash; WPF UI specification * [PowerShell Data Files](http://ramblingcookiemonster.github.io/PowerShell-Configuration-Data/) &mdash; PowerShell-native data specification format #### Motivating candidates for porting to a PowerShell DSL * [Types.ps1xml](https://msdn.microsoft.com/en-us/library/dd878306(v=vs.85).aspx) &mdash; type addition to PowerShell * [Format.ps1xml](https://msdn.microsoft.com/en-us/library/dd878339(v=vs.85).aspx) &mdash; format/presentation specification for PowerShell types and resources The goal, then, would be to create a standard mechanism for PowerShell that all of these DSLs could be written and maintained in. ## Specifications This RFC proposes a PowerShell attribute based mechanism for defining DSLs in PowerShell. Existing DSL keywords are defined as PowerShell functions. All the keywords are separate functions with no real structure, other than documentation, defining how they used together. We schematize the Keyword by marking the function with the `[DslKeyword]` attribute. Within that attribute, properties of the DSL can be specified. **Definition**: Inner Keyword is a DSL keyword that should be defined within the script block of a DSL keyword. A user defines their DSL by marking their keyword function with the `[Keyword()]` attribute. Within the function, functionality is defined in normal PowerShell code. Inner keywords are defined in nested functions inside the main keyword function. Users would be able to specify which nested functions are inner keyword functions. Take this example DSL structure from Pester. ``` Describe "Add-Numbers" { It "adds positive numbers" { $sum = Add-Numbers 2 3 $sum | Should -Be 5 } } ``` To prevent multiple implementations of keywords nested into one block, we define the keyword functionality outside the nested block. ``` function Describe { [DslKeyword(InnerKeywords = @('It'))] param( [Parameter(Mandatory = $true, Position = 0)] [string] $Name, [DslBody()] [scriptblock] $Body, ) function It { [DslKeyword()] param( [Parameter(Mandatory = $true, Position = 0)] [string] $Name, [DslBody()] [scriptblock] $Body, ) _It(); } _Describe(); } function _It() { # It keyword implementation here } function _Describe() { # Describe keyword implementation here } ``` Users could also attach their own error handling function within their keyword, should the a run time error occurs when the structure of the Keyword is malformed. ``` function Describe { [DslKeyword(ErrorHandler = @('catchStructureError'))] param( [Parameter(Mandatory = $true, Position = 0)] [string] $Name, [DslBody()] [scriptblock] $Body, ) function catchStructureError() { # error handling code defined here } } ``` ### Questions My understanding of how this is going to work. Please correct me if I'm wrong. DslKeyword is a PowerShell class that inherites Attribute, which the PowerShell runtime instantiates and invokes at run-time. Assuming we find a way to see what is defined in the scriptblock, if a required InnerKeyword is not defined inside the scriptblock we throw a runtime error telling the user to define the inner keyword. **Unknowns** How can I be able to see the contents inside a scriptblock? ie. I want to check if the InnerKeywords are defined inside the scriptblock. Thoughts: Would this be possible without hacking the parser? If not, with the `DslBody` attribute, could this be possible? ### End of Ivan's changes ## Alternate Proposals and Considerations ### Argument types for `Keyword` methods Currently the `Keyword` base class specifies its arguments with types internal to the PowerShell parser, but it may be preferable to not expose these internal types as a user interface directly. However, since we wish to provide a customizable DSL, most of the internals of a `DynamicKeyword` and its AST will need to be exposed in some way, and `DynamicKeywordStatementAst` is exactly the type conceived to encapsulate that information. Conceivably an interface could be provided along the lines of: ```csharp PostParse(object[] positionalParameters, Dictionary<string, object> namedParameters) { ... } ``` However, this would limit the extent to which a user could customize their keyword instantiation. ### Provided implementations for Keyword functionality Further to the above point, keyword instantiation could be made simpler by providing default virtual methods that simplify the keyword declaration process (for example by parsing parameters). Users with more sophisticated needs could then override these methods to suit their needs. This would provide a useful way to empower most users to make simple DSLs while not precluding more enthusiastic language customizations. Such methods may be out of scope for the current RFC. ### Bootstrapping the C# specification language for a PowerShell version To extend the suggestions for ease of use above, the C# keyword definition language could, in principle, be used to define a keyword definition DSL within PowerShell itself. Such an implementation is likely out of scope for this RFC however. ### DSC modules and CIM configurations Small DSL specification language already exists in PowerShell for DSC module files (`.schema.psm1`) and CIM configurations. These may be of consideration both as a guide for a more general implementation, and in terms of conflicts and code duplication &mdash; although there are certain specific functionalities that are tied to these language features. ### Attributes vs interface implementation for C# Specifications In the current draft above, specification of a DSL keyword involves the redundancy of both declaring the `[Keyword]` attribute and extending the `Keyword` class. This is also the scheme used by `Cmdlet`, however for this reduced case it may not be necessary. ### Runtime Behavior Specification Currently there is no suggestion as to the design or implementation of specifying the runtime behavior of a keyword -- what the keyword actually does when executed. Runtime behavior can presently be added using PowerShell itself, but specification in C# may be a better subject of a different RFC. ### Existing Implementations for Dynamic Keywords in PowerShell The PowerShell parser and AST already support the concept of a `DynamicKeyword`, which this proposal is built around. The following table attempts to draw an equivalence between desired DSL features, current dynamic keyword support, and proposed C# syntax: | DSL Function | `DynamicKeyword` Property | C# Syntax | | :--------------------- | :---------------------------------- | :--------------- | | Keywords | `Name` | `Name = ...` attr | | Nested keywords | Keyword stack | Inner class | | Keyword use constraints | ? (Semantics check?) | `Use = ...` attr | | Body syntax | `BodyMode` | `Body = ...` attr | | Arguments | `DynamicKeywordProperty` | `[Property]` | | Parameters | `DynamicKeywordParameter` | `[Parameter]` | | Custom types (enums) | `DynamicKeywordProperty.Values` | `enum` | | Parse time action | `DynamicKeyword.(Pre|Post)Parse` | C# code/interface | | Semantics check | `DynamicKeyword.SemanticCheck` | C# code/interface | | Runtime action | None | Out of scope | ----- PowerShell Committee Decision Voting Results Jason Shirk: Accept Joey Aiello: Accept Bruce Payette: Accept Steve Lee: Accept Hemant Mahawar: Accept Majority Decision Commmittee agrees that this RFC is sufficient to move to experimental stage and begin prototype code for further feedback. Minority Decision N/A

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully