Compare commits

...

7 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 1d7e48e094 Comments
- Claned up
2024-04-20 23:57:19 +02:00
Tristan B. Velloza Kildaire 52f7e3a5ab Parser (unittests)
- Removed unuueded unittests
2024-04-20 21:34:01 +02:00
Tristan B. Velloza Kildaire 992e96ce79 Parser (unittests)
- Check for the single line
- Fixed comment
2024-04-20 21:32:19 +02:00
Tristan B. Velloza Kildaire df2e28da92 Parser
- Added nw unittest
- Moved other unittests
2024-04-20 21:28:20 +02:00
Tristan B. Velloza Kildaire d876eab69d ExceptionDoc
- Added `getException()`
2024-04-20 21:28:06 +02:00
Tristan B. Velloza Kildaire 48f9b2659f Test cases
- Updated `simple_comments.t`
2024-04-20 20:19:36 +02:00
Tristan B. Velloza Kildaire ec143553cf BasicLexer
- Bug fix for single-line comments consuming first character on next line
2024-04-20 20:07:27 +02:00
4 changed files with 107 additions and 95 deletions

View File

@ -509,7 +509,7 @@ public final class BasicLexer : LexerInterface
while (true) {
if (!multiLine && currentChar == LS.NEWLINE) {
flush();
return advanceLine();
return true;
}
if (multiLine && currentChar == LS.STAR && isForward() && sourceCode[position+1] == LS.FORWARD_SLASH) {
buildAdvance();

View File

@ -2289,96 +2289,6 @@ public final class Parser
WARN("parseComment(): Leave");
}
/**
* Tests the handling of comments
*/
unittest
{
import tlang.compiler.lexer.kinds.arr : ArrLexer;
try
{
string sourceCode = `module myCommentModule;
// Hello`;
File dummyFile;
Compiler compiler = new Compiler(sourceCode, "legitidk.t", dummyFile);
compiler.doLex();
compiler.doParse();
// FIXME: Re-enable when we we have
// a way to extract comments from
// AST nodes
// assert(parser.hasCommentsOnStack());
// assert(parser.getCommentCount() == 1);
}
catch(TError e)
{
assert(false);
}
try
{
string sourceCode = `module myCommntedModule;
/*Hello */
/* Hello*/`;
File dummyFile;
Compiler compiler = new Compiler(sourceCode, "legitidk.t", dummyFile);
compiler.doLex();
compiler.doParse();
// FIXME: Re-enable when we we have
// a way to extract comments from
// AST nodes
// assert(parser.hasCommentsOnStack());
// assert(parser.getCommentCount() == 1);
}
catch(TError e)
{
assert(false);
}
try
{
string sourceCode = `module myCommentedModule;
void function()
{
/*Hello */
/* Hello */
// Hello
//Hello
}
`;
File dummyFile;
Compiler compiler = new Compiler(sourceCode, "legitidk.t", dummyFile);
compiler.doLex();
compiler.doParse();
// FIXME: Re-enable when we we have
// a way to extract comments from
// AST nodes
// assert(parser.hasCommentsOnStack());
// assert(parser.getCommentCount() == 1);
// assert(parser.hasCommentsOnStack());
// assert(parser.getCommentCount() == 4);
}
catch(TError e)
{
assert(false);
}
}
// TODO: We need to add `parseComment()`
// support here (see issue #84)
// TODO: This ic currently dead code and ought to be used/implemented
@ -3748,4 +3658,104 @@ unittest
{
assert(false);
}
}
/**
* Comments tests
*/
unittest
{
string sourceCode = `
module comments;
// Comment for no one
// Comment for Elise
int p;
/**
* This is a comment on a function
*
*
* @param i This is the i
* @param p This is the p
* @throws Exception worst exception eva
* @return Void, so nothing
*/
void function(int i, int p)
{
}
`;
File dummyFile;
Compiler compiler = new Compiler(sourceCode, "legitidk.t", dummyFile);
try
{
compiler.doLex();
assert(true);
}
catch(LexerException e)
{
assert(false);
}
try
{
compiler.doParse();
Program program = compiler.getProgram();
// There is only a single module in this program
Module modulle = program.getModules()[0];
TypeChecker tc = new TypeChecker(compiler);
/* Find the variable named `p` and get its comment */
Entity varEnt = tc.getResolver().resolveBest(modulle, "p");
Variable var = cast(Variable)varEnt;
Comment varComment = var.getComment();
assert(varComment);
assert(varComment.getContent() == "Comment for Elise");
/* Find the function named `function` and get its comment */
Entity funcEnt = tc.getResolver().resolveBest(modulle, "function");
Function func = cast(Function)funcEnt;
Comment funcComment = func.getComment();
assert(funcComment);
/* Ensure the comment is as we want */
DEBUG(funcComment.getContent());
assert(funcComment.getContent() == "This is a comment on a function ");
ParamDoc* iPDoc, pPDoc;
ParamDoc[string] paramDocs = funcComment.getAllParamDocs();
assert((iPDoc = "i" in paramDocs) !is null);
assert((pPDoc = "p" in paramDocs) !is null);
assert(iPDoc.getParam() == "i");
assert(iPDoc.getDescription() == "This is the i");
assert(pPDoc.getParam() == "p");
assert(pPDoc.getDescription() == "This is the p");
ExceptionDoc eDoc;
bool found;
foreach(DocStr dc; funcComment.getDocStrings())
{
if((found = dc.getExceptionDoc(eDoc)) == true)
{
break;
}
}
assert(found);
assert(eDoc.getException() == "Exception");
assert(eDoc.getDescription() == "worst exception eva");
ReturnsDoc retDoc;
assert(funcComment.getReturnDoc(retDoc));
assert(retDoc.getDescription() == "Void, so nothing");
}
catch(TError e)
{
assert(false);
}
}

View File

@ -7,6 +7,7 @@ import std.array : join;
import tlang.misc.logging;
import std.string : format;
import tlang.compiler.lexer.core.tokens : Token;
/**
* The type of docstring
@ -87,6 +88,11 @@ public struct ExceptionDoc
private string exception;
private string description;
public string getException()
{
return this.exception;
}
public string getDescription()
{
return this.description;
@ -519,8 +525,6 @@ unittest
assert("Hello there" == comment.bdy);
}
import tlang.compiler.lexer.core.tokens : Token;
/**
* Represents a comment
* which can be attached
@ -532,7 +536,6 @@ public final class Comment
private this(CommentParts content)
{
// TODO: Parse the comment into text but annotated section
this.content = content;
}

View File

@ -1,7 +1,6 @@
module simple_comments;
// Lol
int i;
/**