From 70f00d1a1e2e82582c576605d7f3e10c01345511 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 3 Jan 2019 13:49:37 -0800 Subject: [PATCH] Give immediate tokens higher implicit precedence than other tokens --- src/build_tables/token_conflicts.rs | 17 ++++++++--------- src/grammars.rs | 2 +- src/prepare_grammar/expand_tokens.rs | 16 +++++++++++----- src/prepare_grammar/extract_simple_aliases.rs | 6 +++--- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/build_tables/token_conflicts.rs b/src/build_tables/token_conflicts.rs index 18a80484..91edadec 100644 --- a/src/build_tables/token_conflicts.rs +++ b/src/build_tables/token_conflicts.rs @@ -2,6 +2,7 @@ use crate::build_tables::item::LookaheadSet; use crate::grammars::LexicalGrammar; use crate::nfa::{CharacterSet, NfaCursor}; use hashbrown::HashSet; +use std::cmp::Ordering; use std::fmt; #[derive(Clone, Debug, Default, PartialEq, Eq)] @@ -71,16 +72,14 @@ impl<'a> TokenConflictMap<'a> { return false; } - match ( - grammar.variables[left.1].is_string, - grammar.variables[right.1].is_string, - ) { - (true, false) => return true, - (false, true) => return false, - _ => {} + match grammar.variables[left.1] + .implicit_precedence + .cmp(&grammar.variables[right.1].implicit_precedence) + { + Ordering::Less => false, + Ordering::Greater => true, + Ordering::Equal => left.1 < right.1, } - - left.0 < right.0 } } diff --git a/src/grammars.rs b/src/grammars.rs index 7f587a8c..f82d6b02 100644 --- a/src/grammars.rs +++ b/src/grammars.rs @@ -36,7 +36,7 @@ pub(crate) struct InputGrammar { pub(crate) struct LexicalVariable { pub name: String, pub kind: VariableType, - pub is_string: bool, + pub implicit_precedence: i32, pub start_state: u32, } diff --git a/src/prepare_grammar/expand_tokens.rs b/src/prepare_grammar/expand_tokens.rs index 61b1897c..6520c432 100644 --- a/src/prepare_grammar/expand_tokens.rs +++ b/src/prepare_grammar/expand_tokens.rs @@ -14,11 +14,17 @@ struct NfaBuilder { precedence_stack: Vec, } -fn is_string(rule: &Rule) -> bool { +fn get_implicit_precedence(rule: &Rule) -> i32 { match rule { - Rule::String(_) => true, - Rule::Metadata { rule, .. } => is_string(rule), - _ => false, + Rule::String(_) => 1, + Rule::Metadata { rule, params } => { + if params.is_main_token { + get_implicit_precedence(rule) + 2 + } else { + get_implicit_precedence(rule) + } + } + _ => 0, } } @@ -67,7 +73,7 @@ pub(crate) fn expand_tokens(mut grammar: ExtractedLexicalGrammar) -> Result