diff --git a/src/parser/parse-shaders.js b/src/parser/parse-shaders.js index b2d6d59..d71fa71 100644 --- a/src/parser/parse-shaders.js +++ b/src/parser/parse-shaders.js @@ -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; diff --git a/src/parser/tokenizer.js b/src/parser/tokenizer.js index f8b20fa..5e03212 100644 --- a/src/parser/tokenizer.js +++ b/src/parser/tokenizer.js @@ -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())) { diff --git a/test/parse-shaders.js b/test/parse-shaders.js index 265378c..957a85d 100644 --- a/test/parse-shaders.js +++ b/test/parse-shaders.js @@ -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); +});