Skip to content

'native' import statements in @parser:header are dropped  #3868

Closed

Description

@ericvergnaud

Consider the following grammar:

grammar G;

@parser:header {
// import X from './X';
import X from './X';
class Y extends X {}
}

a: 'a' EOF;

The generated code will look as follows:

.../...
// import X from './X';
class Y extends X {}
class GParser extends Parser { .../... }

notice that the native statement "import X from './X';" has been dropped.
it doesn't happen with preceding comment or the succeeding declaration.
I suspect it is because the tool sees the 'import...' line as an antlr import statement...

Activity

  1. ericvergnaud commented on Sep 6, 2022

    @ericvergnaud
    ContributorAuthor

    mmm... this only happens during testing, so I should be able to fix it without side effects

  2. ericvergnaud commented on Sep 7, 2022

    @ericvergnaud
    ContributorAuthor

    The issue title is misleading.
    It happened in TypeScript testing only, as a side effect of having the @parser::header text inserted in not only the parser but also the listener and the visitor, which makes no sense.
    As a result, because some of the inserted code would not resolve in the listener/visitor), tsc (the Typescript compiler) would mess up silently and drop the imports in the transpired js files (not in the ts files).
    @parrt this is fixed by removing the named action "header" after generating the parser, which needs your blessing.

  3. KvanTTT commented on Sep 7, 2022

    @KvanTTT
    Member

    I'm afraid I can't bless such a fix. It's unclear why CodeGenPipilne removes name action and method removeParserHeader in Grammar looks irrelevant. Is it possible just not to add header named action?

  4. ericvergnaud commented on Sep 7, 2022

    @ericvergnaud
    ContributorAuthor
  5. ericvergnaud commented on Sep 8, 2022

    @ericvergnaud
    ContributorAuthor

    Closed in favor of #3877

  6. added a commit that references this issue on Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions