diff --git a/benchmark/bars.js b/benchmark/bars.js index 46e03c7..252981f 100644 --- a/benchmark/bars.js +++ b/benchmark/bars.js @@ -1,6 +1,7 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o b; }; exports['<'] = exports.lt; exports['>'] = exports.gt; -},{}],46:[function(require,module,exports){ +},{}],47:[function(require,module,exports){ exports.pathResolver = function pathResolver(base, path) { base = base.slice(); path = path.slice(); @@ -4081,7 +4082,7 @@ function findPath(arg) { exports.findPath = findPath; -},{}],47:[function(require,module,exports){ +},{}],48:[function(require,module,exports){ var Generator = require('generate-js'); var Transfrom = Generator.generate(function Transfrom() {}); @@ -4166,11 +4167,11 @@ Transfrom.definePrototype({ module.exports = Transfrom; -},{"generate-js":54}],48:[function(require,module,exports){ +},{"generate-js":55}],49:[function(require,module,exports){ exports.Compiler = require('./lib/compiler'); exports.Token = require('./lib/token'); -},{"./lib/compiler":50,"./lib/token":52}],49:[function(require,module,exports){ +},{"./lib/compiler":51,"./lib/token":53}],50:[function(require,module,exports){ var Generator = require('generate-js'), utils = require('./utils'); @@ -4352,7 +4353,7 @@ CodeBuffer.definePrototype({ module.exports = CodeBuffer; -},{"./utils":53,"generate-js":54}],50:[function(require,module,exports){ +},{"./utils":54,"generate-js":55}],51:[function(require,module,exports){ var Generator = require('generate-js'), Scope = require('./scope'), Token = require('./token'), @@ -4504,7 +4505,7 @@ Compiler.definePrototype({ module.exports = Compiler; -},{"./code-buffer":49,"./scope":51,"./token":52,"./utils":53,"generate-js":54}],51:[function(require,module,exports){ +},{"./code-buffer":50,"./scope":52,"./token":53,"./utils":54,"generate-js":55}],52:[function(require,module,exports){ var Generator = require('generate-js'), Token = require('./token'), utils = require('./utils'); @@ -4589,7 +4590,7 @@ Scope.definePrototype({ module.exports = Scope; -},{"./token":52,"./utils":53,"generate-js":54}],52:[function(require,module,exports){ +},{"./token":53,"./utils":54,"generate-js":55}],53:[function(require,module,exports){ var Generator = require('generate-js'), utils = require('./utils'); @@ -4654,7 +4655,7 @@ Token.definePrototype({ module.exports = Token; -},{"./utils":53,"generate-js":54}],53:[function(require,module,exports){ +},{"./utils":54,"generate-js":55}],54:[function(require,module,exports){ /** * Assert Error function. * @param {Boolean} condition Whether or not to throw error. @@ -4731,7 +4732,7 @@ function bufferSlice(code, range, format) { } exports.bufferSlice = bufferSlice; -},{}],54:[function(require,module,exports){ +},{}],55:[function(require,module,exports){ /** * @name generate.js * @author Michaelangelo Jong @@ -5093,11 +5094,11 @@ exports.bufferSlice = bufferSlice; }()); -},{}],55:[function(require,module,exports){ +},{}],56:[function(require,module,exports){ module.exports={ "name": "bars", - "version": "0.3.7", - "description": "Client-side html templating system that emits DOM. The templates can be updated with new data without re-writing the DOM.", + "version": "0.4.0", + "description": "Bars is a light weight high performance templating system.Bars emits DOM rather than DOM-strings, this means the DOM state is preserved even if data updates happens.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -5119,7 +5120,7 @@ module.exports={ }, "homepage": "https://github.com/Mike96Angelo/Bars#readme", "dependencies": { - "compileit": "0.0.19", + "compileit": "^1.0.0", "generate-js": "^3.1.2" }, "devDependencies": { diff --git a/demo/bars.js b/demo/bars.js index 46e03c7..252981f 100755 --- a/demo/bars.js +++ b/demo/bars.js @@ -1,6 +1,7 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o b; }; exports['<'] = exports.lt; exports['>'] = exports.gt; -},{}],46:[function(require,module,exports){ +},{}],47:[function(require,module,exports){ exports.pathResolver = function pathResolver(base, path) { base = base.slice(); path = path.slice(); @@ -4081,7 +4082,7 @@ function findPath(arg) { exports.findPath = findPath; -},{}],47:[function(require,module,exports){ +},{}],48:[function(require,module,exports){ var Generator = require('generate-js'); var Transfrom = Generator.generate(function Transfrom() {}); @@ -4166,11 +4167,11 @@ Transfrom.definePrototype({ module.exports = Transfrom; -},{"generate-js":54}],48:[function(require,module,exports){ +},{"generate-js":55}],49:[function(require,module,exports){ exports.Compiler = require('./lib/compiler'); exports.Token = require('./lib/token'); -},{"./lib/compiler":50,"./lib/token":52}],49:[function(require,module,exports){ +},{"./lib/compiler":51,"./lib/token":53}],50:[function(require,module,exports){ var Generator = require('generate-js'), utils = require('./utils'); @@ -4352,7 +4353,7 @@ CodeBuffer.definePrototype({ module.exports = CodeBuffer; -},{"./utils":53,"generate-js":54}],50:[function(require,module,exports){ +},{"./utils":54,"generate-js":55}],51:[function(require,module,exports){ var Generator = require('generate-js'), Scope = require('./scope'), Token = require('./token'), @@ -4504,7 +4505,7 @@ Compiler.definePrototype({ module.exports = Compiler; -},{"./code-buffer":49,"./scope":51,"./token":52,"./utils":53,"generate-js":54}],51:[function(require,module,exports){ +},{"./code-buffer":50,"./scope":52,"./token":53,"./utils":54,"generate-js":55}],52:[function(require,module,exports){ var Generator = require('generate-js'), Token = require('./token'), utils = require('./utils'); @@ -4589,7 +4590,7 @@ Scope.definePrototype({ module.exports = Scope; -},{"./token":52,"./utils":53,"generate-js":54}],52:[function(require,module,exports){ +},{"./token":53,"./utils":54,"generate-js":55}],53:[function(require,module,exports){ var Generator = require('generate-js'), utils = require('./utils'); @@ -4654,7 +4655,7 @@ Token.definePrototype({ module.exports = Token; -},{"./utils":53,"generate-js":54}],53:[function(require,module,exports){ +},{"./utils":54,"generate-js":55}],54:[function(require,module,exports){ /** * Assert Error function. * @param {Boolean} condition Whether or not to throw error. @@ -4731,7 +4732,7 @@ function bufferSlice(code, range, format) { } exports.bufferSlice = bufferSlice; -},{}],54:[function(require,module,exports){ +},{}],55:[function(require,module,exports){ /** * @name generate.js * @author Michaelangelo Jong @@ -5093,11 +5094,11 @@ exports.bufferSlice = bufferSlice; }()); -},{}],55:[function(require,module,exports){ +},{}],56:[function(require,module,exports){ module.exports={ "name": "bars", - "version": "0.3.7", - "description": "Client-side html templating system that emits DOM. The templates can be updated with new data without re-writing the DOM.", + "version": "0.4.0", + "description": "Bars is a light weight high performance templating system.Bars emits DOM rather than DOM-strings, this means the DOM state is preserved even if data updates happens.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -5119,7 +5120,7 @@ module.exports={ }, "homepage": "https://github.com/Mike96Angelo/Bars#readme", "dependencies": { - "compileit": "0.0.19", + "compileit": "^1.0.0", "generate-js": "^3.1.2" }, "devDependencies": { diff --git a/demo/index.html b/demo/index.html index 6689b53..e09e409 100755 --- a/demo/index.html +++ b/demo/index.html @@ -7,19 +7,24 @@ @@ -32,12 +37,9 @@ window.barsData = { persons: [ - // { name: 'John' }, - // { name: 'Jane' }, - // { name: 'Jim' }, - 'John', - 'Jane', - 'Jim' + { name: 'John' }, + { name: 'Jane' }, + { name: 'Jim' }, ], x: 2, title: 'The Cat in the Hat' @@ -50,7 +52,10 @@ }); }); - window.domFrag = bars.compile(template).render(); + preCompiled = [0,"0.4.0","DOM",[1,[[3,"ul",[],0,[[5,"each",[10,"sort",[[9,["persons"]],[8,"length"]]],null,[1,[[3,"li",[],0,[[6,[11,"+",[[10,"number",[[9,["@","index"]]]],[8,1]]]],[2," - "],[6,[9,[]]],[3,"input",[[4,"type",[[2,"text"]],0]],0,[],0]],1]],1],null]],1],[5,"if",[11,"<",[[9,["x"]],[8,5]]],null,[1,[[3,"span",[],0,[[2,"x is less then 5"]],0]],1],[5,"if",[11,">",[[9,["x"]],[8,5]]],null,[1,[[3,"span",[],0,[[2,"x is greater then 5"]],0]],0],[1,[[3,"span",[],0,[[2,"x is equal to 5"]],0]],0]]],[3,"br",[],0,[],0],[3,"br",[],0,[],0],[7,"title",null,null,null]],1]]; + + // window.domFrag = bars.compile(template).render(); + window.domFrag = bars.build(preCompiled).render(); window.domFrag.appendTo(document.getElementById('bars-output')); window.domFrag.update(barsData); diff --git a/lib/bars-runtime.js b/lib/bars-runtime.js index 3494840..5d4bf3f 100755 --- a/lib/bars-runtime.js +++ b/lib/bars-runtime.js @@ -1,5 +1,6 @@ var Generator = require('generate-js'), Renderer = require('./renderer'), + Token = require('./compiler/tokens'), Blocks = require('./blocks'), Transform = require('./transforms'), packageJSON = require('../package'); @@ -17,9 +18,16 @@ var Bars = Generator.generate(function Bars() { Bars.definePrototype({ version: packageJSON.version, build: function build(parsedTemplate) { - var _ = this; + var _ = this, + program = parsedTemplate; + + if (Array.isArray(parsedTemplate)) { + program = new Token.tokens.program(); + + program.fromArray(parsedTemplate); + } - return new Renderer(_, parsedTemplate); + return new Renderer(_, program.fragment); }, registerBlock: function registerBlock(name, block) { diff --git a/lib/bars.js b/lib/bars.js index 2b3958b..4127d76 100644 --- a/lib/bars.js +++ b/lib/bars.js @@ -1,12 +1,11 @@ var Bars = require('./bars-runtime'), - compile = require('./compiler/compiler3'); + compile = require('./compiler'); Bars.definePrototype({ compile: function compile(template, filename, mode, flags) { var _ = this; - return _.build(_.parse(template, filename, mode, flags) - .fragment); + return _.build(_.parse(template, filename, mode, flags)); }, parse: function parse(template, filename, mode, flags) { diff --git a/lib/compiler/code-buffer.js b/lib/compiler/code-buffer.js deleted file mode 100644 index f4fc058..0000000 --- a/lib/compiler/code-buffer.js +++ /dev/null @@ -1,152 +0,0 @@ -var Generator = require('generate-js'); - -var CodeBuffer = Generator.generate( - function CodeBuffer(str, file) { - this.reset(); - this._buffer = str; - this._file = file; - } -); - -CodeBuffer.definePrototype({ - reset: function reset() { - this.line = 1; - this.column = 1; - this._index = 0; - this._currentLine = 0; - }, - currentLine: { - get: function currentLine() { - var lineText = '', - i = this._currentLine; - - while (i < this.length) { - lineText += this._buffer[i]; - if (this._buffer.codePointAt(i) === 10) { - break; - } - i++; - } - - return lineText; - } - }, - - buffer: { - get: function getBuffer() { - return this._buffer; - } - }, - - - index: { - get: function getIndex() { - return this._index; - }, - - set: function setIndex(val) { - var i = this._index, - update = false; - - val = Math.min(this.length, val); - val = Math.max(0, val); - - if (i == val) return; - - if (i > val) { - // throw new Error('========' + val + ' < ' +i+'======='); - this.reset(); - i = this._index; - } - - if (this.buffer.codePointAt(i) === 10) { - update = true; - i++; - } - - for (; i <= val; i++) { - if (update) { - this._currentLine = i; - this.line++; - update = false; - } else { - this.column++; - } - - if (this.buffer.codePointAt(i) === 10) { - update = true; - } - } - this.column = val - this._currentLine + 1; - this._index = val; - } - }, - - length: { - get: function getLength() { - return this._buffer.length; - } - }, - - next: function next() { - this.index++; - return this.charAt(this.index); - }, - - left: { - get: function getLeft() { - return this._index < this.length; - } - }, - - charAt: function charAt(i) { - return this._buffer[i] || 'EOF'; - }, - - codePointAt: function codePointAt(i) { - return this._buffer.codePointAt(i); - }, - - slice: function slice(startIndex, endIndex) { - return this._buffer.slice(startIndex, endIndex); - }, - - makeError: function makeError(message, tokenLength) { - tokenLength = tokenLength || 1; - - var currentLine = this.currentLine, - tokenIdentifier = - currentLine[currentLine.length - 1] === '\n' ? '' : - '\n', - i; - - for (i = 1; i < this.column; i++) { - tokenIdentifier += ' '; - } - - tokenLength = Math.min( - tokenLength, - currentLine.length - tokenIdentifier.length - ) || 1; - - for (i = 0; i < tokenLength; i++) { - tokenIdentifier += '^'; - } - - return 'Syntax Error: ' + - message + - ' at ' + - (this._file ? this._file + ':' : '') + - this.line + - ':' + - this.column + - ' index ' + - this.index + - '\n\n' + - currentLine + - tokenIdentifier + - '\n'; - } -}); - -module.exports = CodeBuffer; diff --git a/lib/compiler/compiler.js b/lib/compiler/compiler.js index 061612b..031ca43 100644 --- a/lib/compiler/compiler.js +++ b/lib/compiler/compiler.js @@ -1,1765 +1,90 @@ -var CodeBuffer = require('./code-buffer'), - Token = require('./token'); +var compileit = require('compileit'); +var parsers = require('./parsers'); -function bufferSlice(code, range) { - return JSON.stringify( - code.slice(Math.max(0, code.index - range), code.index) - ) - .slice(1, -1) + - JSON.stringify(code.charAt(code.index) || 'EOF') - .slice(1, -1) - .green.underline + - JSON.stringify( - code.slice( - code.index + 1, - Math.min(code.length, code.index + 1 + range) - ) - ) - .slice(1, -1); -} - - -var SELF_CLOSEING_TAGS = require('./self-closing-tags'); -var ENTITIES = require('./html-entities'); -var TYPES = require('./token-types'); - -function HTML_IDENTIFIER_START(ch) { - return (0x0041 <= ch && ch <= 0x005a) || - (0x0061 <= ch && ch <= 0x007a); -} - -function HTML_ENTITY(ch) { - /* ^[0-9A-Za-z]$ */ - return (0x0030 <= ch && ch <= 0x0039) || - (0x0041 <= ch && ch <= 0x005a) || - (0x0061 <= ch && ch <= 0x007a); -} - -function HTML_IDENTIFIER(ch) { - /* ^[0-9A-Z_a-z-]$ */ - return ch === 0x002d || - (0x0030 <= ch && ch <= 0x0039) || - (0x0041 <= ch && ch <= 0x005a) || - ch === 0x005f || - (0x0061 <= ch && ch <= 0x007a); -} - -function WHITESPACE(ch) { - /* ^\s$ */ - return (0x0009 <= ch && ch <= 0x000d) || - ch === 0x0020 || - ch === 0x00a0 || - ch === 0x1680 || - ch === 0x180e || - (0x2000 <= ch && ch <= 0x200a) || - (0x2028 <= ch && ch <= 0x2029) || - ch === 0x202f || - ch === 0x205f || - ch === 0x3000 || - ch === 0xfeff; -} - -function getHTMLUnEscape(str) { - var code; - - code = ENTITIES[str.slice(1, -1)]; - - if (typeof code !== 'number' && str[1] === '#') { - code = parseInt(str.slice(2, -1), 0x000a); - } - - if (typeof code === 'number' && !isNaN(code)) { - return String.fromCharCode(code); - } - - return str; -} - -////////// -// TEXT // -////////// - -function TEXT(mode, code, tokens, close) { - var index = code.index, - isEntity = false, - entityStr = '', - text = new Token(code, TYPES.TEXT); - - for (; index < code.length; index++) { - ch = code.codePointAt(index); - - if ( - code.codePointAt(index) === 0x007b /* { */ && - code.codePointAt(index + 1) === 0x007b /* { */ - ) { - break; - } - } - - if (code.index < index) { - code.index = index; - - text.close(code); - - text.value = text.source(code); - - return text; - } - - return null; -} - - -///////// -// DOM // -///////// - -function HTML_COMMENT(mode, code, tokens, close) { - var index = code.index, - length = code.length, - comment; - - if ( /* */ - code.codePointAt(index) === 0x002d && - code.codePointAt(index + 1) === 0x002d && - code.codePointAt(index + 2) === 0x003e - ) { - index += 3; - code.index = index; - comment.close(code); - - comment.value = comment.source(code); - - return comment; - } - } - - throw code.makeError( - 'Unclosed Comment: Expected "-->" to fallow "" to fallow "" to fallow "