Skip to content

Commit bdad2cc

Browse files
committed
Add test for comments
1 parent ad0f45b commit bdad2cc

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

PseudoCode.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* The entry point of pseudocode-js
3+
*
4+
* TODO:
5+
* * demo
6+
* * Support color
7+
* * Support TeX comment
8+
**/
9+
110
var ParseError = require('./src/ParseError');
211
var Lexer = require('./src/Lexer');
312
var Parser = require('./src/Parser');

src/Parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@
3434
* <function> :== \FUNCTION{<name>}{<params>} <block> \ENDFUNCTION
3535
* (same for <procedure>)
3636
*
37-
* <statement> :== <state> | <return> | <print>
37+
* <statement> :== <state> | <return> | <print>
3838
* <state> :== \STATE + <open-text>
3939
* <return> :== \RETURN + <open-text>
4040
* <print> :== \PRINT + <open-text>
4141
*
4242
* <comment> :== \COMMENT{<close-text>}
4343
*
44-
* <call> :== \CALL{<name>}({<close-text>})[0..1]
44+
* <call> :== \CALL{<name>}({<close-text>})
4545
*
4646
* <cond> :== <close-text>
4747
* <open-text> :== <atom> + <open-text> | { <close-text> } | <empty>

src/Renderer.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,16 @@ Renderer.prototype._buildTreeForAllChildren = function(node) {
518518
this._buildTree(children[ci]);
519519
};
520520

521+
// The comment nodes at the beginning of blockNode are comments for controls
522+
// Thus they should be rendered out of block
523+
Renderer.prototype._buildCommentsFromBlock = function(blockNode) {
524+
var children = blockNode.children;
525+
while (children.length > 0 && children[0].type === 'comment') {
526+
var commentNode = children.shift();
527+
this._buildTree(commentNode);
528+
}
529+
}
530+
521531
Renderer.prototype._buildTree = function(node) {
522532
var ci, child, textNode;
523533
switch(node.type) {
@@ -590,6 +600,7 @@ Renderer.prototype._buildTree = function(node) {
590600
this._buildTree(textNode);
591601
this._typeText(')');
592602

603+
this._buildCommentsFromBlock(blockNode);
593604
this._buildTree(blockNode);
594605

595606
if (!this._options.noEnd) {
@@ -612,6 +623,7 @@ Renderer.prototype._buildTree = function(node) {
612623
this._typeKeyword(' then');
613624
// <block>
614625
var ifBlock = node.children[1];
626+
this._buildCommentsFromBlock(ifBlock);
615627
this._buildTree(ifBlock);
616628

617629
// ( \ELIF {<cond>} <block> )[0..n]
@@ -632,6 +644,7 @@ Renderer.prototype._buildTree = function(node) {
632644

633645
// <block>
634646
var elifBlock = node.children[2 + 2 * ei + 1];
647+
this._buildCommentsFromBlock(elifBlock);
635648
this._buildTree(elifBlock);
636649
}
637650

@@ -648,6 +661,7 @@ Renderer.prototype._buildTree = function(node) {
648661

649662
// <block>
650663
var elseBlock = node.children[node.children.length - 1];
664+
this._buildCommentsFromBlock(elseBlock);
651665
this._buildTree(elseBlock);
652666
}
653667

@@ -679,6 +693,7 @@ Renderer.prototype._buildTree = function(node) {
679693

680694
// <block>
681695
var block = node.children[1];
696+
this._buildCommentsFromBlock(block);
682697
this._buildTree(block);
683698

684699
if (!this._options.noEnd) {

static/test-suite.html

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,22 @@
8585
\end{algorithmic}
8686
\end{algorithm}
8787
\begin{algorithm}
88-
\caption{Test statements}
88+
\caption{Test statements and comments}
8989
\begin{algorithmic}
90-
\STATE this is a normal statement
91-
\PRINT \texttt{this is print statement}
92-
\RETURN $retval$
93-
90+
\PROCEDURE{Test-Statements}{}
91+
\STATE this is a normal statement
92+
\PRINT \texttt{this is print statement}
93+
\RETURN $retval$
94+
\ENDPROCEDURE
95+
\PROCEDURE{Test-Comments}{} \COMMENT{comment for procedure}
96+
\STATE a statement \COMMENT{inline comment}
97+
\STATE \COMMENT{line comment}
98+
\IF{some condition}\COMMENT{comment for if}
99+
\RETURN \TRUE \COMMENT{another inline comment}
100+
\ELSE \COMMENT{comment for else}
101+
\RETURN \FALSE \COMMENT{yet another inline comment}
102+
\ENDIF
103+
\ENDPROCEDURE
94104
\end{algorithmic}
95105
\end{algorithm}
96106
</pre>

0 commit comments

Comments
 (0)