grass_compiler 0.13.4

Internal implementation of the grass compiler
Documentation
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
use std::path::Path;

use codemap::Span;

use crate::{ast::*, error::SassResult, lexer::Lexer, ContextFlags, Options, Token};

use super::{BaseParser, StylesheetParser};

pub(crate) struct SassParser<'a> {
    pub toks: Lexer,
    pub path: &'a Path,
    pub empty_span: Span,
    pub flags: ContextFlags,
    pub options: &'a Options<'a>,
    pub current_indentation: usize,
    pub next_indentation: Option<usize>,
    pub spaces: Option<bool>,
    pub next_indentation_end: Option<usize>,
}

impl<'a> BaseParser for SassParser<'a> {
    fn toks(&self) -> &Lexer {
        &self.toks
    }

    fn toks_mut(&mut self) -> &mut Lexer {
        &mut self.toks
    }

    fn whitespace_without_comments(&mut self) {
        while let Some(next) = self.toks.peek() {
            if next.kind != '\t' && next.kind != ' ' {
                break;
            }

            self.toks.next();
        }
    }

    fn skip_loud_comment(&mut self) -> SassResult<()> {
        self.expect_char('/')?;
        self.expect_char('*')?;

        loop {
            let mut next = self.toks.next();
            match next {
                Some(Token { kind: '\n', .. }) => {
                    return Err(("expected */.", self.toks.prev_span()).into())
                }
                Some(Token { kind: '*', .. }) => {}
                _ => continue,
            }

            loop {
                next = self.toks.next();

                if !matches!(next, Some(Token { kind: '*', .. })) {
                    break;
                }
            }

            if matches!(next, Some(Token { kind: '/', .. })) {
                break;
            }
        }

        Ok(())
    }
}

