r107458 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107457‎ | r107458 | r107459 >
Date:01:36, 28 December 2011
Author:neilk
Status:ok (Comments)
Tags:
Comment:
create tokenizer without need to modify namespace with PEG source
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/parser/parserTests.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/parser/parserTests.js
@@ -7,11 +7,13 @@
88 *
99 * Needs smarter compare, as well as search-y helpers.
1010 *
11 - * 2011-07-20 <brion@pobox.com>
 11+ * @author Brion Vibber <brion@pobox.com>
 12+ * @author Gabriel Wicke <gwicke@wikimedia.org>
 13+ * @author Neil Kandalgaonkar <neilk@wikimedia.org>
1214 */
1315
1416 (function() {
15 -//"use strict";
 17+
1618 console.log( "Starting up JS parser tests" );
1719
1820 var fs = require('fs'),
@@ -161,10 +163,16 @@
162164 this.cache_file = "parserTests.cache";
163165
164166 // Preload the grammar file...
165 - PegTokenizer.src = fs.readFileSync(path.join(basePath, 'parser', 'pegTokenizer.pegjs.txt'), 'utf8');
 167+ var peg = fs.readFileSync(path.join(basePath, 'parser', 'pegTokenizer.pegjs.txt'), 'utf8');
 168+ var parserEnv = {};
 169+ //var parserEnv = new MWParserEnvironment({
 170+ // tagHooks: {
 171+ // 'ref': MWRefTagHook,
 172+ // 'references': MWReferencesTagHook
 173+ // }
 174+ //});
 175+ this.wikiTokenizer = new PegTokenizer(parserEnv, peg);
166176
167 - this.wikiTokenizer = new PegTokenizer();
168 -
169177 this.testFileName = '../../../../phase3/tests/parser/parserTests.txt'; // default
170178 this.testFileName2 = '../../../../tests/parser/parserTests.txt'; // Fallback. Not everyone fetch at phase3 level
171179
@@ -439,13 +447,7 @@
440448 this.failParseTests++;
441449 console.log('PARSE FAIL', res.err);
442450 } else {
443 - //var environment = new MWParserEnvironment({
444 - // tagHooks: {
445 - // 'ref': MWRefTagHook,
446 - // 'references': MWReferencesTagHook
447 - // }
448 - //});
449 - //var res = es.HtmlSerializer.stringify(tokens,environment);
 451+ //var res = es.HtmlSerializer.stringify(tokens,environment);
450452
451453 //Slightly better token output debugging:
452454 //console.log( util.inspect( res.tokens, false, null ).yellow);
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js
@@ -8,8 +8,9 @@
99
1010 var PEG = require('pegjs');
1111
12 -function PegTokenizer(env) {
 12+function PegTokenizer(env, src) {
1313 this.env = env || {};
 14+ this.src = src;
1415 }
1516
1617 PegTokenizer.src = false;
@@ -17,7 +18,7 @@
1819 PegTokenizer.prototype.tokenize = function( text ) {
1920 var out, err;
2021 if ( !this.parser ) {
21 - this.parser = PEG.buildParser(PegTokenizer.src);
 22+ this.parser = PEG.buildParser(this.src);
2223 }
2324 try {
2425 out = this.parser.parse(text);

Comments

#Comment by GWicke (talk | contribs)   16:01, 28 December 2011

The tokenizer is pretty stateless and does not depend on an environment any more, so I think we could remove the arguments from the constructor and load the source directly in the tokenizer constructor.

Status & tagging log