-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.js
111 lines (102 loc) · 2.72 KB
/
iterator.js
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
'use strict';
var assert = require('assert');
function Iterator(cursor) {
this.cursor = cursor;
this.reserve = null;
this.reserveFlag = null;
this.plusFlags = false;
this.escaped = false;
}
Iterator.prototype.hasFlag = function hasFlag() {
if (this.reserveFlag !== null) {
return true;
}
// if (this.reserve !== null) {
// return false;
// }
if (this.cursor.done() || this.escaped) {
return false;
}
var arg = this.cursor.peek();
if (arg === '--') {
return false;
} else if (arg.lastIndexOf('--', 0) === 0) {
return true;
} else if (arg === '-') {
return false;
} else if (arg.lastIndexOf('-', 0) === 0) {
return true;
} else if (this.plusFlags && arg.lastIndexOf('+', 0) === 0) {
return true;
} else {
return false;
}
};
Iterator.prototype.shiftFlag = function shiftFlag() {
assert(this.hasFlag(), 'unable to shift flag');
var arg;
if (this.reserveFlag !== null) {
arg = this.reserveFlag + this.reserve;
this.reserveFlag = null;
this.reserve = null;
} else {
arg = this.cursor.shift();
}
// invariant: arg is a flag
// so not --, not -, not + if + not accepted
var index;
if (arg.lastIndexOf('--', 0) === 0) {
index = arg.indexOf('=', 2);
if (index >= 2) {
this.reserve = arg.slice(index + 1);
this.reserveFlag = null;
return arg.slice(0, index);
} else {
return arg;
}
}
// invariant: arg[0] is + or -
if (arg.length > 2) {
this.reserve = arg.slice(2);
this.reserveFlag = arg[0];
return arg.slice(0, 2);
} else {
return arg;
}
};
Iterator.prototype.shiftEscape = function shiftEscape() {
if (
this.reserve === null &&
!this.cursor.done() &&
!this.escaped &&
this.cursor.peek() === '--'
) {
this.escaped = true;
this.cursor.shift();
return true;
}
return false;
};
Iterator.prototype.hasArgument = function hasArgument() {
// invariant: shiftEscape has been called to clear out --
if (this.reserve !== null) {
return true;
}
return !this.cursor.done();
};
Iterator.prototype.shiftArgument = function shiftArgument() {
if (this.reserve !== null) {
var reserve = this.reserve;
this.reserveFlag = null;
this.reserve = null;
return reserve;
}
return this.cursor.shift();
};
Iterator.prototype.initialFlag = function initialFlag() {
if (!this.hasFlag() && this.hasArgument()) {
this.reserve = this.shiftArgument();
this.reserveFlag = '-';
}
};
module.exports = Iterator;