Giter Club home page Giter Club logo

melody's Introduction

Melody Logo Melody Logo

Rust CI Crates.io Crates.io Crates.io melody playground melody playground

Melody is a language that compiles to ECMAScript regular expressions, while aiming to be more readable and maintainable.

code example

Examples

Note: these are for the currently supported syntax and may change

Batman Theme Β try in playground

16 of "na";

2 of match {
  <space>;
  "batman";
}

// πŸ¦‡πŸ¦Έβ€β™‚οΈ

Turns into

(?:na){16}(?: batman){2}

Twitter Hashtag Β try in playground

"#";
some of <word>;

// #melody

Turns into

#(?:\w)+

Introductory Courses Β try in playground

some of <alphabetic>;
<space>;
"1";
2 of <digit>;

// classname 1xx

Turns into

(?:[a-zA-Z])+ 1(?:\d){2}

Indented Code (2 spaces) Β try in playground

some of match {
  2 of <space>;
}

some of <char>;
";";

// let value = 5;

Turns into

(?: {2})+.+;

Semantic Versions Β try in playground

<start>;

option of "v";

capture major {
  some of <digit>;
}

".";

capture minor {
  some of <digit>;
}

".";

capture patch {
  some of <digit>;
}

<end>;

// v1.0.0

Turns into

^v?(?<major>(?:\d)+)\.(?<minor>(?:\d)+)\.(?<patch>(?:\d)+)$

Playground

You can try Melody in your browser using the playground

Book

Read the book here

Install

Cargo

cargo install melody_cli

From Source

git clone https://github.com/yoav-lavi/melody.git
cd melody
cargo install --path crates/melody_cli

Binary

  • macOS binaries (aarch64 and x86_64) can be downloaded from the release page

Community

  • Arch Linux (maintained by @ilai-deutel)

    Installation instructions
    1. Installation with an AUR helper, for instance using paru:

      paru -Syu melody
    2. Install manually with makepkg:

      git clone https://aur.archlinux.org/melody.git
      cd melody
      makepkg -si
  • NixOS (soon, see PR) (maintained by @jyooru)

CLI Usage

melody [OPTIONS] [INPUT_FILE_PATH]

ARGS:
    <INPUT_FILE_PATH>    Read from a file

OPTIONS:
    -h, --help                         Print help information
    -n, --no-color                     Print output with no color
    -o, --output <OUTPUT_FILE_PATH>    Write to a file
    -r, --repl                         Start the Melody REPL
    -V, --version                      Print version information

Changelog

See the changelog here or in the release page

Syntax

Quantifiers

  • ... of - used to express a specific amount of a pattern. equivalent to regex {5} (assuming 5 of ...)
  • ... to ... of - used to express an amount within a range of a pattern. equivalent to regex {5,9} (assuming 5 to 9 of ...)
  • over ... of - used to express more than an amount of a pattern. equivalent to regex {6,} (assuming over 5 of ...)
  • some of - used to express 1 or more of a pattern. equivalent to regex +
  • any of - used to express 0 or more of a pattern. equivalent to regex *
  • option of - used to express 0 or 1 of a pattern. equivalent to regex ?

All quantifiers can be preceded by lazy to match the least amount of characters rather than the most characters (greedy). Equivalent to regex +?, *?, etc.

Symbols

  • <char> - matches any single character. equivalent to regex .
  • <whitespace> - matches any kind of whitespace character. equivalent to regex \s or [ \t\n\v\f\r]
  • <newline> - matches a newline character. equivalent to regex \n
  • <tab> - matches a tab character. equivalent to regex \t
  • <return> - matches a carriage return character. equivalent to regex \r
  • <feed> - matches a form feed character. equivalent to regex \f
  • <null> - matches a null characther. equivalent to regex \0
  • <digit> - matches any single digit. equivalent to regex \d or [0-9]
  • <vertical> - matches a vertical tab character. equivalent to regex \v
  • <word> - matches a word character (any latin letter, any digit or an underscore). equivalent to regex \w or [a-zA-Z0-9_]
  • <alphabetic> - matches any single latin letter. equivalent to regex [a-zA-Z]
  • <alphanumeric> - matches any single latin letter or any single digit. equivalent to regex [a-zA-Z0-9]
  • <boundary> - Matches a character between a character matched by <word> and a character not matched by <word> without consuming the character. equivalent to regex \b
  • <backspace> - matches a backspace control character. equivalent to regex [\b]

All symbols can be preceeded with not to match any character other than the symbol

