Skip to content

Commit f9f8792

Browse files
committed
Math environment \( ... \)
1 parent 2b67dc8 commit f9f8792

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

pseudocode.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* TODO:
55
* * Test on IE8 - IE10
66
* * Support color
7-
* * Math environment using \(, \)
87
**/
98

109
var ParseError = require('./src/ParseError');

src/Lexer.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,50 @@ Lexer.prototype.get = function() {
4646
*/
4747
var mathPattern = {
4848
exec: function(str) {
49-
if (str.indexOf('$') !== 0) return null;
50-
51-
var pos = 1;
52-
var len = str.length;
53-
while (pos < len && ( str[pos] != '$' || str[pos - 1] == '\\' ) ) pos++;
49+
var delimiters = [
50+
{start: '$', end: '$'},
51+
{start: '\\(', end: '\\)'}
52+
];
53+
var totalLen = str.length;
54+
55+
for (var di = 0; di < delimiters.length; di++) {
56+
var startDel = delimiters[di].start;
57+
if (str.indexOf(startDel) !== 0) continue;
58+
59+
var endDel = delimiters[di].end;
60+
var endPos = startDel.length;
61+
var remain = str.slice(endPos);
62+
while (endPos < totalLen) {
63+
var pos = remain.indexOf(endDel);
64+
if (pos < 0)
65+
throw new ParseError('Math environment is not closed', this._pos, this._input);
66+
67+
// false positive, it's escaped, not a match
68+
if (pos > 0 && remain[pos - 1] === '\\') {
69+
var skipLen = pos + endDel.length;
70+
remain = remain.slice(skipLen);
71+
endPos += skipLen;
72+
continue;
73+
}
74+
75+
var res = [str.slice(0, endPos + pos + endDel.length),
76+
str.slice(startDel.length, endPos + pos)];
77+
return res;
78+
}
79+
}
5480

55-
if (pos === len) return null;
56-
return [str.substring(0, pos + 1), str.substring(1, pos)];
81+
return null;
5782
}
5883
};
5984
var atomRegex = {
6085
// TODO: which is correct? func: /^\\(?:[a-zA-Z]+|.)/,
6186
special: /^(\\\\|\\{|\\}|\\\$|\\&|\\#|\\%|\\_)/,
87+
math: mathPattern, ///^\$.*\$/
6288
func: /^\\([a-zA-Z]+)/,
6389
open: /^\{/,
6490
close: /^\}/,
6591
quote: /^(`|``|'|'')/,
66-
ordinary: /^[^\\{}$&#%_\s]+/,
67-
math: mathPattern ///^\$.*\$/,
92+
ordinary: /^[^\\{}$&#%_\s]+/
6893
};
6994
var commentRegex = /^%.*/;
7095
var whitespaceRegex = /^\s+/;

static/test-suite.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
\STATE \textbf{Carriage return:} first line \\ second line
4141
\STATE \textbf{Text-symbols:} \textbackslash
4242
\STATE \textbf{Quote-symbols:} `single quotes', ``double quotes''
43+
\STATE \textbf{Math:} $i \gets i + 1$, \( N^2\), $\$$, \(\$\)
4344
\end{algorithmic}
4445
\end{algorithm}
4546
</pre>

0 commit comments

Comments
 (0)