Carry resolved TeX command meanings through expansion
This commit is contained in:
1 parent
c6b5c9ed56
commit
93d3d38983
3 files changed
+87
-23
No files matched your search
@@ -76,6 +76,10 @@ impl From<Token> for InputToken {
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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>);
|
||||
|
||||
#[derive(Clone)]
|
||||
enum InputFrame {
|
||||
Source {
|
||||
@@ -707,22 +711,37 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
definition: bool,
|
||||
) -> Result<Option<InputToken>, Error> {
|
||||
while let Some(token) = self.raw(files)? {
|
||||
if let Some(token) = self.expand_one(token, files, definition)? {
|
||||
return Ok(Some(token));
|
||||
self.expanded_with_meaning(files, definition)
|
||||
.map(|result| result.map(|(input, _)| input))
|
||||
}
|
||||
fn expanded_with_meaning(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
definition: bool,
|
||||
) -> Result<Option<ExpandedToken>, Error> {
|
||||
while let Some(input) = self.raw(files)? {
|
||||
if let Some(expanded) = self.expand_one_with_meaning(input, files, definition)? {
|
||||
return Ok(Some(expanded));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
fn required_expanded(&mut self, files: &mut impl SourceProvider) -> Result<InputToken, Error> {
|
||||
self.expanded(files)?
|
||||
self.required_expanded_with_meaning(files).map(|(input, _)| input)
|
||||
}
|
||||
fn required_expanded_with_meaning(
|
||||
&mut self,
|
||||
files: &mut impl SourceProvider,
|
||||
) -> Result<ExpandedToken, Error> {
|
||||
self.expanded_with_meaning(files, false)?
|
||||
.ok_or_else(|| self.error("unexpected end of expanded input", self.current))
|
||||
}
|
||||
fn expanded_non_space(&mut self, files: &mut impl SourceProvider) -> Result<InputToken, Error> {
|
||||
loop {
|
||||
let token = self.required_expanded(files)?;
|
||||
if !self.is_spacer(&token.token) {
|
||||
return Ok(token);
|
||||
let (input, meaning) = self.required_expanded_with_meaning(files)?;
|
||||
let meaning = meaning.unwrap_or_else(|| self.meaning(&input.token));
|
||||
if !matches!(meaning, Meaning::Character(TokenKind::Character(_, CatCode::Space))) {
|
||||
return Ok(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -843,6 +862,16 @@ impl Executor {
|
||||
files: &mut impl SourceProvider,
|
||||
definition_context: bool,
|
||||
) -> Result<Option<InputToken>, Error> {
|
||||
self.expand_one_with_meaning(input, files, definition_context)
|
||||
.map(|result| result.map(|(input, _)| input))
|
||||
}
|
||||
|
||||
fn expand_one_with_meaning(
|
||||
&mut self,
|
||||
input: InputToken,
|
||||
files: &mut impl SourceProvider,
|
||||
definition_context: bool,
|
||||
) -> Result<Option<ExpandedToken>, Error> {
|
||||
if self.expansion_depth >= scan_stack::limit(self.limits.input_depth) {
|
||||
return self.fail("native expansion nesting limit exceeded");
|
||||
}
|
||||
@@ -859,12 +888,12 @@ impl Executor {
|
||||
mut input: InputToken,
|
||||
files: &mut impl SourceProvider,
|
||||
mut definition_context: bool,
|
||||
) -> Result<Option<InputToken>, Error> {
|
||||
) -> Result<Option<ExpandedToken>, Error> {
|
||||
let mut pending = Vec::new();
|
||||
let mut first = None;
|
||||
let result = loop {
|
||||
if input.frozen {
|
||||
break Some(input);
|
||||
break Some((input, None));
|
||||
}
|
||||
let meaning = self.meaning(&input.token);
|
||||
let Meaning::Primitive(command @ (Primitive::Expandafter | Primitive::UsePrimitive)) = meaning else {
|
||||
@@ -885,7 +914,7 @@ impl Executor {
|
||||
}
|
||||
self.unread(operand)?;
|
||||
input.token.kind = TokenKind::Command(CommandName::FrozenPrimitivePrefix);
|
||||
break Some(input);
|
||||
break Some((input, None));
|
||||
}
|
||||
// Expanding the second token is the same dispatch with deferred
|
||||
// input. Only a re-entry from a scanner needs a new expansion frame.
|
||||
@@ -901,7 +930,7 @@ impl Executor {
|
||||
let Some(first) = first else {
|
||||
return Ok(result);
|
||||
};
|
||||
if let Some(second) = result {
|
||||
if let Some((second, _)) = result {
|
||||
self.unread(second)?;
|
||||
}
|
||||
self.unread(first)?;
|
||||
@@ -917,7 +946,7 @@ impl Executor {
|
||||
meaning: Meaning,
|
||||
files: &mut impl SourceProvider,
|
||||
definition_context: bool,
|
||||
) -> Result<Option<InputToken>, Error> {
|
||||
) -> Result<Option<ExpandedToken>, Error> {
|
||||
let token = &input.token;
|
||||
use Primitive::*;
|
||||
match meaning {
|
||||
@@ -929,7 +958,7 @@ impl Executor {
|
||||
}
|
||||
Meaning::Macro(definition) => {
|
||||
if definition.protected && definition_context {
|
||||
return Ok(Some(input));
|
||||
return Ok(Some((input, Some(Meaning::Macro(definition)))));
|
||||
}
|
||||
self.call_macro(definition, token, files)?;
|
||||
}
|
||||
@@ -950,12 +979,13 @@ impl Executor {
|
||||
let mut next = self.normal_raw(files)?;
|
||||
// A nonexpandable control sequence keeps its command
|
||||
// meaning, including a character alias in scan_toks.
|
||||
next.frozen |= match self.meaning(&next.token) {
|
||||
let meaning = self.meaning(&next.token);
|
||||
next.frozen |= match &meaning {
|
||||
Meaning::Undefined | Meaning::Macro(_) => true,
|
||||
Meaning::Primitive(p) => p.expandable(),
|
||||
_ => false,
|
||||
};
|
||||
return Ok(Some(next));
|
||||
return Ok(Some((next, Some(meaning))));
|
||||
}
|
||||
Number => {
|
||||
let value = self.integer(files)?;
|
||||
@@ -1232,10 +1262,8 @@ impl Executor {
|
||||
if limit == ConditionalLimit::Scanning {
|
||||
let span = token.span;
|
||||
self.unread(input)?;
|
||||
return Ok(Some(
|
||||
Token::new(TokenKind::Command(CommandName::FrozenRelax), span)
|
||||
.into(),
|
||||
));
|
||||
let relax = Token::new(TokenKind::Command(CommandName::FrozenRelax), span);
|
||||
return Ok(Some((relax.into(), None)));
|
||||
}
|
||||
if !limit.accepts(primitive) {
|
||||
return self.fail("unexpected native conditional branch delimiter");
|
||||
@@ -1252,10 +1280,10 @@ impl Executor {
|
||||
_ if primitive.conditional() => {
|
||||
return self.fail(format!("unsupported native conditional: {primitive:?}"));
|
||||
}
|
||||
_ => return Ok(Some(input)),
|
||||
_ => return Ok(Some((input, Some(Meaning::Primitive(primitive))))),
|
||||
}
|
||||
}
|
||||
_ => return Ok(Some(input)),
|
||||
meaning => return Ok(Some((input, Some(meaning)))),
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
@@ -3924,7 +3952,7 @@ impl Executor {
|
||||
capture_boundary(self, initial_depth, files);
|
||||
self.process_native_pages(files)?;
|
||||
self.check_box_capacity(None)?;
|
||||
let Some(input) = self.expanded(files)? else {
|
||||
let Some((input, meaning)) = self.expanded_with_meaning(files, false)? else {
|
||||
break;
|
||||
};
|
||||
if let Some(boundary) = input.alignment {
|
||||
@@ -3934,7 +3962,7 @@ impl Executor {
|
||||
self.alignment_boundary(boundary, files)?;
|
||||
continue;
|
||||
}
|
||||
let meaning = self.meaning(&input.token);
|
||||
let meaning = meaning.unwrap_or_else(|| self.meaning(&input.token));
|
||||
let frozen_expandable = Self::suppresses_meaning(&input, &meaning);
|
||||
if xe
|
||||
&& (frozen_expandable
|
||||
|
||||
@@ -156,6 +156,16 @@ fn expansion_order_let_ifx_noexpand_and_csname_follow_token_meanings() {
|
||||
assert_eq!(text(&execute(r"\ifx XBAD\else GOOD\fi")), "GOOD");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backed_up_commands_see_changes_from_everypar_and_expandafter() {
|
||||
let result = execute(concat!(
|
||||
r"\let\chosen=A\def\change{\let\chosen=C}",
|
||||
r"\everypar{\let\chosen=B}",
|
||||
r"\chosen\expandafter\change\chosen\par",
|
||||
));
|
||||
assert_eq!(text(&result), "BC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skipped_branches_are_not_expanded_or_executed() {
|
||||
let result = execute(
|
||||
|
||||
Reference in new issue
Block a user