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

Add support for multline #48

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
lib/parser.js

1 change: 1 addition & 0 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ var Tex = module.exports.Tex = new Enum( 'Tex', {
FUN2sq: { args: [ 'string', 'self', 'self' ] },
FUN3: { args: [ 'string', 'self', 'self', 'self'] },
MATRIX: { args: [ 'string', [ [ [ 'self' ] ] ] ] },
MULTLINE: { args: [ 'string', [ [ [ 'self' ] ] ] ] },
DECLh: { args: [ 'string', FontForce, [ 'self' ] ] },
JACOBI: { args: [ 'self', 'self', 'self', 'self']},
LAGUERRE1: { args:['self', 'self']},
Expand Down
25 changes: 14 additions & 11 deletions lib/astutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ ast.RenderT.prototype.tex_contains_func = function(target) {
return t.charAt(0) === '\\' && match(target, t);
};

var matrixFunction = function(target, t, m) {
// \begin{env} .. & .. \\ .. & .. \\ .. & .. \end{env}
// t is the environment name.
// m is a doubly-nested array
var expr_has = function(e) { return arr_contains_func(e, target); };
var line_has = function(l) { return some(l, expr_has); };
var matrix_has = function(m) { return some(m, line_has); };
return match(target, '\\begin{'+t+'}') ||
match(target, '\\end{'+t+'}') ||
matrix_has(m);
};

// This defines a function of one argument, which becomes the first argument
// in the visitor functions. The subsequent arguments in the definition
// are the fields of that particular AST class.
Expand Down Expand Up @@ -185,17 +197,8 @@ ast.Tex.defineVisitor("contains_func", {
// \big\d where big is \big, \Big, \bigg, \Bigg, \biggl, etc
return match(target, big) || d.tex_contains_func(target);
},
MATRIX: function(target, t, m) {
// \begin{env} .. & .. \\ .. & .. \\ .. & .. \end{env}
// t is the environment name.
// m is a doubly-nested array
var expr_has = function(e) { return arr_contains_func(e, target); };
var line_has = function(l) { return some(l, expr_has); };
var matrix_has = function(m) { return some(m, line_has); };
return match(target, '\\begin{'+t+'}') ||
match(target, '\\end{'+t+'}') ||
matrix_has(m);
},
MATRIX: matrixFunction,
MULTLINE: matrixFunction,
LR: function(target, l, r, tl) {
// \left\l tl1 tl2 tl3 ... \right\r (a balanced pair of delimiters)
return match(target, '\\left') || match(target, '\\right') ||
Expand Down
11 changes: 2 additions & 9 deletions lib/build-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ var buildParser = module.exports = function(inFile, outFile) {
var PEG = require('pegjs');
var fs = require('fs');

var parserSource = PEG.buildParser(fs.readFileSync(inFile, 'utf8'), {
var parserSource = PEG.generate(fs.readFileSync(inFile, 'utf8'), {
Copy link
Member

Choose a reason for hiding this comment

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

can you explain, what this change means

Copy link
Author

Choose a reason for hiding this comment

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

In the newest version of pegJS (0.10.0), pegjs.buildParser was changed to pegjs.generate.

/* PEGJS options */
output: "source",
cache: true,// makes repeated calls to generic_func production efficient
allowedStartTules: [ "start" ]
allowedStartRules: [ "start" ]
});
// hack up the source to make it pass jshint
parserSource = parserSource
.replace(/(peg\$subclass\(child, parent\)|peg\$SyntaxError\(message, expected, found, location\)|peg\$parse\(input\)) {/g,
function (m) {
return m + "\n /*jshint validthis:true, newcap:false*/ ";
}).replace(/\n(\s+)([?:+]) (expectedDescs|" or "|peg)/g, ' $2\n$1$3');
parserSource =
'/* jshint latedef: false */\n' +
'module.exports = ' + parserSource + ';';

fs.writeFileSync(outFile, parserSource, 'utf8');
Expand Down
Loading