Skip to content

Commit 111811d

Browse files
committed
Collapse nud methods into a single mthod
1 parent 6cd3285 commit 111811d

File tree

1 file changed

+88
-94
lines changed

1 file changed

+88
-94
lines changed

jmespath.js

Lines changed: 88 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,7 @@
474474
expression: function(rbp) {
475475
var leftToken = this.lookaheadToken(0);
476476
this.advance();
477-
var name = "nud" + leftToken.type;
478-
var nudMethod = this[name] || this.errorToken;
479-
var left = nudMethod.call(this, leftToken);
477+
var left = this.nud(leftToken);
480478
var currentToken = this.lookahead(0);
481479
while (rbp < this.bindingPower[currentToken]) {
482480
var ledMethod = this["led" + currentToken];
@@ -502,6 +500,93 @@
502500
this.index++;
503501
},
504502

503+
nud: function(token) {
504+
switch (token.type) {
505+
case "Literal":
506+
return {type: "Literal", value: token.value};
507+
break;
508+
case "UnquotedIdentifier":
509+
return {type: "Field", name: token.value};
510+
break;
511+
case "QuotedIdentifier":
512+
var node = {type: "Field", name: token.value};
513+
if (this.lookahead(0) === "Lparen") {
514+
throw new Error("Quoted identifier not allowed for function names.");
515+
} else {
516+
return node;
517+
}
518+
break;
519+
case "Not":
520+
var right = this.expression(this.bindingPower.Not);
521+
return {type: "NotExpression", children: [right]};
522+
break;
523+
case "Star":
524+
var left = {type: "Identity"};
525+
var right = null;
526+
if (this.lookahead(0) === "Rbracket") {
527+
// This can happen in a multiselect,
528+
// [a, b, *]
529+
right = {type: "Identity"};
530+
} else {
531+
right = this.parseProjectionRHS(this.bindingPower.Star);
532+
}
533+
return {type: "ValueProjection", children: [left, right]};
534+
break;
535+
case "Filter":
536+
return this.ledFilter({type: "Identity"});
537+
break;
538+
case "Lbrace":
539+
return this.parseMultiselectHash();
540+
break;
541+
case "Flatten":
542+
var left = {type: "Flatten", children: [{type: "Identity"}]};
543+
var right = this.parseProjectionRHS(this.bindingPower.Flatten);
544+
return {type: "Projection", children: [left, right]};
545+
break;
546+
case "Lbracket":
547+
var right;
548+
if (this.lookahead(0) === "Number" || this.lookahead(0) === "Colon") {
549+
right = this.parseIndexExpression();
550+
return this.projectIfSlice({type: "Identity"}, right);
551+
} else if (this.lookahead(0) === "Star" &&
552+
this.lookahead(1) === "Rbracket") {
553+
this.advance();
554+
this.advance();
555+
right = this.parseProjectionRHS(this.bindingPower.Star);
556+
return {type: "Projection",
557+
children: [{type: "Identity"}, right]};
558+
} else {
559+
return this.parseMultiselectList();
560+
}
561+
break;
562+
case "Current":
563+
return {type: "Current"};
564+
break;
565+
case "Expref":
566+
var expression = this.expression(this.bindingPower.Expref);
567+
return {type: "ExpressionReference", children: [expression]};
568+
break;
569+
case "Lparen":
570+
var args = [];
571+
var expression;
572+
while (this.lookahead(0) !== "Rparen") {
573+
if (this.lookahead(0) === "Current") {
574+
expression = {type: "Current"};
575+
this.advance();
576+
} else {
577+
expression = this.expression(0);
578+
}
579+
args.push(expression);
580+
}
581+
this.match("Rparen");
582+
return args[0];
583+
break;
584+
default:
585+
this.errorToken(token);
586+
break;
587+
}
588+
},
589+
505590
match: function(tokenType) {
506591
if (this.lookahead(0) === tokenType) {
507592
this.advance();
@@ -521,33 +606,6 @@
521606
throw error;
522607
},
523608

524-
nudLiteral: function(token) {
525-
return {type: "Literal", value: token.value};
526-
},
527-
528-
nudUnquotedIdentifier: function(token) {
529-
return {type: "Field", name: token.value};
530-
},
531-
532-
nudExpref: function() {
533-
var expression = this.expression(this.bindingPower.Expref);
534-
return {type: "ExpressionReference", children: [expression]};
535-
},
536-
537-
nudQuotedIdentifier: function(token) {
538-
var node = {type: "Field", name: token.value};
539-
if (this.lookahead(0) === "Lparen") {
540-
throw new Error("Quoted identifier not allowed for function names.");
541-
} else {
542-
return node;
543-
}
544-
},
545-
546-
nudNot: function() {
547-
var right = this.expression(this.bindingPower.Not);
548-
return {type: "NotExpression", children: [right]};
549-
},
550-
551609
ledOr: function(left) {
552610
var right = this.expression(this.bindingPower.Or);
553611
return {type: "OrExpression", children: [left, right]};
@@ -563,40 +621,6 @@
563621
return {type: "Pipe", children: [left, right]};
564622
},
565623

566-
nudStar: function() {
567-
var left = {type: "Identity"};
568-
var right = null;
569-
if (this.lookahead(0) === "Rbracket") {
570-
// This can happen in a multiselect,
571-
// [a, b, *]
572-
right = {type: "Identity"};
573-
} else {
574-
right = this.parseProjectionRHS(this.bindingPower.Star);
575-
}
576-
return {type: "ValueProjection", children: [left, right]};
577-
},
578-
579-
nudCurrent: function() {
580-
return {type: "Current"};
581-
},
582-
583-
nudLbracket: function() {
584-
var right;
585-
if (this.lookahead(0) === "Number" || this.lookahead(0) === "Colon") {
586-
right = this.parseIndexExpression();
587-
return this.projectIfSlice({type: "Identity"}, right);
588-
} else if (this.lookahead(0) === "Star" &&
589-
this.lookahead(1) === "Rbracket") {
590-
this.advance();
591-
this.advance();
592-
right = this.parseProjectionRHS(this.bindingPower.Star);
593-
return {type: "Projection",
594-
children: [{type: "Identity"}, right]};
595-
} else {
596-
return this.parseMultiselectList();
597-
}
598-
},
599-
600624
parseIndexExpression: function() {
601625
if (this.lookahead(0) === "Colon" || this.lookahead(1) === "Colon") {
602626
return this.parseSliceExpression();
@@ -651,10 +675,6 @@
651675
};
652676
},
653677

654-
nudLbrace: function() {
655-
return this.parseMultiselectHash();
656-
},
657-
658678
ledDot: function(left) {
659679
var rbp = this.bindingPower.Dot;
660680
var right;
@@ -669,10 +689,6 @@
669689
}
670690
},
671691

672-
nudFilter: function() {
673-
return this.ledFilter({type: "Identity"});
674-
},
675-
676692
ledFilter: function(left) {
677693
var condition = this.expression(0);
678694
var right;
@@ -728,34 +744,12 @@
728744
}
729745
},
730746

731-
nudFlatten: function() {
732-
var left = {type: "Flatten", children: [{type: "Identity"}]};
733-
var right = this.parseProjectionRHS(this.bindingPower.Flatten);
734-
return {type: "Projection", children: [left, right]};
735-
},
736-
737747
ledFlatten: function(left) {
738748
var leftNode = {type: "Flatten", children: [left]};
739749
var rightNode = this.parseProjectionRHS(this.bindingPower.Flatten);
740750
return {type: "Projection", children: [leftNode, rightNode]};
741751
},
742752

743-
nudLparen: function() {
744-
var args = [];
745-
var expression;
746-
while (this.lookahead(0) !== "Rparen") {
747-
if (this.lookahead(0) === "Current") {
748-
expression = {type: "Current"};
749-
this.advance();
750-
} else {
751-
expression = this.expression(0);
752-
}
753-
args.push(expression);
754-
}
755-
this.match("Rparen");
756-
return args[0];
757-
},
758-
759753
ledLparen: function(left) {
760754
var name = left.name;
761755
var args = [];

0 commit comments

Comments
 (0)