ArthurSonzogni / ANTLR-cmake-starter

:heart: A ready to use CMake + ANTLR simple starter with not dependencies. :+1:
10 stars 2 forks source link

Parser and Lexer in one flle #2

Open andr1972 opened 1 year ago

andr1972 commented 1 year ago

Is possible add these handling ? My trial of change original function:

# Function
#   ANTLRone(INPUT <input>)
#
# Description:
#   Take an ANTLR file and produce a CMake rule to generate the corresponding
#   C++ files.
#
# Notes:
#   The ANTLR file path must be relative to ${CMAKE_CURRENT_SOURCE_DIR}
#   Parser and lexer in one file.
#
# Example:
#   ANTLRone(My1.g4)
function(ANTLRone)
  set(source ${ARGN})
  get_filename_component(source_filename ${CMAKE_CURRENT_SOURCE_DIR}/${source} NAME_WE)
  get_filename_component(source_src_dir  ${CMAKE_CURRENT_SOURCE_DIR}/${source} DIRECTORY)
  get_filename_component(source_gen_dir  ${CMAKE_CURRENT_BINARY_DIR}/${source} DIRECTORY)
  add_custom_command(
          DEPENDS
          ${source_src_dir}/${source_filename}.g4
          OUTPUT
          ${source_gen_dir}/${source_filename}Lexer.h
          ${source_gen_dir}/${source_filename}Lexer.cpp
          ${source_gen_dir}/${source_filename}Parser.h
          ${source_gen_dir}/${source_filename}Parser.cpp
          ${source_gen_dir}/${source_filename}.interp
          ${source_gen_dir}/${source_filename}.tokens
          COMMAND
          java
          ARGS
          -jar ${CMAKE_SOURCE_DIR}/tools/antlr/antlr.jar
          -Dlanguage=Cpp
          -no-listener
          -no-visitor
          -o ${source_gen_dir}
          ${source_src_dir}/${source_filename}.g4
  )
  set(output ${source_gen_dir}/${source_filename}Parser.cpp ${source_gen_dir}/${source_filename}Lexer.cpp)
endfunction()
ArthurSonzogni commented 1 year ago

I think you can already do one or two files:

2 files:

ANTLR(INPUT MyLexer.g4)
ANTLR(INPUT MyParser.g4 DEPENDENCIES MyLexer.cpp)

1 file:

ANTLR(INPUT Combined.g4)

Isn't that sufficient?

andr1972 commented 1 year ago

ANTLR for Combined.g4 will generate CombinedLexer.cpp and CombinedParser.cpp, whereas for

MyLexer.g4 will generate MyLexer.cpp - the same base name as g4

Thanks, Your starter is very useful in writing C g4 grammar.