File: ParserBase.java

package info (click to toggle)
liblessen-java 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 276 kB
  • sloc: java: 2,981; xml: 31; makefile: 5
file content (449 lines) | stat: -rw-r--r-- 16,617 bytes parent folder | download
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
package com.metaweb.lessen;

import java.util.LinkedList;
import java.util.List;

import com.metaweb.lessen.expr.Evaluable;
import com.metaweb.lessen.expr.FixedTokenEvaluable;
import com.metaweb.lessen.expr.LessMixinFunction;
import com.metaweb.lessen.expr.OperatorCall;
import com.metaweb.lessen.expr.VariableAccessEvaluable;
import com.metaweb.lessen.tokenizers.BufferedTokenizer;
import com.metaweb.lessen.tokenizers.Tokenizer;
import com.metaweb.lessen.tokens.Token;
import com.metaweb.lessen.tokens.UriToken;
import com.metaweb.lessen.tokens.Token.Type;

abstract public class ParserBase {
    final protected BufferedTokenizer _tokenizer;
    final protected ResourceFinder _resourceFinder;
    final protected Scope _scope;
    final protected boolean _topLevel;

    protected ParserBase(BufferedTokenizer tokenizer, ResourceFinder resourceFinder, Scope scope, boolean topLevel) {
        _tokenizer = tokenizer;
        _resourceFinder = resourceFinder;
        _scope = scope;
        _topLevel = topLevel;
    }
    
    protected LessFunctionDefinitionParser createLessFunctionDefinitionParser() {
        return new LessFunctionDefinitionParser(_tokenizer, _resourceFinder, _scope);
    }

    protected Tokenizer createCssBlockParser(boolean nestChildren) {
        return new BlockParser(_tokenizer, _resourceFinder, new Scope(_scope), null, nestChildren);
    }

    abstract protected void outputToken(Token t);
    
    protected void outputEvaluable(Evaluable e) {
        Token t = e.eval(_scope);
        if (t != null) {
            outputToken(t);
        } else {
            outputToken(new Token(Type.Invalid, -1, -1, "ERROR"));
        }
    }
    
    abstract protected void passInnerBlockTokenThrough(Token t);
    
    protected void parseBody() {
        Token t;
        while ((t = _tokenizer.getToken()) != null) {
            if (t.type == Type.Delimiter && t.text.equals("}")) {
                break;
            }
            parseStatement();
        }
    }
    
    protected void parseStatement() {
        Token t;
        
        while ((t = _tokenizer.getToken()) != null && (
                t.type == Type.Whitespace || 
                t.type == Type.Comment ||
                t.type == Type.CDataOpen ||
                t.type == Type.CDataClose
            )) {
            outputToken(t);
            _tokenizer.next();
        }
        
        if (t != null) {
            if (t.type == Type.Delimiter && t.text.equals("}")) {
                return;
            }
            
            int lookahead = 1;
            
            Token t2;
            while ((t2 = _tokenizer.getToken(lookahead)) != null) {
                if (t2.type == Type.Delimiter) {
                    if (t2.text.equals(";") || t2.text.equals("}")) {
                        break;
                    } else if (t2.text.equals("{")) {
                        parseBlockStatement();
                        return;
                    }
                }
                
                lookahead++;
            }
            parseFlatStatement();
        }
    }
    