impl<'a> StylesheetParser<'a> for SassParser<'a> {
    fn is_plain_css(&self) -> bool {
        false
    }

    fn is_indented(&self) -> bool {
        true
    }

    fn path(&self) -> &'a Path {
        self.path
    }

    fn options(&self) -> &Options {
        self.options
    }

    fn flags(&self) -> &ContextFlags {
        &self.flags
    }

    fn flags_mut(&mut self) -> &mut ContextFlags {
        &mut self.flags
    }

    fn current_indentation(&self) -> usize {
        self.current_indentation
    }

    fn empty_span(&self) -> Span {
        self.empty_span
    }

    fn parse_style_rule_selector(&mut self) -> SassResult<Interpolation> {
        let mut buffer = Interpolation::new();

        loop {
            buffer.add_interpolation(self.almost_any_value(true)?);
            buffer.add_char('\n');

            if !(buffer.trailing_string().trim_end().ends_with(',') && self.scan_char('\n')) {
                break;
            }
        }

        Ok(buffer)
    }

    fn expect_statement_separator(&mut self, _name: Option<&str>) -> SassResult<()> {
        if !self.at_end_of_statement() {
            self.expect_newline()?;
        }

        if self.peek_indentation()? <= self.current_indentation {
            return Ok(());
        }

        // todo: position: _nextIndentationEnd!.position
        // todo: error message, "Nothing may be indented ${name == null ? 'here' : 'beneath a $name'}."

        Err(("Nothing may be indented here", self.toks.current_span()).into())
    }

    fn at_end_of_statement(&self) -> bool {
        matches!(self.toks.peek(), Some(Token { kind: '\n', .. }) | None)
    }

    fn looking_at_children(&mut self) -> SassResult<bool> {
        Ok(self.at_end_of_statement() && self.peek_indentation()? > self.current_indentation)
    }

    fn scan_else(&mut self, if_indentation: usize) -> SassResult<bool> {
        if self.peek_indentation()? != if_indentation {
            return Ok(false);
        }

        let start = self.toks.cursor();
        let start_indentation = self.current_indentation;
        let start_next_indentation = self.next_indentation;
        let start_next_indentation_end = self.next_indentation_end;

        self.read_indentation()?;
        if self.scan_char('@') && self.scan_identifier("else", false)? {
            return Ok(true);
        }

        self.toks.set_cursor(start);
        self.current_indentation = start_indentation;
        self.next_indentation = start_next_indentation;
        self.next_indentation_end = start_next_indentation_end;
        Ok(false)
    }

    fn parse_children(
        &mut self,
        child: fn(&mut Self) -> SassResult<AstStmt>,
    ) -> SassResult<Vec<AstStmt>> {
        let mut children = Vec::new();
        self.while_indented_lower(|parser| {
            if let Some(parsed_child) = parser.parse_child(|parser| Ok(Some(child(parser)?)))? {
                children.push(parsed_child);
            }

            Ok(())
        })?;

        Ok(children)
    }

    fn parse_statements(
        &mut self,
        statement: fn(&mut Self) -> SassResult<Option<AstStmt>>,
    ) -> SassResult<Vec<AstStmt>> {
        if self.toks.next_char_is(' ') || self.toks.next_char_is('\t') {
            return Err((
                "Indenting at the beginning of the document is illegal.",
                self.toks.current_span(),
            )
                .into());
        }

        let mut statements = Vec::new();

        while self.toks.peek().is_some() {
            if let Some(child) = self.parse_child(statement)? {
                statements.push(child);
            }

            let indentation = self.read_indentation()?;
            assert_eq!(indentation, 0);
        }

        Ok(statements)
    }

    fn parse_silent_comment(&mut self) -> SassResult<AstStmt> {
        let start = self.toks.cursor();
        self.expect_char('/')?;
        self.expect_char('/')?;

        let mut buffer = String::new();

        let parent_indentation = self.current_indentation;

        'outer: loop {
            let comment_prefix = if self.scan_char('/') { "///" } else { "//" };

            loop {
                buffer.push_str(comment_prefix);
                //     buffer.write(commentPrefix);

                // Skip the initial characters because we're already writing the
                // slashes.
                for _ in comment_prefix.len()..(self.current_indentation - parent_indentation) {
                    buffer.push(' ');
                }

                while self.toks.peek().is_some() && !self.toks.next_char_is('\n') {
                    buffer.push(self.toks.next().unwrap().kind);
                }

                buffer.push('\n');

                if self.peek_indentation()? < parent_indentation {
                    break 'outer;
                }

                if self.peek_indentation()? == parent_indentation {
                    // Look ahead to the next line to see if it starts another comment.
                    if matches!(
                        self.toks.peek_n(1 + parent_indentation),
                        Some(Token { kind: '/', .. })
                    ) && matches!(
                        self.toks.peek_n(2 + parent_indentation),
                        Some(Token { kind: '/', .. })
                    ) {
                        self.read_indentation()?;
                    }
                    break;
                }

                self.read_indentation()?;
            }

            if !self.scan("//") {
                break;
            }
        }

        Ok(AstStmt::SilentComment(AstSilentComment {
            text: buffer,
            span: self.toks.span_from(start),
        }))
    }

    fn parse_loud_comment(&mut self) -> SassResult<AstLoudComment> {
        let start = self.toks.cursor();
        self.expect_char('/')?;
        self.expect_char('*')?;

        let mut first = true;

        let mut buffer = Interpolation::new_plain("/*".to_owned());
        let parent_indentation = self.current_indentation;

        loop {
            if first {
                let beginning_of_comment = self.toks.cursor();

                self.spaces();

                if self.toks.next_char_is('\n') {
                    self.read_indentation()?;
                    buffer.add_char(' ');
                } else {
                    buffer.add_string(self.toks.raw_text(beginning_of_comment));
                }
            } else {
                buffer.add_string("\n * ".to_owned());
            }

            first = false;

            for _ in 3..(self.current_indentation - parent_indentation) {
                buffer.add_char(' ');
            }

            while self.toks.peek().is_some() {
                match self.toks.peek() {
                    Some(Token {
                        kind: '\n' | '\r', ..
                    }) => break,
                    Some(Token { kind: '#', .. }) => {
                        if matches!(self.toks.peek_n(1), Some(Token { kind: '{', .. })) {
                            buffer.add_interpolation(self.parse_single_interpolation()?);
                        } else {
                            buffer.add_char('#');
                            self.toks.next();
                        }
                    }
                    Some(Token { kind, .. }) => {
                        buffer.add_char(kind);
                        self.toks.next();
                    }
                    None => todo!(),
                }
            }

            if self.peek_indentation()? <= parent_indentation {
                break;
            }

            // Preserve empty lines.
            while self.looking_at_double_newline() {
                self.expect_newline()?;
                buffer.add_char('\n');
                buffer.add_char(' ');
                buffer.add_char('*');
            }

            self.read_indentation()?;
        }

        if !buffer.trailing_string().trim_end().ends_with("*/") {
            buffer.add_string(" */".to_owned());
        }

        Ok(AstLoudComment {
            text: buffer,
            span: self.toks.span_from(start),
        })
    }
}

impl<'a> SassParser<'a> {
    pub fn new(
        toks: Lexer,
        options: &'a Options<'a>,
        empty_span: Span,
        file_name: &'a Path,
    ) -> Self {
        let mut flags = ContextFlags::empty();

        flags.set(ContextFlags::IS_USE_ALLOWED, true);

        SassParser {
            toks,
            path: file_name,
            empty_span,
            flags,
            options,
            current_indentation: 0,
            next_indentation: None,
            next_indentation_end: None,
            spaces: None,
        }
    }

