Yan LR: Move code generation code into function
@@ -53,12 +53,13 @@ function (check_dependencies)
set (FOUND_DEPENDENCIES TRUE PARENT_SCOPE)
endfunction (check_dependencies)
-if (DEPENDENCY_PHASE)
- check_dependencies ()
- if (NOT FOUND_DEPENDENCIES)
- remove_plugin (yanlr ${FAILURE_MESSAGE})
- endif (NOT FOUND_DEPENDENCIES)
-
+# This functions generates the source files of the YAML parser using the given ANTLR executable (`ANTLR_EXECUTABLE`). Afterwards it sets the
+# variable
+#
+# - GENERATED_SOURCE_FILES to the list of source files generated by ANTLR
+#
+# .
+function (generate_code ANTLR_EXECUTABLE)
set (GRAMMAR_NAME YAML)
set (GRAMMAR_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${GRAMMAR_NAME}.g4)
set (TOKEN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${GRAMMAR_NAME}.tokens)
@@ -73,6 +74,21 @@ if (DEPENDENCY_PHASE)
${filepath})
endforeach (file ${GENERATED_SOURCE_FILES_NAMES})
+ set (GENERATED_SOURCE_FILES ${GENERATED_SOURCE_FILES} PARENT_SCOPE)
+
+ add_custom_command (OUTPUT ${GENERATED_SOURCE_FILES}
+ COMMAND ${ANTLR_EXECUTABLE} -Werror -Dlanguage=Cpp -o ${CMAKE_CURRENT_BINARY_DIR} -package antlr ${GRAMMAR_FILE}
+ DEPENDS ${GRAMMAR_FILE}
+ ${TOKEN_FILE}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+endfunction (generate_code)
+
+if (DEPENDENCY_PHASE)
+ check_dependencies ()
+ if (NOT FOUND_DEPENDENCIES)
+ remove_plugin (yanlr ${FAILURE_MESSAGE})
+ else (NOT FOUND_DEPENDENCIES)
+ generate_code (${ANTLR_EXECUTABLE})
set (SOURCE_FILES
"${GENERATED_SOURCE_FILES}"
listener.hpp
@@ -83,12 +99,7 @@ if (DEPENDENCY_PHASE)
yaml_lexer.cpp
yanlr.hpp
yanlr.cpp)
-
- add_custom_command (OUTPUT ${GENERATED_SOURCE_FILES}
- COMMAND ${ANTLR_EXECUTABLE} -Werror -Dlanguage=Cpp -o ${CMAKE_CURRENT_BINARY_DIR} -package antlr ${GRAMMAR_FILE}
- DEPENDS ${GRAMMAR_FILE}
- ${TOKEN_FILE}
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+ endif (NOT FOUND_DEPENDENCIES)
endif (DEPENDENCY_PHASE)
# ~~~