    protected void parseFlatStatement() {
        Token t = _tokenizer.getToken();
        if (t.type == Type.AtIdentifier) {
            int lookahead = lookOverWhitespaceAndComment(1);
            
            Token t2 = _tokenizer.getToken(lookahead);
            if (t2 != null) {
                if (t2.type == Type.Delimiter && t2.text.equals(":")) {
                    _tokenizer.next(lookahead + 1);
                    parseVariableDeclaration(t);
                    return;
                } else if (
                        _resourceFinder != null &&
                        t2.type == Type.Uri && 
                        (t.text.equals("@import") || t.text.equals("@import-less"))
                    ) {
                    
                    String where = ((UriToken) t2).unquotedText;
                    Tokenizer subTokenizer = _resourceFinder.open(where);
                    if (subTokenizer != null) {
                        subTokenizer = new LessParser(
                            subTokenizer, 
                            _resourceFinder.rebase(where), 
                            _scope
                        );
                        
                        if (t.text.equals("@import")) {
                            while (lookahead > 0) {
                                outputToken(_tokenizer.getToken());
                                _tokenizer.next();
                                lookahead--;
                            }
                            outputToken(t2);
                            _tokenizer.next();
                            
                            lookahead = lookOverWhitespaceAndComment(0);
                            t2 = _tokenizer.getToken(lookahead);
                            if (t2 != null && t2.type == Type.Delimiter && t2.text.equals(";")) {
                                while (lookahead > 0) {
                                    outputToken(_tokenizer.getToken());
                                    _tokenizer.next();
                                    lookahead--;
                                }
                                outputToken(t2);
                                _tokenizer.next();
                            }
                        } else if (t.text.equals("@import-less")) {
                            _tokenizer.next(lookahead + 1); // swallow tokens
                            
                            lookahead = lookOverWhitespaceAndComment(0);
                            t2 = _tokenizer.getToken(lookahead);
                            if (t2 != null && t2.type == Type.Delimiter && t2.text.equals(";")) {
                                _tokenizer.next(lookahead + 1); // swallow tokens
                            }
                        }
                        
                        while (subTokenizer.getToken() != null) {
                            subTokenizer.next();
                        }
                        
                        return;
                    }
                }
            }
        }
        
        if (t.type == Type.Operator && t.text.equals(".")) {
            Token t2 = _tokenizer.getToken(1);
            if (t2 != null) {
                if (t2.type == Type.Function) {
                    _tokenizer.next(2);
                    
                    parseRestOfMixinFunctionInvocation(t2);
                    return;
                } else if (t2.type == Type.Identifier) {
                    int lookahead = lookOverWhitespaceAndComment(2);
                    Token t3 = _tokenizer.getToken(lookahead);
                    if (t3 != null && t3.type == Type.Delimiter) {
                        if (t3.text.equals(";")) {
                            _tokenizer.next(lookahead + 1);
                            
                            invokeMixinFunction(t2, null);
                            return;
                        } else if (t3.text.equals("}")) {
                            _tokenizer.next(lookahead);
                            
                            invokeMixinFunction(t2, null);
                            return;
                        } else if (t3.text.equals("(")) {
                            _tokenizer.next(lookahead + 1);
                            
                            parseRestOfMixinFunctionInvocation(t2);
                            return;
                        }
                    }
                }
            }
        }
        
        while ((t = _tokenizer.getToken()) != null) {
            if (t.type == Type.Delimiter) {
                if (t.text.equals("}")) {
                    break;
                } else if (t.text.equals(";")) {
                    _tokenizer.next();
                    
                    outputToken(t);
                    break;
                }
            }
            
            Object o = parseExpression(true);
            if (o instanceof Token) {
                outputToken(t);
            } else {
                outputEvaluable((Evaluable) o);
            }
        }
    }
    
    protected void parseBlockStatement() {
        Token t = _tokenizer.getToken();
        if (t.type == Type.Operator && t.text.equals(".")) {
            Token t2 = _tokenizer.getToken(1);
            if (t2 != null) {
                if (t2.type == Type.Identifier) {
                    int lookahead = lookOverWhitespaceAndComment(2);
                    Token t3 = _tokenizer.getToken(lookahead);
                    if (t3 != null && t3.type == Type.Delimiter && t3.text.equals("(")) {
                        LessFunctionDefinitionParser tokenizer = 
                            createLessFunctionDefinitionParser();
                        
                        _scope.put(tokenizer.getIdentifier(), 
                                tokenizer.getFunction());
                        
                        return;
                    }
                } else if (t2.type == Type.Function) {
                    LessFunctionDefinitionParser tokenizer = 
                        createLessFunctionDefinitionParser();
                    
                    _scope.put(tokenizer.getIdentifier(),
                            tokenizer.getFunction());
                    
                    return;
                }
            } 
        }
        
        Tokenizer tokenizer = createCssBlockParser(t.type == Type.AtIdentifier);
        while ((t = tokenizer.getToken()) != null) {
            passInnerBlockTokenThrough(t);
            tokenizer.next();
        }
    }
    
    protected int lookOverWhitespaceAndComment(int lookahead) {
        return _tokenizer.lookOverWhitespaceAndComment(lookahead);
    }
    
    protected void parseVariableDeclaration(Token atIdentifier) {
        _tokenizer.next(lookOverWhitespaceAndComment(0));
        
        Object expr = parseExpression(true);
        
        int lookahead = lookOverWhitespaceAndComment(0);
        Token t = _tokenizer.getToken(lookahead);
        if (t != null && t.type == Type.Delimiter && t.text.equals(";")) {
            _tokenizer.next(lookahead + 1);
        }
        
        if (expr != null) {
            if (expr instanceof Evaluable) {
                expr = ((Evaluable) expr).eval(_scope);
            }
            _scope.put(atIdentifier.text, expr);
        }
    }
    
