Give immediate tokens higher implicit precedence than other tokens
This commit is contained in:
parent
c0f48dff6f
commit
70f00d1a1e
4 changed files with 23 additions and 18 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,11 +14,17 @@ struct NfaBuilder {
|
|||
precedence_stack: Vec<i32>,
|
||||
}
|
||||
|
||||
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<Lexi
|
|||
variables.push(LexicalVariable {
|
||||
name: variable.name,
|
||||
kind: variable.kind,
|
||||
is_string: is_string(&variable.rule),
|
||||
implicit_precedence: get_implicit_precedence(&variable.rule),
|
||||
start_state: builder.nfa.last_state_id(),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,19 +137,19 @@ mod tests {
|
|||
LexicalVariable {
|
||||
name: "t1".to_string(),
|
||||
kind: VariableType::Anonymous,
|
||||
is_string: true,
|
||||
implicit_precedence: 0,
|
||||
start_state: 0,
|
||||
},
|
||||
LexicalVariable {
|
||||
name: "t2".to_string(),
|
||||
kind: VariableType::Anonymous,
|
||||
is_string: true,
|
||||
implicit_precedence: 0,
|
||||
start_state: 0,
|
||||
},
|
||||
LexicalVariable {
|
||||
name: "t3".to_string(),
|
||||
kind: VariableType::Anonymous,
|
||||
is_string: true,
|
||||
implicit_precedence: 0,
|
||||
start_state: 0,
|
||||
}
|
||||
],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue