pub struct CTParserBuilder<'a, LexemeT, StorageT = u32> where
    StorageT: Eq + Hash
{ /* private fields */ }
Expand description

A CTParserBuilder allows one to specify the criteria for building a statically generated parser.

Implementations

Create a new CTParserBuilder.

StorageT must be an unsigned integer type (e.g. u8, u16) which is:

  • big enough to index (separately) all the tokens, rules, productions in the grammar,
  • big enough to index the state table created from the grammar,
  • less than or equal in size to u32. In other words, if you have a grammar with 256 tokens, 256 rules, and 256 productions, which creates a state table of 256 states you can safely specify u8 here; but if any of those counts becomes 257 or greater you will need to specify u16. If you are parsing large files, the additional storage requirements of larger integer types can be noticeable, and in such cases it can be worth specifying a smaller type. StorageT defaults to u32 if unspecified.
Examples
CTParserBuilder::<DefaultLexeme<u8>, u8>::new()
    .grammar_in_src_dir("grm.y")?
    .process()?;

Set the input grammar path to a file relative to this project’s src directory. This will also set the output path (i.e. you do not need to call CTParserBuilder::output_path).

For example if a/b.y is passed as inp then CTParserBuilder::build will:

  • use src/a/b.y as the input file.
  • write output to a file which can then be imported by calling lrpar_mod!("a/b.y").
  • create a module in that output file named b_y.

You can override the output path and/or module name by calling CTParserBuilder::output_path and/or CTParserBuilder::mod_name, respectively, after calling this function.

This is a convenience function that makes it easier to compile grammar files stored in a project’s src/ directory: please see CTParserBuilder::build for additional constraints and information about the generated files. Note also that each .y file can only be processed once using this function: if you want to generate multiple grammars from a single .y file, you will need to use CTParserBuilder::output_path.

Set the input grammar path to inp. If specified, you must also call CTParserBuilder::output_path. In general it is easier to use CTParserBuilder::grammar_in_src_dir.

Set the output grammar path to outp. Note that there are no requirements on outp: the file can exist anywhere you can create a valid Path to. However, if you wish to use crate::lrpar_mod! you will need to make sure that outp is in std::env::var("OUT_DIR") or one of its subdirectories.

Set the generated module name to mod_name. If no module name is specified, CTParserBuilder::build will attempt to create a sensible default based on the grammar filename.

Set the visibility of the generated module to vis. Defaults to Visibility::Private.

Set the recoverer for this parser to rk. Defaults to RecoveryKind::CPCTPlus.

Set the YaccKind for this parser to ak.

If set to true, CTParserBuilder::build will return an error if the given grammar contains any Shift/Reduce or Reduce/Reduce conflicts. Defaults to true.

Statically compile the Yacc file specified by CTParserBuilder::grammar_path() into Rust, placing the output into the file spec CTParserBuilder::output_path(). Note that three additional files will be created with the same name as specified in [self.output_path] but with the extensions grm, and stable, overwriting any existing files with those names.

If %parse-param is not specified, the generated module follows the form:

  mod <modname> {
    pub fn parse<'lexer, 'input: 'lexer>(lexer: &'lexer dyn NonStreamingLexer<...>)
      -> (Option<ActionT>, Vec<LexParseError<...>> { ... }

    pub fn token_epp<'a>(tidx: ::cfgrammar::TIdx<StorageT>) -> ::std::option::Option<&'a str> {
      ...
    }

    ...
  }

If %parse-param x: t is specified, the generated module follows the form:

  mod <modname> {
    pub fn parse<'lexer, 'input: 'lexer>(lexer: &'lexer dyn NonStreamingLexer<...>, x: t)
      -> (Option<ActionT>, Vec<LexParseError<...>> { ... }

    pub fn token_epp<'a>(tidx: ::cfgrammar::TIdx<StorageT>) -> ::std::option::Option<&'a str> {
      ...
    }

    ...
  }

where:

  • modname is either:
    • the module name specified by CTParserBuilder::mod_name();
    • or, if no module name was explicitly specified, then for the file /a/b/c.y the module name is c_y (i.e. the file’s leaf name, minus its extension, with a prefix of _y).
  • ActionT is either:
    • if the yacckind was set to YaccKind::GrmTools or YaccKind::Original(YaccOriginalActionKind::UserAction), it is the return type of the %start rule;
    • or, if the yacckind was set to YaccKind::Original(YaccOriginalActionKind::GenericParseTree), it is crate::Node.
Panics

If StorageT is not big enough to index the grammar’s tokens, rules, or productions.

👎 Deprecated since 0.11.0:

Please use grammar_in_src_dir(), build(), and token_map() instead

Given the filename a/b.y as input, statically compile the grammar src/a/b.y into a Rust module which can then be imported using lrpar_mod!("a/b.y"). This is a convenience function around process_file which makes it easier to compile grammar files stored in a project’s src/ directory: please see process_file for additional constraints and information about the generated files.

👎 Deprecated since 0.11.0:

Please use grammar_path(), output_path(), and process() instead

Statically compile the Yacc file inp into Rust, placing the output into the file outp. Note that three additional files will be created with the same name as outp but with the extensions grm, and stable, overwriting any existing files with those names.

outp defines a module as follows:

  mod modname {
    pub fn parse(lexemes: &::std::vec::Vec<::lrpar::Lexeme<StorageT>>) { ... }
        -> (::std::option::Option<ActionT>,
            ::std::vec::Vec<::lrpar::LexParseError<StorageT>>)> { ...}

    pub fn token_epp<'a>(tidx: ::cfgrammar::TIdx<StorageT>) -> ::std::option::Option<&'a str> {
      ...
    }

    ...
  }

where:

  • modname is either:
    • the module name specified mod_name
    • or, if no module name was explicitly specified, then for the file /a/b/c.y the module name is c_y (i.e. the file’s leaf name, minus its extension, with a prefix of _y).
  • ActionT is either:
    • the %actiontype value given to the grammar
    • or, if the yacckind was set YaccKind::Original(YaccOriginalActionKind::UserAction), it is Node<StorageT>
Panics

If StorageT is not big enough to index the grammar’s tokens, rules, or productions.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.