Special Symbols

  • <start> - matches the start of the string. equivalent to regex ^
  • <end> - matches the end of the string. equivalent to regex $

Character Ranges

  • ... to ... - used with digits or alphabetic characters to express a character range. equivalent to regex [5-9] (assuming 5 to 9) or [a-z] (assuming a to z)

Literals

  • "..." or '...' - used to mark a literal part of the match. Melody will automatically escape characters as needed. Quotes (of the same kind surrounding the literal) should be escaped

Raw

  • `...` - added directly to the output without any escaping

Groups

  • capture - used to open a capture or named capture block. capture patterns are later available in the list of matches (either positional or named). equivalent to regex (...)
  • match - used to open a match block, matches the contents without capturing. equivalent to regex (?:...)
  • either - used to open an either block, matches one of the statements within the block. equivalent to regex (?:...|...)

Assertions

  • ahead - used to open an ahead block. equivalent to regex (?=...). use after an expression
  • behind - used to open an behind block. equivalent to regex (?<=...). use before an expression

Assertions can be preceeded by not to create a negative assertion (equivalent to regex (?!...), (?<!...))

Variables

  • let .variable_name = { ... } - defines a variable from a block of statements. can later be used with .variable_name. Variables must be declared before being used. Variable invocations cannot be quantified directly, use a group if you want to quantify a variable invocation

    example:

    let .a_and_b = {
      "a";
      "b";
    }
    
    .a_and_b;
    "c";
    
    // abc

Extras

  • /* ... */, // ... - used to mark comments (note: // ... comments must be on separate line)

File Extension

The Melody file extensions are .mdy and .melody

Crates

  • melody_compiler - The Melody compiler πŸ“¦ πŸ“–
  • melody_cli - A CLI wrapping the Melody compiler πŸ“¦ πŸ“–
  • melody_wasm - WASM binding for the Melody compiler

Extensions

Performance

Last measured on V0.13.3

Measured on an 8 core 2021 MacBook Pro 14-inch, Apple M1 Pro using criterion:

  • 8 lines:

    compiler/normal (8 lines)
                            time:   [3.6560 us 3.6596 us 3.6644 us]
    slope  [3.6560 us 3.6644 us] R^2            [0.9999367 0.9999233]
    mean   [3.6577 us 3.6676 us] std. dev.      [3.2234 ns 11.399 ns]
    median [3.6549 us 3.6674 us] med. abs. dev. [642.70 ps 12.973 ns]
    
  • 1M lines:

    compiler/long input (1M lines)
                            time:   [345.99 ms 348.85 ms 351.91 ms]
    mean   [345.99 ms 351.91 ms] std. dev.      [2.8317 ms 6.3397 ms]
    median [344.55 ms 352.85 ms] med. abs. dev. [893.36 us 8.5853 ms]
    
  • Deeply nested:

    compiler/deeply nested
                            time:   [4.8259 us 4.8330 us 4.8399 us]
    slope  [4.8259 us 4.8399 us] R^2            [0.9998793 0.9998830]
    mean   [4.8259 us 4.8476 us] std. dev.      [7.6412 ns 24.306 ns]
    median [4.8234 us 4.8484 us] med. abs. dev. [4.1349 ns 30.340 ns]
    

To reproduce, run cargo benchmark

Future Feature Status

🐣 - Partially implemented

❌ - Not implemented

❔ - Unclear what the syntax will be

❓ - Unclear whether this will be implemented

Melody Regex Status
not "A"; [^A] 🐣
variables / macros 🐣
file watcher ❌
TS / JS build step ❌
multiline groups in REPL ❌
flags: global, multiline, ... /.../gm... ❔
(?) \# ❔
(?) \k<name> ❔
(?) \p{...} ❔
(?) \P{...} ❔
(?) \uYYYY ❔
(?) \xYY ❔
(?) \ddd ❔
(?) \cY ❔
(?) $1 ❔
(?) $` ❔
(?) $& ❔
(?) x20 ❔
(?) x{06fa} ❔
any of "a", "b", "c" * [abc] ❓
multiple ranges * [a-zA-Z0-9] ❓
regex optimization ❓
standard library / patterns ❓
reverse compiler ❓

* these are expressable in the current syntax using other methods

melody's People

Contributors

yoav-lavi avatar addisoncrump avatar amirali avatar legoandmars avatar ilai-deutel avatar joshuakb2 avatar judesafo avatar omikorin avatar aclueless avatar alpheratz0 avatar

Watchers

 avatar

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.