aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/selector/parser.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/core/selector/parser.js')
-rw-r--r--node_modules/montage/core/selector/parser.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/node_modules/montage/core/selector/parser.js b/node_modules/montage/core/selector/parser.js
new file mode 100644
index 00000000..b674218c
--- /dev/null
+++ b/node_modules/montage/core/selector/parser.js
@@ -0,0 +1,62 @@
1
2var Montage = require("montage").Montage;
3
4var Parser = exports.Parser = Montage.create(Montage, {
5
6 newWithLanguage: {
7 value: function (language, callback) {
8 var self = Montage.create(this);
9 self.tokens = [];
10 self.state = language.parsePrevious(function (syntax) {
11 callback && callback(syntax);
12 return language.parseEof();
13 });
14 return self;
15 }
16 },
17
18 state: {
19 value: null,
20 writable: true
21 },
22
23 emit: {
24 value: function (token) {
25 try {
26 this.tokens.push(token);
27 this.state = this.state(token);
28 return this;
29 } catch (exception) {
30 if (exception instanceof SyntaxError) {
31 throw new SyntaxError(exception.message + ' at ' + this.format());
32 } else {
33 throw exception;
34 }
35 }
36 }
37 },
38
39 state: {
40 value: null,
41 writable: true
42 },
43
44 syntax: {
45 value: null,
46 writable: true,
47 serializable: true
48 },
49
50 format: {
51 value: function () {
52 return this.tokens.reduce(function (hither, token) {
53 if (token.type === 'literal') {
54 return hither + '(' + JSON.stringify(token.value) + ')';
55 } else {
56 return hither + '.' + token.type;
57 }
58 }, 'Selector');
59 }
60 }
61
62});