    protected Object parseExpression(boolean bareTokenOK) {
        Object term = parseTerm(bareTokenOK);
        if (term == null || LessParser.canBeIntermediateValue(term)) {
            return term;
        }
        
        Evaluable expr = (Evaluable) term;
        while (true) {
            int lookahead = lookOverWhitespaceAndComment(0);
            
            Token t2 = _tokenizer.getToken(lookahead);
            if (t2 != null && t2.type.equals(Type.Operator) &&
                (t2.text.equals("+") || t2.text.equals("-"))) {
                
                _tokenizer.next(lookahead + 1);
                _tokenizer.next(lookOverWhitespaceAndComment(0));
                
                Object term2 = parseTerm(false);
                if (term2 != null) {
                    expr = new OperatorCall(t2.text, expr, (Evaluable) term2);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return expr;
    }
    
    protected Object parseTerm(boolean bareTokenOK) {
        Object factor = parseFactor(bareTokenOK);
        if (factor == null || !(factor instanceof Evaluable)) {
            return factor;
        }
        
        Evaluable term = (Evaluable) factor;
        while (true) {
            int lookahead = lookOverWhitespaceAndComment(0);
            
            Token t2 = _tokenizer.getToken(lookahead);
            if (t2 != null && t2.type.equals(Type.Operator) &&
                (t2.text.equals("*") || t2.text.equals("/"))) {
                
                _tokenizer.next(lookahead + 1);
                _tokenizer.next(lookOverWhitespaceAndComment(0));
                
                Object factor2 = parseFactor(false);
                if (factor2 != null) {
                    term = new OperatorCall(t2.text, term, (Evaluable) factor2);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return term;
    }
    
    protected Object parseFactor(boolean bareTokenOK) {
        Token t = _tokenizer.getToken();
        if (t == null) {
            return null;
        } else {
            if (t.type == Type.Number || 
                t.type == Type.Percentage ||
                t.type == Type.Dimension ||
                t.type == Type.Color ||
                t.type == Type.HashName ||
                t.type == Type.Identifier) {
                
                _tokenizer.next();
                
                return new FixedTokenEvaluable(t);
                
            } else if (t.type == Type.AtIdentifier) {
                
                _tokenizer.next();
                
                return new VariableAccessEvaluable(t, t.text);
                
            } else if (t.type == Type.Delimiter) {
                if (t.text.equals("(")) {
                    _tokenizer.next();
                    
                    Object expr = null;
                    
                    int lookahead = lookOverWhitespaceAndComment(0);
                    Token t2 = _tokenizer.getToken(lookahead);
                    if (t2 != null) {
                        _tokenizer.next(lookahead); // eat whitespace
                        
                        expr = parseExpression(false);
                    }
                    
                    lookahead = lookOverWhitespaceAndComment(0);
                    t2 = _tokenizer.getToken(lookahead);
                    if (t2 != null && t2.type == Type.Delimiter && t2.text.equals(")")) {
                        _tokenizer.next(lookahead + 1);
                    }
                    
                    return expr;
                }
                
            }
        }
        
        if (bareTokenOK) {
            _tokenizer.next();
            return t;
        } else {
            return null;
        }
    }
    
    protected void parseRestOfMixinFunctionInvocation(Token functionNameToken) {
        List<Token> arguments = new LinkedList<Token>();
        
        while (true) {
            int lookahead = lookOverWhitespaceAndComment(0);
            
            Token t = _tokenizer.getToken(lookahead);
            if (t == null || (t.type == Type.Delimiter && t.text.equals(")"))) {
                break;
            }
            _tokenizer.next(lookahead); // eat the white space
            
            Object o = parseExpression(true);
            if (o == null) {
                break;
            } else if (o instanceof Token) {
                arguments.add((Token) o);
            } else {
                arguments.add(((Evaluable) o).eval(_scope));
            }
            
            lookahead = lookOverWhitespaceAndComment(0);
            t = _tokenizer.getToken(lookahead);
            if (t != null && t.type == Type.Delimiter && t.text.equals(",")) {
                _tokenizer.next(lookahead + 1);
            }
        }
        
        int lookahead = lookOverWhitespaceAndComment(0);
        Token t = _tokenizer.getToken(lookahead);
        if (t != null && t.type == Type.Delimiter && t.text.equals(")")) {
            _tokenizer.next(lookahead + 1);
        }
        
        lookahead = lookOverWhitespaceAndComment(0);
        t = _tokenizer.getToken(lookahead);
        if (t != null && t.type == Type.Delimiter && t.text.equals(";")) {
            _tokenizer.next(lookahead + 1);
        }
        
        invokeMixinFunction(functionNameToken, arguments);
    }
    
    protected void invokeMixinFunction(Token functionNameToken, List<Token> arguments) {
        String name = functionNameToken.type == Type.Function ?
                functionNameToken.text.substring(0, functionNameToken.text.length() - 1) :
                    functionNameToken.text;
                
        Object o = _scope.get(name);
        if (o != null && o instanceof LessMixinFunction) {
            List<Token> results = ((LessMixinFunction) o).invoke(arguments, _scope);
            for (Token t : results) {
                outputToken(t);
            }
        }
    }
}