Giter Site home page Giter Site logo

example-tlm's Introduction

List TLM Example

Relit is a way to extend Reason's syntax to support custom literals, while still producing readable code.

Here's an example of a TLM (typed literal macro) implemented using Relit.

To run it, first make sure you have OCaml version 4.07.0. Then:

opam install ppx_relit

git clone https://github.com/charlesetc/example-tlm
cd example-tlm

make list_parser
make test

Some highlights!


The whole point (test/test.re):

module String = {
  include String;
  include List_example.N({
    type t = string;
  });
};

let l = String.$list `( "hi", "there", "you",)`;

let () = String.concat("! ", l) |> print_endline;

Is that a list literal that's not built into the language? Woah...


The notation definition (list_example/list_example.re):

module N = (A: {type t;}) => {
  notation $list at list(A.t) {
    lexer List_parser.Lexer
    parser List_parser.Parser.parse
    in package list_parser;
    dependencies = {
      module List = List;
      type t = A.t;
    };
  };
};

The entire lexer (list_parser/lexer.mll):

{
open Lexing
open Parser
open Relit
}

rule read =
  parse
  | _ {
    let segment = Relit.Segment.read_to "," lexbuf in
    SPLICED_EXP(segment) }
  | eof      { EOF }

The entire parser (list_parser/parser.mly):

%{
  open Migrate_parsetree.Ast_404
  open Longident
  open Parsetree
  let loc = Relit.loc
%}

%token <Relit.Segment.t> SPLICED_EXP
%token EOF

%start <Migrate_parsetree.Ast_404.Parsetree.expression> parse

%%

parse:
  | splice = SPLICED_EXP l = parse
    { [%expr [%e Relit.ProtoExpr.spliced splice [%type: t]] :: [%e l ]] }
  | EOF
    { [%expr []] }

You just read the entire TLM example!


Todo:

  • Fix required trailing commas

example-tlm's People

Contributors

charlesetc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

crispywalrus

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.