Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Fix ordering when mixing comment types
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott J. Miles committed Mar 29, 2014
1 parent 704610b commit b8d60bd
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions context-free-parser.html
Expand Up @@ -17,37 +17,33 @@
textChanged: function() {

if (!this.text) {
//console.log('textChanged:', this.text);
return;
}

var text = this.text;

var top = {};
var classes = [];
var current = top;
var subCurrent = {};

function makePragma(object, pragma, content) {
var p$ = object;
var p = p$[pragma];
if (!p) {
p$[pragma] = p = [];
}
p.push(content);
}

var js_matches = text.match(/\/\*\*([\s\S]*?)\*\//g) || [];
text = text.replace(/\/\*\*([\s\S]*?)\*\//g, '');
var scriptDocCommentClause = '\\/\\*\\*([\\s\\S]*?)\\*\\/';
var htmlDocCommentClause = '<!--([\\s\\S]*?)-->';

var html_matches = text.match(/<!--([\s\S]*?)-->/g) || [];
var matches = html_matches.concat(js_matches);
// matches text between /** and */ inclusive and <!-- and --> inclusive
var docCommentRegex = new RegExp(scriptDocCommentClause + '|' + htmlDocCommentClause, 'g');

matches.forEach(function(m) {
// acquire all script doc comments
var docComments = text.match(docCommentRegex) || [];

// each match represents a single block of doc comments
docComments.forEach(function(m) {
// unify line ends, remove all comment characters, split into individual lines
var lines = m.replace(/\r\n/g, '\n').replace(/^\s*\/\*\*|^\s*\*\/|^\s*\* ?|^\s*\<\!-\-|^s*\-\-\>/gm, '').split('\n');

// pragmas (@-rules) must occur on a line by themselves
var pragmas = [];
// filter lines whose first non-whitespace character is @ into the pragma list
// (and out of the `lines` array)
lines = lines.filter(function(l) {
var m = l.match(/\s*@([\w-]*) (.*)/);
if (!m) {
Expand All @@ -56,6 +52,7 @@
pragmas.push(m);
});

// collect all other text into a single block
var code = lines.join('\n');

pragmas.forEach(function(m) {
Expand Down Expand Up @@ -84,15 +81,24 @@
case 'default':
case 'type':
subCurrent[pragma] = content;
//makePragma(subCurrent, pragma, content);
break;

default:
current[pragma] = content;
//makePragma(current, pragma, content);
break;
}
});

// utility function, yay hoisting
function makePragma(object, pragma, content) {
var p$ = object;
var p = p$[pragma];
if (!p) {
p$[pragma] = p = [];
}
p.push(content);
}

});

if (classes.length === 0) {
Expand Down

0 comments on commit b8d60bd

Please sign in to comment.