Skip to content

Commit

Permalink
fix comments logic
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Nov 21, 2022
1 parent 8184f54 commit e5d28f7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
67 changes: 55 additions & 12 deletions src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ const MESSAGES = {
UNEXP_EOT: 'Unexpected end of template',
};

const BLOCK_COMMENT_REGEX = /\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g;
const SINGLE_LINE_COMMENT_REGEX = /\/\/[^\n\r]+?(?:\*\)|[\n\r])/g;

export class JsonTemplateLexer {
private readonly codeChars: string[];
private buf: Token[];
private idx = 0;
constructor(template: string) {
this.buf = [];
this.codeChars = template
.replace(BLOCK_COMMENT_REGEX, '')
.replace(SINGLE_LINE_COMMENT_REGEX, '')
.split('');
this.codeChars = template.split('');
}

init() {
Expand Down Expand Up @@ -146,10 +141,62 @@ export class JsonTemplateLexer {
return this.buf[steps];
}

private advance(): Token {
while (JsonTemplateLexer.isWhiteSpace(this.codeChars[this.idx])) {
private isLineCommentStart(): boolean {
return this.codeChars[this.idx] === '/' && this.codeChars[this.idx + 1] === '/';
}

private isLineCommentEnd(): boolean {
return this.codeChars[this.idx] === '\n';
}

private isBlockCommentStart(): boolean {
return this.codeChars[this.idx] === '/' && this.codeChars[this.idx + 1] === '*';
}

private isBlockCommentEnd(): boolean {
return this.codeChars[this.idx] === '*' && this.codeChars[this.idx + 1] === '/';
}

private skipLineComment() {
if (!this.isLineCommentStart()) {
return;
}
while (!this.isLineCommentEnd()) {
++this.idx;
}
++this.idx;
}

private skipBlockComment() {
if (!this.isBlockCommentStart()) {
return;
}
while (!this.isBlockCommentEnd()) {
++this.idx;
}
this.idx = this.idx + 2;
}

private isWhiteSpace() {
return ' \r\n\t'.includes(this.codeChars[this.idx]);
}

private skipWhitespace() {
while (this.isWhiteSpace()) {
++this.idx;
}
}

private skipInput() {
while (this.isWhiteSpace() || this.isBlockCommentStart() || this.isLineCommentStart()) {
this.skipWhitespace();
this.skipLineComment();
this.skipBlockComment();
}
}

private advance(): Token {
this.skipInput();

if (this.idx >= this.codeChars.length) {
return {
Expand Down Expand Up @@ -229,10 +276,6 @@ export class JsonTemplateLexer {
return '0123456789'.indexOf(ch) >= 0;
}

private static isWhiteSpace(ch: string) {
return ' \r\n\t'.indexOf(ch) > -1;
}

private static isIdStart(ch: string) {
return ch === '$' || ch === '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
Expand Down
7 changes: 7 additions & 0 deletions test/scenarios/comments/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Sceanario } from '../../types';

export const data: Sceanario[] = [
{
output: ["////", "/*** /// */"],
},
];
6 changes: 6 additions & 0 deletions test/scenarios/comments/template.jt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// line comment
/**
////////////////
* block comment
*/
["////", "/*** /// */"]

0 comments on commit e5d28f7

Please sign in to comment.