Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseModule overload set should take the parser type as template parameter #460

Open
ghost opened this issue May 12, 2022 · 1 comment
Open
Milestone

Comments

@ghost
Copy link

ghost commented May 12, 2022

Since we can make derived Parser classes.

updated functions would then be like:

/**
 * Params:
 *      parserConfig = a parser configuration.
 * Returns:
 *      The parsed module.
 */
Module parseModule(P = .Parser)(auto ref ParserConfig parserConfig)
{
    auto parser = new P();
    with (parserConfig)
    {
        parser.fileName = fileName;
        parser.tokens = tokens;
        parser.messageFunction = messageFunction;
        parser.messageDelegate = messageDelegate;
        parser.allocator = allocator;
    }
    Module mod = parser.parseModule();
    with (parserConfig)
    {
        if (warningCount !is null)
            *warningCount = parser.warningCount;
        if (errorCount !is null)
            *errorCount = parser.errorCount;
    }
    return mod;
}

/**
 * Params:
 *      tokens = The tokens parsed by dparse.lexer.
 *      fileName = The name of the file being parsed.
 *      allocator = A pointer to a rollback allocator.
 *      messageFuncOrDg = Either a function or a delegate that receives the parser messages.
 *      errorCount = An optional pointer to a variable receiving the error count.
 *      warningCount = An optional pointer to a variable receiving the warning count.
 * Returns:
 *      The parsed module.
 */
Module parseModule(P = .Parser,F)(const(Token)[] tokens, string fileName, RollbackAllocator* allocator,
    F messageFuncOrDg = null, uint* errorCount = null, uint* warningCount = null)
{
    static if (is(F))
    {
        static if (is(F : MessageFunction))
            return ParserConfig(tokens, fileName, allocator, messageFuncOrDg, null,
                errorCount, warningCount).parseModule();
        else static if (is(F : MessageDelegate))
            return ParserConfig(tokens, fileName, allocator, null, messageFuncOrDg,
                errorCount, warningCount).parseModule();
        else static assert(0, "F must be a MessageFunction or a MessageDelegate");
    }
    else
    {
        return ParserConfig(tokens, fileName, allocator, null, null, null, null).parseModule!P();
    }
} 
@WebFreak001
Copy link
Member

seems like an easy change, can you make a PR to let CI run over it?

@WebFreak001 WebFreak001 added this to the v1.0.0 milestone May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant