aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/selector/parser.js
blob: b674218c866c43d0e390d8b4328ef273c168b494 (plain)
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

var Montage = require("montage").Montage;

var Parser = exports.Parser = Montage.create(Montage, {

    newWithLanguage: {
        value: function (language, callback) {
            var self = Montage.create(this);
            self.tokens = [];
            self.state = language.parsePrevious(function (syntax) {
                callback && callback(syntax);
                return language.parseEof();
            });
            return self;
        }
    },

    state: {
        value: null,
        writable: true
    },

    emit: {
        value: function (token) {
            try {
                this.tokens.push(token);
                this.state = this.state(token);
                return this;
            } catch (exception) {
                if (exception instanceof SyntaxError) {
                    throw new SyntaxError(exception.message + ' at ' + this.format());
                } else {
                    throw exception;
                }
            }
        }
    },

    state: {
        value: null,
        writable: true
    },

    syntax: {
        value: null,
        writable: true,
        serializable: true
    },

    format: {
        value: function () {
            return this.tokens.reduce(function (hither, token) {
                if (token.type === 'literal') {
                    return hither + '(' + JSON.stringify(token.value) + ')';
                } else {
                    return hither + '.' + token.type;
                }
            }, 'Selector');
        }
    }

});