Add an option for skipping inline comments (#74)
* Add an option for skipping inline comments * format
This commit is contained in:
parent
01d0fac7d5
commit
ccf0d0e2f6
|
|
@ -2,8 +2,11 @@ import { scan, iterator, Token } from './tokenizer.js';
|
|||
import { is_empty } from '../utils/index.js';
|
||||
|
||||
function parse(input) {
|
||||
input = input.replace(/\/\/[^\n]*(\n|$)/mg, ''); // remove single-line comment
|
||||
let iter = iterator(removeParens(scan(input, true)));
|
||||
let scanOptions = {
|
||||
preserveLineBreak: true,
|
||||
ignoreInlineComment: true,
|
||||
};
|
||||
let iter = iterator(removeParens(scan(input, scanOptions)));
|
||||
let stack = [];
|
||||
let tokens = [];
|
||||
let identifier;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ const is = {
|
|||
dots: (a, b) => is.dot(a) && is.dot(b),
|
||||
letter: (a, b) => String(a).toLowerCase() == String(b).toLowerCase(),
|
||||
comment: (a, b) => a == '/' && b == '*',
|
||||
inlineComment: (a, b) => a == '/' && b === '/',
|
||||
selfClosedTag: (a, b) => a == '/' && b == '>',
|
||||
closedTag: (a, b) => a == '<' && b == '/',
|
||||
}
|
||||
|
|
@ -63,7 +64,7 @@ function iterator(input) {
|
|||
},
|
||||
next(n = 1) {
|
||||
let next = input[pointer += n];
|
||||
if (next == '\n') row++, col = 0;
|
||||
if (next === '\n') row++, col = 0;
|
||||
else col += n;
|
||||
return next;
|
||||
},
|
||||
|
|
@ -90,6 +91,12 @@ function skipComments(iter) {
|
|||
}
|
||||
}
|
||||
|
||||
function skipInlineComments(iter) {
|
||||
while (iter.next()) {
|
||||
if (iter.curr() === '\n') break;
|
||||
}
|
||||
}
|
||||
|
||||
function ignoreSpacingSymbol(value) {
|
||||
return [':', ';', ',', '{', '}', '(', ')', '[', ']'].includes(value);
|
||||
}
|
||||
|
|
@ -158,7 +165,7 @@ function last(array) {
|
|||
return array[array.length - 1];
|
||||
}
|
||||
|
||||
function scan(source, preserveLineBreak = false) {
|
||||
function scan(source, options = {}) {
|
||||
let iter = iterator(String(source).trim());
|
||||
let tokens = [];
|
||||
let quoteStack = [];
|
||||
|
|
@ -168,6 +175,9 @@ function scan(source, preserveLineBreak = false) {
|
|||
if (is.comment(curr, next)) {
|
||||
skipComments(iter);
|
||||
}
|
||||
else if (options.ignoreInlineComment && is.inlineComment(curr, next)) {
|
||||
skipInlineComments(iter);
|
||||
}
|
||||
else if (is.hex(curr, next, next2)) {
|
||||
let num = readHexNumber(iter);
|
||||
tokens.push(new Token({
|
||||
|
|
@ -233,7 +243,7 @@ function scan(source, preserveLineBreak = false) {
|
|||
if (ignoreLeft || ignoreRight) {
|
||||
continue;
|
||||
} else {
|
||||
spaces = preserveLineBreak ? curr : ' ';
|
||||
spaces = options.preserveLineBreak ? curr : ' ';
|
||||
}
|
||||
}
|
||||
if (tokens.length && (next && next.trim())) {
|
||||
|
|
|
|||
|
|
@ -116,3 +116,23 @@ test('handle nested parens', t => {
|
|||
}
|
||||
compare(t, input, result);
|
||||
});
|
||||
|
||||
test('ignore comments', t => {
|
||||
let input = `
|
||||
// this is inline comment
|
||||
// this is another inline comment
|
||||
fragment {
|
||||
void main() {
|
||||
/**
|
||||
* more comments
|
||||
*/
|
||||
float PI = /*pi value*/3.14159;
|
||||
}
|
||||
}
|
||||
`;
|
||||
let result = {
|
||||
fragment: 'void main(){float PI = 3.14159;}',
|
||||
textures: []
|
||||
}
|
||||
compare(t, input, result);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue