Skip to content

Commit e45b23f

Browse files
committed
Move call into open/close text
1 parent 928d10e commit e45b23f

File tree

3 files changed

+38
-46
lines changed

3 files changed

+38
-46
lines changed

src/Parser.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
* <require> :== \REQUIRE + <open-text>
2121
* <ensure> :== \ENSURE + <open-text>
2222
*
23-
* <block> :== ( <control> | <function>
24-
* | <statement> | <comment> | <call> )[0..n]
23+
* <block> :== ( <control> | <function> | <statement> |
24+
* <comment> )[0..n]
2525
*
2626
* <control> :== <if> | <for> | <while>
2727
* <if> :== \IF{<cond>} + <block>
@@ -41,16 +41,17 @@
4141
*
4242
* <comment> :== \COMMENT{<close-text>}
4343
*
44-
* <call> :== \CALL{<name>}({<close-text>})
45-
*
4644
* <cond> :== <close-text>
47-
* <open-text> :== <atom> + <open-text> | { <close-text> } | <empty>
48-
* <close-text> :== <atom> + <close-text> | { <close-text> } | <empty>
45+
* <open-text> :== ( <atom> | <call> ) + <open-text> |
46+
* { <close-text> } | <empty>
47+
* <close-text> :== ( <atom> | <call> ) + <close-text> |
48+
* { <close-text> } | <empty>
4949
*
5050
* <atom> :== <ordinary>[1..n] | <special> | <symbol>
5151
* | <size> | <font> | <bool> | <math>
5252
* <name> :== <ordinary>
5353
*
54+
* <call> :== \CALL{<name>}({<close-text>})
5455
* <special> :== \\ | \{ | \} | \$ | \& | \# | \% | \_
5556
* <cond-symbol> :== \AND | \OR | \NOT | \TRUE | \FALSE | \TO
5657
* <text-symbol> :== \textbackslash
@@ -239,9 +240,6 @@ Parser.prototype._parseBlock = function() {
239240
var commentNode = this._parseComment();
240241
if (commentNode) { blockNode.addChild(commentNode); continue; }
241242

242-
var callNode = this._parseCall();
243-
if (callNode) { blockNode.addChild(callNode); continue; }
244-
245243
break;
246244
}
247245

@@ -390,17 +388,19 @@ Parser.prototype._parseText = function(openOrClose) {
390388
var textNode = new ParseNode(openOrClose + '-text');
391389
// any whitespace between Atom and CloseText
392390
var anyWhitespace = false;
393-
var atomNode;
391+
var subTextNode;
394392
while (true) {
395-
atomNode = this._parseAtom();
396-
if (atomNode) {
397-
if (anyWhitespace) atomNode.whitespace |= anyWhitespace;
398-
textNode.addChild(atomNode);
393+
// atom or call
394+
subTextNode = this._parseAtom() || this._parseCall();
395+
if (subTextNode) {
396+
if (anyWhitespace) subTextNode.whitespace |= anyWhitespace;
397+
textNode.addChild(subTextNode);
399398
continue;
400399
}
401400

401+
// or close text
402402
if (this._lexer.accept('open')) {
403-
var subTextNode = this._parseCloseText();
403+
subTextNode = this._parseCloseText();
404404

405405
anyWhitespace = this._lexer.get().whitespace;
406406
subTextNode.whitespace = anyWhitespace;

src/Renderer.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ function TextEnvironment(nodes, textStyle) {
139139
this._textStyle = textStyle;
140140
}
141141

142+
TextEnvironment.prototype._renderCloseText = function(node) {
143+
var newTextStyle = new TextStyle(this._textStyle.fontSize());
144+
var closeTextEnv = new TextEnvironment(
145+
node.children, newTextStyle);
146+
if (node.whitespace) this._html.putText(' ');
147+
this._html.putSpan(closeTextEnv.renderToHTML());
148+
}
149+
142150
TextEnvironment.prototype.renderToHTML = function() {
143151
this._html = new HTMLBuilder();
144152

@@ -197,12 +205,18 @@ TextEnvironment.prototype.renderToHTML = function() {
197205
var realQuote = quoteReplace[text];
198206
this._html.putText(realQuote);
199207
break;
208+
case 'call':
209+
// \CALL{funcName}{funcArgs}
210+
// ==>
211+
// funcName(funcArgs)
212+
this._html.beginSpan('ps-funcname').putText(text).endSpan()
213+
this._html.write('(');
214+
var argsTextNode = node.children[0];
215+
this._renderCloseText(argsTextNode);
216+
this._html.write(')');
217+
break;
200218
case 'close-text':
201-
var newTextStyle = new TextStyle(this._textStyle.fontSize());
202-
var closeTextEnv = new TextEnvironment(
203-
node.children, newTextStyle);
204-
if (node.whitespace) this._html.putText(' ');
205-
this._html.putSpan(closeTextEnv.renderToHTML());
219+
this._renderCloseText(node);
206220
break;
207221
// There are two kinds of typestyle commands:
208222
// command (e.g. \textrm{...}).
@@ -721,10 +735,10 @@ Renderer.prototype._buildTree = function(node) {
721735
var cmdName = node.value;
722736
var displayName = {
723737
'STATE': '',
724-
'ENSURE': 'Ensure:',
725-
'REQUIRE': 'Require:',
726-
'PRINT': 'print',
727-
'RETURN': 'return'
738+
'ENSURE': 'Ensure: ',
739+
'REQUIRE': 'Require: ',
740+
'PRINT': 'print ',
741+
'RETURN': 'return '
728742
}[cmdName];
729743

730744
this._newLine();
@@ -745,18 +759,6 @@ Renderer.prototype._buildTree = function(node) {
745759
this._buildTree(textNode);
746760
this._html.endSpan();
747761
break;
748-
case 'call':
749-
// \CALL{funcName}{funcArgs}
750-
// ==>
751-
// funcName(funcArgs)
752-
var callFuncName = node.value;
753-
var argsNode = node.children[0];
754-
if (node.whitespace) this._typeText(' ');
755-
this._typeFuncName(callFuncName);
756-
this._typeText('(');
757-
this._buildTree(argsNode);
758-
this._typeText(')');
759-
break;
760762
// ------------------- Text -------------------
761763
case 'open-text':
762764
var openTextEnv = new TextEnvironment(node.children, this._globalTextStyle);

static/test-suite.html

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
<title>Test suite of PseudoCode.js</title>
88
</head>
99
<body>
10-
<pre id="test0" style="display:none">
11-
\begin{algorithmic}
12-
\STATE \tiny tiny \normalsize normalsize
13-
\STATE font sizings: \tiny tiny \scriptsize scriptsize \footnotesize
14-
footnotesize \small small \normalsize normal \large large \Large Large
15-
\LARGE LARGE \huge huge \Huge Huge \normalsize
16-
\STATE should be normal size
17-
\end{algorithmic}
18-
</pre>
1910
<pre id="test-basics" style="display:none">
2011
\begin{algorithm}
2112
\caption{Test text-style}
@@ -120,7 +111,6 @@
120111
\ENDIF
121112
\ENDPROCEDURE
122113
\PROCEDURE{Partition}{$A, p, r$}
123-
\STATE someting
124114
\STATE $x = A[r]$
125115
\STATE $i = p - 1$
126116
\FOR{$j = p$ \TO $r - 1$}

0 commit comments

Comments
 (0)