Giter VIP home page Giter VIP logo

myrpg's Introduction

Grammar

LALR

How does it work

MyRpg use the lang! macro to collect essential grammar details and build LALR parsing table AT COMPILE TIME.

The lexical analysis is proceeded along with parsing, instead of pre-collecting all the tokens.

Subscribe parsing events

MyRpg supports subscribing multiple parsing events, which makes it more flexible:

Token Generation

Triggers when a new token was generated by lexer.

<terminal> => <regex> => |tok: Token| -> Option<Token> {
	// do something with tok
	Some(tok)  // or None
}

Reduction (Ast Generation)

Triggers when a production was reduced by parser.

<non-terminal> => [
	[ <symbol> ] => @reduce |ast: &mut Ast<_>| -> () {
		// do something with ast
	}
]

Execution

Triggers when the parsing job finishes successfully. Make a traversal over the AST.

<non-terminal> => [
	[ <symbol> ] => |/* match this production */| -> () {
		// do something with ast
	}
]

This function is marked as returning Option<ValueType>, in case some node doesn't return anything. As a result, this function has it's default behaviour: expand all child ASTs and return the last's value.

Note that current AST node will be automatically destructured into children, so the function parameters must match your production declaration, e.g:

Expr "+" Term => |lhs, _, rhs| -> _ {
	Some(lhs.gen().unwrap() + rhs.gen().unwrap())
}

If you override this function, you have to call ast.gen() manually to traverse child ASTs.

Usage

Default traverse method

use myrpg::{ast::*, LRParser, *};

lang! {

	Name = MathExpr
	ValueType = i32

	;;

	Number => r"[0-9]+",
	Id => r"[a-zA-Z_]+"

	;;

	S => [
		Expr => |child| -> _ {
			let res = child.gen().unwrap();
			println!("{:?}", child);
			Some(res)
		}
	],
	Expr => [
		Expr "+" Term => |lhs, _, rhs| -> _ {
			Some(lhs.gen().unwrap() + rhs.gen().unwrap())
		},
		Expr "-" Term => |lhs, _, rhs| -> _ {
			Some(lhs.gen().unwrap() - rhs.gen().unwrap())
		},
		Term => |child| -> _ {
			child.gen()
		}
	],
	Term => [
		Term "*" Factor => |lhs, _, rhs| -> _ {
			Some(lhs.gen().unwrap() * rhs.gen().unwrap())
		},
		Term "/" Factor => |lhs, _, rhs| -> _ {
			Some(lhs.gen().unwrap() / rhs.gen().unwrap())
		},
		Factor => |child| -> _ {
			child.gen()
		}
	],
	Factor => [
		Number => |tok| -> Option<i32> {
			Some(tok.val.parse().unwrap())
		},
		"(" Expr ")" => |_, child, _| -> _ {
			child.gen()
		}
	]

}

#[test]
fn test_mathexpr() {
	let parser = LRParser::<MathExpr>::new();
	let res = parser.parse("4 * (2 + 1)");
	println!("{:?}", res.unwrap().gen());
}
use myrpg::{ast::*, LRParser, *};

lang! {

    Name = IfExpr
    ValueType = i32

    ;;

    Id => r"[a-zA-Z_]+"

    ;;

    S => [
        Stmt
    ],
    Stmt => [
        "if" Id Stmt "else" Stmt,
        "if" Id Stmt,
        Id
    ]

}

#[test]
fn test_ifexpr() {
    let parser = LRParser::<IfExpr>::new();

    match parser.parse("if a b else c") {
        Ok(val) => println!("{:?}", val),
        Err(err) => println!("{:?}", err),
    }
}

Status

Ready, under construction.

myrpg's People

Contributors

xlnx avatar

Stargazers

 avatar

Watchers

 avatar  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.