...
 
Commits (41)
image: "rustlang/rust:nightly"
variables:
CARGO_HOME: $CI_PROJECT_DIR/cargo
stages:
- build
- test
- doc
build:
stage: build
script:
- rustc --version
- cargo --version
- cargo build --verbose
test:
stage: test
script:
- rustc --version
- cargo --version
- cargo test --all --verbose
pages:
stage: doc
script:
- cargo doc --no-deps
- mv target/doc public
- echo '<meta http-equiv="refresh" content="0; url={{ LIBRARY NAME }}">' > public/index.html
artifacts:
paths:
- public
cache:
paths:
- cargo/
- target/
[package]
name = "plex"
version = "0.0.2"
version = "0.2.3"
authors = ["Geoffry Song <goffrie@gmail.com>"]
description = "A syntax extension for writing lexers and parsers."
......@@ -14,10 +14,18 @@ keywords = ["tokenizer", "scanner", "lexer", "parser", "generator"]
[lib]
name = "plex"
plugin = true
crate-type = ["dylib"]
proc-macro = true
[features]
default = ["lexer", "parser"]
lexer = ["redfa"]
parser = ["lalr"]
[dependencies]
lalr = "0.0.1"
redfa = "0.0.2"
lalr = { version = "0.0.2", optional = true }
redfa = { version = "0.0.2", optional = true }
syn = { version = "0.14.0", features = ["extra-traits", "full"] }
proc-macro2 = { version = "0.4.3", features = ["nightly"] }
quote = "0.6.0"
This diff is collapsed.
Copyright (c) 2015-2018 Geoffry Song
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
## plex, a parser and lexer generator
[![Build Status](https://travis-ci.org/goffrie/plex.png)](https://travis-ci.org/goffrie/plex)
[![pipeline status](http://labs.phundrak.fr/phundrak/plex/badges/master/pipeline.svg)](http://labs.phundrak.fr/phundrak/plex/commits/master)
This crate provides a couple syntax extensions:
......@@ -17,11 +17,12 @@ You can find a demo in `examples/demo.rs`. Note that nightly Rust is required.
## Usage
First, include the plugin.
First, include `plex`.
```rust
#![feature(plugin)]
#![plugin(plex)]
#![feature(use_extern_macros)]
extern crate plex;
use plex::{lexer, parser};
```
### Creating a lexer
......
#![feature(plugin)]
#![plugin(plex)]
#![feature(proc_macro_hygiene)]
extern crate plex;
use std::io::Read;
mod lexer {
use plex::lexer;
#[derive(Debug, Clone)]
pub enum Token {
Ident(String),
......@@ -41,7 +43,7 @@ mod lexer {
} else {
panic!("integer {} is out of range", text)
}, text)
},
}
r#"[a-zA-Z_][a-zA-Z0-9_]*"# => (Token::Ident(text.to_owned()), text),
......@@ -86,7 +88,8 @@ mod lexer {
type Item = (Token, Span);
fn next(&mut self) -> Option<(Token, Span)> {
loop {
let tok = if let Some(tok) = next_token(&mut self.remaining) {
let tok = if let Some((tok, new_remaining)) = next_token(self.remaining) {
self.remaining = new_remaining;
tok
} else {
return None
......@@ -132,9 +135,10 @@ mod ast {
}
mod parser {
use ::ast::*;
use ::lexer::Token::*;
use ::lexer::*;
use ast::*;
use lexer::Token::*;
use lexer::*;
use plex::parser;
parser! {
fn parse_(Token, Span);
......@@ -214,7 +218,7 @@ mod parser {
}
mod interp {
use ::ast::*;
use ast::*;
use std::collections::HashMap;
pub fn interp<'a>(p: &'a Program) {
......@@ -224,7 +228,7 @@ mod interp {
}
}
fn interp_expr<'a>(env: &mut HashMap<&'a str, i64>, expr: &'a Expr) -> i64 {
use ::ast::Expr_::*;
use ast::Expr_::*;
match expr.node {
Add(ref a, ref b) => interp_expr(env, a) + interp_expr(env, b),
Sub(ref a, ref b) => interp_expr(env, a) - interp_expr(env, b),
......@@ -249,7 +253,8 @@ mod interp {
fn main() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let lexer = lexer::Lexer::new(&s);
let lexer = lexer::Lexer::new(&s)
.inspect(|tok| eprintln!("tok: {:?}", tok));
let program = parser::parse(lexer).unwrap();
interp::interp(&program);
}
This diff is collapsed.
#![feature(plugin_registrar, quote, rustc_private)]
#![recursion_limit = "128"]
#![feature(proc_macro_diagnostic)]
#![feature(proc_macro_span)]
#![warn(unused_extern_crates)]
#[cfg(feature = "parser")]
extern crate lalr;
extern crate proc_macro2;
extern crate proc_macro;
#[macro_use]
extern crate quote;
#[cfg(feature = "lexer")]
extern crate redfa;
extern crate syntax;
extern crate rustc_plugin;
#[macro_use]
extern crate syn;
pub mod lexer;
pub mod parser;
#[cfg(feature = "lexer")]
mod lexer;
#[cfg(feature = "parser")]
mod parser;
use syntax::ext::base;
use syntax::symbol::Symbol;
use rustc_plugin as plugin;
use proc_macro::TokenStream;
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut plugin::Registry) {
reg.register_syntax_extension(Symbol::intern("parser"),
base::SyntaxExtension::NormalTT(Box::new(parser::expand_parser), None, true));
reg.register_syntax_extension(Symbol::intern("lexer"),
base::SyntaxExtension::NormalTT(Box::new(lexer::expand_lexer), None, true));
#[cfg(feature = "lexer")]
#[proc_macro]
pub fn lexer(tok: TokenStream) -> TokenStream {
lexer::lexer(tok.into()).into()
}
#[cfg(feature = "parser")]
#[proc_macro]
pub fn parser(tok: TokenStream) -> TokenStream {
parser::parser(tok.into()).into()
}
This diff is collapsed.