Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexknauth/reconstruct-template

`~list/ctx` pattern and `?list/ctx` template for reconstructing syntax objects for core-form languages
https://github.com/alexknauth/reconstruct-template

Last synced: about 1 month ago
JSON representation

`~list/ctx` pattern and `?list/ctx` template for reconstructing syntax objects for core-form languages

Awesome Lists containing this project

README

        

# reconstruct-template

[![Build Status](https://travis-ci.org/AlexKnauth/reconstruct-template.png?branch=master)](https://travis-ci.org/AlexKnauth/reconstruct-template)

[_Documentation_](http://docs.racket-lang.org/reconstruct-template-list-ctx/index.html).

```racket
(require reconstruct-template/list-ctx)
```

Provides [`~list/ctx`][~list/ctx] and [`?list/ctx`][?list/ctx], as a pattern-expander and a template-metafunction which go together so that if `x` is a syntax-list,

```racket
(syntax-parse x
[{~list/ctx ctx e ...}
#'{?list/ctx ctx e ...}])
=
x
```

You can think of these as similar to `(e ...)`, where `ctx` saves the lexical context, source location, and syntax properties of the parens, and transfers them over to the result syntax object.

The main intended use of [`~list/ctx`][~list/ctx] and [`?list/ctx`][?list/ctx] is for reconstructing syntax objects when making expanders for new *core-form languages*. This purpose is similar to [`syntax/loc/props`][syntax/loc/props] from Alexis King's blog post [_Reimplementing Hackett’s type language: expanding to custom core forms in Racket_][lexi-lambda-core-forms].

The main advantage of [`~list/ctx`][~list/ctx] and [`?list/ctx`][?list/ctx] over [`syntax/loc`][syntax/loc] or [`syntax/loc/props`][syntax/loc/props] is that they work even for stx-list templates nested deeply within a template, as well as for stx-list templates under ellipses. For example, if `x` is a well-formed `let` expression,

```racket
(syntax-parse x
[{~list/ctx p1 l
{~list/ctx p2 {~list/ctx p3 x a} ...}
b}
#'{?list/ctx p1 l
{?list/ctx p2 {?list/ctx p3 x a} ...}
b}])
=
x
```

[~list/ctx]: https://docs.racket-lang.org/reconstruct-template-list-ctx/index.html#%28form._%28%28lib._reconstruct-template%2Flist-ctx..rkt%29._~7elist%2Fctx%29%29

[?list/ctx]: https://docs.racket-lang.org/reconstruct-template-list-ctx/index.html#%28form._%28%28lib._reconstruct-template%2Flist-ctx..rkt%29._~3flist%2Fctx%29%29

[syntax/loc]: https://docs.racket-lang.org/reference/stx-patterns.html#(form._((lib._racket%2Fprivate%2Fstxcase-scheme..rkt)._syntax%2Floc))

[syntax/loc/props]: https://lexi-lambda.github.io/blog/2018/04/15/reimplementing-hackett-s-type-language-expanding-to-custom-core-forms-in-racket/#preserving-syntax-properties-and-source-locations

[lexi-lambda-core-forms]: https://lexi-lambda.github.io/blog/2018/04/15/reimplementing-hackett-s-type-language-expanding-to-custom-core-forms-in-racket/