pagesift_syntax/scanner/
ts.rs1use super::js::{
5 AUTOMATIC_SEMICOLON, ESCAPE_SEQUENCE, HTML_COMMENT, JSX_TEXT, LOGICAL_OR, REGEX_PATTERN,
6 TEMPLATE_CHARS, TERNARY_QMARK, html_comment, in_or_instanceof, jsx_text, template_chars,
7};
8use super::{ScanLexer, Scanner, is_space};
9
10const FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON: usize = 8;
11
12pub struct Ts;
13
14fn ws_and_comments(lx: &mut ScanLexer, scanned_comment: &mut bool) -> bool {
15 loop {
16 while is_space(lx.lookahead()) {
17 lx.skip();
18 }
19 if lx.lookahead() != '/' {
20 return true;
21 }
22 lx.skip();
23 if lx.lookahead() == '/' {
24 lx.skip();
25 while lx.lookahead() != '\0' && lx.lookahead() != '\n' {
26 lx.skip();
27 }
28 *scanned_comment = true;
29 } else if lx.lookahead() == '*' {
30 lx.skip();
31 while lx.lookahead() != '\0' {
32 if lx.lookahead() == '*' {
33 lx.skip();
34 if lx.lookahead() == '/' {
35 lx.skip();
36 break;
37 }
38 } else {
39 lx.skip();
40 }
41 }
42 } else {
43 return false;
44 }
45 }
46}
47
48fn automatic_semicolon(lx: &mut ScanLexer, valid: &[bool], scanned_comment: &mut bool) -> bool {
49 lx.set_result(AUTOMATIC_SEMICOLON as u16);
50 lx.mark_end();
51 loop {
52 if lx.lookahead() == '\0' {
53 return true;
54 }
55 if lx.lookahead() == '}' {
56 loop {
58 lx.skip();
59 if !is_space(lx.lookahead()) {
60 break;
61 }
62 }
63 if lx.lookahead() == ':' {
64 return valid[LOGICAL_OR];
65 }
66 return true;
67 }
68 if !is_space(lx.lookahead()) {
69 return false;
70 }
71 if lx.lookahead() == '\n' {
72 break;
73 }
74 lx.skip();
75 }
76 lx.skip();
77 if !ws_and_comments(lx, scanned_comment) {
78 return false;
79 }
80 match lx.lookahead() {
81 '`' | ',' | '.' | ';' | '*' | '%' | '>' | '<' | '=' | '?' | '^' | '|' | '&' | '/' | ':' => {
82 return false;
83 }
84 '{' => {
85 if valid[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON] {
86 return false;
87 }
88 }
89 '(' | '[' => {
90 if valid[LOGICAL_OR] {
91 return false;
92 }
93 }
94 '+' => {
95 lx.skip();
96 return lx.lookahead() == '+';
97 }
98 '-' => {
99 lx.skip();
100 return lx.lookahead() == '-';
101 }
102 '!' => {
103 lx.skip();
104 return lx.lookahead() != '=';
105 }
106 'i' => return in_or_instanceof(lx),
107 _ => {}
108 }
109 true
110}
111
112fn ternary_qmark(lx: &mut ScanLexer) -> bool {
113 while is_space(lx.lookahead()) {
114 lx.skip();
115 }
116 if lx.lookahead() != '?' {
117 return false;
118 }
119 lx.next();
120 if lx.lookahead() == '?' || lx.lookahead() == '.' {
121 return false;
122 }
123 lx.mark_end();
124 lx.set_result(TERNARY_QMARK as u16);
125 while is_space(lx.lookahead()) {
127 lx.next();
128 }
129 if matches!(lx.lookahead(), ':' | ')' | ',') {
130 return false;
131 }
132 if lx.lookahead() == '.' {
133 lx.next();
134 return lx.lookahead().is_ascii_digit();
135 }
136 true
137}
138
139impl Scanner for Ts {
140 fn serialize(&self, _out: &mut Vec<u8>) {}
141 fn deserialize(&mut self, _d: &[u8]) {}
142
143 fn scan(&mut self, lx: &mut ScanLexer, valid: &[bool]) -> bool {
144 if valid[TEMPLATE_CHARS] {
145 if valid[AUTOMATIC_SEMICOLON] {
146 return false;
147 }
148 return template_chars(lx);
149 }
150 if valid[JSX_TEXT] && jsx_text(lx) {
151 return true;
152 }
153 if valid[AUTOMATIC_SEMICOLON] || valid[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON] {
154 let mut scanned_comment = false;
155 let ret = automatic_semicolon(lx, valid, &mut scanned_comment);
156 if !ret && !scanned_comment && valid[TERNARY_QMARK] && lx.lookahead() == '?' {
157 return ternary_qmark(lx);
158 }
159 return ret;
160 }
161 if valid[TERNARY_QMARK] {
162 return ternary_qmark(lx);
163 }
164 if valid[HTML_COMMENT]
165 && !valid[LOGICAL_OR]
166 && !valid[ESCAPE_SEQUENCE]
167 && !valid[REGEX_PATTERN]
168 {
169 return html_comment(lx);
170 }
171 false
172 }
173}