Skip to content

Commit b35d714

Browse files
committed
fix minor bugs:
fix(parser) error 'use of moved value' for same mapto name in group pattern fix(core) assert macro to debug_asert macro fix(ruety_lr, core) `shift` predefined variable's default value is whether the shift action is possible. fix(parser) surpress warnings for generated `Stack` fix(parser) add explicit typename to __rustylr_terminals remove(parser) don't print expected tokens on grammar parse error fix lib.rs doc comments typo fix(core,parser) remove rule_id fix `%lalr` was not present in QuickReference
1 parent 4d5abed commit b35d714

File tree

17 files changed

+70
-108
lines changed

17 files changed

+70
-108
lines changed

SYNTAX.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [`%left`, `%right`](#reduce-type-optional)
1515
- [`%err`, `%error`](#error-type-optional)
1616
- [`%glr`](#glr-parser-generation)
17+
- [`%lalr`](#lalr-parser-generation)
1718

1819

1920
---

example/lrtest/src/main.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ fn main() {
55

66
let _ = grammar.set_reduce_type('0', ReduceType::Right);
77

8-
grammar.add_rule(
9-
"Num",
10-
vec![Token::NonTerm("Digit"), Token::NonTerm("Num")],
11-
0,
12-
);
13-
grammar.add_rule("Num", vec![Token::NonTerm("Digit")], 0);
8+
grammar.add_rule("Num", vec![Token::NonTerm("Digit"), Token::NonTerm("Num")]);
9+
grammar.add_rule("Num", vec![Token::NonTerm("Digit")]);
1410

15-
grammar.add_rule("Digit", vec![Token::Term('0')], 0);
11+
grammar.add_rule("Digit", vec![Token::Term('0')]);
1612

17-
grammar.add_rule("Token", vec![Token::NonTerm("Num")], 0);
13+
grammar.add_rule("Token", vec![Token::NonTerm("Num")]);
1814
grammar.add_rule(
1915
"Tokens",
2016
vec![Token::NonTerm("Token"), Token::NonTerm("Tokens")],
21-
0,
2217
);
23-
grammar.add_rule("Tokens", vec![Token::NonTerm("Token")], 0);
18+
grammar.add_rule("Tokens", vec![Token::NonTerm("Token")]);
2419

25-
grammar.add_rule("Aug", vec![Token::NonTerm("Tokens"), Token::Term('\0')], 0);
20+
grammar.add_rule("Aug", vec![Token::NonTerm("Tokens"), Token::Term('\0')]);
2621

2722
match grammar.build("Aug") {
2823
Ok(_) => {

rusty_lr/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rusty_lr"
3-
version = "3.1.0"
3+
version = "3.1.1"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "GLR, LR(1) and LALR(1) parser generator with custom reduce action"
@@ -10,9 +10,9 @@ keywords = ["parser", "bison", "lr", "glr", "compiler"]
1010
categories = ["parsing", "compilers", "parser-implementations"]
1111

1212
[dependencies]
13-
rusty_lr_core = { version = "3.1.0", path = "../rusty_lr_core" }
14-
rusty_lr_derive = { version = "2.1.0", path = "../rusty_lr_derive", optional = true }
15-
rusty_lr_buildscript = { version = "0.19.0", path = "../rusty_lr_buildscript", optional = true }
13+
rusty_lr_core = { version = "3.2.0", path = "../rusty_lr_core" }
14+
rusty_lr_derive = { version = "2.2.0", path = "../rusty_lr_derive", optional = true }
15+
rusty_lr_buildscript = { version = "0.20.0", path = "../rusty_lr_buildscript", optional = true }
1616

1717
[features]
1818
default = ["derive"]

rusty_lr/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
//! The `Context` struct has the following functions:
8484
//! - `feed(&mut self, &Parser, TerminalType, &mut UserData) -> Result<(), ParseError>` : feed token to the parser
8585
//!
86-
//! Node that `UserData` is `()` by default, unless it is defined by [`%userdata`](#userdata-type-optional) directive.
86+
//! Note that `UserData` is `()` by default, unless it is defined by [`%userdata`](#userdata-type-optional) directive.
8787
//! All you need to do is to call `new()` to generate the parser, a context.
8888
//! Then, you can feed the input sequence one by one with `feed()` function.
8989
//! Once the input sequence is feeded (including `eof` token), without errors,
@@ -267,6 +267,7 @@
267267
//! - [`%left`, `%right`](#reduce-type-optional)
268268
//! - [`%err`, `%error`](#error-type-optional)
269269
//! - [`%glr`](#glr-parser-generation)
270+
//! - [`%lalr`](#lalr-parser-generation)
270271
//!
271272
//!
272273
//! ---
@@ -374,7 +375,8 @@
374375
//! **predefined variables** can be used in `ReduceAction`:
375376
//! - `data` ( `&mut UserData` ) : userdata passed to the `feed()` function.
376377
//! - `lookahead` ( `&Term` ) : lookahead token that caused the reduce action.
377-
//! - `shift` ( `&mut bool` ) : revoke the shift action if set to `false`. See [Resolving Ambiguities](#resolving-ambiguities) section.
378+
//! - `shift` ( `&mut bool` ) : revoke the shift action if set to `false`. Default value is whether the shift action is possible.
379+
//! See [Resolving Ambiguities](#resolving-ambiguities) section.
378380
//!
379381
//! To access the data of each token, you can directly use the name of the token as a variable.
380382
//! - For non-terminal symbols, the type of variable is `RuleType`.

rusty_lr_buildscript/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rusty_lr_buildscript"
3-
version = "0.19.0"
3+
version = "0.20.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "buildscipt tools for rusty_lr"
@@ -11,8 +11,8 @@ categories = ["parsing"]
1111

1212

1313
[dependencies]
14-
rusty_lr_parser = { version = "3.22.0", path = "../rusty_lr_parser" }
15-
rusty_lr_core = { version = "3.1.0", path = "../rusty_lr_core", features = [
14+
rusty_lr_parser = { version = "3.23.0", path = "../rusty_lr_parser" }
15+
rusty_lr_core = { version = "3.2.0", path = "../rusty_lr_core", features = [
1616
"builder",
1717
] }
1818
codespan-reporting = "0.11"

rusty_lr_core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rusty_lr_core"
3-
version = "3.1.0"
3+
version = "3.2.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "core library for rusty_lr"

rusty_lr_core/src/builder/grammar.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,28 @@ impl<Term, NonTerm> Grammar<Term, NonTerm> {
4949
}
5050

5151
/// add new production rule for given nonterminal 'name'
52-
pub fn add_rule(&mut self, name: NonTerm, rule: Vec<Token<Term, NonTerm>>, id: usize) -> usize
52+
pub fn add_rule(&mut self, name: NonTerm, rule: Vec<Token<Term, NonTerm>>) -> usize
5353
where
5454
NonTerm: Clone + Hash + Eq,
5555
{
5656
let index = self.rules.len();
5757
self.rules_map.entry(name.clone()).or_default().push(index);
58-
let rule = ProductionRule { name, rule, id };
58+
let rule = ProductionRule { name, rule };
5959
self.rules.push((rule, None));
6060
index
6161
}
6262
pub fn add_rule_with_lookaheads(
6363
&mut self,
6464
name: NonTerm,
6565
rule: Vec<Token<Term, NonTerm>>,
66-
id: usize,
6766
lookaheads: BTreeSet<Term>,
6867
) -> usize
6968
where
7069
NonTerm: Clone + Hash + Eq,
7170
{
7271
let index = self.rules.len();
7372
self.rules_map.entry(name.clone()).or_default().push(index);
74-
let rule = ProductionRule { name, rule, id };
73+
let rule = ProductionRule { name, rule };
7574
self.rules.push((rule, Some(lookaheads)));
7675
index
7776
}

rusty_lr_core/src/glr/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ where
6969
Rc::clone(&node),
7070
context,
7171
&term,
72+
next_term_shift_state.is_some(),
7273
userdata,
7374
);
7475
}
@@ -79,6 +80,7 @@ where
7980
Rc::clone(&node),
8081
context,
8182
&term,
83+
true,
8284
userdata,
8385
);
8486
if shift_for_this_node {
@@ -97,7 +99,15 @@ where
9799
.push(Rc::new(next_node));
98100
}
99101
} else {
100-
reduce(parser, reduce_rules[0], node, context, &term, userdata);
102+
reduce(
103+
parser,
104+
reduce_rules[0],
105+
node,
106+
context,
107+
&term,
108+
false,
109+
userdata,
110+
);
101111
}
102112
}
103113
} else if let Some(next_term_shift_state) = next_term_shift_state {
@@ -216,6 +226,7 @@ fn reduce<P: Parser, Data: NodeData<Term = P::Term, NonTerm = P::NonTerm> + Clon
216226
node: Rc<Node<Data>>,
217227
context: &mut Context<Data>,
218228
term: &P::Term,
229+
has_shift: bool,
219230
userdata: &mut Data::UserData,
220231
) -> bool
221232
where
@@ -230,7 +241,7 @@ where
230241
#[cfg(not(feature = "tree"))]
231242
let parent = data_extracted;
232243

233-
let mut do_shift = true;
244+
let mut do_shift = has_shift;
234245
match Data::new_nonterm(
235246
reduce_rule,
236247
&mut context.reduce_args,

rusty_lr_core/src/glr/node.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ impl<Data: NodeData> Node<Data> {
8383
/// This function should not be called from root node.
8484
#[cfg(feature = "tree")]
8585
pub fn to_tree(&self) -> &Tree<Data::Term, Data::NonTerm> {
86-
assert!(self.parent.is_some());
86+
debug_assert!(self.parent.is_some());
8787
self.tree.as_ref().unwrap()
8888
}
8989
/// Get token tree for this node.
9090
/// This function should not be called from root node.
9191
#[cfg(feature = "tree")]
9292
pub fn into_tree(self) -> Tree<Data::Term, Data::NonTerm> {
93-
assert!(self.parent.is_some());
93+
debug_assert!(self.parent.is_some());
9494
self.tree.unwrap()
9595
}
9696

@@ -113,13 +113,13 @@ impl<Data: NodeData> Node<Data> {
113113
/// Get data for this node.
114114
/// This function should not be called from root node.
115115
pub fn to_data(&self) -> &Data {
116-
assert!(self.parent.is_some());
116+
debug_assert!(self.parent.is_some());
117117
self.data.as_ref().unwrap()
118118
}
119119
/// Get data for this node.
120120
/// This function should not be called from root node.
121121
pub fn into_data(self) -> Data {
122-
assert!(self.parent.is_some());
122+
debug_assert!(self.parent.is_some());
123123
self.data.unwrap()
124124
}
125125
/// Get list of data from root to this node.

rusty_lr_core/src/rule.rs

-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ impl Display for ReduceType {
3030
pub struct ProductionRule<Term, NonTerm> {
3131
pub name: NonTerm,
3232
pub rule: Vec<Token<Term, NonTerm>>,
33-
/// id user assigned to this rule
34-
pub id: usize,
3533
}
3634
impl<Term: Display, NonTerm: Display> Display for ProductionRule<Term, NonTerm> {
3735
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -76,7 +74,6 @@ impl<Term, NonTerm> ProductionRule<Term, NonTerm> {
7674
Token::NonTerm(nonterm) => Token::NonTerm(nonterm_map(nonterm)),
7775
})
7876
.collect(),
79-
id: self.id,
8077
}
8178
}
8279
}

rusty_lr_derive/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rusty_lr_derive"
3-
version = "2.1.0"
3+
version = "2.2.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "proc-macro definitions for rusty_lr"
@@ -14,7 +14,7 @@ proc-macro = true
1414

1515
[dependencies]
1616
proc-macro2 = "1.0.86"
17-
rusty_lr_parser = { version = "3.22.0", path = "../rusty_lr_parser" }
17+
rusty_lr_parser = { version = "3.23.0", path = "../rusty_lr_parser" }
1818

1919

2020
[features]

rusty_lr_executable/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustylr"
3-
version = "1.0.1"
3+
version = "1.0.2"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "executable for rusty_lr"
@@ -11,6 +11,6 @@ categories = ["parsing"]
1111

1212
[dependencies]
1313
clap = { version = "4.5.7", features = ["derive"] }
14-
rusty_lr_buildscript = { version = "0.19.0", path = "../rusty_lr_buildscript", features = [
14+
rusty_lr_buildscript = { version = "0.20.0", path = "../rusty_lr_buildscript", features = [
1515
"fxhash",
1616
] }

rusty_lr_parser/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rusty_lr_parser"
3-
version = "3.22.0"
3+
version = "3.23.0"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "macro line parser for rusty_lr"
@@ -12,7 +12,7 @@ categories = ["parsing"]
1212
[dependencies]
1313
proc-macro2 = "1.0.86"
1414
quote = "1.0"
15-
rusty_lr_core = { version = "3.1.0", path = "../rusty_lr_core", features = [
15+
rusty_lr_core = { version = "3.2.0", path = "../rusty_lr_core", features = [
1616
"builder",
1717
] }
1818

rusty_lr_parser/src/emit.rs

+10-25
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ impl Grammar {
288288
// =====================================================================
289289
let mut tokens_initializer = TokenStream::new();
290290
let mut rule_names_initializer = TokenStream::new();
291-
let mut rule_id_initializer = TokenStream::new();
292291

293292
{
294293
for rule in rules.into_iter() {
@@ -314,10 +313,6 @@ impl Grammar {
314313
rule_names_initializer.extend(quote! {
315314
#nonterminals_enum_name::#name,
316315
});
317-
let id = Literal::usize_unsuffixed(rule.id);
318-
rule_id_initializer.extend(quote! {
319-
#id,
320-
});
321316
}
322317
};
323318

@@ -491,29 +486,20 @@ impl Grammar {
491486
}
492487
};
493488

494-
let rule_id_typename = integer_typename(
495-
self.nonterminals
496-
.iter()
497-
.map(|nonterm| nonterm.rules.iter().map(|rule| rule.id).max().unwrap())
498-
.max()
499-
.unwrap()
500-
+ 1,
501-
);
502489
let reduce_terminals_cache_typename = integer_typename(reduce_terminals_cache_count);
503490
let ruleset0_cache_typename = integer_typename(ruleset0_cache_count);
504491

492+
let token_typename = &self.token_typename;
493+
505494
Ok(quote! {
506-
let __rustylr_terminals = vec![#comma_separated_terminals];
495+
let __rustylr_terminals:Vec<#token_typename> = vec![#comma_separated_terminals];
507496
const RUSTYLR_RULES_TOKENS: &[&[#module_prefix::Token<#terminal_index_typename, #nonterminals_enum_name>]] = &[#tokens_initializer];
508497
const RUSTYLR_RULES_NAME: &[#nonterminals_enum_name] = &[#rule_names_initializer];
509-
const RUSTYLR_RULES_ID: &[#rule_id_typename] = &[#rule_id_initializer];
510498

511499
let rules: Vec<#rule_typename> = RUSTYLR_RULES_NAME.iter().zip(
512-
RUSTYLR_RULES_TOKENS.iter().zip(
513-
RUSTYLR_RULES_ID.iter()
514-
)
500+
RUSTYLR_RULES_TOKENS.iter()
515501
).map(
516-
| (name, (tokens, id)) | {
502+
| (name, tokens) | {
517503
#rule_typename {
518504
name: *name,
519505
rule: tokens.iter().map(
@@ -524,7 +510,6 @@ impl Grammar {
524510
}
525511
}
526512
).collect(),
527-
id: *id as usize,
528513
}
529514
}
530515
).collect();
@@ -800,15 +785,15 @@ impl Grammar {
800785
/// struct that holds internal parser data,
801786
/// including data stack for each non-terminal,
802787
/// and state stack for DFA
803-
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut)]
788+
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, non_snake_case, non_camel_case_types)]
804789
pub struct #stack_struct_name {
805790
#stack_def_streams
806791
}
807-
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, dead_code)]
792+
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, non_snake_case, non_camel_case_types, dead_code)]
808793
impl #stack_struct_name {
809794
#fn_reduce_for_each_rule_stream
810795
}
811-
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, dead_code)]
796+
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, non_snake_case, non_camel_case_types, dead_code)]
812797
impl #module_prefix::lr::Stack for #stack_struct_name {
813798
type Term = #token_typename;
814799
type NonTerm = #nonterminals_enum_name;
@@ -844,7 +829,7 @@ impl Grammar {
844829
}
845830

846831
/// struct that holds parser data, DFA tables
847-
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut)]
832+
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, dead_code)]
848833
pub struct #parser_struct_name {
849834
/// production rules
850835
pub rules: Vec<#rule_typename>,
@@ -863,7 +848,7 @@ impl Grammar {
863848
}
864849
}
865850

866-
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut)]
851+
#[allow(unused_braces, unused_parens, unused_variables, non_snake_case, unused_mut, dead_code)]
867852
impl #parser_struct_name {
868853
/// Create new parser instance.
869854
/// Parser can be reused with different context, for multiple parsing.

0 commit comments

Comments
 (0)