Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hand-written parser #22

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
indent_style = space
indent_size = 4
indent_size = 2

# Input/output is 2-space indent
[test/cases/*]
Expand Down
3 changes: 2 additions & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"preset": "crockford",
"requireMultipleVarDecl": null,
"requireCamelCaseOrUpperCaseIdentifiers": "ignoreProperties",
"disallowDanglingUnderscores": null
"disallowDanglingUnderscores": null,
"validateIndentation": 2
}
147 changes: 0 additions & 147 deletions lib/actions.js

This file was deleted.

20 changes: 10 additions & 10 deletions lib/beautify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ var beautifyWithBugs = require('js-beautify').html;
var inlineElements = require('inline-elements');

module.exports = function beautify(html) {
var originalOutput = beautifyWithBugs(html, {
indent_size: 2,
wrap_line_length: 0,
unformatted: ['emu-const', 'emu-val', 'emu-nt'].concat(inlineElements)
});
var originalOutput = beautifyWithBugs(html, {
indent_size: 2,
wrap_line_length: 0,
unformatted: ['emu-const', 'emu-val', 'emu-nt'].concat(inlineElements)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess just add 'ins' and 'del' here.

});

// https://github.com/beautify-web/js-beautify/issues/524#issuecomment-82791022
var fixNewlines = originalOutput.replace(/(<\/emu-[^>]+>)\n *<\/li>/g, '$1</li>');
// https://github.com/beautify-web/js-beautify/issues/524#issuecomment-82791022
var fixNewlines = originalOutput.replace(/(<\/emu-[^>]+>)\n *<\/li>/g, '$1</li>');

// Remove empty =""s
var withSimplifiedAttributes = fixNewlines.replace(/=""/g, '');
// Remove empty =""s
var withSimplifiedAttributes = fixNewlines.replace(/=""/g, '');

return withSimplifiedAttributes;
return withSimplifiedAttributes;
};
11 changes: 7 additions & 4 deletions lib/ecmarkdown.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';
var parser = require('./generated-parser.js');

var escapeHtml = require('escape-html');
var beautify = require('./beautify.js');
var Emitter = require('./emitter.js');
var parse = require('./parser');

exports.list = function (ecmarkdown) {
return parser.parse(ecmarkdown.trim() + '\n', { startRule: 'list' });
exports.document = function (ecmarkdown) {
return beautify(Emitter.document(parse(ecmarkdown)));
};

exports.fragment = function (ecmarkdown) {
return parser.parse(escapeHtml(ecmarkdown.trim()), { startRule: 'fragment' });
return beautify(Emitter.fragment(parse(escapeHtml(ecmarkdown))));
};
71 changes: 71 additions & 0 deletions lib/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

exports.document = function (doc) {
return doc.map(emit).join('');
};

exports.fragment = function (doc) {
if (doc.length === 0) {
return '';
}

return doc[0].contents.map(emit).join('');
};

function emit(node) {
var str = '';

if (node.name === 'list') {
if (node.start === '1') {
str += '<ol>';
} else {
str += '<ol start="' + node.start + '">';
}
str += node.contents.map(function (item) {
return '<li>' + item.contents.map(emit).join('') + '</li>';
}).join('');
str += '</ol>';
} else if (node.name === 'chars') {
// trim any trailing line breaks and indents
str += node.contents.replace(/\n+\s*$/, '');
} else if (node.name === 'pipe') {
str += '<emu-nt';

if (node.params) {
str += ' params="' + node.params + '"';
}

if (node.optional) {
str += ' optional';
}

str += '>' + node.nonTerminal + '</emu-nt>';
} else if (node.name === 'non-list') {
str += '<p>';
str += node.contents.map(function (item) {
return emit(item);
}).join('');
str += '</p>';
} else if (node.name === 'comment' || node.name === 'tag') {
str += node.contents;
} else {
str += wrap(node);
}

return str;
}

var wrappers = {
star: 'emu-val',
underscore: 'var',
tick: 'code',
string: 'code',
tilde: 'emu-const'
};

function wrap(atom) {
var wrapping = wrappers[atom.name];

return '<' + wrapping + '>' + atom.contents.map(emit).join('') + '</' + wrapping + '>';
}

55 changes: 0 additions & 55 deletions lib/grammar.pegjs

This file was deleted.

Loading