diff --git a/source/dietc/complete.d b/source/dietc/complete.d index 9146e1c..ca181e9 100644 --- a/source/dietc/complete.d +++ b/source/dietc/complete.d @@ -420,13 +420,14 @@ class DietComplete } } -void extractD(DietComplete complete, size_t offset, out string code, out size_t codeOffset) +void extractD(DietComplete complete, size_t offset, out string code, out size_t codeOffset, string prefix = null) { - return complete.parser.root.extractD(offset, code, codeOffset); + return complete.parser.root.extractD(offset, code, codeOffset, prefix); } -void extractD(AST root, size_t offset, out string code, out size_t codeOffset) +void extractD(AST root, size_t offset, out string code, out size_t codeOffset, string prefix = null) { + code = prefix; codeOffset = size_t.max; class CodeVisitorImpl : ASTVisitor { diff --git a/source/dietc/parser.d b/source/dietc/parser.d index 0d3f421..e5ca4b7 100644 --- a/source/dietc/parser.d +++ b/source/dietc/parser.d @@ -757,17 +757,16 @@ struct ASTParser Comment parseComment() { auto tok = input.front; - if (input.matchText("//-")) + if (input.matchText("//")) { + bool hidden = input.front.content.startsWith("-"); string content = parseText(true, true); + if (hidden) + content = content[1 .. $]; + tok.type = TokenType.raw; + tok.content = hidden ? "//-" : "//"; tok.range[1] = input.front.range[0]; - return new HiddenComment(tok, content); - } - else if (input.matchText("//")) - { - string content = parseText(true, true); - tok.range[1] = input.front.range[0]; - return new Comment(tok, content); + return hidden ? new HiddenComment(tok, content) : new Comment(tok, content); } return null; } @@ -775,9 +774,12 @@ struct ASTParser DStatement parseDStatement() { auto tok = input.front; - if (input.matchText("-")) + if (tok.content.startsWith("-")) { string content = parseText(false, false); + content = content[1 .. $]; + tok.content = "-"; + tok.type = TokenType.raw; tok.range[1] = input.front.range[0]; return new DStatement(tok, content); } @@ -1536,8 +1538,10 @@ unittest bar1.tag.assertToken(TokenType.identifier, "div", [5, 5]); bar2.tag.assertToken(TokenType.identifier, "div", [18, 18]); - assert(cast(TextLine) bar1.contents, "Expected string contents but got " ~ bar1.contents.to!string); - assert(cast(TextLine) bar2.contents, "Expected string contents but got " ~ bar2.contents.to!string); + assert(cast(TextLine) bar1.contents, + "Expected string contents but got " ~ bar1.contents.to!string); + assert(cast(TextLine) bar2.contents, + "Expected string contents but got " ~ bar2.contents.to!string); assert((cast(TextLine) bar1.contents)._parts.length == 1); assert((cast(TextLine) bar2.contents)._parts.length == 1); @@ -1585,3 +1589,43 @@ unittest assert(content._parts[0].inlineExpr.content == "item.foo"); assert(content._parts[1].raw == " bar"); } + +unittest +{ + DietInput input; + input.file = "stdin"; + input.code = `//-foo`; + + auto parser = new ASTParser; + parser.input = input.save; + parser.parseDocument(); + + assert(parser.input.errors.length == 0); + + assert(parser.root); + assert(parser.root.children.length == 1); + auto root = cast(HiddenComment) parser.root.children[0]; + assert(root); + assertToken(root.token, TokenType.raw, "//-"); + assert(root.content == "foo"); +} + +unittest +{ + DietInput input; + input.file = "stdin"; + input.code = `-foo`; + + auto parser = new ASTParser; + parser.input = input.save; + parser.parseDocument(); + + assert(parser.input.errors.length == 0); + + assert(parser.root); + assert(parser.root.children.length == 1); + auto root = cast(DStatement) parser.root.children[0]; + assert(root); + assertToken(root.token, TokenType.raw, "-"); + assert(root.content == "foo"); +}