Skip to content

Commit c910a29

Browse files
committed
[assembler] Support super/subscript arrow.
1 parent 3fe900b commit c910a29

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

assembler/src/asmlib/lexer.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) enum Token {
9898
/// section 6-3.2, "RULES FOR SYMEX FORMATION".
9999
Hold,
100100
NotHold, // handled specially, there is no glyph for this.
101-
Arrow,
101+
Arrow(Script),
102102
Hand(Script),
103103
Hash(Script),
104104
Equals(Script),
@@ -177,7 +177,7 @@ impl Display for Token {
177177
Token::RightParen(script) => write_elevated(script, ")"),
178178
Token::Hold => f.write_char('h'),
179179
Token::NotHold => f.write_char('ℏ'),
180-
Token::Arrow => f.write_str("->"),
180+
Token::Arrow(script) => write_elevated(script, "->"),
181181
Token::Hand(script) => write_elevated(script, "☛"),
182182
Token::Asterisk => f.write_char('*'),
183183
Token::Dot(script) => write_elevated(script, DOT_STR),
@@ -495,10 +495,7 @@ mod lexer_impl_new {
495495
GlyphShape::Solidus => Some(Token::Solidus(script)),
496496
GlyphShape::Times => Some(Token::Times(script)),
497497
GlyphShape::Hash => Some(Token::Hash(script)),
498-
GlyphShape::Arrow => Some(match script {
499-
Script::Super | Script::Sub => unimplemented!(),
500-
Script::Normal => Token::Arrow,
501-
}),
498+
GlyphShape::Arrow => Some(Token::Arrow(script)),
502499
GlyphShape::LessThan => only_normal(Token::LessThan),
503500
GlyphShape::GreaterThan => only_normal(Token::GreaterThan),
504501
GlyphShape::Overbar | GlyphShape::Square | GlyphShape::n => make_symex(),
@@ -576,7 +573,7 @@ mod lexer_impl_new {
576573
let merged_span = current_span.start..incoming_span.end;
577574
match current {
578575
Token::Minus(Script::Normal) if incoming == Token::GreaterThan => {
579-
TokenMergeResult::Merged(Token::Arrow, merged_span)
576+
TokenMergeResult::Merged(Token::Arrow(Script::Normal), merged_span)
580577
}
581578
Token::SymexSyllable(existing_script, mut existing_name) => match incoming {
582579
Token::Hold if existing_script == Script::Normal => {

assembler/src/asmlib/lexer/tests.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,31 @@ fn test_hand_hand_normal() {
138138

139139
#[test]
140140
fn test_arrow_plain() {
141-
assert_eq!(scan_tokens_only("->"), Ok(vec![Token::Arrow]));
141+
assert_eq!(
142+
scan_tokens_only("->"),
143+
Ok(vec![Token::Arrow(Script::Normal)])
144+
);
145+
// Since the user has @arr@, @sup_arr@, etc. we don't actually
146+
// require @sub_minus@@sub_greater@ to be understood as an arrow
147+
// (though it likely is).
142148
}
143149

144150
#[test]
145151
fn test_double_arrow_plain() {
152+
use Script::Normal;
146153
assert_eq!(
147154
scan_tokens_only("->->"),
148-
Ok(vec![Token::Arrow, Token::Arrow])
155+
Ok(vec![Token::Arrow(Normal), Token::Arrow(Normal)])
149156
);
150157
}
151158

152159
#[test]
153160
fn test_arrow_as_glyph() {
154-
assert_eq!(scan_tokens_only("@arr@"), Ok(vec![Token::Arrow]));
161+
use Script::*;
162+
assert_eq!(scan_tokens_only("@arr@"), Ok(vec![Token::Arrow(Normal)]));
163+
164+
assert_eq!(scan_tokens_only("@sup_arr@"), Ok(vec![Token::Arrow(Super)]));
165+
assert_eq!(scan_tokens_only("@sub_arr@"), Ok(vec![Token::Arrow(Sub)]));
155166
}
156167

157168
#[test]
@@ -163,7 +174,7 @@ fn test_upper_lexer_span() {
163174
scan_slices("{->}"),
164175
Ok(vec![
165176
(Token::LeftBrace, "{"),
166-
(Token::Arrow, "->"),
177+
(Token::Arrow(Script::Normal), "->"),
167178
(Token::RightBrace, "}"),
168179
])
169180
);
@@ -1084,9 +1095,10 @@ fn test_superscript_minus_unicode() {
10841095

10851096
#[test]
10861097
fn test_annotations_are_ignored() {
1098+
use Script::Normal;
10871099
assert_eq!(
10881100
scan_tokens_only("->[THIS IS AN ANNOTATION]->"),
1089-
Ok(vec![Token::Arrow, Token::Arrow,])
1101+
Ok(vec![Token::Arrow(Normal), Token::Arrow(Normal),])
10901102
);
10911103
}
10921104

assembler/src/asmlib/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ where
188188
name,
189189
span: extra.span(),
190190
})
191-
.then_ignore(just(Tok::Arrow))
191+
.then_ignore(just(Tok::Arrow(Script::Normal)))
192192
.labelled("tag definition")
193193
}
194194

@@ -313,7 +313,7 @@ where
313313
just(Tok::Hand(Script::Normal)).to('☛'),
314314
just(Tok::Dot(Script::Normal)).to(lexer::DOT_CHAR),
315315
just(Tok::Equals(Script::Normal)).to('='),
316-
just(Tok::Arrow).to('→'),
316+
just(Tok::Arrow(Script::Normal)).to('→'),
317317
just(Tok::Pipe(Script::Normal)).to('|'),
318318
just(Tok::ProperSuperset).to('⊃'),
319319
just(Tok::IdenticalTo).to('≡'),

0 commit comments

Comments
 (0)