Skip to content

Medvedev/charsets fixes - #469

Merged
Max Medvedev (maxmedvedev) merged 5 commits into
masterfrom
medvedev/charsets-fixes
Oct 30, 2026
Merged

Max Medvedev (maxmedvedev) merged 5 commits into
masterfrom
medvedev/charsets-fixes

Conversation

@maxmedvedev

Copy link
Copy Markdown
Collaborator

No description provided.

Both test tasks set `ignoreFailures = true`, so a failing test was recorded in
the HTML report while the build stayed green. That is tolerable for an
informational task and not for one whose job is to catch regressions.

The full suite passes, so nothing is silenced by this today.
Regenerating a parser on Windows from a CRLF grammar inserted a blank line
after every line but the last of the `// rule expression` comment (#468). Two
independent causes, both as old as the generator:

* text read from disk kept its `\r`. `LightPsi` loaded the grammar raw, and
  `generateNode` splits the rule text on `\n` only, so every comment line but
  the last ended in `\r`. `Generator.getStringOrFile` read a `classHeader`
  file the same way, which leaks a `\r` into every generated file on any host.
* `FilePrinter` terminated lines with `PrintWriter.println`, i.e. the platform
  separator, turning that into `\r\r\n`.

Normalize separators on the way in - PSI text is `\n`-only by platform
contract - and write `\n` on the way out, so generated sources are
byte-identical everywhere.

`OutputOpener` hands out a `Writer` rather than a `PrintWriter` so that the
hazard is gone from the type: `println` does not exist on it, and the banners
the test openers prepend to a generated file can no longer switch separators
mid-stream. `PrintWriter` now appears once, inside `FilePrinter`, which writes
'\n' explicitly.
Standalone generation decoded the grammar with `Charset.defaultCharset()`,
and a `LightVirtualFile` reports that same default as its charset, which is
what the output is then encoded with. A grammar containing non-ASCII produced
different bytes - or mojibake - depending on the host platform (below JDK 18)
or on `-Dfile.encoding`, and a `classHeader` file was read the same way.

Read and write UTF-8 instead, and let a byte order mark override it:
`CharsetToolkit.bytesToString` picks the charset from the mark when there is
one and strips it. That also fixes two cases that were plainly broken before
- a UTF-16 grammar was decoded as UTF-8 and produced no output at all, and a
UTF-8 mark on a `classHeader` file was copied into the first line of every
generated file as U+FEFF.

The IDE path is untouched: it keeps following the project encoding of the
real `VirtualFile`, and `getStringOrFile`, which is shared between the two
modes, now reads the header with the grammar's own charset rather than a
hardcoded one.

The CLI tests run under `-Dfile.encoding=ISO-8859-1` so a regression here
fails instead of hiding behind a UTF-8 default.
`String.toUpperCase()/toLowerCase()` and `String.format` without a locale
follow the default locale, so generation was not reproducible across machines
that differ only in locale:

* the `INSTANCE.getXxx()` element-type accessors capitalized with the default
  locale, so an element type starting with a lowercase `i` gets a dotted
  capital under tr_TR and does not compile;
* `%d` in generated code rendered with the default numbering system, so a
  locale such as hi-IN-u-nu-deva emitted Devanagari digits for pin counts and
  operator priorities.

Route both through the locale-independent `StringUtil` helpers and
`Locale.ROOT`. `ConsumeType.getMethodSuffix`, JFlex keyword completion and
the suspicious-token / unused-attribute inspections had the same defect.

The Live Preview token lookup did too, and also disagreed with the map it
queries: the keys are built with `Case.UPPER`, whose full-string mapping
expands `ß` to `SS` where a char-wise helper leaves it alone. Use
`Case.UPPER` on both sides.
`GrammarPattern.of` split the grammar argument on `File.separator` only. On
Windows that is `\`, so a forward-slash path - valid there, and what build
scripts normally produce - yielded no separator at all: the grammar directory
silently fell back to `.` and the run reported no matching grammars.

Splitting on `/` as well makes two more forms reachable, and neither meant
what it said: `/Grammar.bnf` left an empty directory part, which resolves to
the working directory rather than the filesystem root, and `C:/Grammar.bnf`
left `C:`, which on Windows is the current directory on drive C rather than
the drive root. Map both to the directory they name; the drive-letter branch
is inert where `C:` is an ordinary relative name.

`collectGrammarFiles` also returned `File.listFiles()` order, which is
filesystem-dependent; sort by name so a wildcard run processes grammars in
the same order everywhere.
@github-actions

Copy link
Copy Markdown
Contributor

Qodana for JVM

5 new problems were found

Inspection name Severity Problems
Unstable API Usage 🔶 Warning 4
Unnecessary Unicode escape sequence 🔶 Warning 1

💡 Qodana analysis was run in the pull request mode: only the changed files were checked

View the detailed Qodana report

To be able to view the detailed Qodana report, you can either:

To get *.log files or any other Qodana artifacts, run the action with upload-result option set to true,
so that the action will upload the files as the job artifacts:

      - name: 'Qodana Scan'
        uses: JetBrains/qodana-action@v2025.1.1
        with:
          upload-result: true
Contact Qodana team

Contact us at qodana-support@jetbrains.com

@maxmedvedev Max Medvedev (maxmedvedev) linked an issue Sep 22, 2026 that may be closed by this pull request
@hurricup

Alexandr Evstigneev (hurricup) commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

looks like it is fine now.
after generation it produced tons of files with no changes, but this seems to be some gradle thing.
commiting did not produce any changes and this is what my concern was about.

Here is with local artifact from the branch: Camelcade/Perl5-IDEA@01dec73

Here is commit reverted and regenerated (with actual release version): Camelcade/Perl5-IDEA@6f722a9

@maxmedvedev
Max Medvedev (maxmedvedev) merged commit 8d6e024 into master Oct 30, 2026
6 checks passed
@maxmedvedev
Max Medvedev (maxmedvedev) deleted the medvedev/charsets-fixes branch October 30, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated parser has extra newlines on windows

2 participants