Skip to content

cpp target: Lexer::setTokenFactory template function unusable #2727

Description

@vinoski

The Lexer class provides the setTokenFactory template function, but its function signature means that its body can't compile. The function is defined as follows:

template<typename T1>
void setTokenFactory(TokenFactory<T1> * factory) {
    this->_factory = factory;
}

where this->_factory is declared as a std::shared_ptr:

Ref<TokenFactory<CommonToken>> _factory;

The function can't work because std::shared_ptr<T> provides no assignment operator from T*, nor does it provide an implicitly converting constructor from T* (i.e., its T* constructor is marked explicit). Lexer works with the default CommonTokenFactory only because it assigns its _factory using CommonTokenFactory::DEFAULT, which is a shared_ptr of the same type as _factory.

I believe setTokenFactory on Lexer, Parser, and TokenSource should be changed to the following signature:

template<typename T1>
void setTokenFactory(Ref<TokenFactory<T1>> const& factory);

and the implementation of Lexer::setTokenFactory should be:

template<typename T1>
void setTokenFactory(Ref<TokenFactory<T1>> const& factory) {
    this->_factory = std::static_pointer_cast<TokenFactory<CommonToken>>(factory);
}

Because of the declared type of Lexer::_factory, it's necessary for the factory argument here to be convertible to a std::shared_ptr<TokenFactory<CommonToken>>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions