Skip to content

ANTLR Usage

Lee Surprenant edited this page Sep 16, 2022 · 4 revisions

We use antlr4 in two places:

  • generating UCUM lexer/parser
  • generating FHIRPath lexer/parser and visitor

Steps to regenerate:

  1. Download the latest antlr4 "Complete Java binaries jar" from https://www.antlr.org/download.html

  2. (Optional) Follow their getting started doc to set up an alias for antlr4

  3. Execute antlr4 with the proper options for our two projects

Sample commands that generate the java classes, replace tabs with spaces, and move them to the proper locations:

antlr4 -no-listener -package org.linuxforhealth.fhir.model.ucum fhir-model/UCUM.g4
&& sed -i.bak $'s/\t/    /g' fhir-model/UCUM*.java && mv
fhir-model/UCUM*.java fhir-model/src/main/java/org/linuxforhealth/fhir/model/ucum/
antlr4 -visitor -no-listener -package org.linuxforhealth.fhir.path fhir-path/FHIRPath.g4 &&
sed -i.bak $'s/\t/    /g' fhir-path/FHIRPath*.java && mv
fhir-path/FHIRPath*.java fhir-path/src/main/java/org/linuxforhealth/fhir/path/
  1. Add our copyright / license header (required to pass checkstyle rules) and save the files with "organize imports" and "strip trailing whitespace" save actions to reduce noise in the PR
Clone this wiki locally