Permalink
Browse files

Added quick implementation of a switch statement

  • Loading branch information...
1 parent 938b059 commit cf4217ab1bb967728566d0686ce295e58b3400cb @tj tj committed Oct 3, 2011
Showing with 151 additions and 1 deletion.
  1. +33 −0 lib/compiler.js
  2. +28 −1 lib/lexer.js
  3. +42 −0 lib/nodes/case.js
  4. +1 −0 lib/nodes/index.js
  5. +47 −0 lib/parser.js
View
@@ -159,6 +159,39 @@ Compiler.prototype = {
},
/**
+ * Visit case `node`.
+ *
+ * @param {Literal} node
+ * @api public
+ */
+
+ visitCase: function(node){
+ var _ = this.withinCase;
+ this.withinCase = true;
+ this.buf.push('switch (' + node.expr + '){');
+ this.visit(node.block);
+ this.buf.push('}');
+ this.withinCase = _;
+ },
+
+ /**
+ * Visit when `node`.
+ *
+ * @param {Literal} node
+ * @api public
+ */
+
+ visitWhen: function(node){
+ if ('default' == node.expr) {
+ this.buf.push('default:');
+ } else {
+ this.buf.push('case ' + node.expr + ':');
+ }
+ this.visit(node.block);
+ this.buf.push(' break;');
+ },
+
+ /**
* Visit literal `node`.
*
* @param {Literal} node
View
@@ -265,6 +265,30 @@ Lexer.prototype = {
},
/**
+ * Case.
+ */
+
+ case: function() {
+ return this.scan(/^case +([^\n]+)/, 'case');
+ },
+
+ /**
+ * When.
+ */
+
+ when: function() {
+ return this.scan(/^when +([^:\n]+)/, 'when');
+ },
+
+ /**
+ * Default.
+ */
+
+ default: function() {
+ return this.scan(/^default */, 'default');
+ },
+
+ /**
* Assignment.
*/
@@ -274,7 +298,7 @@ Lexer.prototype = {
this.consume(captures[0].length);
var name = captures[1]
, val = captures[2];
- return this.tok('code', 'var ' + name + ' = (' + val + ')');
+ return this.tok('code', 'var ' + name + ' = (' + val + ');');
}
},
@@ -607,6 +631,9 @@ Lexer.prototype = {
|| this.eos()
|| this.pipelessText()
|| this.doctype()
+ || this.case()
+ || this.when()
+ || this.default()
|| this.extends()
|| this.block()
|| this.include()
View
@@ -0,0 +1,42 @@
+
+/*!
+ * Jade - nodes - Case
+ * Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var Node = require('./node');
+
+/**
+ * Initialize a new `Case` with `expr`.
+ *
+ * @param {String} expr
+ * @api public
+ */
+
+var Case = exports = module.exports = function Case(expr, block){
+ this.expr = expr;
+ this.block = block;
+};
+
+/**
+ * Inherit from `Node`.
+ */
+
+Case.prototype.__proto__ = Node.prototype;
+
+var When = exports.When = function When(expr, block){
+ this.expr = expr;
+ this.block = block;
+};
+
+/**
+ * Inherit from `Node`.
+ */
+
+When.prototype.__proto__ = Node.prototype;
+
View
@@ -9,6 +9,7 @@ exports.Node = require('./node');
exports.Tag = require('./tag');
exports.Code = require('./code');
exports.Each = require('./each');
+exports.Case = require('./case');
exports.Text = require('./text');
exports.Block = require('./block');
exports.Mixin = require('./mixin');
View
@@ -190,6 +190,12 @@ Parser.prototype = {
return this.parseMixin();
case 'block':
return this.parseBlock();
+ case 'case':
+ return this.parseCase();
+ case 'when':
+ return this.parseWhen();
+ case 'default':
+ return this.parseDefault();
case 'extends':
return this.parseExtends();
case 'include':
@@ -227,8 +233,49 @@ Parser.prototype = {
node.line = this.line();
return node;
},
+
+ /**
+ * ':' expr
+ * | block
+ */
+
+ parseBlockExpansion: function(){
+ if (':' == this.peek().type) {
+ this.advance();
+ return new nodes.Block(this.parseExpr());
+ } else {
+ return this.block();
+ }
+ },
+
+ /**
+ * case
+ */
+
+ parseCase: function(){
+ var val = this.expect('case').val;
+ return new nodes.Case(val, this.block());
+ },
+
+ /**
+ * when
+ */
+
+ parseWhen: function(){
+ var val = this.expect('when').val
+ return new nodes.Case.When(val, this.parseBlockExpansion());
+ },
/**
+ * default
+ */
+
+ parseDefault: function(){
+ this.expect('default');
+ return new nodes.Case.When('default', this.parseBlockExpansion());
+ },
+
+ /**
* code
*/

0 comments on commit cf4217a

Please sign in to comment.