Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export generator and docs api in the pest_generator trait #961

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions generator/src/docs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Type and helper to collect the gramamr and rule documentation.

use pest::iterators::Pairs;
use pest_meta::parser::Rule;
use std::collections::HashMap;

/// Abstraction for the grammer and rule doc.
#[derive(Debug)]
pub(crate) struct DocComment {
pub struct DocComment {
/// The grammar documentation is defined at the beginning of a file with //!.
pub grammar_doc: String,

/// HashMap for store all doc_comments for rules.
@@ -33,7 +37,7 @@ pub(crate) struct DocComment {
/// grammar_doc = "This is a grammar doc"
/// line_docs = { "foo": "line doc 1\nline doc 2", "bar": "line doc 3" }
/// ```
pub(crate) fn consume(pairs: Pairs<'_, Rule>) -> DocComment {
pub fn consume(pairs: Pairs<'_, Rule>) -> DocComment {
let mut grammar_doc = String::new();

let mut line_docs: HashMap<String, String> = HashMap::new();
7 changes: 6 additions & 1 deletion generator/src/generator.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

//! Helpers to generate the code for the Parser `derive``.

use std::path::PathBuf;

use proc_macro2::TokenStream;
@@ -20,7 +22,10 @@ use pest_meta::optimizer::*;
use crate::docs::DocComment;
use crate::ParsedDerive;

pub(crate) fn generate(
/// Generates the corresponding parser based based on the processed macro input. If `include_grammar`
/// is set to true, it'll generate an explicit "include_str" statement (done in pest_derive, but
/// turned off in the local bootstrap).
pub fn generate(
parsed_derive: ParsedDerive,
paths: Vec<PathBuf>,
rules: Vec<OptimizedRule>,
19 changes: 12 additions & 7 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -26,13 +26,14 @@ use std::fs::File;
use std::io::{self, Read};
use std::path::Path;

use generator::generate;
use proc_macro2::TokenStream;
use syn::{Attribute, DeriveInput, Expr, ExprLit, Generics, Ident, Lit, Meta};

#[macro_use]
mod macros;
mod docs;
mod generator;
pub mod docs;
pub mod generator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be feature-guarded


use pest_meta::parser::{self, rename_meta_rule, Rule};
use pest_meta::{optimizer, unwrap_or_report, validator};
@@ -96,7 +97,7 @@ pub fn derive_parser(input: TokenStream, include_grammar: bool) -> TokenStream {
let ast = unwrap_or_report(parser::consume_rules(pairs));
let optimized = optimizer::optimize(ast);

generator::generate(
generate(
parsed_derive,
paths,
optimized,
@@ -119,10 +120,14 @@ enum GrammarSource {
Inline(String),
}

struct ParsedDerive {
pub(crate) name: Ident,
pub(crate) generics: Generics,
pub(crate) non_exhaustive: bool,
/// Parsed information of the derive and the attributes.
pub struct ParsedDerive {
/// The identifier of the deriving struct, union, or enum.
pub name: Ident,
/// The generics of the deriving struct, union, or enum.
pub generics: Generics,
/// Indicates whether the 'non_exhaustive' attribute is added to the 'Rule' enum.
pub non_exhaustive: bool,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this can be feature guarded -- perhaps it can be moved to some child module and here, the feature-guard would be pub use vs pub(crate) use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, a child module is a good approach. I will add a commit with the feature guard and module.


fn parse_derive(ast: DeriveInput) -> (ParsedDerive, Vec<GrammarSource>) {