Generate files in OUT_DIR by default

The macro lalrpop_mod! can be used to define the mod with the generated code.
This commit is contained in:
Ayose 2018-02-19 04:21:18 +00:00
parent 867938d6ee
commit e7db1ee2e2
8 changed files with 73 additions and 48 deletions

View File

@ -1,6 +1,6 @@
extern crate lalrpop_util;
#[macro_use] extern crate lalrpop_util;
pub mod calculator1; // synthesized by LALRPOP
lalrpop_mod!(pub calculator1); // syntesized by LALRPOP
#[test]
fn calculator1() {
@ -10,7 +10,7 @@ fn calculator1() {
assert!(calculator1::TermParser::new().parse("((22)").is_err());
}
pub mod calculator2;
lalrpop_mod!(calculator2);
#[test]
fn calculator2() {
@ -20,7 +20,7 @@ fn calculator2() {
assert!(calculator2::TermParser::new().parse("((22)").is_err());
}
pub mod calculator2b;
lalrpop_mod!(calculator2b);
#[test]
fn calculator2b() {
@ -37,7 +37,7 @@ fn calculator2b() {
assert_eq!(result, "222");
}
pub mod calculator3;
lalrpop_mod!(calculator3);
#[cfg_attr(not(test), allow(unused_macros))]
macro_rules! test3 {
@ -56,7 +56,7 @@ fn calculator3() {
test3!(22 * (44 + 66) / 3);
}
pub mod calculator4;
lalrpop_mod!(calculator4);
pub mod ast;
#[test]
@ -67,7 +67,7 @@ fn calculator4() {
assert_eq!(&format!("{:?}", expr), "((22 * 44) + 66)");
}
pub mod calculator5;
lalrpop_mod!(calculator5);
#[test]
fn calculator5() {
@ -95,7 +95,7 @@ fn calculator5() {
assert_eq!(&format!("{:?}", expr), "[((22 * 44) + 66), (13 * 3)]");
}
pub mod calculator6;
lalrpop_mod!(calculator6);
#[test]
fn calculator6() {

View File

@ -3,7 +3,6 @@ extern crate lalrpop;
fn main() {
lalrpop::Configuration::new()
.emit_comments(true)
.use_cargo_dir_conventions()
.process_current_dir()
.unwrap();
}

View File

@ -2,6 +2,7 @@ extern crate docopt;
#[macro_use]
extern crate serde_derive;
extern crate serde;
#[macro_use] extern crate lalrpop_util;
use docopt::Docopt;
use std::env;
@ -9,7 +10,7 @@ use std::io::Read;
use std::fs::File;
use std::time::Instant;
mod pascal;
lalrpop_mod!(pascal);
fn main() {
let args: Args = Docopt::new(USAGE)

View File

@ -1,8 +1,11 @@
#[macro_use] extern crate lalrpop_util;
pub mod lexer;
pub mod parser;
pub mod ast;
pub mod eval;
lalrpop_mod!(parser);
pub fn compile(input: &str) -> Result<ast::Program, String> {
match parser::ProgramParser::new().parse(lexer::Lexer::new(input)) {
Ok(s) => Ok(ast::Program::new(s)),

View File

@ -1,4 +1,4 @@
#![cfg_attr(feature = "cargo-clippy", allow(clippy))]
//#![cfg_attr(feature = "cargo-clippy", allow(clippy))]
grammar(scale: i32);
use util::tok::Tok;

View File

@ -1,7 +1,7 @@
#![cfg_attr(not(test), allow(dead_code, unused_imports))]
extern crate diff;
extern crate lalrpop_util;
#[macro_use] extern crate lalrpop_util;
use std::cell::RefCell;
@ -11,105 +11,105 @@ use util::tok::Tok;
/// Tests that actions can return the grammar's type parameters' associated
/// types.
mod associated_types;
lalrpop_mod!(associated_types);
mod associated_types_lib;
/// demonstration from the Greene text; one of the simplest grammars
/// that still ensures we get parse tree correct
mod sub;
lalrpop_mod!(sub);
/// test something other than test-all
mod sub_ascent;
mod sub_table;
lalrpop_mod!(sub_ascent);
lalrpop_mod!(sub_table);
/// more interesting demonstration of parsing full expressions
mod expr;
lalrpop_mod!(expr);
/// more interesting demonstration of parsing full expressions, using LALR not LR
mod expr_lalr;
lalrpop_mod!(expr_lalr);
/// more interesting demonstration of parsing full expressions, using intern tok
mod expr_intern_tok;
lalrpop_mod!(expr_intern_tok);
/// tests #![attributes] for generated module
#[allow(dead_code, unknown_lints)]
mod expr_module_attributes;
lalrpop_mod!(expr_module_attributes);
/// test that passes in lifetime/type/formal parameters and threads
/// them through, building an AST from the result
mod expr_arena;
lalrpop_mod!(expr_arena);
/// definitions of the AST
mod expr_arena_ast;
/// expr defined with a generic type `F`
mod expr_generic;
lalrpop_mod!(expr_generic);
mod generics_issue_104;
lalrpop_mod!(generics_issue_104);
mod generics_issue_104_lib;
/// Grammar parameterized by `F` with where clause `where F: for<'a> FnMut(&'a
/// str)`.
mod where_clause_with_forall;
lalrpop_mod!(where_clause_with_forall);
/// test of inlining
mod inline;
lalrpop_mod!(inline);
/// test that exercises internal token generation, as well as locations and spans
mod intern_tok;
lalrpop_mod!(intern_tok);
/// test that exercises using a lifetime parameter in the token type
mod lifetime_tok;
lalrpop_mod!(lifetime_tok);
/// library for lifetime_tok test
mod lifetime_tok_lib;
/// test that exercises locations and spans
mod loc;
lalrpop_mod!(loc);
/// regression test for location issue #90
mod loc_issue_90;
lalrpop_mod!(loc_issue_90);
mod loc_issue_90_lib;
/// test that uses `super` in paths in various places
mod use_super;
lalrpop_mod!(use_super);
/// Custom error type (issue #113)
#[derive(Debug, PartialEq)]
pub struct MyCustomError(char);
/// test that exercises locations, spans, and custom errors
mod error;
mod error_issue_113;
lalrpop_mod!(error);
lalrpop_mod!(error_issue_113);
/// Test error recovery
mod error_recovery;
mod error_recovery_pull_182;
mod error_recovery_issue_240;
mod error_recovery_lalr_loop;
mod error_recovery_lock_in;
mod error_recovery_span;
mod error_recovery_type_in_macro;
lalrpop_mod!(error_recovery);
lalrpop_mod!(error_recovery_pull_182);
lalrpop_mod!(error_recovery_issue_240);
lalrpop_mod!(error_recovery_lalr_loop);
lalrpop_mod!(error_recovery_lock_in);
lalrpop_mod!(error_recovery_span);
lalrpop_mod!(error_recovery_type_in_macro);
/// test for inlining expansion issue #55
mod issue_55;
lalrpop_mod!(issue_55);
/// test for unit action code
mod unit;
lalrpop_mod!(unit);
/// test for match section
mod match_section;
mod match_alternatives;
lalrpop_mod!(match_section);
lalrpop_mod!(match_alternatives);
/// regression test for issue #253.
mod partial_parse;
lalrpop_mod!(partial_parse);
/// regression test for issue #278.
mod error_issue_278;
lalrpop_mod!(error_issue_278);
// Check that error recovery (which requires cloneable tokens) is not created if it is not used
#[allow(unused)]
mod no_clone_tok;
lalrpop_mod!(no_clone_tok);
mod util;

View File

@ -143,6 +143,13 @@ pub struct ErrorRecovery<L, T, E> {
pub dropped_tokens: Vec<(L, T, L)>,
}
#[macro_export]
macro_rules! lalrpop_mod {
($modname:ident) => { lalrpop_mod!($modname, concat!("/", stringify!($modname), ".rs")); };
($modname:ident, $source:expr) => { mod $modname { include!(concat!(env!("OUT_DIR"), $source)); } };
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -149,7 +149,22 @@ impl Configuration {
/// Process all `.lalrpop` files in `path`.
pub fn process_dir<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<Error>> {
let session = Rc::new(self.session.clone());
let mut session = self.session.clone();
// If in/out dir are empty, use cargo conventions by default.
// See https://github.com/lalrpop/lalrpop/issues/280
if session.in_dir.is_none() {
let mut in_dir = try!(env::current_dir());
in_dir.push("src");
session.in_dir = Some(in_dir);
}
if session.out_dir.is_none() {
let out_dir = env::var_os("OUT_DIR").expect("missing OUT_DIR variable");
session.out_dir = Some(PathBuf::from(out_dir));
}
let session = Rc::new(session);
try!(build::process_dir(session, path));
Ok(())
}