From 1b60163ba380cba55b687090e5d0d08c32192e02 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Tue, 9 Sep 2014 20:57:48 +0400 Subject: [PATCH] Moved empty lines logic from rules to block parser --- lib/lexer_block.js | 23 +++++++++++++++++++---- lib/lexer_block/blockquote.js | 3 +-- lib/lexer_block/fences.js | 3 +-- lib/lexer_block/heading.js | 3 +-- lib/lexer_block/hr.js | 3 +-- lib/lexer_block/lheading.js | 3 +-- lib/lexer_block/list.js | 3 +-- lib/lexer_block/paragraph.js | 3 +-- lib/lexer_block/table.js | 5 +---- lib/parser.js | 10 +++------- 10 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/lexer_block.js b/lib/lexer_block.js index 08c96da..0fbfc50 100644 --- a/lib/lexer_block.js +++ b/lib/lexer_block.js @@ -4,6 +4,10 @@ 'use strict'; +var isEmpty = require('./helpers').isEmpty; +var skipEmptyLines = require('./helpers').skipEmptyLines; + + var rules = []; // `list` should be after `hr`, but before `heading` @@ -113,7 +117,9 @@ LexerBlock.prototype.tokenize = function (state, startLine, endLine) { len = this.rules.length, line = startLine; - while (line < endLine) { + for (;;) { + line = state.line = skipEmptyLines(state, line, endLine); + if (line >= endLine) { break; } // Try all possible rules. // On success, rule should: @@ -127,9 +133,18 @@ LexerBlock.prototype.tokenize = function (state, startLine, endLine) { if (ok) { break; } } - if (ok) { - line = state.line; - continue; + if (!ok) { throw new Error('No matching rules found'); } + + if (line >= state.line) { + throw new Error("Parser didn't update state.line"); + } + + line = state.line; + if (isEmpty(state, line)) { + line++; + + // two empty lines should stop the parser + if (isEmpty(state, line + 1)) { break; } } } }; diff --git a/lib/lexer_block/blockquote.js b/lib/lexer_block/blockquote.js index 8338815..6dfa3a6 100644 --- a/lib/lexer_block/blockquote.js +++ b/lib/lexer_block/blockquote.js @@ -3,7 +3,6 @@ 'use strict'; -var skipEmptyLines = require('../helpers').skipEmptyLines; var skipSpaces = require('../helpers').skipSpaces; var getLines = require('../helpers').getLines; @@ -85,6 +84,6 @@ module.exports = function blockquote(state, startLine, endLine, silent) { state.tokens.push({ type: 'blockquote_close' }); - state.line = skipEmptyLines(state, nextLine); + state.line = nextLine; return true; }; diff --git a/lib/lexer_block/fences.js b/lib/lexer_block/fences.js index 4c61696..27bd99d 100644 --- a/lib/lexer_block/fences.js +++ b/lib/lexer_block/fences.js @@ -3,7 +3,6 @@ 'use strict'; -var skipEmptyLines = require('../helpers').skipEmptyLines; var skipSpaces = require('../helpers').skipSpaces; var skipChars = require('../helpers').skipChars; var getLines = require('../helpers').getLines; @@ -73,6 +72,6 @@ module.exports = function fences(state, startLine, endLine, silent) { content: getLines(state, startLine + 1, nextLine, true) }); - state.line = skipEmptyLines(state, nextLine + 1); + state.line = nextLine + 1; return true; }; diff --git a/lib/lexer_block/heading.js b/lib/lexer_block/heading.js index 3d0929a..5b2dad9 100644 --- a/lib/lexer_block/heading.js +++ b/lib/lexer_block/heading.js @@ -4,7 +4,6 @@ var isWhiteSpace = require('../helpers').isWhiteSpace; -var skipEmptyLines = require('../helpers').skipEmptyLines; var skipSpaces = require('../helpers').skipSpaces; @@ -67,6 +66,6 @@ module.exports = function heading(state, startLine, endLine, silent) { } state.tokens.push({ type: 'heading_close', level: level }); - state.line = skipEmptyLines(state, ++startLine); + state.line = startLine + 1; return true; }; diff --git a/lib/lexer_block/hr.js b/lib/lexer_block/hr.js index c31dfd0..79f6029 100644 --- a/lib/lexer_block/hr.js +++ b/lib/lexer_block/hr.js @@ -4,7 +4,6 @@ var isWhiteSpace = require('../helpers').isWhiteSpace; -var skipEmptyLines = require('../helpers').skipEmptyLines; module.exports = function hr(state, startLine, endLine, silent) { @@ -43,6 +42,6 @@ module.exports = function hr(state, startLine, endLine, silent) { state.tokens.push({ type: 'hr' }); - state.line = skipEmptyLines(state, ++startLine); + state.line = startLine + 1; return true; }; diff --git a/lib/lexer_block/lheading.js b/lib/lexer_block/lheading.js index 5bf5e04..eccf81e 100644 --- a/lib/lexer_block/lheading.js +++ b/lib/lexer_block/lheading.js @@ -3,7 +3,6 @@ 'use strict'; -var skipEmptyLines = require('../helpers').skipEmptyLines; var skipSpaces = require('../helpers').skipSpaces; var skipChars = require('../helpers').skipChars; @@ -39,6 +38,6 @@ module.exports = function lheading(state, startLine, endLine, silent) { state.lexerInline.tokenize(state, state.bMarks[startLine], state.eMarks[startLine]); state.tokens.push({ type: 'heading_close', level: marker === 0x3D/* = */ ? 1 : 2 }); - state.line = skipEmptyLines(state, ++next); + state.line = next + 1; return true; }; diff --git a/lib/lexer_block/list.js b/lib/lexer_block/list.js index bf5d5b8..b52c61b 100644 --- a/lib/lexer_block/list.js +++ b/lib/lexer_block/list.js @@ -5,7 +5,6 @@ var isEmpty = require('../helpers').isEmpty; var skipSpaces = require('../helpers').skipSpaces; -var skipEmptyLines = require('../helpers').skipEmptyLines; // Search `[-+*][\n ]`, returns next pos arter marker on success @@ -243,6 +242,6 @@ module.exports = function list(state, startLine, endLine, silent) { state.tokens.push({ type: 'bullet_list_close' }); } - state.line = skipEmptyLines(state, nextLine); + state.line = nextLine; return true; }; diff --git a/lib/lexer_block/paragraph.js b/lib/lexer_block/paragraph.js index 1eae922..9a9c486 100644 --- a/lib/lexer_block/paragraph.js +++ b/lib/lexer_block/paragraph.js @@ -4,7 +4,6 @@ var isEmpty = require('../helpers').isEmpty; -var skipEmptyLines = require('../helpers').skipEmptyLines; module.exports = function paragraph(state, startLine, endLine) { @@ -34,6 +33,6 @@ module.exports = function paragraph(state, startLine, endLine) { ); state.tokens.push({ type: 'paragraph_close' }); - state.line = skipEmptyLines(state, nextLine); + state.line = nextLine; return true; }; diff --git a/lib/lexer_block/table.js b/lib/lexer_block/table.js index 93eeb2b..de6a158 100644 --- a/lib/lexer_block/table.js +++ b/lib/lexer_block/table.js @@ -3,9 +3,6 @@ 'use strict'; -var skipEmptyLines = require('../helpers').skipEmptyLines; - - function lineMatch(state, line, reg) { var pos = state.bMarks[line], max = state.eMarks[line]; @@ -77,6 +74,6 @@ module.exports = function table(state, startLine, endLine, silent) { } state.tokens.push({ type: 'table_close' }); - state.line = skipEmptyLines(state, nextLine); + state.line = nextLine; return true; }; diff --git a/lib/parser.js b/lib/parser.js index b0ca53b..bad3970 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -45,13 +45,9 @@ Parser.prototype.render = function (src) { this.options ); - // TODO: skip leading empty lines - - state.lexerBlock.tokenize(state, state.line, state.lineMax); - - // TODO: ??? eat empty paragraphs from tail - - //console.log(state.tokens) + while (state.line < state.lineMax) { + state.lexerBlock.tokenize(state, state.line, state.lineMax); + } return this.renderer.render(state); };