Skip to content

Commit a1f72af

Browse files
committed
Commands are no longer case-insensitive
1 parent 48e3316 commit a1f72af

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

pseudocode.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
2-
* The entry point of pseudocode-js
2+
* The entry points of pseudocode-js
33
*
44
* TODO:
5-
* * demo
65
* * Support color
76
* * Case-insensitive
87
* * elsif

src/Lexer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,13 @@ Lexer.prototype._matchText = function(text) {
137137
// don't need to match
138138
if (text === null || text === undefined) return true;
139139

140+
// string comparisons are case-insensitive
140141
if (utils.isString(text)) // is a string, exactly the same?
141-
return text === this._nextAtom.text;
142-
else // is a list, match any of them?
143-
return text.indexOf(this._nextAtom.text) >= 0;
142+
return text.toLowerCase() === this._nextAtom.text.toLowerCase();
143+
else {// is a list, match any of them?
144+
text = text.map(function(str) { return str.toLowerCase(); });
145+
return text.indexOf(this._nextAtom.text.toLowerCase()) >= 0;
146+
}
144147
};
145148

146149
module.exports = Lexer;

src/Parser.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Parser.prototype._parseControl = function() {
254254

255255
Parser.prototype._parseFunction = function() {
256256
var lexer = this._lexer;
257-
if (!lexer.accept('func', ['FUNCTION', 'PROCEDURE'])) return null;
257+
if (!lexer.accept('func', ['function', 'procedure'])) return null;
258258

259259
// \FUNCTION{funcName}{funcArgs}
260260
var funcType = this._lexer.get().text; // FUNCTION or PROCEDURE
@@ -267,7 +267,7 @@ Parser.prototype._parseFunction = function() {
267267
// <block>
268268
var blockNode = this._parseBlock();
269269
// \ENDFUNCTION
270-
lexer.expect('func', 'END' + funcType);
270+
lexer.expect('func', 'end' + funcType);
271271

272272
var functionNode = new ParseNode('function',
273273
{type: funcType, name: funcName});
@@ -277,7 +277,7 @@ Parser.prototype._parseFunction = function() {
277277
};
278278

279279
Parser.prototype._parseIf = function() {
280-
if (!this._lexer.accept('func', 'IF')) return null;
280+
if (!this._lexer.accept('func', 'if')) return null;
281281

282282
var ifNode = new ParseNode('if');
283283

@@ -289,7 +289,7 @@ Parser.prototype._parseIf = function() {
289289

290290
// ( \ELIF { <cond> } <block> )[0...n]
291291
var numElif = 0;
292-
while (this._lexer.accept('func', 'ELIF')) {
292+
while (this._lexer.accept('func', ['elif', 'elsif', 'elseif'])) {
293293
this._lexer.expect('open');
294294
ifNode.addChild(this._parseCond());
295295
this._lexer.expect('close');
@@ -299,13 +299,13 @@ Parser.prototype._parseIf = function() {
299299

300300
// ( \ELSE <block> )[0..1]
301301
var hasElse = false;
302-
if (this._lexer.accept('func', 'ELSE')) {
302+
if (this._lexer.accept('func', 'else')) {
303303
hasElse = true;
304304
ifNode.addChild(this._parseBlock());
305305
}
306306

307307
// \ENDIF
308-
this._lexer.expect('func', 'ENDIF');
308+
this._lexer.expect('func', 'endif');
309309

310310
ifNode.value = {numElif: numElif, hasElse: hasElse};
311311
return ifNode;
@@ -314,7 +314,7 @@ Parser.prototype._parseIf = function() {
314314
Parser.prototype._parseLoop = function() {
315315
if (!this._lexer.accept('func', ['FOR', 'FORALL', 'WHILE'])) return null;
316316

317-
var loopName = this._lexer.get().text;
317+
var loopName = this._lexer.get().text.toLowerCase();
318318
var loopNode = new ParseNode('loop', loopName);
319319

320320
// { <cond> } <block>
@@ -324,25 +324,25 @@ Parser.prototype._parseLoop = function() {
324324
loopNode.addChild(this._parseBlock());
325325

326326
// \ENDFOR
327-
var endLoop = loopName !== 'FORALL' ? 'END' + loopName : 'ENDFOR';
327+
var endLoop = loopName !== 'forall' ? 'end' + loopName : 'endfor';
328328
this._lexer.expect('func', endLoop);
329329

330330
return loopNode;
331331
};
332332

333-
var INPUTS_OUTPUTS_COMMANDS = ['ENSURE', 'REQUIRE'];
334-
var STATEMENT_COMMANDS = ['STATE', 'PRINT', 'RETURN'];
333+
var INPUTS_OUTPUTS_COMMANDS = ['ensure', 'require'];
334+
var STATEMENT_COMMANDS = ['state', 'print', 'return'];
335335
Parser.prototype._parseCommand = function(acceptCommands) {
336336
if (!this._lexer.accept('func', acceptCommands)) return null;
337337

338-
var cmdName = this._lexer.get().text;
338+
var cmdName = this._lexer.get().text.toLowerCase();
339339
var cmdNode = new ParseNode('command', cmdName);
340340
cmdNode.addChild(this._parseOpenText());
341341
return cmdNode;
342342
};
343343

344344
Parser.prototype._parseComment = function() {
345-
if (!this._lexer.accept('func', 'COMMENT')) return null;
345+
if (!this._lexer.accept('func', 'comment')) return null;
346346

347347
var commentNode = new ParseNode('comment');
348348

@@ -356,7 +356,7 @@ Parser.prototype._parseComment = function() {
356356

357357
Parser.prototype._parseCall = function() {
358358
var lexer = this._lexer;
359-
if (!lexer.accept('func', 'CALL')) return null;
359+
if (!lexer.accept('func', 'call')) return null;
360360

361361
var anyWhitespace = lexer.get().whitespace;
362362

@@ -424,7 +424,7 @@ var ACCEPTED_TOKEN_BY_ATOM = {
424424
'special': { tokenType: 'special' },
425425
'cond-symbol': {
426426
tokenType: 'func',
427-
tokenValues: ['AND', 'OR', 'NOT', 'TRUE', 'FALSE', 'TO']
427+
tokenValues: ['and', 'or', 'not', 'true', 'false', 'to']
428428
},
429429
'quote-symbol': {
430430
tokenType: 'quote'
@@ -461,7 +461,7 @@ Parser.prototype._parseAtom = function() {
461461
if (tokenText === null) continue;
462462

463463
var anyWhitespace = this._lexer.get().whitespace;
464-
return new AtomNode(atomType, tokenText, anyWhitespace);
464+
return new AtomNode(atomType, tokenText.toLowerCase(), anyWhitespace);
465465
}
466466
return null;
467467
};

src/Renderer.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ Renderer.prototype._newLine = function() {
503503
this._html.beginSpan('ps-linenum', {
504504
'left': - ((this._blockLevel - 1)*(indentSize* 1.25)) + 'em'
505505
})
506-
.putText(this._numLOC + this._options.lineNumberPunc + ' ')
506+
.putText(this._numLOC + this._options.lineNumberPunc)
507507
.endSpan();
508508
}
509509
}
@@ -706,9 +706,9 @@ Renderer.prototype._buildTree = function(node) {
706706
this._newLine();
707707
var loopType = node.value;
708708
var displayLoopName = {
709-
'FOR': 'for',
710-
'FORALL': 'for all',
711-
'WHILE': 'while'
709+
'for': 'for',
710+
'forall': 'for all',
711+
'while': 'while'
712712
};
713713
this._typeKeyword(displayLoopName[loopType] + ' ');
714714
var loopCond = node.children[0];
@@ -736,11 +736,11 @@ Renderer.prototype._buildTree = function(node) {
736736
// commands: \STATE, \ENSURE, \PRINT, \RETURN, etc.
737737
var cmdName = node.value;
738738
var displayName = {
739-
'STATE': '',
740-
'ENSURE': 'Ensure: ',
741-
'REQUIRE': 'Require: ',
742-
'PRINT': 'print ',
743-
'RETURN': 'return '
739+
'state': '',
740+
'ensure': 'Ensure: ',
741+
'require': 'Require: ',
742+
'print': 'print ',
743+
'return': 'return '
744744
}[cmdName];
745745

746746
this._newLine();

static/pseudocode.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
text-align: right;
5151
display: inline-block;
5252
position: relative;
53+
padding-right: 0.3em;
5354
}
5455
.ps-root .ps-algorithmic.with-linenum .ps-line.ps-code {
5556
text-indent: -1.6em;

0 commit comments

Comments
 (0)