swc_css_parser/lexer/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
use std::{borrow::Cow, cell::RefCell, char::REPLACEMENT_CHARACTER, rc::Rc};
use swc_atoms::{Atom, AtomStoreCell, JsWord};
use swc_common::{
comments::{Comment, CommentKind, Comments},
input::Input,
util::take::Take,
BytePos, Span,
};
use swc_css_ast::{
matches_eq_ignore_ascii_case, DimensionToken, NumberType, Token, TokenAndSpan, UrlKeyValue,
};
use crate::{
error::{Error, ErrorKind},
parser::{input::ParserInput, ParserConfig},
};
pub(crate) type LexResult<T> = Result<T, ErrorKind>;
#[derive(Clone)]
pub struct Lexer<'a, I>
where
I: Input,
{
comments: Option<&'a dyn Comments>,
pending_leading_comments: Vec<Comment>,
input: I,
cur: Option<char>,
cur_pos: BytePos,
start_pos: BytePos,
/// Used to override last_pos
override_pos: Option<BytePos>,
config: ParserConfig,
buf: Rc<RefCell<String>>,
raw_buf: Rc<RefCell<String>>,
sub_buf: Rc<RefCell<String>>,
errors: Rc<RefCell<Vec<Error>>>,
atoms: Rc<AtomStoreCell>,
}
impl<'a, I> Lexer<'a, I>
where
I: Input,
{
pub fn new(input: I, comments: Option<&'a dyn Comments>, config: ParserConfig) -> Self {
let start_pos = input.last_pos();
Lexer {
comments,
input,
cur: None,
cur_pos: start_pos,
start_pos,
override_pos: None,
config,
buf: Rc::new(RefCell::new(String::with_capacity(256))),
raw_buf: Rc::new(RefCell::new(String::with_capacity(256))),
sub_buf: Rc::new(RefCell::new(String::with_capacity(32))),
errors: Default::default(),
pending_leading_comments: Default::default(),
atoms: Default::default(),
}
}
fn with_buf<F, Ret>(&mut self, op: F) -> LexResult<Ret>
where
F: for<'any> FnOnce(&mut Lexer<I>, &mut String) -> LexResult<Ret>,
{
let b = self.buf.clone();
let mut buf = b.borrow_mut();
buf.clear();
op(self, &mut buf)
}
fn with_sub_buf<F, Ret>(&mut self, op: F) -> LexResult<Ret>
where
F: for<'any> FnOnce(&mut Lexer<I>, &mut String) -> LexResult<Ret>,
{
let b = self.sub_buf.clone();
let mut sub_buf = b.borrow_mut();
sub_buf.clear();
op(self, &mut sub_buf)
}
fn with_buf_and_raw_buf<F, Ret>(&mut self, op: F) -> LexResult<Ret>
where
F: for<'any> FnOnce(&mut Lexer<I>, &mut String, &mut String) -> LexResult<Ret>,
{
let b = self.buf.clone();
let r = self.raw_buf.clone();
let mut buf = b.borrow_mut();
let mut raw = r.borrow_mut();
buf.clear();
raw.clear();
op(self, &mut buf, &mut raw)
}
}
impl<I: Input> Iterator for Lexer<'_, I> {
type Item = TokenAndSpan;
fn next(&mut self) -> Option<Self::Item> {
let token = self.consume_token();
match token {
Ok(token) => {
let end = self
.override_pos
.take()
.unwrap_or_else(|| self.input.last_pos());
let span = Span::new(self.start_pos, end);
let token_and_span = TokenAndSpan { span, token };
return Some(token_and_span);
}
Err(..) => {
return None;
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct LexerState {
pos: BytePos,
}
impl<I> ParserInput for Lexer<'_, I>
where
I: Input,
{
type State = LexerState;
fn start_pos(&mut self) -> BytePos {
self.input.last_pos()
}
fn state(&mut self) -> Self::State {
LexerState {
pos: self.input.last_pos(),
}
}
fn reset(&mut self, state: &Self::State) {
unsafe {
// Safety: state.pos is created from a valid position.
self.input.reset_to(state.pos);
}
}
fn take_errors(&mut self) -> Vec<Error> {
self.errors.take()
}
fn skip_ws(&mut self) -> Option<BytePos> {
self.read_comments();
if let Some(c) = self.input.cur() {
if !is_whitespace(c) {
return None;
}
}
loop {
self.read_comments();
if self.input.uncons_while(is_whitespace).is_empty() {
break;
}
}
Some(self.input.last_pos())
}
fn atom(&self, s: Cow<str>) -> JsWord {
self.atoms.atom(s)
}
}
impl<I> Lexer<'_, I>
where
I: Input,
{
#[inline(always)]
fn cur(&mut self) -> Option<char> {
self.cur
}
#[inline(always)]
fn next(&mut self) -> Option<char> {
self.input.cur()
}
#[inline(always)]
fn next_next(&mut self) -> Option<char> {
self.input.peek()
}
#[inline(always)]
fn next_next_next(&mut self) -> Option<char> {
self.input.peek_ahead()
}
#[inline(always)]
fn consume(&mut self) -> Option<char> {
let cur = self.input.cur();
self.cur = cur;
self.cur_pos = self.input.last_pos();
if cur.is_some() {
unsafe {
// Safety: cur is Some
self.input.bump();
}
}
cur
}
#[inline(always)]
fn reconsume(&mut self) {
unsafe {
// Safety: self.cur_pos is a position generated by self.input, meaning it is
// valid.
self.input.reset_to(self.cur_pos);
}
}
#[cold]
fn emit_error(&mut self, kind: ErrorKind) {
self.errors.borrow_mut().push(Error::new(
Span::new(self.cur_pos, self.input.last_pos()),
kind,
));
}
fn consume_token(&mut self) -> LexResult<Token> {
self.read_comments();
self.start_pos = self.input.last_pos();
if let Some(comments) = self.comments {
if !self.pending_leading_comments.is_empty() {
comments.add_leading_comments(self.start_pos, self.pending_leading_comments.take());
}
}
// Consume the next input code point.
match self.consume() {
// whitespace
// Consume as much whitespace as possible. Return a <whitespace-token>.
Some(c) if is_whitespace(c) => self.with_buf(|l, buf| {
buf.push(c);
loop {
let c = l.next();
match c {
Some(c) if is_whitespace(c) => {
l.consume();
buf.push(c);
}
_ => {
break;
}
}
}
return Ok(Token::WhiteSpace {
value: l.atoms.atom(&**buf),
});
}),
// U+0022 QUOTATION MARK (")
// Consume a string token and return it.
Some('"') => self.read_str(None),
// U+0023 NUMBER SIGN (#)
Some('#') => {
let first = self.next();
let second = self.next_next();
// If the next input code point is a name code point or the next two input code
// points are a valid escape, then:
if (first.is_some() && is_name(first.unwrap()))
|| self.is_valid_escape(first, second)
{
// Create a <hash-token>.
// If the next 3 input code points would start an identifier, set the
// <hash-token>’s type flag to "id".
let third = self.next_next_next();
let is_would_start_ident = self.would_start_ident(first, second, third);
// Consume an ident sequence, and set the <hash-token>’s value to the returned
// string.
let ident_sequence = self.read_ident_sequence()?;
// Return the <hash-token>.
return Ok(Token::Hash {
is_id: is_would_start_ident,
value: ident_sequence.0,
raw: ident_sequence.1,
});
}
Ok(Token::Delim { value: '#' })
}
// U+0027 APOSTROPHE (')
// Consume a string token and return it.
Some('\'') => self.read_str(None),
// U+0028 LEFT PARENTHESIS (()
// Return a <(-token>.
Some('(') => Ok(tok!("(")),
// U+0029 RIGHT PARENTHESIS ())
// Return a <)-token>.
Some(')') => Ok(tok!(")")),
// U+002B PLUS SIGN (+)
Some('+') => {
// If the input stream starts with a number, reconsume the current input code
// point, consume a numeric token and return it.
if self.would_start_number(None, None, None) {
self.reconsume();
return self.read_numeric();
}
// Otherwise, return a <delim-token> with its value set to the current input
// code point.
Ok(tok!("+"))
}
// U+002C COMMA (,)
// Return a <comma-token>.
Some(',') => Ok(tok!(",")),
// U+002D HYPHEN-MINUS (-)
Some('-') => {
// If the input stream starts with a number, reconsume the current input code
// point, consume a numeric token, and return it.
if self.would_start_number(None, None, None) {
self.reconsume();
return self.read_numeric();
}
// Otherwise, if the next 2 input code points are U+002D HYPHEN-MINUS U+003E
// GREATER-THAN SIGN (->), consume them and return a <CDC-token>.
else if self.next() == Some('-') && self.next_next() == Some('>') {
self.consume();
self.consume();
return Ok(Token::CDC);
}
// Otherwise, if the input stream starts with an identifier, reconsume the current
// input code point, consume an ident-like token, and return it.
else if self.would_start_ident(None, None, None) {
self.reconsume();
return self.read_ident_like();
}
// Otherwise, return a <delim-token> with its value set to the current input
// code point.
Ok(tok!("-"))
}
// U+002E FULL STOP (.)
Some('.') => {
// If the input stream starts with a number, reconsume the current input code
// point, consume a numeric token, and return it.
if self.would_start_number(None, None, None) {
self.reconsume();
return self.read_numeric();
}
// Otherwise, return a <delim-token> with its value set to the current input
// code point.
Ok(tok!("."))
}
// U+003A COLON (:)
// Return a <colon-token>.
Some(':') => Ok(tok!(":")),
// U+003B SEMICOLON (;)
// Return a <semicolon-token>.
Some(';') => Ok(tok!(";")),
// U+003C LESS-THAN SIGN (<)
Some('<') => {
// If the next 3 input code points are U+0021 EXCLAMATION MARK U+002D
// HYPHEN-MINUS U+002D HYPHEN-MINUS (!--), consume them and return a
// <CDO-token>.
if self.next() == Some('!')
&& self.next_next() == Some('-')
&& self.next_next_next() == Some('-')
{
self.consume(); // !
self.consume(); // -
self.consume(); // -
return Ok(tok!("<!--"));
}
// Otherwise, return a <delim-token> with its value set to the current input
// code point.
Ok(tok!("<"))
}
// U+0040 COMMERCIAL AT (@)
Some('@') => {
let first = self.next();
let second = self.next_next();
let third = self.next_next_next();
// If the next 3 input code points would start an identifier, consume a name,
// create an <at-keyword-token> with its value set to the returned value, and
// return it.
if self.would_start_ident(first, second, third) {
let ident_sequence = self.read_ident_sequence()?;
return Ok(Token::AtKeyword {
value: ident_sequence.0,
raw: ident_sequence.1,
});
}
// Otherwise, return a <delim-token> with its value set to the current input
// code point.
Ok(Token::Delim { value: '@' })
}
// U+005B LEFT SQUARE BRACKET ([)
// Return a <[-token>.
Some('[') => Ok(tok!("[")),
// U+005C REVERSE SOLIDUS (\)
Some('\\') => {
// If the input stream starts with a valid escape, reconsume the current input
// code point, consume an ident-like token, and return it.
if self.is_valid_escape(None, None) {
self.reconsume();
return self.read_ident_like();
}
// Otherwise, this is a parse error. Return a <delim-token> with its value set
// to the current input code point.
self.emit_error(ErrorKind::InvalidEscape);
Ok(Token::Delim { value: '\\' })
}
// U+005D RIGHT SQUARE BRACKET (])
// Return a <]-token>.
Some(']') => Ok(tok!("]")),
// U+007B LEFT CURLY BRACKET ({)
// Return a <{-token>.
Some('{') => Ok(tok!("{")),
// U+007D RIGHT CURLY BRACKET (})
// Return a <}-token>.
Some('}') => Ok(tok!("}")),
// digit
// Reconsume the current input code point, consume a numeric token, and return it.
Some('0'..='9') => {
self.reconsume();
self.read_numeric()
}
// name-start code point
// Reconsume the current input code point, consume an ident-like token, and return it.
Some(c) if is_name_start(c) => {
self.reconsume();
self.read_ident_like()
}
// EOF
// Return an <EOF-token>.
None => Err(ErrorKind::Eof),
// anything else
// Return a <delim-token> with its value set to the current input code point.
Some(c) => Ok(Token::Delim { value: c }),
}
}
// Consume comments.
// This section describes how to consume comments from a stream of code points.
// It returns nothing.
fn read_comments(&mut self) {
// If the next two input code point are U+002F SOLIDUS (/) followed by a U+002A
// ASTERISK (*), consume them and all following code points up to and including
// the first U+002A ASTERISK (*) followed by a U+002F SOLIDUS (/), or up to an
// EOF code point. Return to the start of this step.
// NOTE: We allow to parse line comments under the option.
if self.next() == Some('/') && self.next_next() == Some('*') {
let cmt_start = self.input.last_pos();
while self.next() == Some('/') && self.next_next() == Some('*') {
self.consume(); // '*'
self.consume(); // '/'
loop {
match self.consume() {
Some('*') if self.next() == Some('/') => {
self.consume(); // '/'
if self.comments.is_some() {
let last_pos = self.input.last_pos();
let text = unsafe {
// Safety: last_pos is a valid position
self.input.slice(cmt_start, last_pos)
};
self.pending_leading_comments.push(Comment {
kind: CommentKind::Block,
span: (self.start_pos, last_pos).into(),
text: self.atoms.atom(text),
});
}
break;
}
None => {
let span = Span::new(self.start_pos, self.input.last_pos());
self.errors
.borrow_mut()
.push(Error::new(span, ErrorKind::UnterminatedBlockComment));
return;
}
_ => {}
}
}
}
} else if self.config.allow_wrong_line_comments
&& self.next() == Some('/')
&& self.next_next() == Some('/')
{
while self.next() == Some('/') && self.next_next() == Some('/') {
self.consume(); // '/'
self.consume(); // '/'
let start_of_content = self.input.last_pos();
loop {
match self.consume() {
Some(c) if is_newline(c) => {
if self.comments.is_some() {
let last_pos = self.input.last_pos();
let text = unsafe {
// Safety: last_pos is a valid position
self.input.slice(start_of_content, last_pos)
};
self.pending_leading_comments.push(Comment {
kind: CommentKind::Line,
span: (self.start_pos, last_pos).into(),
text: self.atoms.atom(text),
});
}
break;
}
None => return,
_ => {}
}
}
}
}
}
// This section describes how to consume a numeric token from a stream of code
// points. It returns either a <number-token>, <percentage-token>, or
// <dimension-token>.
fn read_numeric(&mut self) -> LexResult<Token> {
// Consume a number and let number be the result.
let number = self.read_number()?;
let next_first = self.next();
let next_second = self.next_next();
let next_third = self.next_next_next();
// If the next 3 input code points would start an identifier, then:
if self.would_start_ident(next_first, next_second, next_third) {
// Swap logic to avoid create empty strings, because it doesn't make sense
//
// Consume a name. Set the <dimension-token>’s unit to the returned value.
let ident_sequence = self.read_ident_sequence()?;
// Create a <dimension-token> with the same value and type flag as number, and a
// unit set initially to the empty string.
let token = Token::Dimension(Box::new(DimensionToken {
value: number.0,
raw_value: number.1,
unit: ident_sequence.0,
raw_unit: ident_sequence.1,
type_flag: number.2,
}));
// Return the <dimension-token>.
return Ok(token);
}
// Otherwise, if the next input code point is U+0025 PERCENTAGE SIGN (%), consume it. Create
// a <percentage-token> with the same value as number, and return it.
else if next_first == Some('%') {
self.consume();
return Ok(Token::Percentage {
value: number.0,
raw: number.1,
});
}
// Otherwise, create a <number-token> with the same value and type flag as
// number, and return it.
Ok(Token::Number {
value: number.0,
raw: number.1,
type_flag: number.2,
})
}
// This section describes how to consume an ident-like token from a stream of
// code points. It returns an <ident-token>, <function-token>, <url-token>, or
// <bad-url-token>.
fn read_ident_like(&mut self) -> LexResult<Token> {
// Consume a name, and let string be the result.
let ident_sequence = self.read_ident_sequence()?;
// If string’s value is an ASCII case-insensitive match for "url", and the next
// input code point is U+0028 LEFT PARENTHESIS ((), consume it.
if matches_eq_ignore_ascii_case!(ident_sequence.0, "url") && self.next() == Some('(') {
self.consume();
let start_whitespace = self.input.last_pos();
// While the next two input code points are whitespace, consume the next input
// code point.
let whitespaces = self.with_buf(|l, buf| {
while let (Some(next), Some(next_next)) = (l.next(), l.next_next()) {
if is_whitespace(next) && is_whitespace(next_next) {
l.consume();
buf.push(next);
} else {
break;
}
}
Ok(buf.to_string())
})?;
match self.next() {
// If the next one or two input code points are U+0022 QUOTATION MARK ("), U+0027
// APOSTROPHE ('), or whitespace followed by U+0022 QUOTATION MARK (") or U+0027
// APOSTROPHE ('), then create a <function-token> with its value set to string and
// return it.
Some(c)
if is_whitespace(c)
&& (self.next_next() == Some('"') || self.next_next() == Some('\'')) =>
{
// Override last position because we consumed whitespaces, but they
// should not be part of token
self.override_pos = Some(start_whitespace);
return Ok(Token::Function {
value: ident_sequence.0,
raw: ident_sequence.1,
});
}
Some('"' | '\'') => {
return Ok(Token::Function {
value: ident_sequence.0,
raw: ident_sequence.1,
});
}
// Otherwise, consume a url token, and return it.
_ => {
return self.read_url(ident_sequence, whitespaces);
}
}
}
// Otherwise, if the next input code point is U+0028 LEFT PARENTHESIS ((), consume it.
// Create a <function-token> with its value set to string and return it.
else if self.next() == Some('(') {
self.consume();
return Ok(Token::Function {
value: ident_sequence.0,
raw: ident_sequence.1,
});
}
// Otherwise, create an <ident-token> with its value set to string and return
// it.
Ok(Token::Ident {
value: ident_sequence.0,
raw: ident_sequence.1,
})
}
// This section describes how to consume a string token from a stream of code
// points. It returns either a <string-token> or <bad-string-token>.
fn read_str(&mut self, maybe_ending_code_point: Option<char>) -> LexResult<Token> {
self.with_buf_and_raw_buf(|l, buf, raw| {
// This algorithm may be called with an ending code point, which denotes the
// code point that ends the string. If an ending code point is not specified,
// the current input code point is used.
let ending_code_point = maybe_ending_code_point.or_else(|| l.cur());
// Initially create a <string-token> with its value set to the empty string.
// Done above
raw.push(ending_code_point.unwrap());
// Repeatedly consume the next input code point from the stream:
loop {
match l.consume() {
// ending code point
// Return the <string-token>.
Some(c) if c == ending_code_point.unwrap() => {
raw.push(c);
break;
}
// EOF
// This is a parse error. Return the <string-token>.
None => {
l.emit_error(ErrorKind::UnterminatedString);
return Ok(Token::String {
value: l.atoms.atom(&**buf),
raw: l.atoms.atom(&**raw),
});
}
// Newline
// This is a parse error. Reconsume the current input code point, create a
// <bad-string-token>, and return it.
Some(c) if is_newline(c) => {
l.emit_error(ErrorKind::NewlineInString);
l.reconsume();
return Ok(Token::BadString {
raw: l.atoms.atom(&**raw),
});
}
// U+005C REVERSE SOLIDUS (\)
Some(c) if c == '\\' => {
let next = l.next();
// If the next input code point is EOF, do nothing.
if l.next().is_none() {
continue;
}
// Otherwise, if the next input code point is a newline, consume it.
else if l.next().is_some() && is_newline(l.next().unwrap()) {
l.consume();
raw.push(c);
raw.push(next.unwrap());
}
// Otherwise, (the stream starts with a valid escape) consume an escaped
// code point and append the returned code point to
// the <string-token>’s value.
else if l.is_valid_escape(None, None) {
let escape = l.read_escape()?;
buf.push(escape.0);
raw.push(c);
raw.push_str(&escape.1);
}
}
// Anything else
// Append the current input code point to the <string-token>’s value.
Some(c) => {
buf.push(c);
raw.push(c);
}
}
}
Ok(Token::String {
value: l.atoms.atom(&**buf),
raw: l.atoms.atom(&**raw),
})
})
}
// This section describes how to consume a url token from a stream of code
// points. It returns either a <url-token> or a <bad-url-token>.
fn read_url(&mut self, name: (JsWord, Atom), before: String) -> LexResult<Token> {
// Initially create a <url-token> with its value set to the empty string.
self.with_buf_and_raw_buf(|l, out, raw| {
raw.push_str(&before);
// Consume as much whitespace as possible.
while let Some(c) = l.next() {
if is_whitespace(c) {
l.consume();
raw.push(c);
} else {
break;
}
}
// Repeatedly consume the next input code point from the stream:
loop {
match l.consume() {
// U+0029 RIGHT PARENTHESIS ())
// Return the <url-token>.
Some(')') => {
return Ok(Token::Url {
value: l.atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, l.atoms.atom(&**raw))),
});
}
// EOF
// This is a parse error. Return the <url-token>.
None => {
l.emit_error(ErrorKind::UnterminatedUrl);
return Ok(Token::Url {
value: l.atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, l.atoms.atom(&**raw))),
});
}
// whitespace
Some(c) if is_whitespace(c) => {
// Consume as much whitespace as possible.
let whitespaces: String = l.with_sub_buf(|l, buf| {
buf.push(c);
while let Some(c) = l.next() {
if is_whitespace(c) {
l.consume();
buf.push(c);
} else {
break;
}
}
Ok(buf.to_string())
})?;
// if the next input code point is U+0029 RIGHT PARENTHESIS ()) or EOF,
// consume it and return the <url-token> (if EOF was
// encountered, this is a parse error);
match l.next() {
Some(')') => {
l.consume();
raw.push_str(&whitespaces);
return Ok(Token::Url {
value: l.atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, l.atoms.atom(&**raw))),
});
}
None => {
l.emit_error(ErrorKind::UnterminatedUrl);
raw.push_str(&whitespaces);
return Ok(Token::Url {
value: l.atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, l.atoms.atom(&**raw))),
});
}
_ => {}
}
// otherwise, consume the remnants of a bad url, create a <bad-url-token>,
// and return it.
raw.push_str(&whitespaces);
let remnants = l.read_bad_url_remnants()?;
raw.push_str(&remnants);
return Ok(Token::BadUrl {
raw: Atom::new(format!("{}{}{}", name.1, "(", raw)),
});
}
// U+0022 QUOTATION MARK (")
// U+0027 APOSTROPHE (')
// U+0028 LEFT PARENTHESIS (()
// non-printable code point
// This is a parse error. Consume the remnants of a bad url, create a
// <bad-url-token>, and return it.
Some(c) if c == '"' || c == '\'' || c == '(' || is_non_printable(c) => {
l.emit_error(ErrorKind::UnexpectedCharInUrl);
let remnants = l.read_bad_url_remnants()?;
raw.push(c);
raw.push_str(&remnants);
return Ok(Token::BadUrl {
raw: Atom::new(format!("{}{}{}", name.1, "(", raw)),
});
}
// U+005C REVERSE SOLIDUS (\)
Some(c) if c == '\\' => {
// If the stream starts with a valid escape, consume an escaped code point
// and append the returned code point to the
// <url-token>’s value.
if l.is_valid_escape(None, None) {
let escaped = l.read_escape()?;
out.push(escaped.0);
raw.push(c);
raw.push_str(&escaped.1);
}
// Otherwise, this is a parse error. Consume the remnants of a bad url,
// create a <bad-url-token>, and return it.
else {
l.emit_error(ErrorKind::InvalidEscape);
let remnants = l.read_bad_url_remnants()?;
raw.push(c);
raw.push_str(&remnants);
return Ok(Token::BadUrl {
raw: Atom::new(format!("{}{}{}", name.1, "(", raw)),
});
}
}
// anything else
// Append the current input code point to the <url-token>’s value.
Some(c) => {
out.push(c);
raw.push(c);
}
}
}
})
}
// Consume an escaped code point
// This section describes how to consume an escaped code point. It assumes that
// the U+005C REVERSE SOLIDUS (\) has already been consumed and that the next
// input code point has already been verified to be part of a valid escape. It
// will return a code point.
fn read_escape(&mut self) -> LexResult<(char, String)> {
self.with_sub_buf(|l, buf| {
// Consume the next input code point.
match l.consume() {
// hex digit
Some(c) if is_hex_digit(c) => {
let mut hex = c.to_digit(16).unwrap();
buf.push(c);
// Consume as many hex digits as possible, but no more than 5.
// Note that this means 1-6 hex digits have been consumed in total.
for _ in 0..5 {
let next = l.next();
let digit = match next.and_then(|c| c.to_digit(16)) {
Some(v) => v,
None => break,
};
l.consume();
buf.push(next.unwrap());
hex = hex * 16 + digit;
}
// If the next input code point is whitespace, consume it as well.
let next = l.next();
if let Some(next) = next {
if is_whitespace(next) {
l.consume();
buf.push(next);
}
}
// Interpret the hex digits as a hexadecimal number. If this number is zero, or
// is for a surrogate, or is greater than the maximum allowed code point, return
// U+FFFD REPLACEMENT CHARACTER (�).
let hex = match hex {
// If this number is zero
0 => REPLACEMENT_CHARACTER,
// or is for a surrogate
55296..=57343 => REPLACEMENT_CHARACTER,
// or is greater than the maximum allowed code point
1114112.. => REPLACEMENT_CHARACTER,
_ => char::from_u32(hex).unwrap_or(REPLACEMENT_CHARACTER),
};
// Otherwise, return the code point with that value.
Ok((hex, (&**buf).into()))
}
// EOF
// This is a parse error. Return U+FFFD REPLACEMENT CHARACTER (�).
None => {
l.emit_error(ErrorKind::InvalidEscape);
let value = REPLACEMENT_CHARACTER;
buf.push(value);
Ok((value, (&**buf).into()))
}
// anything else
// Return the current input code point.
Some(c) => {
buf.push(c);
Ok((c, (&**buf).into()))
}
}
})
}
// Check if two code points are a valid escape
// This section describes how to check if two code points are a valid escape.
// The algorithm described here can be called explicitly with two code points,
// or can be called with the input stream itself. In the latter case, the two
// code points in question are the current input code point and the next input
// code point, in that order.
fn is_valid_escape(&mut self, maybe_first: Option<char>, maybe_second: Option<char>) -> bool {
// If the first code point is not U+005C REVERSE SOLIDUS (\), return false.
if maybe_first.or_else(|| self.cur()) != Some('\\') {
return false;
}
match maybe_second.or_else(|| self.next()) {
// Otherwise, if the second code point is a newline, return false.
Some(second) => !is_newline(second),
// Otherwise, return true.
None => false,
}
}
// Check if three code points would start an identifier
// This section describes how to check if three code points would start an
// identifier. The algorithm described here can be called explicitly with three
// code points, or can be called with the input stream itself. In the latter
// case, the three code points in question are the current input code point and
// the next two input code points, in that order.
fn would_start_ident(
&mut self,
maybe_first: Option<char>,
maybe_second: Option<char>,
maybe_third: Option<char>,
) -> bool {
// Look at the first code point:
let first = maybe_first.or_else(|| self.cur());
match first {
// U+002D HYPHEN-MINUS
Some('-') => {
let second = maybe_second.or_else(|| self.next());
match second {
// If the second code point is a name-start code point
// return true.
Some(c) if is_name_start(c) => true,
// or a U+002D HYPHEN-MINUS,
// return true.
Some('-') => true,
// or the second and third code points are a valid escape
// return true.
Some(_) => {
let third = maybe_third.or_else(|| self.next_next());
self.is_valid_escape(second, third)
}
// Otherwise, return false.
_ => false,
}
}
// name-start code point
// Return true.
Some(c) if is_name_start(c) => true,
// U+005C REVERSE SOLIDUS (\)
// If the first and second code points are a valid escape, return true. Otherwise,
// return false.
Some('\\') => {
let second = maybe_second.or_else(|| self.next());
self.is_valid_escape(first, second)
}
_ => false,
}
}
// Check if three code points would start a number
// This section describes how to check if three code points would start a
// number. The algorithm described here can be called explicitly with three code
// points, or can be called with the input stream itself. In the latter case,
// the three code points in question are the current input code point and the
// next two input code points, in that order.
#[allow(clippy::needless_return)]
fn would_start_number(
&mut self,
maybe_first: Option<char>,
maybe_second: Option<char>,
maybe_third: Option<char>,
) -> bool {
// Look at the first code point:
let first = maybe_first.or_else(|| self.cur());
match first {
// U+002B PLUS SIGN (+)
// U+002D HYPHEN-MINUS (-)
Some('+') | Some('-') => {
match maybe_second.or_else(|| self.next()) {
// If the second code point is a digit, return true.
Some(second) if second.is_ascii_digit() => return true,
// Otherwise, if the second code point is a U+002E FULL STOP (.) and the
// third code point is a digit, return true.
Some('.') => {
if let Some(third) = maybe_third.or_else(|| self.next_next()) {
if third.is_ascii_digit() {
return true;
}
}
return false;
}
// Otherwise, return false.
_ => return false,
};
}
// U+002E FULL STOP (.)
Some('.') => {
// If the second code point is a digit, return true.
if let Some(second) = self.next() {
if second.is_ascii_digit() {
return true;
}
}
// Otherwise, return false.
false
}
// digit
// Return true.
Some(first) if first.is_ascii_digit() => true,
// anything else
// Return false.
_ => false,
}
}
// Consume an ident sequence
// This section describes how to consume an ident sequence from a stream of code
// points. It returns a string containing the largest name that can be formed
// from adjacent code points in the stream, starting from the first.
fn read_ident_sequence(&mut self) -> LexResult<(JsWord, Atom)> {
self.with_buf_and_raw_buf(|l, buf, raw| {
// Let result initially be an empty string.
// Done above
// Repeatedly consume the next input code point from the stream:
loop {
match l.consume() {
// name code point
// Append the code point to result.
Some(c) if is_name(c) => {
buf.push(c);
raw.push(c);
}
// the stream starts with a valid escape
// Consume an escaped code point. Append the returned code point to result.
Some(c) if l.is_valid_escape(None, None) => {
let escaped = l.read_escape()?;
buf.push(escaped.0);
raw.push(c);
raw.push_str(&escaped.1);
}
// anything else
// Reconsume the current input code point. Return result.
_ => {
l.reconsume();
break;
}
}
}
Ok((l.atoms.atom(&**buf), l.atoms.atom(&**raw)))
})
}
// This section describes how to consume a number from a stream of code points.
// It returns a numeric value, and a type which is either "integer" or "number".
fn read_number(&mut self) -> LexResult<(f64, Atom, NumberType)> {
let parsed: (Atom, NumberType) = self.with_buf(|l, out| {
// Initially set type to "integer". Let repr be the empty string.
let mut type_flag = NumberType::Integer;
// If the next input code point is U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS
// (-), consume it and append it to repr.
let next = l.next();
if next == Some('+') || next == Some('-') {
l.consume();
out.push(next.unwrap());
}
// While the next input code point is a digit, consume it and append it to repr.
while let Some(c) = l.next() {
if c.is_ascii_digit() {
l.consume();
out.push(c);
} else {
break;
}
}
// If the next 2 input code points are U+002E FULL STOP (.) followed by a digit,
// then:
let next = l.next();
if next == Some('.') {
if let Some(n) = l.next_next() {
if n.is_ascii_digit() {
// Consume them.
l.consume();
l.consume();
// Append them to repr.
out.push(next.unwrap());
out.push(n);
// Set type to "number".
type_flag = NumberType::Number;
// While the next input code point is a digit, consume it and append it to
// repr.
while let Some(c) = l.next() {
if c.is_ascii_digit() {
l.consume();
out.push(c);
} else {
break;
}
}
}
}
}
// If the next 2 or 3 input code points are U+0045 LATIN CAPITAL LETTER E (E) or
// U+0065 LATIN SMALL LETTER E (e), optionally followed by U+002D HYPHEN-MINUS
// (-) or U+002B PLUS SIGN (+), followed by a digit, then:
let next = l.next();
if next == Some('E') || next == Some('e') {
let next_next = l.next_next();
let next_next_next = l.next_next_next();
if (next_next == Some('-')
|| next_next == Some('+')
&& next_next_next.is_some()
&& next_next_next.unwrap().is_ascii_digit())
|| next_next.is_some() && next_next.unwrap().is_ascii_digit()
{
// Consume them.
l.consume();
l.consume();
// Append them to repr.
out.push(next.unwrap());
out.push(next_next.unwrap());
// Set type to "number".
type_flag = NumberType::Number;
// While the next input code point is a digit, consume it and append it
// to repr.
while let Some(c) = l.next() {
if c.is_ascii_digit() {
l.consume();
out.push(c);
} else {
break;
}
}
}
}
// Return value and type.
Ok((l.atoms.atom(&**out), type_flag))
})?;
// Convert repr to a number, and set the value to the returned value.
let value = lexical::parse(&*parsed.0).unwrap_or_else(|err| {
unreachable!("failed to parse `{}` using lexical: {:?}", parsed.0, err)
});
Ok((value, parsed.0, parsed.1))
}
// Consume the remnants of a bad url
// This section describes how to consume the remnants of a bad url from a stream
// of code points, "cleaning up" after the tokenizer realizes that it’s in the
// middle of a <bad-url-token> rather than a <url-token>. It returns nothing;
// its sole use is to consume enough of the input stream to reach a recovery
// point where normal tokenizing can resume. But for recovery purpose we return
// bad URL remnants.
fn read_bad_url_remnants(&mut self) -> LexResult<String> {
self.with_sub_buf(|l, raw| {
// Repeatedly consume the next input code point from the stream:
loop {
match l.consume() {
// U+0029 RIGHT PARENTHESIS ())
// EOF
// Return.
Some(c @ ')') => {
raw.push(c);
break;
}
None => {
break;
}
// the input stream starts with a valid escape
Some(c) if l.is_valid_escape(None, None) => {
// Consume an escaped code point. This allows an escaped right parenthesis
// ("\)") to be encountered without ending the <bad-url-token>.
let escaped = l.read_escape()?;
raw.push(c);
raw.push_str(&escaped.1);
}
// anything else
// Do nothing.
Some(c) => {
raw.push(c);
}
}
}
Ok((&**raw).into())
})
}
}
#[inline(always)]
fn is_digit(c: char) -> bool {
c.is_ascii_digit()
}
#[inline(always)]
fn is_hex_digit(c: char) -> bool {
match c {
c if is_digit(c) => true,
'A'..='F' => true,
'a'..='f' => true,
_ => false,
}
}
#[inline(always)]
fn is_uppercase_letter(c: char) -> bool {
c.is_ascii_uppercase()
}
#[inline(always)]
fn is_lowercase_letter(c: char) -> bool {
c.is_ascii_lowercase()
}
#[inline(always)]
fn is_letter(c: char) -> bool {
is_uppercase_letter(c) || is_lowercase_letter(c)
}
#[inline(always)]
fn is_non_ascii(c: char) -> bool {
c as u32 >= 0x80
}
#[inline(always)]
fn is_name_start(c: char) -> bool {
matches!(c, c if is_letter(c) || is_non_ascii(c) || c == '_' || c == '\x00')
}
#[inline(always)]
fn is_name(c: char) -> bool {
is_name_start(c) || matches!(c, c if c.is_ascii_digit() || c == '-')
}
#[inline(always)]
fn is_non_printable(c: char) -> bool {
matches!(c, '\x00'..='\x08' | '\x0B' | '\x0E'..='\x1F' | '\x7F')
}
#[inline(always)]
fn is_newline(c: char) -> bool {
matches!(c, '\n' | '\r' | '\x0C')
}
#[inline(always)]
fn is_whitespace(c: char) -> bool {
matches!(c, c if c == ' ' || c == '\t' || is_newline(c))
}