Skip to content

#858 Fixed the COBOL parser so reserved words can be used as field names - #859

Merged
yruslan merged 2 commits into
masterfrom
bugfix/858-field-is-reserved-word
Jul 26, 2026
Merged

#858 Fixed the COBOL parser so reserved words can be used as field names#859
yruslan merged 2 commits into
masterfrom
bugfix/858-field-is-reserved-word

Conversation

@yruslan

@yruslan yruslan commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Closes #858

Summary by CodeRabbit

  • New Features

    • Expanded COBOL copybook parsing to recognize additional reserved words as valid field names.
    • Improved compatibility with copybooks using varied COBOL formatting and terminology.
  • Bug Fixes

    • Preserved validation for invalid uses of reserved keywords, including REDEFINES.
  • Tests

    • Added coverage for reserved-word field names and expected record layouts.

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

ANTLR is upgraded from 4.8 to 4.9.3. The COBOL identifier grammar accepts additional keyword tokens, generated parser sources are regenerated, and syntax tests cover reserved-word-like field names and REDEFINES rejection.

Changes

COBOL parser compatibility update

Layer / File(s) Summary
Grammar and ANTLR toolchain alignment
project/Dependencies.scala, cobol-parser/pom.xml, cobol-parser/src/main/scala/.../antlr/*
ANTLR runtime and generated-source metadata are updated to 4.9.3, and the identifier rule accepts additional COBOL token types.
Regenerated COBOL parser
cobol-parser/src/main/scala/.../copybookParser.java
ANTLR-generated parsing control flow and serialized automaton are regenerated across identifier, values, usage, occurrence, picture, group, primitive, and item rules.
Reserved-word syntax coverage
cobol-parser/src/test/scala/.../SyntaxErrorsSpec.scala
Tests validate reserved-word-like field names, expected record layouts, and rejection diagnostics for REDEFINES used as a field token.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SyntaxErrorsSpec
  participant copybookLexer
  participant copybookParser
  participant ParsedRecordLayout
  SyntaxErrorsSpec->>copybookLexer: provide COBOL copybook text
  copybookLexer->>copybookParser: provide token stream
  copybookParser->>ParsedRecordLayout: parse identifier and field clauses
  ParsedRecordLayout-->>SyntaxErrorsSpec: return parsed names and layout
Loading

Poem

A rabbit hops through grammar lines,
Where reserved names now fit just fine.
ANTLR gears turn, fresh and bright,
Tests guard each parsing byte.
REDEFINES still rings the bell—
“Not this field name!” the parsers tell.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main fix: allowing reserved COBOL words as field names.
Linked Issues check ✅ Passed The grammar change, regenerated parser, and tests directly address the reserved-word field-name parsing bug.
Out of Scope Changes check ✅ Passed The dependency bump and regenerated artifacts support the same parser fix and no unrelated scope is evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/858-field-is-reserved-word

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

JaCoCo code coverage report - 'cobol-parser'

Overall Project 90.17% -1.93% 🍏
Files changed 27.02% 🍏

File Coverage
copybookLexer.java 99.15% 🍏
jsonLexer.java 94.7% 🍏
jsonParser.java 79.09% 🍏
copybookParser.java 47.22% -14% 🍏
jsonBaseVisitor.java 14.89% 🍏
copybookParserBaseVisitor.java 8.52% 🍏

@github-actions

Copy link
Copy Markdown

JaCoCo code coverage report - 'spark-cobol'

Overall Project 83.4% 🍏

There is no coverage information present for the Files changed

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParser.g4 (2)

53-53: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

The pre-existing empty alternative on the preceding line (X_S | | N_S) becomes riskier with 60+ new alternatives.

It compiles to case 7: enterOuterAlt(_localctx, 7); { } in copybookParser.java (Lines 634-638) — identifier can match the empty string. With the alternative set now this large, adaptive prediction is far more likely to select the epsilon branch during error recovery and yield an empty field name instead of a syntax error. Consider removing the stray | while regenerating.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParser.g4`
at line 53, Remove the empty alternative from the identifier rule by changing
the preceding X_S/N_S alternative so it no longer includes a standalone “|”.
Regenerate the parser artifacts, including copybookParser.java, and preserve
only the valid identifier alternatives.

53-68: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add reserved-word field-name syntax tests for COBOL occurrence/sort/renames clauses.

The identifier rule already accepts the tokens used as clause separators, and the regenerated parser now resolves ambiguity with adaptive prediction. Add syntax tests for fields named like ON, BY, KEY, IS, TO, TIMES, DEPENDING, INDEXED, or THRU, covering OCCURS ... DEPENDING ON, INDEXED BY, ASCENDING/DESCENDING KEY IS, and RENAMES A THRU B.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParser.g4`
around lines 53 - 68, Add syntax tests for reserved-word field names in the
COBOL parser, covering ON, BY, KEY, IS, TO, TIMES, DEPENDING, INDEXED, and THRU
within OCCURS ... DEPENDING ON, INDEXED BY, ASCENDING/DESCENDING KEY IS, and
RENAMES A THRU B clauses. Ensure the tests assert these declarations parse
successfully through the existing identifier rule and regenerated parser.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In
`@cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParser.g4`:
- Line 53: Remove the empty alternative from the identifier rule by changing the
preceding X_S/N_S alternative so it no longer includes a standalone “|”.
Regenerate the parser artifacts, including copybookParser.java, and preserve
only the valid identifier alternatives.
- Around line 53-68: Add syntax tests for reserved-word field names in the COBOL
parser, covering ON, BY, KEY, IS, TO, TIMES, DEPENDING, INDEXED, and THRU within
OCCURS ... DEPENDING ON, INDEXED BY, ASCENDING/DESCENDING KEY IS, and RENAMES A
THRU B clauses. Ensure the tests assert these declarations parse successfully
through the existing identifier rule and regenerated parser.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5c9a37f4-7646-43aa-a3a3-be029d298793

📥 Commits

Reviewing files that changed from the base of the PR and between 62dab45 and 7173557.

📒 Files selected for processing (12)
  • cobol-parser/pom.xml
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookLexer.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParser.g4
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParser.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParserBaseVisitor.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/copybookParserVisitor.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/jsonBaseVisitor.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/jsonLexer.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/jsonParser.java
  • cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/antlr/jsonVisitor.java
  • cobol-parser/src/test/scala/za/co/absa/cobrix/cobol/parser/parse/SyntaxErrorsSpec.scala
  • project/Dependencies.scala

@yruslan
yruslan merged commit 5937f2d into master Jul 26, 2026
7 checks passed
@yruslan
yruslan deleted the bugfix/858-field-is-reserved-word branch July 26, 2026 12:45
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.

Cobrix fails to parse a field if its name is a reserved word

1 participant