    fn peek_indentation(&mut self) -> SassResult<usize> {
        if let Some(next) = self.next_indentation {
            return Ok(next);
        }

        if self.toks.peek().is_none() {
            self.next_indentation = Some(0);
            self.next_indentation_end = Some(self.toks.cursor());
            return Ok(0);
        }

        let start = self.toks.cursor();

        if !self.scan_char('\n') {
            return Err(("Expected newline.", self.toks.current_span()).into());
        }

        let mut contains_tab;
        let mut contains_space;
        let mut next_indentation;

        loop {
            contains_tab = false;
            contains_space = false;
            next_indentation = 0;

            while let Some(next) = self.toks.peek() {
                match next.kind {
                    ' ' => contains_space = true,
                    '\t' => contains_tab = true,
                    _ => break,
                }

                next_indentation += 1;
                self.toks.next();
            }

            if self.toks.peek().is_none() {
                self.next_indentation = Some(0);
                self.next_indentation_end = Some(self.toks.cursor());
                self.toks.set_cursor(start);
                return Ok(0);
            }

            if !self.scan_char('\n') {
                break;
            }
        }

        self.check_indentation_consistency(contains_tab, contains_space, start)?;

        self.next_indentation = Some(next_indentation);

        if next_indentation > 0 {
            self.spaces.get_or_insert(contains_space);
        }

        self.next_indentation_end = Some(self.toks.cursor());
        self.toks.set_cursor(start);

        Ok(next_indentation)
    }

    fn check_indentation_consistency(
        &mut self,
        contains_tab: bool,
        contains_space: bool,
        start: usize,
    ) -> SassResult<()> {
        // NOTE: error message spans here start from the beginning of the line
        if contains_tab {
            if contains_space {
                return Err((
                    "Tabs and spaces may not be mixed.",
                    self.toks.span_from(start),
                )
                    .into());
            } else if self.spaces == Some(true) {
                return Err(("Expected spaces, was tabs.", self.toks.span_from(start)).into());
            }
        } else if contains_space && self.spaces == Some(false) {
            return Err(("Expected tabs, was spaces.", self.toks.span_from(start)).into());
        }

        Ok(())
    }

    fn expect_newline(&mut self) -> SassResult<()> {
        match self.toks.peek() {
            Some(Token { kind: ';', .. }) => Err((
                "semicolons aren't allowed in the indented syntax.",
                self.toks.current_span(),
            )
                .into()),
            Some(Token { kind: '\r', .. }) => {
                self.toks.next();
                self.scan_char('\n');
                Ok(())
            }
            Some(Token { kind: '\n', .. }) => {
                self.toks.next();
                Ok(())
            }
            _ => Err(("expected newline.", self.toks.current_span()).into()),
        }
    }

    fn read_indentation(&mut self) -> SassResult<usize> {
        self.current_indentation = match self.next_indentation {
            Some(indent) => indent,
            None => {
                let indent = self.peek_indentation()?;
                self.next_indentation = Some(indent);
                indent
            }
        };

        self.toks.set_cursor(self.next_indentation_end.unwrap());
        self.next_indentation = None;
        self.next_indentation_end = None;

        Ok(self.current_indentation)
    }

    fn while_indented_lower(
        &mut self,
        mut body: impl FnMut(&mut Self) -> SassResult<()>,
    ) -> SassResult<()> {
        let parent_indentation = self.current_indentation;
        let mut child_indentation = None;

        while self.peek_indentation()? > parent_indentation {
            let indentation = self.read_indentation()?;
            let child_indent = *child_indentation.get_or_insert(indentation);

            if child_indent != indentation {
                return Err((
                    format!(
                        "Inconsistent indentation, expected {child_indent} spaces.",
                        child_indent = child_indent
                    ),
                    self.toks.current_span(),
                )
                    .into());
            }

            body(self)?;
        }

        Ok(())
    }

    fn parse_child(
        &mut self,
        child: impl FnOnce(&mut Self) -> SassResult<Option<AstStmt>>,
    ) -> SassResult<Option<AstStmt>> {
        Ok(Some(match self.toks.peek() {
            Some(Token {
                kind: '\n' | '\r', ..
            }) => return Ok(None),
            Some(Token { kind: '$', .. }) => AstStmt::VariableDecl(
                self.parse_variable_declaration_without_namespace(None, None)?,
            ),
            Some(Token { kind: '/', .. }) => match self.toks.peek_n(1) {
                Some(Token { kind: '/', .. }) => self.parse_silent_comment()?,
                Some(Token { kind: '*', .. }) => AstStmt::LoudComment(self.parse_loud_comment()?),
                _ => return child(self),
            },
            _ => return child(self),
        }))
    }

    fn looking_at_double_newline(&mut self) -> bool {
        match self.toks.peek() {
            // todo: is this branch reachable
            Some(Token { kind: '\r', .. }) => match self.toks.peek_n(1) {
                Some(Token { kind: '\n', .. }) => {
                    matches!(self.toks.peek_n(2), Some(Token { kind: '\n', .. }))
                }
                Some(Token { kind: '\r', .. }) => true,
                _ => false,
            },
            Some(Token { kind: '\n', .. }) => matches!(
                self.toks.peek_n(1),
                Some(Token {
                    kind: '\n' | '\r',
                    ..
                })
            ),
            _ => false,
        }
    }
}