Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion cfgrammar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "cfgrammar"
description = "Grammar manipulation"
repository = "https://github.com/softdevteam/grmtools"
version = "0.13.10"
edition = "2021"
edition = "2024"
readme = "README.md"
license = "Apache-2.0/MIT"
categories = ["parsing"]
Expand Down
4 changes: 2 additions & 2 deletions cfgrammar/src/lib/header.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
Location, Span, Spanned,
markmap::{Entry, MarkMap},
yacc::{
parser::SpansKind, YaccGrammarError, YaccGrammarErrorKind, YaccKind, YaccOriginalActionKind,
YaccGrammarError, YaccGrammarErrorKind, YaccKind, YaccOriginalActionKind, parser::SpansKind,
},
Location, Span, Spanned,
};
use lazy_static::lazy_static;
use regex::{Regex, RegexBuilder};
Expand Down
2 changes: 1 addition & 1 deletion cfgrammar/src/lib/span.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "bincode")]
use bincode::{Decode, Encode};
use proc_macro2::TokenStream;
use quote::{quote, ToTokens, TokenStreamExt};
use quote::{ToTokens, TokenStreamExt, quote};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down
54 changes: 32 additions & 22 deletions cfgrammar/src/lib/yacc/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ use std::{
use indexmap::{IndexMap, IndexSet};

use super::{
parser::YaccParser, Precedence, YaccGrammarError, YaccGrammarErrorKind, YaccGrammarWarning,
YaccGrammarWarningKind, YaccKind,
Precedence, YaccGrammarError, YaccGrammarErrorKind, YaccGrammarWarning, YaccGrammarWarningKind,
YaccKind, parser::YaccParser,
};

use crate::{
header::{GrmtoolsSectionParser, HeaderError, HeaderErrorKind, HeaderValue},
Span,
header::{GrmtoolsSectionParser, HeaderError, HeaderErrorKind, HeaderValue},
};
/// Contains a `GrammarAST` structure produced from a grammar source file.
/// As well as any errors which occurred during the construction of the AST.
Expand Down Expand Up @@ -96,11 +96,13 @@ impl FromStr for ASTWithValidityInfo {
yacc_kind,
})
} else {
Err(vec![HeaderError {
kind: HeaderErrorKind::InvalidEntry("yacckind"),
locations: vec![Span::new(0, 0)],
}
.into()])
Err(vec![
HeaderError {
kind: HeaderErrorKind::InvalidEntry("yacckind"),
locations: vec![Span::new(0, 0)],
}
.into(),
])
}
}
}
Expand Down Expand Up @@ -704,9 +706,11 @@ mod test {
start: "a" a;
a: "c";"#,
);
assert!(ast_validity.ast().prods[0]
.symbols
.contains(&Symbol::Rule("a".to_string(), Span::new(64, 65))));
assert!(
ast_validity.ast().prods[0]
.symbols
.contains(&Symbol::Rule("a".to_string(), Span::new(64, 65)))
);
let ast_validity = ASTWithValidityInfo::new(
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
r#"
Expand All @@ -715,9 +719,11 @@ mod test {
start: "a" x;
x: "c";"#,
);
assert!(ast_validity.ast().prods[0]
.symbols
.contains(&Symbol::Rule("x".to_string(), Span::new(64, 65))));
assert!(
ast_validity.ast().prods[0]
.symbols
.contains(&Symbol::Rule("x".to_string(), Span::new(64, 65)))
);
let ast_validity = ASTWithValidityInfo::new(
YaccKind::Original(YaccOriginalActionKind::GenericParseTree),
r#"
Expand Down Expand Up @@ -752,13 +758,17 @@ mod test {
start: "a" a "b";
"#,
);
assert!(ast_validity
.ast()
.token_directives
.contains(&ast_validity.ast().tokens.get_index_of("a").unwrap()));
assert!(!ast_validity
.ast()
.token_directives
.contains(&ast_validity.ast().tokens.get_index_of("b").unwrap()));
assert!(
ast_validity
.ast()
.token_directives
.contains(&ast_validity.ast().tokens.get_index_of("a").unwrap())
);
assert!(
!ast_validity
.ast()
.token_directives
.contains(&ast_validity.ast().tokens.get_index_of("b").unwrap())
);
}
}
15 changes: 6 additions & 9 deletions cfgrammar/src/lib/yacc/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use serde::{Deserialize, Serialize};
use vob::Vob;

use super::{
ast,
YaccKind, ast,
firsts::YaccFirsts,
follows::YaccFollows,
parser::{YaccGrammarError, YaccGrammarResult},
YaccKind,
};
use crate::{PIdx, RIdx, SIdx, Span, Symbol, TIdx};

Expand Down Expand Up @@ -234,7 +233,9 @@ where
}
for p in &ast.prods {
if p.symbols.len() > num_traits::cast(StorageT::max_value()).unwrap() {
panic!("StorageT is not big enough to store the symbols of at least one of this grammar's productions.");
panic!(
"StorageT is not big enough to store the symbols of at least one of this grammar's productions."
);
}
}

Expand Down Expand Up @@ -811,11 +812,7 @@ where
.rule_max_costs
.borrow_mut()
.get_or_insert_with(|| rule_max_costs(self.grm, &self.token_costs))[usize::from(ridx)];
if v == u16::MAX {
None
} else {
Some(v)
}
if v == u16::MAX { None } else { Some(v) }
}

/// Non-deterministically return a minimal sentence from the set of minimal sentences for the
Expand Down Expand Up @@ -1134,7 +1131,7 @@ where
mod test {
use super::{
super::{AssocKind, Precedence, YaccGrammar, YaccKind, YaccOriginalActionKind},
rule_max_costs, rule_min_costs, IMPLICIT_RULE, IMPLICIT_START_RULE,
IMPLICIT_RULE, IMPLICIT_START_RULE, rule_max_costs, rule_min_costs,
};
use crate::{PIdx, RIdx, Span, Symbol, TIdx};
use std::collections::HashMap;
Expand Down
41 changes: 22 additions & 19 deletions cfgrammar/src/lib/yacc/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ use regex::Regex;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
collections::{hash_map::Entry, HashMap},
collections::{HashMap, hash_map::Entry},
error::Error,
fmt,
str::FromStr,
};

use crate::{
header::{GrmtoolsSectionParser, HeaderErrorKind},
Span, Spanned,
header::{GrmtoolsSectionParser, HeaderErrorKind},
};

pub type YaccGrammarResult<T> = Result<T, Vec<YaccGrammarError>>;

use super::{
ast::{GrammarAST, Symbol},
AssocKind, Precedence, YaccKind,
ast::{GrammarAST, Symbol},
};

