Skip to content

Commit 423b877

Browse files
committed
Combine led methods to a single method
1 parent 111811d commit 423b877

File tree

1 file changed

+91
-111
lines changed

1 file changed

+91
-111
lines changed

jmespath.js

Lines changed: 91 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,8 @@
477477
var left = this.nud(leftToken);
478478
var currentToken = this.lookahead(0);
479479
while (rbp < this.bindingPower[currentToken]) {
480-
var ledMethod = this["led" + currentToken];
481-
if (ledMethod === undefined) {
482-
this.errorToken(this.lookaheadToken(0));
483-
}
484480
this.advance();
485-
left = ledMethod.call(this, left);
481+
left = this.led(currentToken, left);
486482
currentToken = this.lookahead(0);
487483
}
488484
return left;
@@ -533,7 +529,7 @@
533529
return {type: "ValueProjection", children: [left, right]};
534530
break;
535531
case "Filter":
536-
return this.ledFilter({type: "Identity"});
532+
return this.led(token.type, {type: "Identity"});
537533
break;
538534
case "Lbrace":
539535
return this.parseMultiselectHash();
@@ -587,6 +583,95 @@
587583
}
588584
},
589585

586+
led: function(tokenName, left) {
587+
switch(tokenName) {
588+
case "Dot":
589+
var rbp = this.bindingPower.Dot;
590+
var right;
591+
if (this.lookahead(0) !== "Star") {
592+
right = this.parseDotRHS(rbp);
593+
return {type: "Subexpression", children: [left, right]};
594+
} else {
595+
// Creating a projection.
596+
this.advance();
597+
right = this.parseProjectionRHS(rbp);
598+
return {type: "ValueProjection", children: [left, right]};
599+
}
600+
break;
601+
case "Pipe":
602+
var right = this.expression(this.bindingPower.Pipe);
603+
return {type: "Pipe", children: [left, right]};
604+
break;
605+
case "Or":
606+
var right = this.expression(this.bindingPower.Or);
607+
return {type: "OrExpression", children: [left, right]};
608+
break;
609+
case "And":
610+
var right = this.expression(this.bindingPower.And);
611+
return {type: "AndExpression", children: [left, right]};
612+
break;
613+
case "Lparen":
614+
var name = left.name;
615+
var args = [];
616+
var expression, node;
617+
while (this.lookahead(0) !== "Rparen") {
618+
if (this.lookahead(0) === "Current") {
619+
expression = {type: "Current"};
620+
this.advance();
621+
} else {
622+
expression = this.expression(0);
623+
}
624+
if (this.lookahead(0) === "Comma") {
625+
this.match("Comma");
626+
}
627+
args.push(expression);
628+
}
629+
this.match("Rparen");
630+
node = {type: "Function", name: name, children: args};
631+
return node;
632+
break;
633+
case "Filter":
634+
var condition = this.expression(0);
635+
var right;
636+
this.match("Rbracket");
637+
if (this.lookahead(0) === "Flatten") {
638+
right = {type: "Identity"};
639+
} else {
640+
right = this.parseProjectionRHS(this.bindingPower.Filter);
641+
}
642+
return {type: "FilterProjection", children: [left, right, condition]};
643+
break;
644+
case "Flatten":
645+
var leftNode = {type: "Flatten", children: [left]};
646+
var rightNode = this.parseProjectionRHS(this.bindingPower.Flatten);
647+
return {type: "Projection", children: [leftNode, rightNode]};
648+
break;
649+
case "EQ":
650+
case "NE":
651+
case "GT":
652+
case "GTE":
653+
case "LT":
654+
case "LTE":
655+
return this.parseComparator(left, tokenName);
656+
break
657+
case "Lbracket":
658+
var token = this.lookaheadToken(0);
659+
var right;
660+
if (token.type === "Number" || token.type === "Colon") {
661+
right = this.parseIndexExpression();
662+
return this.projectIfSlice(left, right);
663+
} else {
664+
this.match("Star");
665+
this.match("Rbracket");
666+
right = this.parseProjectionRHS(this.bindingPower.Star);
667+
return {type: "Projection", children: [left, right]};
668+
}
669+
break;
670+
default:
671+
this.errorToken(this.lookaheadToken(0));
672+
}
673+
},
674+
590675
match: function(tokenType) {
591676
if (this.lookahead(0) === tokenType) {
592677
this.advance();
@@ -606,20 +691,6 @@
606691
throw error;
607692
},
608693

609-
ledOr: function(left) {
610-
var right = this.expression(this.bindingPower.Or);
611-
return {type: "OrExpression", children: [left, right]};
612-
},
613-
614-
ledAnd: function(left) {
615-
var right = this.expression(this.bindingPower.And);
616-
return {type: "AndExpression", children: [left, right]};
617-
},
618-
619-
ledPipe: function(left) {
620-
var right = this.expression(this.bindingPower.Pipe);
621-
return {type: "Pipe", children: [left, right]};
622-
},
623694

624695
parseIndexExpression: function() {
625696
if (this.lookahead(0) === "Colon" || this.lookahead(1) === "Colon") {
@@ -675,102 +746,11 @@
675746
};
676747
},
677748

678-
ledDot: function(left) {
679-
var rbp = this.bindingPower.Dot;
680-
var right;
681-
if (this.lookahead(0) !== "Star") {
682-
right = this.parseDotRHS(rbp);
683-
return {type: "Subexpression", children: [left, right]};
684-
} else {
685-
// Creating a projection.
686-
this.advance();
687-
right = this.parseProjectionRHS(rbp);
688-
return {type: "ValueProjection", children: [left, right]};
689-
}
690-
},
691-
692-
ledFilter: function(left) {
693-
var condition = this.expression(0);
694-
var right;
695-
this.match("Rbracket");
696-
if (this.lookahead(0) === "Flatten") {
697-
right = {type: "Identity"};
698-
} else {
699-
right = this.parseProjectionRHS(this.bindingPower.Filter);
700-
}
701-
return {type: "FilterProjection", children: [left, right, condition]};
702-
},
703-
704-
ledEQ: function(left) {
705-
return this.parseComparator(left, "EQ");
706-
},
707-
708-
ledNE: function(left) {
709-
return this.parseComparator(left, "NE");
710-
},
711-
712-
ledGT: function(left) {
713-
return this.parseComparator(left, "GT");
714-
},
715-
716-
ledGTE: function(left) {
717-
return this.parseComparator(left, "GTE");
718-
},
719-
720-
ledLT: function(left) {
721-
return this.parseComparator(left, "LT");
722-
},
723-
724-
ledLTE: function(left) {
725-
return this.parseComparator(left, "LTE");
726-
},
727-
728749
parseComparator: function(left, comparator) {
729750
var right = this.expression(this.bindingPower[comparator]);
730751
return {type: "Comparator", name: comparator, children: [left, right]};
731752
},
732753

733-
ledLbracket: function(left) {
734-
var token = this.lookaheadToken(0);
735-
var right;
736-
if (token.type === "Number" || token.type === "Colon") {
737-
right = this.parseIndexExpression();
738-
return this.projectIfSlice(left, right);
739-
} else {
740-
this.match("Star");
741-
this.match("Rbracket");
742-
right = this.parseProjectionRHS(this.bindingPower.Star);
743-
return {type: "Projection", children: [left, right]};
744-
}
745-
},
746-
747-
ledFlatten: function(left) {
748-
var leftNode = {type: "Flatten", children: [left]};
749-
var rightNode = this.parseProjectionRHS(this.bindingPower.Flatten);
750-
return {type: "Projection", children: [leftNode, rightNode]};
751-
},
752-
753-
ledLparen: function(left) {
754-
var name = left.name;
755-
var args = [];
756-
var expression, node;
757-
while (this.lookahead(0) !== "Rparen") {
758-
if (this.lookahead(0) === "Current") {
759-
expression = {type: "Current"};
760-
this.advance();
761-
} else {
762-
expression = this.expression(0);
763-
}
764-
if (this.lookahead(0) === "Comma") {
765-
this.match("Comma");
766-
}
767-
args.push(expression);
768-
}
769-
this.match("Rparen");
770-
node = {type: "Function", name: name, children: args};
771-
return node;
772-
},
773-
774754
parseDotRHS: function(rbp) {
775755
var lookahead = this.lookahead(0);
776756
var exprTokens = ["UnquotedIdentifier", "QuotedIdentifier", "Star"];

0 commit comments

Comments
 (0)