🐞 Bugfix: expect(string) should throw ParserException atleast (#20)

* Parser

- Removed commented-out code

* Parser

- Removed `isUnitTest` variable and how `expect(string)` relies on it

* Parser

- Now throw a new `ParserException` instead of a `TError` when calling `expect(string)`

* Parser

- Made `expect(string)` not static

* TypeChecker

- Implemented `expect(string)` which throws a `TypeCheckerException` in a similar fashion to `Parser`'s `expect(string)`

* Dependency

- Implemented `expect(string)` which throws a `DependencyException` in a similar fashion to `Parser`'s `expect(string)`

Exceptions (dependency)

- Added enum member `GENERAL_ERROR` to `DependencyError`

* Parser (unit tests)

- Made more specific
This commit is contained in:
Tristan B. Velloza Kildaire 2023-07-16 20:33:51 +02:00 committed by GitHub
parent fa9399b90d
commit 0b3a06efde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 39 deletions

View File

@ -10,15 +10,6 @@ import core.stdc.stdlib;
import misc.exceptions : TError;
import tlang.compiler.parsing.exceptions;
// public final class ParserError : TError
// {
// }
bool isUnitTest;
// TODO: Technically we could make a core parser etc
public final class Parser
{
@ -46,24 +37,18 @@ public final class Parser
}
}
/**
* Crashes the parser with the given message
*/
public static void expect(string message)
/**
* Crashes the parser with an expectation message
* by throwing a new `ParserException`.
*
* Params:
* message = the expectation message
*/
public void expect(string message)
{
//throw new TError(message);
gprintln(message, DebugType.ERROR);
if(isUnitTest)
{
throw new TError(message);
assert(false);
}
else
{
throw new TError(message);
//exit(0); /* TODO: Exit code */ /* TODO: Version that returns or asserts for unit tests */
}
throw new ParserException(this, ParserException.ParserErrorType.GENERAL_ERROR, message);
}
/**
@ -3130,9 +3115,12 @@ int wrongFunction()
assert(false);
}
// TODO: Make more specific (see https://deavmi.assigned.network/git/tlang/tlang/issues/147)
catch(TError)
catch(ParserException)
{
assert(true);
}
catch(TError)
{
assert(false);
}
}

View File

@ -78,6 +78,18 @@ public final class TypeChecker
return config;
}
/**
* Crashes the type checker with an expectation message
* by throwing a new `TypeCheckerException`.
*
* Params:
* message = the expectation message
*/
public void expect(string message)
{
throw new TypeCheckerException(this, TypeCheckerException.TypecheckError.GENERAL_ERROR, message);
}
/**
* I guess this should be called rather
* when processing assignments but I also
@ -2316,19 +2328,19 @@ public final class TypeChecker
}
else
{
Parser.expect("Cannot inherit from self");
expect("Cannot inherit from self");
}
}
/* Error */
else
{
Parser.expect("Can only inherit from classes");
expect("Can only inherit from classes");
}
}
/* If the entity doesn't exist then it is an error */
else
{
Parser.expect("Could not find any entity named " ~ parent);
expect("Could not find any entity named " ~ parent);
}
}
}
@ -2593,7 +2605,7 @@ public final class TypeChecker
*/
if (resolver.resolveUp(c, clazz.getName()) != clazz)
{
Parser.expect("Cannot define class \"" ~ resolver.generateName(modulle,
expect("Cannot define class \"" ~ resolver.generateName(modulle,
clazz) ~ "\" as one with same name, \"" ~ resolver.generateName(modulle,
resolver.resolveUp(c, clazz.getName())) ~ "\" exists in container \"" ~ resolver.generateName(
modulle, containerEntity) ~ "\"");
@ -2608,7 +2620,7 @@ public final class TypeChecker
// {
if (cmp(containerEntity.getName(), clazz.getName()) == 0)
{
Parser.expect("Class \"" ~ resolver.generateName(modulle,
expect("Class \"" ~ resolver.generateName(modulle,
clazz) ~ "\" cannot be defined within container with same name, \"" ~ resolver.generateName(
modulle, containerEntity) ~ "\"");
}

View File

@ -479,6 +479,19 @@ public class DNodeGenerator
//generate();
}
/**
* Crashes the dependency generator with an
* expectation message by throwing a new
* `DependencyException`.
*
* Params:
* message = the expectation message
*/
public void expect(string message)
{
throw new DependencyException(DependencyError.GENERAL_ERROR, message);
}
public DNode root;
@ -702,14 +715,14 @@ public class DNodeGenerator
}
else
{
Parser.expect("Only class-type may be used with `new`");
expect("Only class-type may be used with `new`");
assert(false);
}
gprintln("Poe naais");
}
else
{
Parser.expect("Invalid ryp");
expect("Invalid ryp");
assert(false);
}
// FunctionCall
@ -817,7 +830,7 @@ public class DNodeGenerator
}
else
{
Parser.expect("Cannot reference variable "~nearestName~" which exists but has not been declared yet");
expect("Cannot reference variable "~nearestName~" which exists but has not been declared yet");
}
@ -865,7 +878,7 @@ public class DNodeGenerator
}
else
{
Parser.expect("No entity by the name "~nearestName~" exists (at all)");
expect("No entity by the name "~nearestName~" exists (at all)");
}
@ -960,7 +973,7 @@ public class DNodeGenerator
}
else
{
Parser.expect("Could not acces \""~remainingSegment~"\" as it is not a container");
expect("Could not acces \""~remainingSegment~"\" as it is not a container");
}
}
@ -971,7 +984,7 @@ public class DNodeGenerator
*/
else
{
Parser.expect("Could not find an entity named "~remainingSegment);
expect("Could not find an entity named "~remainingSegment);
}
}
@ -1391,7 +1404,7 @@ public class DNodeGenerator
}
else
{
Parser.expect("Cannot reference variable "~vAsStdAl.getVariableName()~" which exists but has not been declared yet");
expect("Cannot reference variable "~vAsStdAl.getVariableName()~" which exists but has not been declared yet");
return null;
}
}
@ -1791,7 +1804,7 @@ public class DNodeGenerator
/* Sanity check */
if(clazz.getModifierType() != InitScope.STATIC)
{
Parser.expect("SanityCheck: poolClassStatic(): Cannot pool a non-static class");
expect("SanityCheck: poolClassStatic(): Cannot pool a non-static class");
// assert(clazz.getModifierType() == InitScope.STATIC);
}

View File

@ -8,6 +8,7 @@ public enum DependencyError
{
NOT_YET_LINEARIZED,
ALREADY_LINEARIZED,
GENERAL_ERROR
}
public final class DependencyException : TError