Skip to content
Open
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
396 changes: 102 additions & 294 deletions cfgrammar/src/lib/header.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cfgrammar/src/lib/yacc/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1570,11 +1570,11 @@ mod test {
"%grmtools{yacckind: YaccKind::Original(GenericParseTree)}
%%
Start: ;",
"%grmtools{yacckind: YaccKind::Original(yaccoriginalactionkind::useraction)}
"%grmtools{yacckind: YaccKind::Original(YaccOriginalActionKind::UserAction)}
%actiontype ()
%%
Start: {};",
"%grmtools{yacckind: Original(YACCOriginalActionKind::NoAction)}
"%grmtools{yacckind: Original(YaccOriginalActionKind::NoAction)}
%%
Start: ;",
"%grmtools{yacckind: YaccKind::Grmtools}
Expand Down
129 changes: 31 additions & 98 deletions lrlex/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Build grammars at run-time.

use cfgrammar::{
header::{Header, HeaderError, HeaderErrorKind, HeaderValue, Namespaced, Setting, Value},
header::{GrmtoolsSectionValue, Header, HeaderError, HeaderErrorKind, HeaderValue},
markmap::MergeBehavior,
span::{Location, Span},
};
Expand Down Expand Up @@ -47,79 +47,23 @@ pub enum LexerKind {
LRNonStreamingLexer,
}

impl<T: Clone> TryFrom<&Value<T>> for LexerKind {
impl<T: Clone> TryFrom<&GrmtoolsSectionValue<T>> for LexerKind {
type Error = cfgrammar::header::HeaderError<T>;
fn try_from(it: &Value<T>) -> Result<LexerKind, Self::Error> {
fn try_from(it: &GrmtoolsSectionValue<T>) -> Result<LexerKind, Self::Error> {
match it {
Value::Flag(_, loc) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Expected `LexerKind` found bool",
),
locations: vec![loc.clone()],
}),
Value::Setting(Setting::Num(_, loc)) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Expected `LexerKind` found numeric",
),
locations: vec![loc.clone()],
}),
Value::Setting(Setting::String(_, loc)) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Expected `LexerKind` found string",
),
locations: vec![loc.clone()],
}),
Value::Setting(Setting::Constructor {
ctor:
Namespaced {
namespace: _,
member: (_, loc),
},
arg: _,
}) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Expected `LexerKind` found constructor",
),
locations: vec![loc.clone()],
}),
Value::Setting(Setting::Array(_, arr_loc, _)) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Expected `LexerKind` found array",
),
locations: vec![arr_loc.clone()],
}),
Value::Setting(Setting::Unitary(Namespaced {
namespace,
member: (member, member_loc),
})) => {
if let Some((ns, loc)) = namespace
&& ns.to_lowercase() != "lexerkind"
{
return Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Expected namespace `LexerKind`",
),
locations: vec![loc.clone()],
});
}
if member.to_lowercase() != "lrnonstreaminglexer" {
return Err(HeaderError {
kind: HeaderErrorKind::ConversionError(
"LexerKind",
"Unknown `LexerKind` Variant",
),
locations: vec![member_loc.clone()],
});
GrmtoolsSectionValue::RustLike(rs, loc) => match rs.as_str() {
"LexerKind::LRNonStreamingLexer" | "LRNonStreamingLexer" => {
Ok(LexerKind::LRNonStreamingLexer)
}

Ok(LexerKind::LRNonStreamingLexer)
}
_ => Err(HeaderError {
kind: HeaderErrorKind::ConversionError("LexerKind", "Expected `LexerKind`"),
locations: vec![loc.clone()],
}),
},
val => Err(HeaderError {
kind: HeaderErrorKind::ConversionError("LexerKind", "Expected `LexerKind`"),
locations: vec![val.primary_location().clone()],
}),
}
}
}
Expand Down Expand Up @@ -516,10 +460,10 @@ where
}
};
match test_glob {
Some(HeaderValue(_, Value::Setting(Setting::Array(test_globs, _, _)))) => {
Some(HeaderValue(_, GrmtoolsSectionValue::Array(test_globs, _))) => {
for setting in test_globs {
match setting {
Setting::String(test_files, _) => {
GrmtoolsSectionValue::String(test_files, _) => {
let path_joined = grm_path.parent().unwrap().join(test_files);
let path_str = &path_joined.to_string_lossy();
let mut glob_paths = glob(path_str).map_err(|e| e.to_string())?.peekable();
Expand Down Expand Up @@ -846,7 +790,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -862,7 +806,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -878,7 +822,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -894,7 +838,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -910,7 +854,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -926,7 +870,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -942,7 +886,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -958,7 +902,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -974,7 +918,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Flag(flag, Location::Other("CTLexerBuilder".to_string())),
GrmtoolsSectionValue::Bool(flag, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -990,10 +934,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Setting(Setting::Num(
sz as u64,
Location::Other("CTLexerBuilder".to_string()),
)),
GrmtoolsSectionValue::Num(sz as u64, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -1009,10 +950,7 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Setting(Setting::Num(
sz as u64,
Location::Other("CTLexerBuilder".to_string()),
)),
GrmtoolsSectionValue::Num(sz as u64, Location::Other("CTLexerBuilder".to_string())),
),
);
self
Expand All @@ -1028,10 +966,10 @@ where
key,
HeaderValue(
Location::Other("CTLexerBuilder".to_string()),
Value::Setting(Setting::Num(
GrmtoolsSectionValue::Num(
lim as u64,
Location::Other("CTLexerBuilder".to_string()),
)),
),
),
);
self
Expand Down Expand Up @@ -1284,12 +1222,7 @@ mod test {
use super::{CTLexerBuilder, LexerKind};
#[test]
fn test_grmtools_section_lexerkind() {
let lexerkinds = [
"LRNonStreamingLexer",
"lrnonstreaminglexer",
"LexerKind::lrnonstreaminglexer",
"lexerkind::LRNonStreamingLexer",
];
let lexerkinds = ["LRNonStreamingLexer", "LexerKind::LRNonStreamingLexer"];
for (i, kind) in lexerkinds.iter().enumerate() {
let lex_src = format!(
"
Expand Down
32 changes: 18 additions & 14 deletions lrlex/src/lib/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use std::{

use cfgrammar::{
NewlineCache, Span,
header::{GrmtoolsSectionParser, Header, HeaderError, HeaderErrorKind, HeaderValue, Value},
header::{
GrmtoolsSectionParser, GrmtoolsSectionValue, Header, HeaderError, HeaderErrorKind,
HeaderValue,
},
span::Location,
};
use num_traits::{AsPrimitive, PrimInt, Unsigned};
Expand Down Expand Up @@ -47,7 +50,6 @@ pub struct LexFlags {
impl<T: Clone> TryFrom<&mut Header<T>> for LexFlags {
type Error = HeaderError<T>;
fn try_from(header: &mut Header<T>) -> Result<LexFlags, HeaderError<T>> {
use cfgrammar::header::Setting;
let mut lex_flags = UNSPECIFIED_LEX_FLAGS;
let LexFlags {
dot_matches_new_line,
Expand All @@ -67,10 +69,10 @@ impl<T: Clone> TryFrom<&mut Header<T>> for LexFlags {
($prefix:ident, $it:ident) => {
header.mark_used(&stringify!($prefix.$it).to_string());
*$it = match header.get(stringify!($prefix.$it)) {
Some(HeaderValue(_, Value::Flag(flag, _))) => Some(*flag),
Some(HeaderValue(loc, _)) => Err(HeaderError {
Some(HeaderValue(_, GrmtoolsSectionValue::Bool(flag, _))) => Some(*flag),
Some(HeaderValue(_, val)) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError("LexFlags", "Expected boolean"),
locations: vec![loc.clone()],
locations: vec![val.primary_location().clone()],
})?,
None => None,
}
Expand All @@ -89,10 +91,10 @@ impl<T: Clone> TryFrom<&mut Header<T>> for LexFlags {
($prefix:ident, $it:ident, $num_ty: ty) => {
header.mark_used(&stringify!($prefix.$it).to_string());
*$it = match header.get(stringify!($prefix.$it)) {
Some(HeaderValue(_, Value::Setting(Setting::Num(n, _)))) => Some(*n as $num_ty),
Some(HeaderValue(loc, _)) => Err(HeaderError {
Some(HeaderValue(_, GrmtoolsSectionValue::Num(n, _))) => Some(*n as $num_ty),
Some(HeaderValue(_, val)) => Err(HeaderError {
kind: HeaderErrorKind::ConversionError("LexFlags", "Expected numeric"),
locations: vec![loc.clone()],
locations: vec![val.primary_location().clone()],
})?,
None => None,
}
Expand Down Expand Up @@ -128,8 +130,11 @@ impl From<&LexFlags> for Header<Location> {
header.insert(
stringify!($it).to_string(),
HeaderValue(
Location::Other("From<&LexFlags".to_string()),
Value::Flag(x, Location::Other("From<&LexFlags>".to_string())),
Location::Other("From<&LexFlags>".to_string()),
GrmtoolsSectionValue::Bool(
x,
Location::Other("From<&LexFlags>".to_string()),
),
),
)
});
Expand All @@ -148,15 +153,14 @@ impl From<&LexFlags> for Header<Location> {
macro_rules! cvt_num {
($it: ident) => {
$it.map(|x| {
use cfgrammar::header::Setting;
header.insert(
stringify!($it).to_string(),
HeaderValue(
Location::Other("From<&LexFlags".to_string()),
Value::Setting(Setting::Num(
Location::Other("From<&LexFlags>".to_string()),
GrmtoolsSectionValue::Num(
x as u64,
Location::Other("From<&LexFlags>".to_string()),
)),
),
),
)
});
Expand Down
Loading