Skip to content

Commit 9e0f8a1

Browse files
committed
Add support for Tex-style comment
1 parent bdad2cc commit 9e0f8a1

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/Lexer.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,32 @@ var atomRegex = {
6363
open: /^\{/,
6464
close: /^\}/,
6565
ordinary: /^[^\\{}$&#%_\s]+/,
66-
math: mathPattern ///^\$.*\$/
66+
math: mathPattern ///^\$.*\$/,
6767
};
68-
var whitespaceRegex = /^\s*/;
68+
var commentRegex = /^%.*/
69+
var whitespaceRegex = /^\s+/;
70+
71+
Lexer.prototype._skip = function(len) {
72+
this._pos += len;
73+
this._remain = this._remain.slice(len);
74+
}
6975

7076
/* Get the next atom */
7177
Lexer.prototype._next = function() {
72-
// Skip whitespace (zero or more)
73-
var whitespaceLen = whitespaceRegex.exec(this._remain)[0].length;
74-
this._pos += whitespaceLen;
75-
this._remain = this._remain.slice(whitespaceLen);
78+
while (1) {
79+
// Skip whitespace (one or more)
80+
var whitespaceMatch = whitespaceRegex.exec(this._remain);
81+
if (whitespaceMatch) {
82+
var whitespaceLen = whitespaceMatch[0].length;
83+
this._skip(whitespaceLen);
84+
}
85+
86+
// Skip comment
87+
var commentMatch = commentRegex.exec(this._remain);
88+
if (!commentMatch) break;
89+
var commentLen = commentMatch[0].length;
90+
this._skip(commentLen);
91+
}
7692

7793
// Remember the current atom
7894
this._currentAtom = this._nextAtom;

static/test-suite.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@
104104
\end{algorithmic}
105105
\end{algorithm}
106106
</pre>
107-
<!-- Chapter 7, Introduction to Algorithms (3rd edition)-->
108107
<pre id="test-examples" style="display:none">
108+
% This quicksort algorithm is extracted from Chapter 7, Introduction
109+
% to Algorithms (3rd edition)
109110
\begin{algorithm}
110111
\caption{Quicksort}
111112
\begin{algorithmic}

0 commit comments

Comments
 (0)