From bdad2cc462a4e5a9bcaf1da95d96b6317af00e2b Mon Sep 17 00:00:00 2001
From: "Tate, Hongliang Tian" <tatetian@gmail.com>
Date: Wed, 11 Mar 2015 11:10:08 +0800
Subject: [PATCH] Add test for comments

---
 PseudoCode.js          |  9 +++++++++
 src/Parser.js          |  4 ++--
 src/Renderer.js        | 15 +++++++++++++++
 static/test-suite.html | 20 +++++++++++++++-----
 4 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/PseudoCode.js b/PseudoCode.js
index 7ab5fbf..cb1838a 100644
--- a/PseudoCode.js
+++ b/PseudoCode.js
@@ -1,3 +1,12 @@
+/*
+ * The entry point of pseudocode-js
+ *
+ * TODO:
+ *      * demo
+ *      * Support color
+ *      * Support TeX comment
+ **/
+
 var ParseError = require('./src/ParseError');
 var Lexer = require('./src/Lexer');
 var Parser = require('./src/Parser');
diff --git a/src/Parser.js b/src/Parser.js
index fab1a1b..b755da1 100644
--- a/src/Parser.js
+++ b/src/Parser.js
@@ -34,14 +34,14 @@
  *     <function>      :== \FUNCTION{<name>}{<params>} <block> \ENDFUNCTION
  *                         (same for <procedure>)
  *
- *     <statement>     :== <state> |  <return> | <print>
+ *     <statement>     :== <state> | <return> | <print>
  *     <state>         :== \STATE + <open-text>
  *     <return>        :== \RETURN + <open-text>
  *     <print>         :== \PRINT + <open-text>
  *
  *     <comment>       :== \COMMENT{<close-text>}
  *
- *     <call>          :== \CALL{<name>}({<close-text>})[0..1]
+ *     <call>          :== \CALL{<name>}({<close-text>})
  *
  *     <cond>          :== <close-text>
  *     <open-text>     :== <atom> + <open-text> | { <close-text> } | <empty>
diff --git a/src/Renderer.js b/src/Renderer.js
index 1abbbc1..e49a488 100644
--- a/src/Renderer.js
+++ b/src/Renderer.js
@@ -518,6 +518,16 @@ Renderer.prototype._buildTreeForAllChildren = function(node) {
         this._buildTree(children[ci]);
 };
 
+// The comment nodes at the beginning of blockNode are comments for controls
+// Thus they should be rendered out of block
+Renderer.prototype._buildCommentsFromBlock = function(blockNode) {
+    var children = blockNode.children;
+    while (children.length > 0 && children[0].type === 'comment') {
+        var commentNode = children.shift();
+        this._buildTree(commentNode);
+    }
+}
+
 Renderer.prototype._buildTree = function(node) {
     var ci, child, textNode;
     switch(node.type) {
@@ -590,6 +600,7 @@ Renderer.prototype._buildTree = function(node) {
         this._buildTree(textNode);
         this._typeText(')');
 
+        this._buildCommentsFromBlock(blockNode);
         this._buildTree(blockNode);
 
         if (!this._options.noEnd) {
@@ -612,6 +623,7 @@ Renderer.prototype._buildTree = function(node) {
         this._typeKeyword(' then');
         // <block>
         var ifBlock = node.children[1];
+        this._buildCommentsFromBlock(ifBlock);
         this._buildTree(ifBlock);
 
         // ( \ELIF {<cond>} <block> )[0..n]
@@ -632,6 +644,7 @@ Renderer.prototype._buildTree = function(node) {
 
             // <block>
             var elifBlock = node.children[2 + 2 * ei + 1];
+            this._buildCommentsFromBlock(elifBlock);
             this._buildTree(elifBlock);
         }
 
@@ -648,6 +661,7 @@ Renderer.prototype._buildTree = function(node) {
 
             // <block>
             var elseBlock = node.children[node.children.length - 1];
+            this._buildCommentsFromBlock(elseBlock);
             this._buildTree(elseBlock);
         }
 
@@ -679,6 +693,7 @@ Renderer.prototype._buildTree = function(node) {
 
         // <block>
         var block = node.children[1];
+        this._buildCommentsFromBlock(block);
         this._buildTree(block);
 
         if (!this._options.noEnd) {
diff --git a/static/test-suite.html b/static/test-suite.html
index a67887c..109342b 100644
--- a/static/test-suite.html
+++ b/static/test-suite.html
@@ -85,12 +85,22 @@
         \end{algorithmic}
         \end{algorithm}
         \begin{algorithm}
-        \caption{Test statements}
+        \caption{Test statements and comments}
         \begin{algorithmic}
-        \STATE this is a normal statement
-        \PRINT \texttt{this is print statement}
-        \RETURN $retval$
-        
+        \PROCEDURE{Test-Statements}{}
+            \STATE this is a normal statement
+            \PRINT \texttt{this is print statement}
+            \RETURN $retval$        
+        \ENDPROCEDURE
+        \PROCEDURE{Test-Comments}{} \COMMENT{comment for procedure}
+            \STATE a statement \COMMENT{inline comment}
+            \STATE \COMMENT{line comment}
+            \IF{some condition}\COMMENT{comment for if}
+                \RETURN \TRUE \COMMENT{another inline comment}
+            \ELSE \COMMENT{comment for else}
+                \RETURN \FALSE \COMMENT{yet another inline comment}
+            \ENDIF
+        \ENDPROCEDURE
         \end{algorithmic}
         \end{algorithm}
     </pre>
-- 
GitLab