pub struct LemonMintBuilder { /* private fields */ }
Expand description
Builder class that will finally generate LemonMint
. Call builder methods to supply parser rules and options - everything that you would normally put to Lemon’s Y-grammar file.
Or you can feed the Y-file itself (it’s syntax is similar to Lemon’s one).
§Example
use lemon_mint::LemonMintBuilder;
use std::sync::Arc;
let fake_filename = Arc::new("source.y".to_string()); // will appear in error messages
let builder = LemonMintBuilder::new().load_y(&fake_filename, "%token_type {f64}\nUnit ::= NEW_LINE.".as_bytes()).unwrap();
let lemon = builder.try_into_lemon().unwrap();
Or:
use lemon_mint::LemonMintBuilder;
use std::sync::Arc;
let fake_filename = Arc::new("source.y".to_string()); // will appear in error messages
let builder = LemonMintBuilder::new()
.set_token_type("f64".to_string()).unwrap()
.add_rule(&fake_filename, 1, "Unit".to_string(), "NEW_LINE", "".to_string()).unwrap();
let lemon = builder.try_into_lemon().unwrap();
Implementations§
Source§impl LemonMintBuilder
impl LemonMintBuilder
Sourcepub fn load_y_file(self, filename: &Arc<String>) -> Result<Self, LemonMintError>
pub fn load_y_file(self, filename: &Arc<String>) -> Result<Self, LemonMintError>
Load a Y-grammar file. You can call this function several times to load grammar by parts, and you can call other builder methods to add/override settings.
Sourcepub fn load_y<R>(
self,
filename: &Arc<String>,
input: R,
) -> Result<Self, LemonMintError>where
R: Read,
pub fn load_y<R>(
self,
filename: &Arc<String>,
input: R,
) -> Result<Self, LemonMintError>where
R: Read,
Like load_y_file(), but you give file contents as io::Read object.
§Example
use lemon_mint::LemonMintBuilder;
use std::sync::Arc;
let fake_filename = Arc::new("source.y".to_string()); // will appear in error messages
let builder = LemonMintBuilder::new().load_y(&fake_filename, "%token_type {f64}\nUnit ::= NEW_LINE.".as_bytes()).unwrap();
Sourcepub fn set_start_symbol(
self,
filename: &Arc<String>,
n_line: usize,
value: String,
) -> Result<Self, LemonMintError>
pub fn set_start_symbol( self, filename: &Arc<String>, n_line: usize, value: String, ) -> Result<Self, LemonMintError>
Set the parser “%start_symbol”.
Sourcepub fn set_token_type(self, value: String) -> Result<Self, LemonMintError>
pub fn set_token_type(self, value: String) -> Result<Self, LemonMintError>
Set the parser “%token_type”.
Sourcepub fn set_default_type(self, value: String) -> Result<Self, LemonMintError>
pub fn set_default_type(self, value: String) -> Result<Self, LemonMintError>
Set the parser “%default_type”.
Sourcepub fn set_trace_prompt(self, value: String) -> Result<Self, LemonMintError>
pub fn set_trace_prompt(self, value: String) -> Result<Self, LemonMintError>
Enable trace, and set prompt that will be printed before each message. The prompt can be empty string.
Sourcepub fn set_extra_argument_type(
self,
value: String,
) -> Result<Self, LemonMintError>
pub fn set_extra_argument_type( self, value: String, ) -> Result<Self, LemonMintError>
Set the parser %extra_argument. Only it’s type, like:
let builder = LemonMintBuilder::new().set_extra_argument_type("String".to_string()).unwrap();
In your rust code that uses the generated parser, you initialize the extra argument when you create a Parser object:
%code {
fn main()
{ let mut parser = Parser::new("Initial value".to_string()); // Initial value of the extra argument
assert_eq!(parser.extra, "Initial value".to_string());
}
}
In actions code, the extra argument is available as variable &mut extra
:
%token_type {String}
Unit ::= NEW_LINE(tok). extra.push_str(&tok);
Sourcepub fn set_left(
self,
filename: &Arc<String>,
n_line: usize,
symbol_names: &str,
) -> Result<Self, LemonMintError>
pub fn set_left( self, filename: &Arc<String>, n_line: usize, symbol_names: &str, ) -> Result<Self, LemonMintError>
Add the parser “%left A B C”.
§Example
let fake_filename = Arc::new("source.y".to_string()); // will appear in error messages
let builder = LemonMintBuilder::new().set_left(&fake_filename, 1, "PLUS MINUS").unwrap().set_left(&fake_filename, 2, "TIMES DIVIDE").unwrap();
- symbol_names - Terminal symbol names, separated by whitespace characters. All the symbols will have the same precedence.
Further calls to
set_left()
,set_right()
orset_nonassoc()
will set higher precedence.
Sourcepub fn set_right(
self,
filename: &Arc<String>,
n_line: usize,
symbol_names: &str,
) -> Result<Self, LemonMintError>
pub fn set_right( self, filename: &Arc<String>, n_line: usize, symbol_names: &str, ) -> Result<Self, LemonMintError>
Add the parser “%right A B C”.
Sourcepub fn set_nonassoc(
self,
filename: &Arc<String>,
n_line: usize,
symbol_names: &str,
) -> Result<Self, LemonMintError>
pub fn set_nonassoc( self, filename: &Arc<String>, n_line: usize, symbol_names: &str, ) -> Result<Self, LemonMintError>
Add the parser “%nonassoc A B C”.
Sourcepub fn add_type(
self,
filename: &Arc<String>,
n_line: usize,
symbol_name: String,
type_name: String,
) -> Result<Self, LemonMintError>
pub fn add_type( self, filename: &Arc<String>, n_line: usize, symbol_name: String, type_name: String, ) -> Result<Self, LemonMintError>
Add the parser “%type symbol_name {Type}”.
Sourcepub fn get_type(&self, symbol_name: &str) -> Option<(Arc<String>, usize)>
pub fn get_type(&self, symbol_name: &str) -> Option<(Arc<String>, usize)>
Return Some((filename, n_line))
if given symbol was added through add_type()
.
Sourcepub fn add_fallback(
self,
filename: &Arc<String>,
n_line: usize,
fallback_to_symbol_name: String,
symbol_names: &str,
) -> Result<Self, LemonMintError>
pub fn add_fallback( self, filename: &Arc<String>, n_line: usize, fallback_to_symbol_name: String, symbol_names: &str, ) -> Result<Self, LemonMintError>
Add the parser “%fallback FB A B C”.
Sourcepub fn add_rule(
self,
filename: &Arc<String>,
n_line: usize,
lhs_name: String,
rhs_names_aliases: &str,
code: String,
) -> Result<Self, LemonMintError>
pub fn add_rule( self, filename: &Arc<String>, n_line: usize, lhs_name: String, rhs_names_aliases: &str, code: String, ) -> Result<Self, LemonMintError>
Add parser rule like “a”, “b(v_b) COMMA c(v_c)”, “vec![v_b, v_c]”
Sourcepub fn add_code(self, code: String) -> Result<Self, LemonMintError>
pub fn add_code(self, code: String) -> Result<Self, LemonMintError>
Add the parser %code or %include
Sourcepub fn set_no_compress(
self,
new_no_compress: bool,
) -> Result<Self, LemonMintError>
pub fn set_no_compress( self, new_no_compress: bool, ) -> Result<Self, LemonMintError>
Allows to disable finite-state machine tables compression. Don’t do this.
Sourcepub fn set_no_resort(self, new_no_resort: bool) -> Result<Self, LemonMintError>
pub fn set_no_resort(self, new_no_resort: bool) -> Result<Self, LemonMintError>
Disable resorting states. You don’t need this functionality.
Sourcepub fn try_into_lemon(self) -> Result<LemonMint, LemonMintError>
pub fn try_into_lemon(self) -> Result<LemonMint, LemonMintError>
When you feed all the parser rules and options, call this method to finally build the parser finite-state machine transition tables.
If there are problems with your grammar or if there are parser conflicts, this will return Err.
If parser build succeeds this returns the parser representation as LemonMint
object.