From f5df2c9aab3e23793fab03d25de54d5fcc9d7905 Mon Sep 17 00:00:00 2001 From: falsycat Date: Fri, 4 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Implement skipping comments. --- sjscript/src/sjscript/Token.d | 10 ++++++++-- sjscript/src/sjscript/package.d | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/sjscript/src/sjscript/Token.d b/sjscript/src/sjscript/Token.d index ebae53e..529d3d9 100644 --- a/sjscript/src/sjscript/Token.d +++ b/sjscript/src/sjscript/Token.d @@ -12,11 +12,11 @@ unittest { import std; with (TokenType) { - assert("0 0.1 _hoge0_ $hoge". + assert("0 0.1 _hoge0_ $hoge // hoge". Tokenize!TokenType. map!"a.type". filter!(x => x != Whitespace). - equal([Number, Number, Ident, PreprocessCommand])); + equal([Number, Number, Ident, PreprocessCommand, Comment])); } } @@ -80,6 +80,12 @@ enum TokenType { @TextCompleteMatcher!"/" Div, @TextAllMatcher!isWhite Whitespace, + @TextFuncMatcher!((string text) { + if (text.length < 2 || text[0..2] != "//") return 0; + + const index = text.countUntil('\n'); + return index >= 0? index.to!size_t: text.length; + }) Comment, End, } diff --git a/sjscript/src/sjscript/package.d b/sjscript/src/sjscript/package.d index 4946b9c..c063b59 100644 --- a/sjscript/src/sjscript/package.d +++ b/sjscript/src/sjscript/package.d @@ -20,6 +20,7 @@ ParametersBlock[] CreateScriptAst(string src) { return src. Tokenize!TokenType(). filter!(x => x.type != TokenType.Whitespace). + filter!(x => x.type != TokenType.Comment). Preprocess(). Parse(); }