/// The various different possible Yacc parser errors.
Expand Down Expand Up @@ -129,27 +129,27 @@ impl fmt::Display for YaccGrammarErrorKind {
YaccGrammarErrorKind::NoStartRule => return write!(f, "No start rule specified"),
YaccGrammarErrorKind::UnknownSymbol => "Unknown symbol, expected a rule or token",
YaccGrammarErrorKind::InvalidStartRule(name) => {
return write!(f, "Start rule '{}' does not appear in grammar", name)
return write!(f, "Start rule '{}' does not appear in grammar", name);
}
YaccGrammarErrorKind::UnknownRuleRef(name) => {
return write!(f, "Unknown reference to rule '{}'", name)
return write!(f, "Unknown reference to rule '{}'", name);
}
YaccGrammarErrorKind::UnknownToken(name) => {
return write!(f, "Unknown token '{}'", name)
return write!(f, "Unknown token '{}'", name);
}
YaccGrammarErrorKind::NoPrecForToken(name) => {
return write!(
f,
"Token '{}' used in %prec has no precedence attached",
name
)
);
}
YaccGrammarErrorKind::UnknownEPP(name) => {
return write!(
f,
"Token '{}' in %epp declaration is not referenced in the grammar",
name
)
);
}
YaccGrammarErrorKind::InvalidYaccKind => "Invalid yacc kind",
YaccGrammarErrorKind::Header(hk, _) => &format!("Error in '%grmtools' {}", hk),
Expand Down Expand Up @@ -482,7 +482,7 @@ impl YaccParser<'_> {
j
}
Err(_) => {
return Err(self.mk_error(YaccGrammarErrorKind::UnknownSymbol, i))
return Err(self.mk_error(YaccGrammarErrorKind::UnknownSymbol, i));
}
},
};
Expand Down Expand Up @@ -1053,8 +1053,8 @@ impl YaccParser<'_> {
mod test {
use super::{
super::{
ast::{GrammarAST, Production, Symbol},
AssocKind, Precedence, YaccKind, YaccOriginalActionKind,
ast::{GrammarAST, Production, Symbol},
},
Span, Spanned, YaccGrammarError, YaccGrammarErrorKind, YaccParser,
};
Expand Down Expand Up @@ -2549,15 +2549,18 @@ B";
A: ;
"#;
let grm = parse(YaccKind::Original(YaccOriginalActionKind::NoAction), src).unwrap();
assert!(grm
.expect_unused
.contains(&Symbol::Rule("A".to_string(), Span::new(24, 25))));
assert!(grm
.expect_unused
.contains(&Symbol::Token("b".to_string(), Span::new(27, 28))));
assert!(grm
.expect_unused
.contains(&Symbol::Token("c".to_string(), Span::new(31, 32))));
assert!(
grm.expect_unused
.contains(&Symbol::Rule("A".to_string(), Span::new(24, 25)))
);
assert!(
grm.expect_unused
.contains(&Symbol::Token("b".to_string(), Span::new(27, 28)))
);
assert!(
grm.expect_unused
.contains(&Symbol::Token("c".to_string(), Span::new(31, 32)))
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion lrlex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "lrlex"
description = "Simple lexer generator"
repository = "https://github.com/softdevteam/grmtools"
version = "0.13.10"
edition = "2021"
edition = "2024"
readme = "README.md"
license = "Apache-2.0/MIT"
categories = ["parsing"]
Expand Down
2 changes: 1 addition & 1 deletion lrlex/examples/calc_manual_lex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "calc_manual_lex"
version = "0.1.0"
authors = ["Laurence Tratt <http://tratt.net/laurie/>"]
edition = "2021"
edition = "2024"
license = "Apache-2.0/MIT"

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion lrlex/examples/calc_manual_lex/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lrlex::{ct_token_map, DefaultLexerTypes};
use lrlex::{DefaultLexerTypes, ct_token_map};
use lrpar::CTParserBuilder;

// Some of the token names in the parser do not lead to valid Rust identifiers, so we map them to
Expand Down
4 changes: 2 additions & 2 deletions lrlex/examples/calc_manual_lex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::io::{self, BufRead, Write};

use cfgrammar::{NewlineCache, Span};
use lrlex::{lrlex_mod, DefaultLexeme, DefaultLexerTypes, LRNonStreamingLexer};
use lrpar::{lrpar_mod, Lexeme, NonStreamingLexer};
use lrlex::{DefaultLexeme, DefaultLexerTypes, LRNonStreamingLexer, lrlex_mod};
use lrpar::{Lexeme, NonStreamingLexer, lrpar_mod};

lrlex_mod!("token_map");
// Using `lrpar_mod!` brings the parser for `calc.y` into scope. By default the module name will be
Expand Down
2 changes: 1 addition & 1 deletion lrlex/examples/calclex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "calclex"
version = "0.1.0"
authors = ["Laurence Tratt <http://tratt.net/laurie/>"]
edition = "2021"
edition = "2024"
license = "Apache-2.0/MIT"

[[bin]]
Expand Down
16 changes: 10 additions & 6 deletions lrlex/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
env::{current_dir, var},
error::Error,
fmt::{self, Debug, Display, Write as _},
fs::{self, create_dir_all, read_to_string, File},
fs::{self, File, create_dir_all, read_to_string},
hash::Hash,
io::Write,
path::{Path, PathBuf},
Expand All @@ -26,12 +26,12 @@ use cfgrammar::{
use glob::glob;
use lazy_static::lazy_static;
use lrpar::{
diagnostics::{DiagnosticFormatter, SpannedDiagnosticFormatter},
CTParserBuilder, LexerTypes,
diagnostics::{DiagnosticFormatter, SpannedDiagnosticFormatter},
};
use num_traits::{AsPrimitive, PrimInt, Unsigned};
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
use quote::{ToTokens, TokenStreamExt, format_ident, quote};
use regex::Regex;

use crate::{DefaultLexerTypes, LRNonStreamingLexer, LRNonStreamingLexerDef, LexFlags, LexerDef};
Expand Down Expand Up @@ -616,7 +616,9 @@ where
ct_parser.grammar_path(),
);

eprintln!("{ERROR} these tokens are not referenced in the lexer but defined as follows");
eprintln!(
"{ERROR} these tokens are not referenced in the lexer but defined as follows"
);
eprintln!(
"{err_indent} {}",
yacc_diag.file_location_msg("in the grammar", None)
Expand All @@ -633,7 +635,9 @@ where
}
eprintln!();
} else {
eprintln!("{ERROR} the following tokens are used in the grammar but are not defined in the lexer:");
eprintln!(
"{ERROR} the following tokens are used in the grammar but are not defined in the lexer:"
);
for n in mfl {
eprintln!(" {}", n);
}
Expand Down Expand Up @@ -785,7 +789,7 @@ where
let mut rim_sorted = Vec::from_iter(rim.iter());
rim_sorted.sort_by_key(|(k, _)| *k);
for (name, id) in rim_sorted {
if RE_TOKEN_ID.is_match(&name) {
if RE_TOKEN_ID.is_match(name) {
let tok_ident = format_ident!("N_{}", name.to_ascii_uppercase());
let storaget =
str::parse::<TokenStream>(type_name::<LexerTypesT::StorageT>()).unwrap();
Expand Down
Loading