diff --git a/source/Ox/js/JavaScript.js b/source/Ox/js/JavaScript.js index 53603f41..d69eea84 100644 --- a/source/Ox/js/JavaScript.js +++ b/source/Ox/js/JavaScript.js @@ -127,6 +127,72 @@ Ox.doc = (function() { }).join('\n') }; } + function parseSource(source, file) { + var blocks = [], + items = [], + section = '', + tokens = []; + Ox.tokenize(source).forEach(function(token) { + var match; + token.source = source.substr(token.offset, token.length); + if (token.type == 'comment' && (match = + re.multiline.exec(token.source)|| re.singleline.exec(token.source) + )) { + blocks.push(match[1]); + tokens.push([]); + } else if (tokens.length) { + tokens[tokens.length - 1].push(token); + } + }); + /* + var blocks = Ox.map(Ox.tokenize(source), function(token) { + // filter out tokens that are not comments + // or don't match the doc comment pattern + var match; + token.source = source.substr(token.offset, token.length); + return token.type == 'comment' && (match = + re.multiline(token.source) || re.singleline(token.source) + ) ? match[1] : null; + }), + items = []; + */ + blocks.forEach(function(block, i) { + var item, lastItem, + lines = block + .replace(re.script, encodeLinebreaks) + .replace(re.test, encodeLinebreaks) + .split('\n'), + tree = parseTree(lines); + if (re.item.test(tree.line)) { + // parse the tree's root node + item = parseNode(tree); + item.file = file || ''; + if (section) { + item.section = section; + } + if (/^[A-Z]/.test(item.name)) { + // main item + // include leading whitespace + item.source = parseTokens(tokens[i]); + item.line = source.substr(0, item.source[0].offset) + .split('\n').length; + items.push(item); + } else { + // property of a function item + lastItem = items[items.length - 1]; + lastItem.properties = lastItem.properties || []; + lastItem.properties.push(item); + // include leading linebreaks and whitespace + lastItem.source = Ox.merge( + lastItem.source, parseTokens(tokens[i], true) + ); + } + } else { + section = tree.line.split(' ')[0] + } + }); + return items; + } function parseTest(str) { // fixme: we cannot properly handle tests where a string contains '\n ' var lines = decodeLinebreaks(str).split('\n '); @@ -232,73 +298,20 @@ Ox.doc = (function() { } return ret; } - return function(file, callback) { - Ox.get(file, function(source) { - var blocks = [], - items = [], - section = '', - tokens = []; - Ox.tokenize(source).forEach(function(token) { - var match; - token.source = source.substr(token.offset, token.length); - if (token.type == 'comment' && (match = - re.multiline.exec(token.source)|| re.singleline.exec(token.source) - )) { - blocks.push(match[1]); - tokens.push([]); - } else if (tokens.length) { - tokens[tokens.length - 1].push(token); - } + return function(/*source or file, callback*/) { + var callback, file, ret, source + if (arguments.length == 1) { + source = arguments[0] + ret = parseSource(source); + } else { + file = arguments[0]; + callback = arguments[1]; + Ox.get(file, function(source) { + callback(parseSource(source, file)); }); - /* - var blocks = Ox.map(Ox.tokenize(source), function(token) { - // filter out tokens that are not comments - // or don't match the doc comment pattern - var match; - token.source = source.substr(token.offset, token.length); - return token.type == 'comment' && (match = - re.multiline(token.source) || re.singleline(token.source) - ) ? match[1] : null; - }), - items = []; - */ - blocks.forEach(function(block, i) { - var item, lastItem, - lines = block - .replace(re.script, encodeLinebreaks) - .replace(re.test, encodeLinebreaks) - .split('\n'), - tree = parseTree(lines); - if (re.item.test(tree.line)) { - // parse the tree's root node - item = parseNode(tree); - item.file = file; - if (section) { - item.section = section; - } - if (/^[A-Z]/.test(item.name)) { - // main item - // include leading whitespace - item.source = parseTokens(tokens[i]); - item.line = source.substr(0, item.source[0].offset) - .split('\n').length; - items.push(item); - } else { - // property of a function item - lastItem = items[items.length - 1]; - lastItem.properties = lastItem.properties || []; - lastItem.properties.push(item); - // include leading linebreaks and whitespace - lastItem.source = Ox.merge( - lastItem.source, parseTokens(tokens[i], true) - ); - } - } else { - section = tree.line.split(' ')[0] - } - }); - callback(items); - }); + } + Ox.print('>>>>>>>', arguments, ret) + return ret; } }());