Skip to content

Commit a111071

Browse files
heydariplusplusSaswatPadhi
authored andcommitted
adding upon
1 parent 79da579 commit a111071

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Parser.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* <block> :== ( <comment> | <command> | <control> | <function> |
2424
* <statement> )[0..n]
2525
*
26-
* <control> :== <if> | <for> | <while> | <repeat>
26+
* <control> :== <if> | <for> | <while> | <repeat> | <upon>
2727
* <if> :== \IF{<cond>} <block>
2828
* ( \ELIF{<cond>} <block> )[0..n]
2929
* ( \ELSE <block> )[0..1]
@@ -32,6 +32,7 @@
3232
* <for> :== \FOR{<cond>} <block> \ENDFOR
3333
* <while> :== \WHILE{<cond>} <block> \ENDWHILE
3434
* <repeat> :== \REPEAT <block> \UNTIL{<cond>}
35+
* <upon> :== \UPON{<cond>} <block> \EDNUPON
3536
*
3637
* <function> :== \FUNCTION{<name>}{<params>} <block> \ENDFUNCTION
3738
* (same for <procedure>)
@@ -261,6 +262,7 @@ Parser.prototype._parseControl = function() {
261262
if ((controlNode = this._parseIf())) return controlNode;
262263
if ((controlNode = this._parseLoop())) return controlNode;
263264
if ((controlNode = this._parseRepeat())) return controlNode;
265+
if ((controlNode = this._parseUpon())) return controlNode;
264266
};
265267

266268
Parser.prototype._parseFunction = function() {
@@ -361,6 +363,23 @@ Parser.prototype._parseRepeat = function() {
361363
return repeatNode;
362364
};
363365

366+
Parser.prototype._parseUpon = function() {
367+
if (!this._lexer.accept('func', 'upon')) return null;
368+
369+
var uponNode = new ParseNode('upon');
370+
371+
// { <cond> } <block>
372+
this._lexer.expect('open');
373+
uponNode.addChild(this._parseCond());
374+
this._lexer.expect('close');
375+
uponNode.addChild(this._parseBlock());
376+
377+
// \ENDUPON
378+
this._lexer.expect('func', 'endupon');
379+
380+
return uponNode;
381+
};
382+
364383
var IO_STATEMENTS = ['ensure', 'require', 'input', 'output'];
365384
var STATEMENTS = ['state', 'print', 'return'];
366385
Parser.prototype._parseStatement = function(acceptStatements) {

src/Renderer.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ Renderer.prototype._buildTree = function(node) {
785785
this._buildCommentsFromBlock(repeatBlock);
786786
this._buildTree(repeatBlock);
787787

788-
788+
789789
// \UNTIL{<cond>}
790790
// ==>
791791
// <p class="ps-line">
@@ -795,7 +795,28 @@ Renderer.prototype._buildTree = function(node) {
795795
this._typeKeyword('until ');
796796
var repeatCond = node.children[1];
797797
this._buildTree(repeatCond);
798-
798+
799+
break;
800+
case 'upon':
801+
// \UPON { <cond> }
802+
// ==>
803+
// <p class="ps-line">
804+
// <span class="ps-keyword">upon</span>
805+
// </p>
806+
this._newLine();
807+
this._typeKeyword('upon ');
808+
uponCond = node.children[0];
809+
this._buildTree(uponCond);
810+
// <block>
811+
var uponBlock = node.children[1];
812+
this._buildCommentsFromBlock(uponBlock);
813+
this._buildTree(uponBlock);
814+
815+
if (!this._options.noEnd) {
816+
// ENDUPON
817+
this._newLine();
818+
this._typeKeyword('end upon');
819+
}
799820
break;
800821
// ------------------- Lines -------------------
801822
case 'command':

0 commit comments

Comments
 (0)