Skip to content

Commit 2762b8e

Browse files
committed
Deprecate lrpar::Node, moving it into the mod of lrpar_mod!
1 parent f8f5157 commit 2762b8e

File tree

6 files changed

+107
-18
lines changed

6 files changed

+107
-18
lines changed

lrpar/examples/calc_parsetree/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::{self, BufRead, Write};
22

33
use cfgrammar::RIdx;
44
use lrlex::{DefaultLexeme, lrlex_mod};
5-
use lrpar::{Lexeme, Node, lrpar_mod};
5+
use lrpar::{Lexeme, lrpar_mod};
66

77
// Using `lrlex_mod!` brings the lexer for `calc.l` into scope. By default the module name will be
88
// `calc_l` (i.e. the file name, minus any extensions, with a suffix of `_l`).
@@ -11,6 +11,8 @@ lrlex_mod!("calc.l");
1111
// `calc_y` (i.e. the file name, minus any extensions, with a suffix of `_y`).
1212
lrpar_mod!("calc.y");
1313

14+
use calc_y::Node;
15+
1416
fn main() {
1517
// Get the `LexerDef` for the `calc` language.
1618
let lexerdef = calc_l::lexerdef();

lrpar/examples/start_states/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::io::{self, BufRead, Write};
22

33
use cfgrammar::RIdx;
44
use lrlex::{DefaultLexeme, lrlex_mod};
5-
use lrpar::{Lexeme, Node, lrpar_mod};
5+
use lrpar::{Lexeme, lrpar_mod};
66

77
// Using `lrlex_mod!` brings the lexer for `comment.l` into scope. By default the module name will be
88
// `comment_l` (i.e. the file name, minus any extensions, with a suffix of `_l`).
@@ -11,6 +11,8 @@ lrlex_mod!("comment.l");
1111
// `comment_y` (i.e. the file name, minus any extensions, with a suffix of `_y`).
1212
lrpar_mod!("comment.y");
1313

14+
use comment_y::Node;
15+
1416
fn main() {
1517
// Get the `LexerDef` for the `comment` language.
1618
let lexerdef = comment_l::lexerdef();

lrpar/src/lib/ctbuilder.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ where
522522
/// the return type of the `%start` rule;
523523
/// * or, if the `yacckind` was set to
524524
/// `YaccKind::Original(YaccOriginalActionKind::GenericParseTree)`, it
525-
/// is [`crate::Node<StorageT>`].
525+
/// is [`modname::Node<StorageT>`].
526526
///
527527
/// # Panics
528528
///
@@ -958,6 +958,21 @@ where
958958
| YaccKind::Original(YaccOriginalActionKind::GenericParseTree) => None,
959959
_ => unreachable!(),
960960
};
961+
962+
let additional_decls =
963+
if let Some(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)) =
964+
self.yacckind
965+
{
966+
// `lrpar::Node`` is deprecated within the lrpar crate, but not from within this module,
967+
// Once it is removed from `lrpar`, we should move the declaration here entirely.
968+
Some(quote! {
969+
#[allow(unused_imports)]
970+
pub use ::lrpar::parser::_deprecated_moved_::Node;
971+
})
972+
} else {
973+
None
974+
};
975+
961976
let mod_name = format_ident!("{}", mod_name);
962977
let out_tokens = quote! {
963978
#visibility mod #mod_name {
@@ -969,6 +984,7 @@ where
969984
#![deny(unsafe_code)]
970985
#[allow(unused_imports)]
971986
use super::*;
987+
#additional_decls
972988
#parse_function
973989
#rule_consts
974990
#token_epp
@@ -977,7 +993,7 @@ where
977993
#[allow(unused_imports)]
978994
pub use _parser_::*;
979995
#[allow(unused_imports)]
980-
use ::lrpar::{Lexeme, Node};
996+
use ::lrpar::Lexeme;
981997
} // End of `mod #mod_name`
982998
};
983999
// Try and run a code formatter on the generated code.
@@ -1164,7 +1180,7 @@ where
11641180
}
11651181
}
11661182
YaccKind::Original(YaccOriginalActionKind::GenericParseTree) => quote! {
1167-
(::std::option::Option<::lrpar::Node<<#lexertypest as ::lrpar::LexerTypes>::LexemeT, #storaget>>,
1183+
(::std::option::Option<crate::Node<<#lexertypest as ::lrpar::LexerTypes>::LexemeT, #storaget>>,
11681184
::std::vec::Vec<::lrpar::LexParseError<#storaget, #lexertypest>>)
11691185
},
11701186
YaccKind::Original(YaccOriginalActionKind::NoAction) => quote! {

lrpar/src/lib/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,15 @@ pub mod test_utils;
208208
pub use crate::{
209209
ctbuilder::{CTParser, CTParserBuilder, RustEdition, Visibility},
210210
lex_api::{LexError, Lexeme, Lexer, LexerTypes, NonStreamingLexer},
211-
parser::{LexParseError, Node, ParseError, ParseRepair, RTParserBuilder, RecoveryKind},
211+
parser::{LexParseError, ParseError, ParseRepair, RTParserBuilder, RecoveryKind},
212212
};
213213

214214
#[allow(deprecated)]
215215
pub use crate::parser::action_generictree;
216+
217+
#[allow(deprecated)]
218+
pub use parser::Node;
219+
216220
/// A convenience macro for including statically compiled `.y` files. A file `src/a/b/c.y`
217221
/// processed by [CTParserBuilder::grammar_in_src_dir] can then be used in a crate with
218222
/// `lrpar_mod!("a/b/c.y")`.

lrpar/src/lib/parser.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,29 @@ const RECOVERY_TIME_BUDGET: u64 = 60_000; // milliseconds
2929
#[cfg(not(test))]
3030
const RECOVERY_TIME_BUDGET: u64 = 500; // milliseconds
3131

32-
/// A generic parse tree.
33-
#[derive(Debug, Clone, PartialEq)]
34-
pub enum Node<LexemeT: Lexeme<StorageT>, StorageT> {
35-
/// Terminals store a single lexeme.
36-
Term { lexeme: LexemeT },
37-
/// Nonterminals reference a rule and have zero or more `Node`s as children.
38-
Nonterm {
39-
ridx: RIdx<StorageT>,
40-
nodes: Vec<Node<LexemeT, StorageT>>,
41-
},
32+
#[deprecated(
33+
since = "0.14",
34+
note = "Use the version of `Node` exported from your `lrpar_mod!`"
35+
)]
36+
pub type Node<T, S> = _deprecated_moved_::Node<T, S>;
37+
38+
#[doc(hidden)]
39+
pub mod _deprecated_moved_ {
40+
use super::*;
41+
/// A generic parse tree.
42+
#[derive(Debug, Clone, PartialEq)]
43+
pub enum Node<LexemeT: Lexeme<StorageT>, StorageT> {
44+
/// Terminals store a single lexeme.
45+
Term { lexeme: LexemeT },
46+
/// Nonterminals reference a rule and have zero or more `Node`s as children.
47+
Nonterm {
48+
ridx: RIdx<StorageT>,
49+
nodes: Vec<Node<LexemeT, StorageT>>,
50+
},
51+
}
4252
}
4353

54+
#[allow(deprecated)]
4455
impl<LexemeT: Lexeme<StorageT>, StorageT: 'static + PrimInt + Unsigned> Node<LexemeT, StorageT>
4556
where
4657
usize: AsPrimitive<StorageT>,
@@ -210,6 +221,7 @@ where
210221
since = "0.14",
211222
note = "Deprecated with `parse_generictree` there is no direct replacement, besides a custom action"
212223
)]
224+
#[allow(deprecated)]
213225
/// The action which implements [`cfgrammar::yacc::YaccOriginalActionKind::GenericParseTree`].
214226
/// Usually you should just use the action kind directly. But you can also call this from
215227
/// within a custom action to return a generic parse tree with custom behavior.
@@ -529,6 +541,7 @@ where
529541
/// Note that if `lexeme_prefix` is specified, `laidx` will still be incremented, and thus
530542
/// `end_laidx` *must* be set to `laidx + 1` in order that the parser doesn't skip the real
531543
/// lexeme at position `laidx`.
544+
#[allow(deprecated)]
532545
pub(super) fn lr_cactus(
533546
&self,
534547
lexeme_prefix: Option<LexerTypesT::LexemeT>,
@@ -927,6 +940,7 @@ where
927940
since = "0.14",
928941
note = "Use `parse_map` to return a `lrpar::Node` instead"
929942
)]
943+
#[allow(deprecated)]
930944
/// Parse input, and (if possible) return a generic parse tree. See the arguments for
931945
/// [`parse_actions`](#method.parse_actions) for more details about the return value.
932946
pub fn parse_generictree(
@@ -1082,6 +1096,7 @@ pub(crate) mod test {
10821096
test_utils::{TestLexError, TestLexeme, TestLexerTypes},
10831097
};
10841098

1099+
#[allow(deprecated)]
10851100
pub(crate) fn do_parse<'input>(
10861101
rcvry_kind: RecoveryKind,
10871102
lexs: &str,
@@ -1101,6 +1116,7 @@ pub(crate) mod test {
11011116
do_parse_with_costs(rcvry_kind, lexs, grms, input, &HashMap::new())
11021117
}
11031118

1119+
#[allow(deprecated)]
11041120
fn do_parse_with_costs<'input>(
11051121
rcvry_kind: RecoveryKind,
11061122
lexs: &str,

nimbleparse/src/main.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ use cfgrammar::{
33
header::{GrmtoolsSectionParser, Header, HeaderError, HeaderValue, Value},
44
markmap::Entry,
55
yacc::{YaccGrammar, YaccKind, YaccOriginalActionKind, ast::ASTWithValidityInfo},
6+
RIdx, TIdx,
67
};
78
use getopts::Options;
89
use lrlex::{DefaultLexerTypes, LRLexError, LRNonStreamingLexerDef, LexerDef};
910
use lrpar::{
10-
LexerTypes, Node,
11+
LexerTypes,
1112
diagnostics::{DiagnosticFormatter, SpannedDiagnosticFormatter},
1213
parser::{RTParserBuilder, RecoveryKind},
14+
Lexeme
1315
};
1416
use lrtable::{Minimiser, StateTable, from_yacc};
15-
use num_traits::AsPrimitive;
17+
use num_traits::{PrimInt, AsPrimitive, Unsigned};
1618
use num_traits::ToPrimitive as _;
1719
use std::{
1820
env,
@@ -22,8 +24,55 @@ use std::{
2224
io::Read,
2325
path::{Path, PathBuf},
2426
process,
27+
fmt::Write,
2528
};
2629

30+
31+
/// A generic parse tree.
32+
#[derive(Debug, Clone, PartialEq)]
33+
pub enum Node<LexemeT: Lexeme<StorageT>, StorageT> {
34+
/// Terminals store a single lexeme.
35+
Term { lexeme: LexemeT },
36+
/// Nonterminals reference a rule and have zero or more `Node`s as children.
37+
Nonterm {
38+
ridx: RIdx<StorageT>,
39+
nodes: Vec<Node<LexemeT, StorageT>>,
40+
},
41+
}
42+
43+
#[allow(deprecated)]
44+
impl<LexemeT: Lexeme<StorageT>, StorageT: 'static + PrimInt + Unsigned> Node<LexemeT, StorageT>
45+
where
46+
usize: AsPrimitive<StorageT>,
47+
{
48+
/// Return a pretty-printed version of this node.
49+
pub fn pp(&self, grm: &YaccGrammar<StorageT>, input: &str) -> String {
50+
let mut st = vec![(0, self)]; // Stack of (indent level, node) pairs
51+
let mut s = String::new();
52+
while let Some((indent, e)) = st.pop() {
53+
for _ in 0..indent {
54+
s.push(' ');
55+
}
56+
match *e {
57+
Node::Term { lexeme } => {
58+
let tidx = TIdx(lexeme.tok_id());
59+
let tn = grm.token_name(tidx).unwrap();
60+
let lt = &input[lexeme.span().start()..lexeme.span().end()];
61+
writeln!(s, "{} {}", tn, lt).ok();
62+
}
63+
Node::Nonterm { ridx, ref nodes } => {
64+
writeln!(s, "{}", grm.rule_name_str(ridx)).ok();
65+
for x in nodes.iter().rev() {
66+
st.push((indent + 1, x));
67+
}
68+
}
69+
}
70+
}
71+
s
72+
}
73+
}
74+
75+
2776
const WARNING: &str = "[Warning]";
2877
const ERROR: &str = "[Error]";
2978

0 commit comments

Comments
 (0)