Commits

masklinn  committed e89ebe6

Tokenize and parse keyword arguments in function calls

  • Participants
  • Parent commits 7be96c3
  • Branches kwargs

Comments (0)

Files changed (2)

     symbol(':'); symbol(')'); symbol(']'); symbol('}'); symbol(',');
     symbol('else');
 
+    infix('=', 10, function (left) {
+        if (left.id !== '(name)') {
+            throw new Error("Expected keyword argument name, got " + token.id);
+        }
+        this.first = left;
+        this.second = expression();
+        return this;
+    });
+
     symbol('lambda', 20).nud = function () {
         this.first = [];
         if (token.id !== ':') {

File test/parser.js

                 'expected ' + this.obj + ' to not have an end token');
 };
 
+expect.Assertion.prototype.named = function (value) {
+    this.assert(this.obj.id === '(name)',
+                'expected ' + this.obj + ' to be a name token',
+                'expected ' + this.obj + ' not to be a name token');
+    this.assert(this.obj.value === value,
+                'expected ' + this.obj + ' to have tokenized ' + value,
+                'expected ' + this.obj + ' not to have tokenized ' + value);
+};
 expect.Assertion.prototype.constant = function (value) {
     this.assert(this.obj.id === '(constant)',
                 'expected ' + this.obj + ' to be a constant token',
             expect(toks[1].id).to.be(')');
         });
     });
+    describe('functions', function () {
+        it('tokenizes kwargs', function () {
+            var toks = py.tokenize('foo(bar=3, qux=4)');
+            expect(toks).to.have.tokens(10);
+        });
+    });
+});
+
+describe('Parser', function () {
+    describe('functions', function () {
+        var ast = py.parse(py.tokenize('foo(bar=3, qux=4)'));
+        expect(ast.id).to.be('(');
+        expect(ast.first).to.be.named('foo');
+
+        args = ast.second;
+        expect(args[0].id).to.be('=');
+        expect(args[0].first).to.be.named('bar');
+        expect(args[0].second).to.be.number(3);
+
+        expect(args[1].id).to.be('=');
+        expect(args[1].first).to.be.named('qux');
+        expect(args[1].second).to.be.number(4);
+    });
 });