Skip to content

Commit e65a482

Browse files
committed
transpile to js and py
1 parent e2feb0a commit e65a482

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

js/vimlparser.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ var TOKEN_DOTDOTDOT = 63;
397397
var TOKEN_SHARP = 64;
398398
var TOKEN_ARROW = 65;
399399
var TOKEN_BLOB = 66;
400+
var TOKEN_LITCOPEN = 67;
400401
var MAX_FUNC_ARGS = 20;
401402
function isalpha(c) {
402403
return viml_eqregh(c, "^[A-Za-z]$");
@@ -2552,8 +2553,14 @@ ExprTokenizer.prototype.get2 = function() {
25522553
return this.token(TOKEN_COLON, ":", pos);
25532554
}
25542555
else if (c == "#") {
2555-
r.seek_cur(1);
2556-
return this.token(TOKEN_SHARP, "#", pos);
2556+
if (r.p(1) == "{") {
2557+
r.seek_cur(2);
2558+
return this.token(TOKEN_LITCOPEN, "#{", pos);
2559+
}
2560+
else {
2561+
r.seek_cur(1);
2562+
return this.token(TOKEN_SHARP, "#", pos);
2563+
}
25572564
}
25582565
else if (c == "(") {
25592566
r.seek_cur(1);
@@ -2697,6 +2704,29 @@ ExprTokenizer.prototype.get_dstring = function() {
26972704
return s;
26982705
}
26992706

2707+
ExprTokenizer.prototype.get_dict_literal_key = function() {
2708+
this.reader.skip_white();
2709+
var r = this.reader;
2710+
var c = r.peek();
2711+
if (!isalnum(c) && c != "_" && c != "-") {
2712+
throw Err(viml_printf("unexpected token: %s", token.value), token.pos);
2713+
}
2714+
var s = c;
2715+
this.reader.seek_cur(1);
2716+
while (TRUE) {
2717+
var c = this.reader.p(0);
2718+
if (c == "<EOF>" || c == "<EOL>") {
2719+
throw Err("unexpectd EOL", this.reader.getpos());
2720+
}
2721+
if (!isalnum(c) && c != "_" && c != "-") {
2722+
break;
2723+
}
2724+
this.reader.seek_cur(1);
2725+
s += c;
2726+
}
2727+
return s;
2728+
}
2729+
27002730
function ExprParser() { this.__init__.apply(this, arguments); }
27012731
ExprParser.prototype.__init__ = function(reader) {
27022732
this.reader = reader;
@@ -3228,6 +3258,7 @@ ExprParser.prototype.parse_expr8 = function() {
32283258
// 'string'
32293259
// [expr1, ...]
32303260
// {expr1: expr1, ...}
3261+
// #{literal_key1: expr1, ...}
32313262
// {args -> expr1}
32323263
// &option
32333264
// (expr1)
@@ -3292,7 +3323,8 @@ ExprParser.prototype.parse_expr9 = function() {
32923323
}
32933324
}
32943325
}
3295-
else if (token.type == TOKEN_COPEN) {
3326+
else if (token.type == TOKEN_COPEN || token.type == TOKEN_LITCOPEN) {
3327+
var is_litdict = token.type == TOKEN_LITCOPEN;
32963328
var savepos = this.reader.tell();
32973329
var nodepos = token.pos;
32983330
var token = this.tokenizer.get();
@@ -3387,7 +3419,7 @@ ExprParser.prototype.parse_expr9 = function() {
33873419
return node;
33883420
}
33893421
while (1) {
3390-
var key = this.parse_expr1();
3422+
var key = is_litdict ? this.parse_dict_literal_key() : this.parse_expr1();
33913423
var token = this.tokenizer.get();
33923424
if (token.type == TOKEN_CCLOSE) {
33933425
if (!viml_empty(node.value)) {
@@ -3463,6 +3495,13 @@ ExprParser.prototype.parse_expr9 = function() {
34633495
return node;
34643496
}
34653497

3498+
ExprParser.prototype.parse_dict_literal_key = function() {
3499+
var node = Node(NODE_STRING);
3500+
node.pos = this.reader.tell();
3501+
node.value = "'" + this.tokenizer.get_dict_literal_key() + "'";
3502+
return node;
3503+
}
3504+
34663505
// SUBSCRIPT or CONCAT
34673506
// dict "." [0-9A-Za-z_]+ => (subscript dict key)
34683507
// str "." expr6 => (concat str expr6)

py/vimlparser.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ def viml_type(obj):
351351
TOKEN_SHARP = 64
352352
TOKEN_ARROW = 65
353353
TOKEN_BLOB = 66
354+
TOKEN_LITCOPEN = 67
354355
MAX_FUNC_ARGS = 20
355356
def isalpha(c):
356357
return viml_eqregh(c, "^[A-Za-z]$")
@@ -2023,8 +2024,12 @@ def get2(self):
20232024
r.seek_cur(1)
20242025
return self.token(TOKEN_COLON, ":", pos)
20252026
elif c == "#":
2026-
r.seek_cur(1)
2027-
return self.token(TOKEN_SHARP, "#", pos)
2027+
if r.p(1) == "{":
2028+
r.seek_cur(2)
2029+
return self.token(TOKEN_LITCOPEN, "#{", pos)
2030+
else:
2031+
r.seek_cur(1)
2032+
return self.token(TOKEN_SHARP, "#", pos)
20282033
elif c == "(":
20292034
r.seek_cur(1)
20302035
return self.token(TOKEN_POPEN, "(", pos)
@@ -2131,6 +2136,24 @@ def get_dstring(self):
21312136
s += c
21322137
return s
21332138

2139+
def get_dict_literal_key(self):
2140+
self.reader.skip_white()
2141+
r = self.reader
2142+
c = r.peek()
2143+
if not isalnum(c) and c != "_" and c != "-":
2144+
raise VimLParserException(Err(viml_printf("unexpected token: %s", token.value), token.pos))
2145+
s = c
2146+
self.reader.seek_cur(1)
2147+
while TRUE:
2148+
c = self.reader.p(0)
2149+
if c == "<EOF>" or c == "<EOL>":
2150+
raise VimLParserException(Err("unexpectd EOL", self.reader.getpos()))
2151+
if not isalnum(c) and c != "_" and c != "-":
2152+
break
2153+
self.reader.seek_cur(1)
2154+
s += c
2155+
return s
2156+
21342157
class ExprParser:
21352158
def __init__(self, reader):
21362159
self.reader = reader
@@ -2575,6 +2598,7 @@ def parse_expr8(self):
25752598
# 'string'
25762599
# [expr1, ...]
25772600
# {expr1: expr1, ...}
2601+
# #{literal_key1: expr1, ...}
25782602
# {args -> expr1}
25792603
# &option
25802604
# (expr1)
@@ -2627,7 +2651,8 @@ def parse_expr9(self):
26272651
break
26282652
else:
26292653
raise VimLParserException(Err(viml_printf("unexpected token: %s", token.value), token.pos))
2630-
elif token.type == TOKEN_COPEN:
2654+
elif token.type == TOKEN_COPEN or token.type == TOKEN_LITCOPEN:
2655+
is_litdict = token.type == TOKEN_LITCOPEN
26312656
savepos = self.reader.tell()
26322657
nodepos = token.pos
26332658
token = self.tokenizer.get()
@@ -2703,7 +2728,7 @@ def parse_expr9(self):
27032728
self.tokenizer.get()
27042729
return node
27052730
while 1:
2706-
key = self.parse_expr1()
2731+
key = self.parse_dict_literal_key() if is_litdict else self.parse_expr1()
27072732
token = self.tokenizer.get()
27082733
if token.type == TOKEN_CCLOSE:
27092734
if not viml_empty(node.value):
@@ -2759,6 +2784,12 @@ def parse_expr9(self):
27592784
raise VimLParserException(Err(viml_printf("unexpected token: %s", token.value), token.pos))
27602785
return node
27612786

2787+
def parse_dict_literal_key(self):
2788+
node = Node(NODE_STRING)
2789+
node.pos = self.reader.tell()
2790+
node.value = "'" + self.tokenizer.get_dict_literal_key() + "'"
2791+
return node
2792+
27622793
# SUBSCRIPT or CONCAT
27632794
# dict "." [0-9A-Za-z_]+ => (subscript dict key)
27642795
# str "." expr6 => (concat str expr6)

0 commit comments

Comments
 (0)