Store single native input tokens inline
This commit is contained in:
1 parent
9c93ff9750
commit
fb5cb5ada9
6 files changed
+64
-16
No files matched your search
@@ -76,6 +76,23 @@ impl From<Token> for InputToken {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Clone)]
|
||||
enum InputTokens {
|
||||
One(InputToken),
|
||||
Many(Vec<InputToken>),
|
||||
}
|
||||
|
||||
impl std::ops::Deref for InputTokens {
|
||||
type Target = [InputToken];
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match self {
|
||||
Self::One(token) => std::slice::from_ref(token),
|
||||
Self::Many(tokens) => tokens,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A meaning is valid until the returned token is used or put back. Tokens
|
||||
// that bypass normal expansion may still need to be looked up by the caller.
|
||||
type ExpandedToken = (InputToken, Option<Meaning>);
|
||||
@@ -91,7 +108,7 @@ enum InputFrame {
|
||||
conditional_boundary: usize,
|
||||
},
|
||||
Tokens {
|
||||
tokens: Vec<InputToken>,
|
||||
tokens: InputTokens,
|
||||
cursor: usize,
|
||||
kind: TokenInputKind,
|
||||
},
|
||||
@@ -580,6 +597,12 @@ impl Executor {
|
||||
self.push_kind(tokens, TokenInputKind::Inserted)
|
||||
}
|
||||
fn push_kind(&mut self, tokens: Vec<InputToken>, kind: TokenInputKind) -> Result<(), Error> {
|
||||
self.push_input_tokens(InputTokens::Many(tokens), kind)
|
||||
}
|
||||
fn push_token(&mut self, token: InputToken) -> Result<(), Error> {
|
||||
self.push_input_tokens(InputTokens::One(token), TokenInputKind::Inserted)
|
||||
}
|
||||
fn push_input_tokens(&mut self, tokens: InputTokens, kind: TokenInputKind) -> Result<(), Error> {
|
||||
if tokens.len() > self.limits.tokens {
|
||||
return self.fail("native token limit exceeded");
|
||||
}
|
||||
@@ -587,7 +610,7 @@ impl Executor {
|
||||
return self.fail("native input stack limit exceeded");
|
||||
}
|
||||
if matches!(kind, TokenInputKind::BackedUp) && !self.alignments.is_empty() {
|
||||
for token in &tokens {
|
||||
for token in tokens.iter() {
|
||||
self.alignment_balance -= Self::alignment_brace_token(&token.token);
|
||||
}
|
||||
}
|
||||
@@ -603,7 +626,7 @@ impl Executor {
|
||||
fn unread(&mut self, mut token: InputToken) -> Result<(), Error> {
|
||||
token.xe_backed_char = false;
|
||||
self.discard_exhausted_tokens();
|
||||
self.push_kind(vec![token], TokenInputKind::BackedUp)
|
||||
self.push_input_tokens(InputTokens::One(token), TokenInputKind::BackedUp)
|
||||
}
|
||||
fn push_source(&mut self, file: SourceFile) -> Result<(), Error> {
|
||||
if self.inputs.len() >= self.limits.input_depth {
|
||||
@@ -831,12 +854,12 @@ impl Executor {
|
||||
} else {
|
||||
TokenKind::Character(c, cat)
|
||||
};
|
||||
self.push(vec![Token {
|
||||
self.push_token(Token {
|
||||
kind,
|
||||
span: token.span,
|
||||
invocation: token.invocation,
|
||||
}
|
||||
.into()])
|
||||
.into())
|
||||
}
|
||||
|
||||
fn roman_numeral(&self, mut number: usize) -> Result<String, Error> {
|
||||
@@ -1853,9 +1876,7 @@ impl Executor {
|
||||
tokens.into_iter().map(InputToken::from).collect(),
|
||||
TokenInputKind::Write,
|
||||
)?;
|
||||
self.push(vec![
|
||||
Token::new(TokenKind::Character('{', CatCode::BeginGroup), span).into(),
|
||||
])?;
|
||||
self.push_token(Token::new(TokenKind::Character('{', CatCode::BeginGroup), span).into())?;
|
||||
let previous_writing = std::mem::replace(&mut self.writing, true);
|
||||
let expanded = self.scan_expanded_group(files);
|
||||
self.writing = previous_writing;
|
||||
@@ -3981,7 +4002,7 @@ impl Executor {
|
||||
}
|
||||
fn insert_paragraph_token(&mut self, span: Span) -> Result<(), Error> {
|
||||
self.reads.insert(Dependency::ParagraphToken);
|
||||
self.push(vec![Token::new(self.state.paragraph_token(), span).into()])
|
||||
self.push_token(Token::new(self.state.paragraph_token(), span).into())
|
||||
}
|
||||
|
||||
fn in_unrestricted_horizontal_mode(&self) -> bool {
|
||||
|
||||
@@ -128,7 +128,7 @@ impl Executor {
|
||||
let mut t: InputToken =
|
||||
Token::new(TokenKind::Command(CommandName::FrozenRelax), self.current).into();
|
||||
t.alignment = Some(boundary);
|
||||
self.push(vec![t])
|
||||
self.push_token(t)
|
||||
}
|
||||
// Called by raw before a token can be consumed by any expandable scanner.
|
||||
pub(super) fn alignment_raw(&mut self, token: &InputToken) -> Result<bool, Error> {
|
||||
|
||||
@@ -38,7 +38,6 @@ impl Executor {
|
||||
let Some(input) = self.inputs.last_mut() else {
|
||||
return Ok(None);
|
||||
};
|
||||
let mut warnings = Vec::new();
|
||||
let next = match input {
|
||||
InputFrame::Tokens { tokens, cursor, .. } => {
|
||||
let t = tokens.get(*cursor).cloned();
|
||||
@@ -88,8 +87,8 @@ impl Executor {
|
||||
..
|
||||
} => {
|
||||
let token = mouth.next(&self.state, &mut self.reads);
|
||||
warnings = mouth.take_utf8_warnings();
|
||||
match token {
|
||||
let warnings = mouth.take_utf8_warnings();
|
||||
let next = match token {
|
||||
Ok(Some(token)) => {
|
||||
self.input_position = Some((*source, mouth.line_number()));
|
||||
Next::Token(Some(token.into()))
|
||||
@@ -123,7 +122,11 @@ impl Executor {
|
||||
}
|
||||
}
|
||||
Err((span, text)) => Next::Error(span, text),
|
||||
};
|
||||
if !warnings.is_empty() {
|
||||
self.observe_xe_utf8_warnings(warnings, files)?;
|
||||
}
|
||||
next
|
||||
}
|
||||
InputFrame::Hook { tokens, cursor, .. } => {
|
||||
let t = tokens.get(*cursor).cloned().map(InputToken::from);
|
||||
@@ -134,7 +137,6 @@ impl Executor {
|
||||
}
|
||||
InputFrame::Boundary => Next::Boundary,
|
||||
};
|
||||
self.observe_xe_utf8_warnings(warnings, files)?;
|
||||
match next {
|
||||
Next::Token(Some(token)) => {
|
||||
self.current = token.token.invocation.unwrap_or(token.token.span);
|
||||
|
||||
@@ -117,7 +117,7 @@ impl Executor {
|
||||
});
|
||||
character.xe_backed_char = true;
|
||||
self.discard_exhausted_tokens();
|
||||
self.push_kind(vec![character], TokenInputKind::BackedUp)?;
|
||||
self.push_input_tokens(InputTokens::One(character), TokenInputKind::BackedUp)?;
|
||||
self.push_xe_inter_char_tokens(tokens)?;
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ impl Executor {
|
||||
display: self.display.take(),
|
||||
});
|
||||
self.inputs.push(InputFrame::Tokens {
|
||||
tokens: tokens.iter().cloned().map(InputToken::from).collect(),
|
||||
tokens: InputTokens::Many(tokens.iter().cloned().map(InputToken::from).collect()),
|
||||
cursor: 0,
|
||||
kind: TokenInputKind::Output,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user