Skip to content

Commit b827347

Browse files
committed
update js
1 parent 66fc854 commit b827347

File tree

2 files changed

+72
-29
lines changed

2 files changed

+72
-29
lines changed

js/vimlfunc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,20 @@ function viml_stridx(a, b) {
191191
return a.indexOf(b);
192192
}
193193

194+
function viml_type(obj) {
195+
if (typeof obj == 'number' && Math.round(obj) == obj) {
196+
return 0;
197+
} else if (typeof obj == 'string') {
198+
return 1;
199+
} else if (typeof obj == 'function') {
200+
return 2;
201+
} else if (obj instanceof Array) {
202+
return 3;
203+
} else if (obj instanceof Object) {
204+
return 4;
205+
} else if (typeof obj == 'number') {
206+
return 5;
207+
}
208+
throw 'Unknown Type';
209+
}
210+

js/vimlparser.js

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ function viml_stridx(a, b) {
191191
return a.indexOf(b);
192192
}
193193

194+
function viml_type(obj) {
195+
if (typeof obj == 'number' && Math.round(obj) == obj) {
196+
return 0;
197+
} else if (typeof obj == 'string') {
198+
return 1;
199+
} else if (typeof obj == 'function') {
200+
return 2;
201+
} else if (obj instanceof Array) {
202+
return 3;
203+
} else if (obj instanceof Object) {
204+
return 4;
205+
} else if (typeof obj == 'number') {
206+
return 5;
207+
}
208+
throw 'Unknown Type';
209+
}
210+
194211
var NIL = [];
195212
var NODE_TOPLEVEL = 1;
196213
var NODE_COMMENT = 2;
@@ -380,10 +397,6 @@ function isnamec1(c) {
380397
return viml_eqregh(c, "^[A-Za-z_]$");
381398
}
382399

383-
function isattrc(c) {
384-
return viml_eqregh(c, "^[0-9A-Za-z_]$");
385-
}
386-
387400
function isargname(s) {
388401
return viml_eqregh(s, "^[A-Za-z_][0-9A-Za-z_]*$");
389402
}
@@ -2862,14 +2875,12 @@ ExprParser.prototype.parse_expr8 = function() {
28622875
}
28632876
var left = node;
28642877
}
2865-
else if (!iswhite(c) && token.type == TOKEN_DOT && isattrc(this.reader.p(0)) && (left.type == NODE_IDENTIFIER || left.type == NODE_CURLYNAME || left.type == NODE_DICT || left.type == NODE_SUBSCRIPT || left.type == NODE_CALL || left.type == NODE_DOT)) {
2866-
// SUBSCRIPT or CONCAT
2867-
var node = Node(NODE_DOT);
2868-
node.pos = token.pos;
2869-
node.left = left;
2870-
node.right = Node(NODE_IDENTIFIER);
2871-
node.right.pos = this.reader.getpos();
2872-
node.right.value = this.reader.read_attr();
2878+
else if (!iswhite(c) && token.type == TOKEN_DOT) {
2879+
var node = this.parse_dot(token, left);
2880+
if (node === NIL) {
2881+
this.reader.seek_set(pos);
2882+
break;
2883+
}
28732884
var left = node;
28742885
}
28752886
else {
@@ -3023,6 +3034,31 @@ ExprParser.prototype.parse_expr9 = function() {
30233034
return node;
30243035
}
30253036

3037+
// SUBSCRIPT or CONCAT
3038+
// dict "." [0-9A-Za-z_]+ => (subscript dict key)
3039+
// str "." expr6 => (concat str expr6)
3040+
ExprParser.prototype.parse_dot = function(token, left) {
3041+
if (left.type != NODE_IDENTIFIER && left.type != NODE_CURLYNAME && left.type != NODE_DICT && left.type != NODE_SUBSCRIPT && left.type != NODE_CALL && left.type != NODE_DOT) {
3042+
return NIL;
3043+
}
3044+
if (!iswordc(this.reader.p(0))) {
3045+
return NIL;
3046+
}
3047+
var pos = this.reader.getpos();
3048+
var name = this.reader.read_word();
3049+
if (isnamec(this.reader.p(0))) {
3050+
// foo.s:bar or foo.bar#baz
3051+
return NIL;
3052+
}
3053+
var node = Node(NODE_DOT);
3054+
node.pos = token.pos;
3055+
node.left = left;
3056+
node.right = Node(NODE_IDENTIFIER);
3057+
node.right.pos = pos;
3058+
node.right.value = name;
3059+
return node;
3060+
}
3061+
30263062
ExprParser.prototype.parse_identifier = function() {
30273063
var id = [];
30283064
this.reader.skip_white();
@@ -3128,14 +3164,12 @@ LvalueParser.prototype.parse_lv8 = function() {
31283164
}
31293165
var left = node;
31303166
}
3131-
else if (!iswhite(c) && token.type == TOKEN_DOT && isattrc(this.reader.p(0)) && (left.type == NODE_IDENTIFIER || left.type == NODE_CURLYNAME || left.type == NODE_DICT || left.type == NODE_SUBSCRIPT || left.type == NODE_CALL || left.type == NODE_DOT)) {
3132-
// SUBSCRIPT or CONCAT
3133-
var node = Node(NODE_DOT);
3134-
node.pos = token.pos;
3135-
node.left = left;
3136-
node.right = Node(NODE_IDENTIFIER);
3137-
node.right.pos = this.reader.getpos();
3138-
node.right.value = this.reader.read_attr();
3167+
else if (!iswhite(c) && token.type == TOKEN_DOT) {
3168+
var node = this.parse_dot(token, left);
3169+
if (node === NIL) {
3170+
this.reader.seek_set(pos);
3171+
break;
3172+
}
31393173
var left = node;
31403174
}
31413175
else {
@@ -3190,7 +3224,7 @@ LvalueParser.prototype.parse_lv9 = function() {
31903224

31913225
function StringReader() { this.__init__.apply(this, arguments); }
31923226
StringReader.prototype.__init__ = function(lines) {
3193-
this.lines = lines;
3227+
var lines = viml_type(lines) == 3 ? lines : [lines];
31943228
this.buf = [];
31953229
this.pos = [];
31963230
var lnum = 0;
@@ -3406,14 +3440,6 @@ StringReader.prototype.read_name = function() {
34063440
return r;
34073441
}
34083442

3409-
StringReader.prototype.read_attr = function() {
3410-
var r = "";
3411-
while (isattrc(this.peekn(1))) {
3412-
r += this.getn(1);
3413-
}
3414-
return r;
3415-
}
3416-
34173443
StringReader.prototype.skip_white = function() {
34183444
while (iswhite(this.peekn(1))) {
34193445
this.seek_cur(1);

0 commit comments

Comments
 (0)