Skip to content

FalkZ/rust-macro-parser-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Macro Parser Generator

This is a Rust library for generating parsers from macro definitions. The definitions are inspired by EBNF.

Example

An implemented toy language that compiles to TypeScript can be found here:

How it works

First we define some kind of grammar in the macro. In this case simple arithmetic expressions:

Lexer:

Lexer!(
    { // Defining all the tokens of the grammar
        {'0'..='9' =>} => NUMBER, // matching subsequent digits
        {'+'} => PLUS, // matching a single +
        {'-'} => MINUS,
        {'*'} => MUL,
        {'/'} => DIV,
        {' '} => SPACE,
        {'\n'} => NEWLINE
    }

    ( SPACE | NEWLINE ) => _ // Spaces and newlines are skipped when matching with the parser

    {
       // Remapping of token strings from above, not necessary in that case
    });

Parser:

Parser!(
    operator = (  PLUS | MINUS | DIV | MUL ),
    half_statement = [ #operator => operator, NUMBER, *],
    statement = { NUMBER => left, *half_statement => right},
);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages