Apply nom-recursive

This commit is contained in:
dalance 2019-07-26 15:37:13 +09:00
parent 03de070220
commit bf92c5550f
34 changed files with 245 additions and 260 deletions

View File

@ -5,4 +5,3 @@ members = [
"sv-parser-parser",
"sv-parser-macros",
]

View File

@ -6,9 +6,7 @@ use crate::proc_macro::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::Data::{Enum, Struct};
use syn::{
self, parse_macro_input, AttributeArgs, DeriveInput, Expr, ItemFn, Meta, NestedMeta, Stmt,
};
use syn::{self, parse_macro_input, AttributeArgs, DeriveInput, ItemFn, Meta, NestedMeta, Stmt};
#[proc_macro_derive(Node)]
pub fn node_derive(input: TokenStream) -> TokenStream {
@ -166,17 +164,11 @@ pub fn parser(attr: TokenStream, item: TokenStream) -> TokenStream {
}
fn impl_parser(attr: &AttributeArgs, item: &ItemFn) -> TokenStream {
let (maybe_recursive, ambiguous) = impl_parser_attribute(attr);
let ambiguous = impl_parser_attribute(attr);
let trace = impl_parser_trace(&item);
let trace = parse_macro_input!(trace as Stmt);
let check_recursive_flag = impl_parser_check_recursive_flag(&item);
let check_recursive_flag = parse_macro_input!(check_recursive_flag as Stmt);
let set_recursive_flag = impl_parser_set_recursive_flag(&item);
let set_recursive_flag = parse_macro_input!(set_recursive_flag as Stmt);
let body = if ambiguous {
impl_parser_body_ambiguous(&item)
} else {
@ -184,24 +176,11 @@ fn impl_parser(attr: &AttributeArgs, item: &ItemFn) -> TokenStream {
};
let body = parse_macro_input!(body as Stmt);
let body_unwrap = impl_parser_body_unwrap(&item);
let body_unwrap = parse_macro_input!(body_unwrap as Stmt);
let clear_recursive_flags = impl_parser_clear_recursive_flags(&item);
let clear_recursive_flags = parse_macro_input!(clear_recursive_flags as Expr);
let clear_recursive_flags = Stmt::Expr(clear_recursive_flags);
let mut item = item.clone();
item.block.stmts.clear();
item.block.stmts.push(trace);
if maybe_recursive {
item.block.stmts.push(check_recursive_flag);
item.block.stmts.push(set_recursive_flag);
}
item.block.stmts.push(body);
item.block.stmts.push(body_unwrap);
item.block.stmts.push(clear_recursive_flags);
let gen = quote! {
#item
@ -209,19 +188,17 @@ fn impl_parser(attr: &AttributeArgs, item: &ItemFn) -> TokenStream {
gen.into()
}
fn impl_parser_attribute(attr: &AttributeArgs) -> (bool, bool) {
let mut maybe_recursive = false;
fn impl_parser_attribute(attr: &AttributeArgs) -> bool {
let mut ambiguous = false;
for a in attr {
match a {
NestedMeta::Meta(Meta::Word(x)) if x == "MaybeRecursive" => maybe_recursive = true,
NestedMeta::Meta(Meta::Word(x)) if x == "Ambiguous" => ambiguous = true,
_ => panic!(),
}
}
(maybe_recursive, ambiguous)
ambiguous
}
fn impl_parser_trace(item: &ItemFn) -> TokenStream {
@ -234,42 +211,6 @@ fn impl_parser_trace(item: &ItemFn) -> TokenStream {
gen.into()
}
fn impl_parser_check_recursive_flag(item: &ItemFn) -> TokenStream {
let ident = &item.ident;
let gen = quote! {
if thread_context::PARSER_INDEX.with(|p| {
if let Some(i) = p.borrow_mut().get(stringify!(#ident)) {
return check_recursive_flag(s, i);
} else {
return false
}
}) {
#[cfg(feature = "trace")]
println!("{:<128} : loop detect", format!("{}{}", " ".repeat(s.extra.depth), stringify!(#ident)));
return Err(nom::Err::Error(nom::error::make_error(s, nom::error::ErrorKind::Fix)));
}
};
gen.into()
}
fn impl_parser_set_recursive_flag(item: &ItemFn) -> TokenStream {
let ident = &item.ident;
let gen = quote! {
let s = thread_context::PARSER_INDEX.with(|p| {
if let Some(i) = p.borrow_mut().get(stringify!(#ident)) {
set_recursive_flag(s, i, true)
} else {
#[cfg(feature = "trace")]
println!("{:<128} : allocate failed", format!("{}{}", " ".repeat(s.extra.depth), stringify!(#ident)));
s
}
});
};
gen.into()
}
fn impl_parser_body(item: &ItemFn) -> TokenStream {
let mut gen = quote! {};
for s in &item.block.stmts {
@ -279,7 +220,9 @@ fn impl_parser_body(item: &ItemFn) -> TokenStream {
};
}
let gen = quote! {
let body_ret = { #gen };
{
#gen
}
};
gen.into()
}
@ -336,22 +279,10 @@ fn impl_parser_body_ambiguous(item: &ItemFn) -> TokenStream {
};
let gen = quote! {
let body_ret = { #gen };
{
#gen
}
};
gen.into()
}
fn impl_parser_body_unwrap(_item: &ItemFn) -> TokenStream {
let gen = quote! {
let (s, ret) = body_ret?;
};
gen.into()
}
fn impl_parser_clear_recursive_flags(_item: &ItemFn) -> TokenStream {
let gen = quote! {
Ok((clear_recursive_flags(s), ret))
};
gen.into()
}

View File

@ -6,11 +6,12 @@ edition = "2018"
[features]
default = []
trace = []
trace = ["sv-parser-syntaxtree/trace"]
[dependencies]
nom = "5.0.0"
nom-packrat = "0.1.17"
nom-recursive = { path = "../../nom-recursive/nom-recursive" }
str-concat = "*"
sv-parser-syntaxtree = { path = "../sv-parser-syntaxtree" }
sv-parser-macros = { path = "../sv-parser-macros" }

View File

@ -84,7 +84,8 @@ pub(crate) fn case_item(s: Span) -> IResult<Span, CaseItem> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn case_item_nondefault(s: Span) -> IResult<Span, CaseItem> {
let (s, a) = list(symbol(","), case_item_expression)(s)?;
let (s, b) = symbol(":")(s)?;
@ -111,7 +112,8 @@ pub(crate) fn case_pattern_item(s: Span) -> IResult<Span, CasePatternItem> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn case_pattern_item_nondefault(s: Span) -> IResult<Span, CasePatternItem> {
let (s, a) = pattern(s)?;
let (s, b) = opt(pair(symbol("&&&"), expression))(s)?;
@ -133,7 +135,8 @@ pub(crate) fn case_inside_item(s: Span) -> IResult<Span, CaseInsideItem> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn case_inside_item_nondefault(s: Span) -> IResult<Span, CaseInsideItem> {
let (s, a) = open_range_list(s)?;
let (s, b) = symbol(":")(s)?;
@ -164,7 +167,8 @@ pub(crate) fn randcase_statement(s: Span) -> IResult<Span, RandcaseStatement> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn randcase_item(s: Span) -> IResult<Span, RandcaseItem> {
let (s, a) = expression(s)?;
let (s, b) = symbol(":")(s)?;
@ -172,7 +176,8 @@ pub(crate) fn randcase_item(s: Span) -> IResult<Span, RandcaseItem> {
Ok((s, RandcaseItem { nodes: (a, b, c) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn open_range_list(s: Span) -> IResult<Span, OpenRangeList> {
let (s, a) = list(symbol(","), open_value_range)(s)?;
Ok((s, OpenRangeList { nodes: (a,) }))

View File

@ -35,7 +35,8 @@ pub(crate) fn unique_priority(s: Span) -> IResult<Span, UniquePriority> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn cond_predicate(s: Span) -> IResult<Span, CondPredicate> {
let (s, a) = list(symbol("&&&"), expression_or_cond_pattern)(s)?;
Ok((s, CondPredicate { nodes: (a,) }))
@ -53,7 +54,8 @@ pub(crate) fn expression_or_cond_pattern(s: Span) -> IResult<Span, ExpressionOrC
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn cond_pattern(s: Span) -> IResult<Span, CondPattern> {
let (s, a) = expression(s)?;
let (s, b) = keyword("matches")(s)?;

View File

@ -38,13 +38,15 @@ pub(crate) fn continuous_assign_variable(s: Span) -> IResult<Span, ContinuousAss
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn list_of_net_assignments(s: Span) -> IResult<Span, ListOfNetAssignments> {
let (s, a) = list(symbol(","), net_assignment)(s)?;
Ok((s, ListOfNetAssignments { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn list_of_variable_assignments(s: Span) -> IResult<Span, ListOfVariableAssignments> {
let (s, a) = list(symbol(","), variable_assignment)(s)?;
Ok((s, ListOfVariableAssignments { nodes: (a,) }))
@ -66,7 +68,8 @@ pub(crate) fn net_alias(s: Span) -> IResult<Span, NetAlias> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn net_assignment(s: Span) -> IResult<Span, NetAssignment> {
let (s, a) = net_lvalue(s)?;
let (s, b) = symbol("=")(s)?;

View File

@ -102,7 +102,8 @@ pub(crate) fn for_initialization(s: Span) -> IResult<Span, ForInitialization> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn for_initialization_declaration(s: Span) -> IResult<Span, ForInitialization> {
let (s, a) = list(symbol(","), for_variable_declaration)(s)?;
Ok((
@ -128,7 +129,8 @@ pub(crate) fn var(s: Span) -> IResult<Span, Var> {
Ok((s, Var { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn for_step(s: Span) -> IResult<Span, ForStep> {
let (s, a) = list(symbol(","), for_step_assignment)(s)?;
Ok((s, ForStep { nodes: (a,) }))

View File

@ -51,7 +51,8 @@ pub(crate) fn blocking_assignment(s: Span) -> IResult<Span, BlockingAssignment>
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn blocking_assignment_variable(s: Span) -> IResult<Span, BlockingAssignment> {
let (s, a) = variable_lvalue(s)?;
let (s, b) = symbol("=")(s)?;
@ -65,7 +66,8 @@ pub(crate) fn blocking_assignment_variable(s: Span) -> IResult<Span, BlockingAss
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn blocking_assignment_nonrange_variable(s: Span) -> IResult<Span, BlockingAssignment> {
let (s, a) = nonrange_variable_lvalue(s)?;
let (s, b) = symbol("=")(s)?;
@ -97,7 +99,8 @@ pub(crate) fn blocking_assignment_hierarchical_variable(
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn operator_assignment(s: Span) -> IResult<Span, OperatorAssignment> {
let (s, a) = variable_lvalue(s)?;
let (s, b) = assignment_operator(s)?;
@ -124,7 +127,8 @@ pub(crate) fn assignment_operator(s: Span) -> IResult<Span, AssignmentOperator>
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn nonblocking_assignment(s: Span) -> IResult<Span, NonblockingAssignment> {
let (s, a) = variable_lvalue(s)?;
let (s, b) = symbol("<=")(s)?;
@ -236,7 +240,8 @@ pub(crate) fn procedural_continuous_assignment_release_net(
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn variable_assignment(s: Span) -> IResult<Span, VariableAssignment> {
let (s, a) = variable_lvalue(s)?;
let (s, b) = symbol("=")(s)?;

View File

@ -163,7 +163,8 @@ pub(crate) fn rs_case_item(s: Span) -> IResult<Span, RsCaseItem> {
alt((rs_case_item_nondefault, rs_case_item_default))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn rs_case_item_nondefault(s: Span) -> IResult<Span, RsCaseItem> {
let (s, a) = list(symbol(","), case_item_expression)(s)?;
let (s, b) = symbol(":")(s)?;

View File

@ -20,7 +20,8 @@ pub(crate) fn statement_or_null_attribute(s: Span) -> IResult<Span, StatementOrN
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn statement(s: Span) -> IResult<Span, Statement> {
let (s, a) = opt(pair(block_identifier, symbol(":")))(s)?;
let (s, b) = many0(attribute_instance)(s)?;

View File

@ -129,7 +129,8 @@ pub(crate) fn event_expression(s: Span) -> IResult<Span, EventExpression> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn event_expression_expression(s: Span) -> IResult<Span, EventExpression> {
let (s, a) = opt(edge_identifier)(s)?;
let (s, b) = expression(s)?;
@ -150,7 +151,8 @@ pub(crate) fn event_expression_sequence(s: Span) -> IResult<Span, EventExpressio
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn event_expression_or(s: Span) -> IResult<Span, EventExpression> {
let (s, a) = event_expression(s)?;
let (s, b) = keyword("or")(s)?;
@ -161,7 +163,8 @@ pub(crate) fn event_expression_or(s: Span) -> IResult<Span, EventExpression> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn event_expression_comma(s: Span) -> IResult<Span, EventExpression> {
let (s, a) = event_expression(s)?;
let (s, b) = symbol(",")(s)?;

View File

@ -150,7 +150,8 @@ pub(crate) fn property_list_of_arguments(s: Span) -> IResult<Span, PropertyListO
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_list_of_arguments_ordered(
s: Span,
) -> IResult<Span, PropertyListOfArguments> {
@ -267,7 +268,8 @@ pub(crate) fn property_formal_type(s: Span) -> IResult<Span, PropertyFormalType>
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_spec(s: Span) -> IResult<Span, PropertySpec> {
let (s, a) = opt(clocking_event)(s)?;
let (s, b) = opt(triple(
@ -361,7 +363,8 @@ pub(crate) fn property_expr_not(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_or(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("or")(s)?;
@ -372,7 +375,8 @@ pub(crate) fn property_expr_or(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_and(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("and")(s)?;
@ -383,7 +387,8 @@ pub(crate) fn property_expr_and(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_implication_overlapped(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = symbol("|->")(s)?;
@ -396,7 +401,8 @@ pub(crate) fn property_expr_implication_overlapped(s: Span) -> IResult<Span, Pro
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_implication_nonoverlapped(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = symbol("|=>")(s)?;
@ -438,7 +444,8 @@ pub(crate) fn property_expr_case(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_followed_by_overlapped(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = symbol("#-#")(s)?;
@ -451,7 +458,8 @@ pub(crate) fn property_expr_followed_by_overlapped(s: Span) -> IResult<Span, Pro
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_followed_by_nonoverlapped(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = symbol("#=#")(s)?;
@ -530,7 +538,8 @@ pub(crate) fn property_expr_s_eventually(s: Span) -> IResult<Span, PropertyExpr>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_until(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("until")(s)?;
@ -541,7 +550,8 @@ pub(crate) fn property_expr_until(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_s_until(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("s_until")(s)?;
@ -552,7 +562,8 @@ pub(crate) fn property_expr_s_until(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_until_with(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("until_with")(s)?;
@ -563,7 +574,8 @@ pub(crate) fn property_expr_until_with(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_s_until_with(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("s_until_with")(s)?;
@ -574,7 +586,8 @@ pub(crate) fn property_expr_s_until_with(s: Span) -> IResult<Span, PropertyExpr>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_implies(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("implies")(s)?;
@ -585,7 +598,8 @@ pub(crate) fn property_expr_implies(s: Span) -> IResult<Span, PropertyExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_expr_iff(s: Span) -> IResult<Span, PropertyExpr> {
let (s, a) = property_expr(s)?;
let (s, b) = keyword("iff")(s)?;
@ -655,7 +669,8 @@ pub(crate) fn property_case_item(s: Span) -> IResult<Span, PropertyCaseItem> {
alt((property_case_item_nondefault, property_case_item_default))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn property_case_item_nondefault(s: Span) -> IResult<Span, PropertyCaseItem> {
let (s, a) = list(symbol(","), expression_or_dist)(s)?;
let (s, b) = symbol(":")(s)?;
@ -783,7 +798,8 @@ pub(crate) fn sequence_expr_cycle_delay_expr(s: Span) -> IResult<Span, SequenceE
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_expr_cycle_delay_expr(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = cycle_delay_range(s)?;
@ -797,7 +813,8 @@ pub(crate) fn sequence_expr_expr_cycle_delay_expr(s: Span) -> IResult<Span, Sequ
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_expression(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = expression_or_dist(s)?;
let (s, b) = opt(boolean_abbrev)(s)?;
@ -830,7 +847,8 @@ pub(crate) fn sequence_expr_paren(s: Span) -> IResult<Span, SequenceExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_and(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = keyword("and")(s)?;
@ -841,7 +859,8 @@ pub(crate) fn sequence_expr_and(s: Span) -> IResult<Span, SequenceExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_intersect(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = keyword("intersect")(s)?;
@ -852,7 +871,8 @@ pub(crate) fn sequence_expr_intersect(s: Span) -> IResult<Span, SequenceExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_or(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = keyword("or")(s)?;
@ -876,7 +896,8 @@ pub(crate) fn sequence_expr_first_match(s: Span) -> IResult<Span, SequenceExpr>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_throughout(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = expression_or_dist(s)?;
let (s, b) = keyword("throughout")(s)?;
@ -887,7 +908,8 @@ pub(crate) fn sequence_expr_throughout(s: Span) -> IResult<Span, SequenceExpr> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_expr_within(s: Span) -> IResult<Span, SequenceExpr> {
let (s, a) = sequence_expr(s)?;
let (s, b) = keyword("within")(s)?;
@ -996,7 +1018,8 @@ pub(crate) fn sequence_list_of_arguments(s: Span) -> IResult<Span, SequenceListO
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn sequence_list_of_arguments_ordered(
s: Span,
) -> IResult<Span, SequenceListOfArguments> {
@ -1132,7 +1155,8 @@ pub(crate) fn cycle_delay_const_range_expression(
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn cycle_delay_const_range_expression_binary(
s: Span,
) -> IResult<Span, CycleDelayConstRangeExpression> {
@ -1147,7 +1171,8 @@ pub(crate) fn cycle_delay_const_range_expression_binary(
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn cycle_delay_const_range_expression_dollar(
s: Span,
) -> IResult<Span, CycleDelayConstRangeExpression> {
@ -1162,7 +1187,8 @@ pub(crate) fn cycle_delay_const_range_expression_dollar(
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn expression_or_dist(s: Span) -> IResult<Span, ExpressionOrDist> {
let (s, a) = expression(s)?;
let (s, b) = opt(pair(keyword("dist"), brace(dist_list)))(s)?;

View File

@ -12,7 +12,8 @@ pub(crate) fn block_item_declaration(s: Span) -> IResult<Span, BlockItemDeclarat
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn block_item_declaration_data(s: Span) -> IResult<Span, BlockItemDeclaration> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = data_declaration(s)?;

View File

@ -133,7 +133,8 @@ pub(crate) fn block_event_expression(s: Span) -> IResult<Span, BlockEventExpress
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn block_event_expression_or(s: Span) -> IResult<Span, BlockEventExpression> {
let (s, a) = block_event_expression(s)?;
let (s, b) = keyword("or")(s)?;
@ -387,7 +388,8 @@ pub(crate) fn trans_list(s: Span) -> IResult<Span, TransList> {
Ok((s, TransList { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn trans_set(s: Span) -> IResult<Span, TransSet> {
let (s, a) = list(symbol("=>"), trans_range_list)(s)?;
Ok((s, TransSet { nodes: (a,) }))
@ -403,7 +405,8 @@ pub(crate) fn trans_range_list(s: Span) -> IResult<Span, TransRangeList> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn trans_range_list_asterisk(s: Span) -> IResult<Span, TransRangeList> {
let (s, a) = trans_item(s)?;
let (s, b) = bracket(pair(symbol("*"), repeat_range))(s)?;
@ -413,7 +416,8 @@ pub(crate) fn trans_range_list_asterisk(s: Span) -> IResult<Span, TransRangeList
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn trans_range_list_arrow(s: Span) -> IResult<Span, TransRangeList> {
let (s, a) = trans_item(s)?;
let (s, b) = bracket(pair(symbol("->"), repeat_range))(s)?;
@ -423,7 +427,8 @@ pub(crate) fn trans_range_list_arrow(s: Span) -> IResult<Span, TransRangeList> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn trans_range_list_equal(s: Span) -> IResult<Span, TransRangeList> {
let (s, a) = trans_item(s)?;
let (s, b) = bracket(pair(symbol("="), repeat_range))(s)?;
@ -449,7 +454,8 @@ pub(crate) fn repeat_range(s: Span) -> IResult<Span, RepeatRange> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn repeat_range_binary(s: Span) -> IResult<Span, RepeatRange> {
let (s, a) = covergroup_expression(s)?;
let (s, b) = symbol(":")(s)?;
@ -594,7 +600,8 @@ pub(crate) fn select_expression_not(s: Span) -> IResult<Span, SelectExpression>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn select_expression_and(s: Span) -> IResult<Span, SelectExpression> {
let (s, a) = select_expression(s)?;
let (s, b) = symbol("&&")(s)?;
@ -605,7 +612,8 @@ pub(crate) fn select_expression_and(s: Span) -> IResult<Span, SelectExpression>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn select_expression_or(s: Span) -> IResult<Span, SelectExpression> {
let (s, a) = select_expression(s)?;
let (s, b) = symbol("||")(s)?;
@ -625,7 +633,8 @@ pub(crate) fn select_expression_paren(s: Span) -> IResult<Span, SelectExpression
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn select_expression_with(s: Span) -> IResult<Span, SelectExpression> {
let (s, a) = select_expression(s)?;
let (s, b) = keyword("with")(s)?;
@ -639,7 +648,8 @@ pub(crate) fn select_expression_with(s: Span) -> IResult<Span, SelectExpression>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn select_expression_cross_set(s: Span) -> IResult<Span, SelectExpression> {
let (s, a) = cross_set_expression(s)?;
let (s, b) = opt(pair(keyword("matches"), integer_covergroup_expression))(s)?;
@ -677,7 +687,8 @@ pub(crate) fn bins_expression_cover_point(s: Span) -> IResult<Span, BinsExpressi
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn covergroup_range_list(s: Span) -> IResult<Span, CovergroupRangeList> {
let (s, a) = list(symbol(","), covergroup_value_range)(s)?;
Ok((s, CovergroupRangeList { nodes: (a,) }))

View File

@ -68,7 +68,8 @@ pub(crate) fn let_list_of_arguments(s: Span) -> IResult<Span, LetListOfArguments
alt((let_list_of_arguments_ordered, let_list_of_arguments_named))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn let_list_of_arguments_ordered(s: Span) -> IResult<Span, LetListOfArguments> {
let (s, a) = list(symbol(","), opt(let_actual_arg))(s)?;
let (s, b) = many0(tuple((

View File

@ -76,7 +76,8 @@ pub(crate) fn stream_concatenation(s: Span) -> IResult<Span, StreamConcatenation
Ok((s, StreamConcatenation { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn stream_expression(s: Span) -> IResult<Span, StreamExpression> {
let (s, a) = expression(s)?;
let (s, b) = opt(pair(keyword("with"), bracket(array_range_expression)))(s)?;
@ -95,7 +96,8 @@ pub(crate) fn array_range_expression(s: Span) -> IResult<Span, ArrayRangeExpress
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn array_range_expression_colon(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol(":")(s)?;
@ -106,7 +108,8 @@ pub(crate) fn array_range_expression_colon(s: Span) -> IResult<Span, ArrayRangeE
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn array_range_expression_plus_colon(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol("+:")(s)?;
@ -119,7 +122,8 @@ pub(crate) fn array_range_expression_plus_colon(s: Span) -> IResult<Span, ArrayR
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn array_range_expression_minus_colon(s: Span) -> IResult<Span, ArrayRangeExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol("-:")(s)?;

View File

@ -18,7 +18,8 @@ pub(crate) fn inc_or_dec_expression_prefix(s: Span) -> IResult<Span, IncOrDecExp
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn inc_or_dec_expression_suffix(s: Span) -> IResult<Span, IncOrDecExpression> {
let (s, a) = variable_lvalue(s)?;
let (s, b) = many0(attribute_instance)(s)?;
@ -29,7 +30,8 @@ pub(crate) fn inc_or_dec_expression_suffix(s: Span) -> IResult<Span, IncOrDecExp
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn conditional_expression(s: Span) -> IResult<Span, ConditionalExpression> {
let (s, a) = cond_predicate(s)?;
let (s, b) = symbol("?")(s)?;
@ -69,7 +71,8 @@ pub(crate) fn constant_expression_unary(s: Span) -> IResult<Span, ConstantExpres
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constant_expression_binary(s: Span) -> IResult<Span, ConstantExpression> {
let (s, a) = constant_expression(s)?;
let (s, b) = binary_operator(s)?;
@ -83,7 +86,8 @@ pub(crate) fn constant_expression_binary(s: Span) -> IResult<Span, ConstantExpre
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constant_expression_ternary(s: Span) -> IResult<Span, ConstantExpression> {
let (s, a) = constant_expression(s)?;
let (s, b) = symbol("?")(s)?;
@ -109,7 +113,8 @@ pub(crate) fn constant_mintypmax_expression(s: Span) -> IResult<Span, ConstantMi
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constant_mintypmax_expression_ternary(
s: Span,
) -> IResult<Span, ConstantMintypmaxExpression> {
@ -176,7 +181,8 @@ pub(crate) fn constant_part_select_range(s: Span) -> IResult<Span, ConstantPartS
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constant_range(s: Span) -> IResult<Span, ConstantRange> {
let (s, a) = constant_expression(s)?;
let (s, b) = symbol(":")(s)?;
@ -184,7 +190,8 @@ pub(crate) fn constant_range(s: Span) -> IResult<Span, ConstantRange> {
Ok((s, ConstantRange { nodes: (a, b, c) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constant_indexed_range(s: Span) -> IResult<Span, ConstantIndexedRange> {
let (s, a) = constant_expression(s)?;
let (s, b) = alt((symbol("+:"), symbol("-:")))(s)?;
@ -235,7 +242,8 @@ pub(crate) fn expression_operator_assignment(s: Span) -> IResult<Span, Expressio
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn expression_binary(s: Span) -> IResult<Span, Expression> {
let (s, a) = expression(s)?;
let (s, b) = binary_operator(s)?;
@ -257,7 +265,8 @@ pub(crate) fn tagged_union_expression(s: Span) -> IResult<Span, TaggedUnionExpre
Ok((s, TaggedUnionExpression { nodes: (a, b, c) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn inside_expression(s: Span) -> IResult<Span, InsideExpression> {
let (s, a) = expression(s)?;
let (s, b) = keyword("inside")(s)?;
@ -290,7 +299,8 @@ pub(crate) fn mintypmax_expression(s: Span) -> IResult<Span, MintypmaxExpression
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn mintypmax_expression_ternary(s: Span) -> IResult<Span, MintypmaxExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol(":")(s)?;
@ -305,7 +315,8 @@ pub(crate) fn mintypmax_expression_ternary(s: Span) -> IResult<Span, MintypmaxEx
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn module_path_conditional_expression(
s: Span,
) -> IResult<Span, ModulePathConditionalExpression> {
@ -348,7 +359,8 @@ pub(crate) fn module_path_expression_unary(s: Span) -> IResult<Span, ModulePathE
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn module_path_expression_binary(s: Span) -> IResult<Span, ModulePathExpression> {
let (s, a) = module_path_expression(s)?;
let (s, b) = binary_module_path_operator(s)?;
@ -374,7 +386,8 @@ pub(crate) fn module_path_mintypmax_expression(
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn module_path_mintypmax_expression_ternary(
s: Span,
) -> IResult<Span, ModulePathMintypmaxExpression> {
@ -403,7 +416,8 @@ pub(crate) fn part_select_range(s: Span) -> IResult<Span, PartSelectRange> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn indexed_range(s: Span) -> IResult<Span, IndexedRange> {
let (s, a) = expression(s)?;
let (s, b) = alt((symbol("+:"), symbol("-:")))(s)?;

View File

@ -226,7 +226,8 @@ pub(crate) fn class_qualifier_or_package_scope(
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn class_qualifier(s: Span) -> IResult<Span, ClassQualifier> {
let (s, a) = opt(local)(s)?;
let (s, b) = opt(implicit_class_handle_or_class_scope)(s)?;
@ -356,7 +357,8 @@ pub(crate) fn constant_select(s: Span) -> IResult<Span, ConstantSelect> {
Ok((s, ConstantSelect { nodes: (a, b, c) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constant_cast(s: Span) -> IResult<Span, ConstantCast> {
let (s, a) = casting_type(s)?;
let (s, b) = symbol("'")(s)?;
@ -370,7 +372,8 @@ pub(crate) fn constant_let_expression(s: Span) -> IResult<Span, ConstantLetExpre
Ok((s, ConstantLetExpression { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn cast(s: Span) -> IResult<Span, Cast> {
let (s, a) = casting_type(s)?;
let (s, b) = symbol("'")(s)?;

View File

@ -91,7 +91,8 @@ pub(crate) fn list_of_arguments(s: Span) -> IResult<Span, ListOfArguments> {
alt((list_of_arguments_ordered, list_of_arguments_named))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn list_of_arguments_ordered(s: Span) -> IResult<Span, ListOfArguments> {
let (s, a) = list(symbol(","), opt(expression))(s)?;
let (s, b) = many0(tuple((
@ -125,7 +126,8 @@ pub(crate) fn list_of_arguments_named(s: Span) -> IResult<Span, ListOfArguments>
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn method_call(s: Span) -> IResult<Span, MethodCall> {
let (s, a) = method_call_root(s)?;
let (s, b) = symbol(".")(s)?;

View File

@ -26,7 +26,8 @@ pub(crate) fn list_of_checker_port_connections(
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn list_of_checker_port_connections_ordered(
s: Span,
) -> IResult<Span, ListOfCheckerPortConnections> {
@ -52,7 +53,8 @@ pub(crate) fn list_of_checker_port_connections_named(
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn ordered_checker_port_connection(
s: Span,
) -> IResult<Span, OrderedCheckerPortConnection> {

View File

@ -125,7 +125,8 @@ pub(crate) fn case_generate_item(s: Span) -> IResult<Span, CaseGenerateItem> {
alt((case_generate_item_nondefault, case_generate_item_default))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn case_generate_item_nondefault(s: Span) -> IResult<Span, CaseGenerateItem> {
let (s, a) = list(symbol(","), constant_expression)(s)?;
let (s, b) = symbol(":")(s)?;

View File

@ -31,7 +31,8 @@ pub(crate) fn list_of_parameter_assignments(s: Span) -> IResult<Span, ListOfPara
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn list_of_parameter_assignments_ordered(
s: Span,
) -> IResult<Span, ListOfParameterAssignments> {
@ -93,7 +94,8 @@ pub(crate) fn list_of_port_connections(s: Span) -> IResult<Span, ListOfPortConne
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn list_of_port_connections_ordered(s: Span) -> IResult<Span, ListOfPortConnections> {
let (s, a) = list(symbol(","), ordered_port_connection)(s)?;
Ok((
@ -111,7 +113,8 @@ pub(crate) fn list_of_port_connections_named(s: Span) -> IResult<Span, ListOfPor
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn ordered_port_connection(s: Span) -> IResult<Span, OrderedPortConnection> {
let (s, x) = many0(attribute_instance)(s)?;
let (s, y) = opt(expression)(s)?;

View File

@ -32,6 +32,7 @@ pub(crate) use nom::multi::*;
pub(crate) use nom::sequence::*;
pub(crate) use nom::{Err, IResult};
pub(crate) use nom_packrat::{self, packrat_parser};
pub(crate) use nom_recursive::{recursive_parser, RecursiveTracer};
pub(crate) use sv_parser_macros::*;
pub(crate) use sv_parser_syntaxtree::*;
@ -40,7 +41,7 @@ pub(crate) use sv_parser_syntaxtree::*;
nom_packrat::storage!(AnyNode);
pub fn parse_sv(s: &str) -> Result<SourceText, ()> {
let s = Span::new_extra(s, Extra::default());
let s = Span::new_extra(s, SpanInfo::default());
match source_text(s) {
Ok((_, x)) => Ok(x),
Err(_) => Err(()),
@ -48,50 +49,9 @@ pub fn parse_sv(s: &str) -> Result<SourceText, ()> {
}
pub fn parse_lib(s: &str) -> Result<LibraryText, ()> {
let s = Span::new_extra(s, Extra::default());
let s = Span::new_extra(s, SpanInfo::default());
match library_text(s) {
Ok((_, x)) => Ok(x),
Err(_) => Err(()),
}
}
// -----------------------------------------------------------------------------
mod thread_context {
use std::cell::RefCell;
use std::collections::HashMap;
use sv_parser_syntaxtree::RECURSIVE_FLAG_WORDS;
pub struct ParserIndex {
index: HashMap<&'static str, usize>,
allocated: [u128; RECURSIVE_FLAG_WORDS],
}
impl ParserIndex {
pub fn get(&mut self, key: &'static str) -> Option<usize> {
if let Some(x) = self.index.get(key) {
Some(*x)
} else {
for i in 0..128 * RECURSIVE_FLAG_WORDS {
let upper = i / 128;
let lower = i % 128;
if ((self.allocated[upper] >> lower) & 1) == 0 {
let val = 1u128 << lower;
let mask = !(1u128 << lower);
self.allocated[upper] = (self.allocated[upper] & mask) | val;
self.index.insert(key, i);
return Some(i);
}
}
None
}
}
}
thread_local!(
pub static PARSER_INDEX: RefCell<ParserIndex> = {
RefCell::new(ParserIndex{index: HashMap::new(), allocated: [0;RECURSIVE_FLAG_WORDS]})
}
);
}

View File

@ -81,7 +81,8 @@ pub(crate) fn constraint_expression(s: Span) -> IResult<Span, ConstraintExpressi
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constraint_expression_expression(s: Span) -> IResult<Span, ConstraintExpression> {
let (s, a) = opt(soft)(s)?;
let (s, b) = expression_or_dist(s)?;
@ -100,7 +101,8 @@ pub(crate) fn soft(s: Span) -> IResult<Span, Soft> {
Ok((s, Soft { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn constraint_expression_arrow(s: Span) -> IResult<Span, ConstraintExpression> {
let (s, a) = expression(s)?;
let (s, b) = symbol("->")(s)?;
@ -179,13 +181,15 @@ pub(crate) fn constraint_set_brace(s: Span) -> IResult<Span, ConstraintSet> {
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn dist_list(s: Span) -> IResult<Span, DistList> {
let (s, a) = list(symbol(","), dist_item)(s)?;
Ok((s, DistList { nodes: (a,) }))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn dist_item(s: Span) -> IResult<Span, DistItem> {
let (s, a) = value_range(s)?;
let (s, b) = opt(dist_weight)(s)?;

View File

@ -10,7 +10,8 @@ pub(crate) fn interface_or_generate_item(s: Span) -> IResult<Span, InterfaceOrGe
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn interface_or_generate_item_module(s: Span) -> IResult<Span, InterfaceOrGenerateItem> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = module_common_item(s)?;

View File

@ -176,7 +176,8 @@ pub(crate) fn module_or_generate_item_module(s: Span) -> IResult<Span, ModuleOrG
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn module_or_generate_item_module_item(s: Span) -> IResult<Span, ModuleOrGenerateItem> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = module_common_item(s)?;

View File

@ -165,7 +165,8 @@ pub(crate) fn port(s: Span) -> IResult<Span, Port> {
alt((port_non_named, port_named))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn port_non_named(s: Span) -> IResult<Span, Port> {
let (s, a) = opt(port_expression)(s)?;
Ok((s, Port::NonNamed(Box::new(PortNonNamed { nodes: (a,) }))))

View File

@ -41,7 +41,8 @@ pub(crate) fn non_port_program_item_assign(s: Span) -> IResult<Span, NonPortProg
))
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn non_port_program_item_module(s: Span) -> IResult<Span, NonPortProgramItem> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = module_or_generate_item_declaration(s)?;

View File

@ -38,7 +38,8 @@ pub(crate) fn description(s: Span) -> IResult<Span, Description> {
))(s)
}
#[parser(MaybeRecursive)]
#[recursive_parser]
#[parser]
pub(crate) fn description_package_item(s: Span) -> IResult<Span, Description> {
let (s, a) = many0(attribute_instance)(s)?;
let (s, b) = package_item(s)?;

View File

@ -4,7 +4,7 @@ use crate::*;
macro_rules! test {
( $x:expr, $y:expr, $z:pat ) => {
let ret = all_consuming($x)(Span::new_extra($y, Extra::default()));
let ret = all_consuming($x)(Span::new_extra($y, SpanInfo::default()));
if let $z = ret {
} else {
assert!(false, "{:?}", ret)

View File

@ -273,7 +273,7 @@ pub(crate) fn symbol<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>, S
let (s, x) = map(ws(map(tag(t.clone()), |x: Span| x.into())), |x| Symbol {
nodes: x,
})(s)?;
Ok((clear_recursive_flags(s), x))
Ok((s, x))
}
}
@ -288,7 +288,7 @@ pub(crate) fn keyword<'a>(t: &'a str) -> impl Fn(Span<'a>) -> IResult<Span<'a>,
)),
|x| Keyword { nodes: x },
)(s)?;
Ok((clear_recursive_flags(s), x))
Ok((s, x))
}
}
@ -448,31 +448,6 @@ pub(crate) fn concat<'a>(a: Span<'a>, b: Span<'a>) -> Option<Span<'a>> {
}
}
pub(crate) fn check_recursive_flag(s: Span, id: usize) -> bool {
let upper = id / 128;
let lower = id % 128;
((s.extra.recursive_flag[upper] >> lower) & 1) == 1
}
pub(crate) fn set_recursive_flag(mut s: Span, id: usize, bit: bool) -> Span {
let upper = id / 128;
let lower = id % 128;
let val = if bit { 1u128 << lower } else { 0u128 };
let mask = !(1u128 << lower);
let mut recursive_flag = s.extra.recursive_flag;
recursive_flag[upper] = (recursive_flag[upper] & mask) | val;
s.extra.recursive_flag = recursive_flag;
s
}
pub(crate) fn clear_recursive_flags(mut s: Span) -> Span {
s.extra.recursive_flag = [0; RECURSIVE_FLAG_WORDS];
s
}
pub(crate) fn is_keyword(s: &Span) -> bool {
for k in KEYWORDS {
if &s.fragment == k {
@ -485,10 +460,11 @@ pub(crate) fn is_keyword(s: &Span) -> bool {
#[cfg(feature = "trace")]
pub(crate) fn trace<'a>(mut s: Span<'a>, name: &str) -> Span<'a> {
println!(
"{:<128} : {:<4},{:>032x} : {}",
//"{:<128} : {:<4},{:>032x} : {}",
"{:<128} : {:<4} : {}",
format!("{}{}", " ".repeat(s.extra.depth), name),
s.offset,
s.extra.recursive_flag[0],
//s.extra.recursive_flag[0],
s.fragment
);
s.extra.depth += 1;

View File

@ -5,9 +5,14 @@ authors = ["dalance <dalance@gmail.com>"]
edition = "2018"
build = "build.rs"
[features]
default = []
trace = []
[dependencies]
sv-parser-macros = { path = "../sv-parser-macros" }
nom_locate = { path = "../../nom_locate" }
nom_locate = { path = "../../nom_locate" }
nom-recursive = { path = "../../nom-recursive/nom-recursive" }
[build-dependencies]
walkdir = "2"

View File

@ -23,20 +23,30 @@ pub use special_node::*;
pub use specify_section::*;
pub use udp_declaration_and_instantiation::*;
pub(crate) use nom_recursive::{RecursiveInfo, RecursiveTracer};
pub(crate) use sv_parser_macros::*;
// -----------------------------------------------------------------------------
pub const RECURSIVE_FLAG_WORDS: usize = 1;
#[derive(Copy, Clone, Default, Debug, PartialEq)]
pub struct Extra {
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SpanInfo {
#[cfg(feature = "trace")]
pub depth: usize,
pub recursive_flag: [u128; RECURSIVE_FLAG_WORDS],
pub recursive_info: RecursiveInfo,
}
pub type Span<'a> = nom_locate::LocatedSpanEx<&'a str, Extra>;
pub type Span<'a> = nom_locate::LocatedSpanEx<&'a str, SpanInfo>;
impl RecursiveTracer for SpanInfo {
fn get_info(&self) -> RecursiveInfo {
self.recursive_info
}
fn set_info(mut self, info: RecursiveInfo) -> Self {
self.recursive_info = info;
self
}
}
// -----------------------------------------------------------------------------

View File

@ -10,6 +10,10 @@ readme = "../README.md"
description = ""
edition = "2018"
[features]
default = []
trace = ["sv-parser-parser/trace"]
[dependencies]
sv-parser-syntaxtree = { path = "../sv-parser-syntaxtree" }
sv-parser-parser = { path = "../sv-parser-parser" }