Skip to content

Commit

Permalink
Embed default syntax files for all three languages into each binary.
Browse files Browse the repository at this point in the history
Previously re2c was built with syntax file for C, re2go with that for Go
and re2rust with that for Rust. However each binary needs all three
configs, as the language may be changed dynamically with --lang option.
  • Loading branch information
skvadrik committed Nov 18, 2023
1 parent e0c985b commit aeb4f7a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
23 changes: 9 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ add_library(re2c_objects_autogen OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/parse/lex.h"
"${CMAKE_CURRENT_BINARY_DIR}/src/parse/lex_conf.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/options/parse_opts.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_c.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_go.cc"
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_rust.cc"
"${re2c_docs}"
)
add_library(re2c_objects_autogen_ver_to_vernum OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/msg/ver_to_vernum.cc"
)
add_library(re2c_objects_autogen_syntax_c OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_c.cc"
)

set(re2c_sources
src/codegen/helpers.cc
Expand Down Expand Up @@ -209,28 +209,23 @@ re2c_bootstrap_parser(
"src/codegen/syntax_parser.ypp"
"src/codegen/syntax_parser.cc"
"src/codegen/syntax_parser.h")

re2c_bootstrap_syntax("include/syntax/c" "src/default_syntax_c.cc")
re2c_bootstrap_syntax("include/syntax/go" "src/default_syntax_go.cc")
re2c_bootstrap_syntax("include/syntax/rust" "src/default_syntax_rust.cc")

# re2c
add_executable(re2c ${re2c_sources} $<TARGET_OBJECTS:re2c_objects_autogen_syntax_c>)
add_executable(re2c ${re2c_sources})

# re2go
if (RE2C_BUILD_RE2GO)
re2c_bootstrap_syntax("include/syntax/go" "src/default_syntax_go.cc")
add_library(re2c_objects_autogen_syntax_go OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_go.cc"
)
add_executable(re2go ${re2c_sources} $<TARGET_OBJECTS:re2c_objects_autogen_syntax_go>)
add_executable(re2go ${re2c_sources})
target_compile_definitions(re2go PUBLIC "RE2C_LANG=Lang::GO")
endif()

# re2rust
if (RE2C_BUILD_RE2RUST)
re2c_bootstrap_syntax("include/syntax/rust" "src/default_syntax_rust.cc")
add_library(re2c_objects_autogen_syntax_rust OBJECT
"${CMAKE_CURRENT_BINARY_DIR}/src/default_syntax_rust.cc"
)
add_executable(re2rust ${re2c_sources} $<TARGET_OBJECTS:re2c_objects_autogen_syntax_rust>)
add_executable(re2rust ${re2c_sources})
target_compile_definitions(re2rust PUBLIC "RE2C_LANG=Lang::RUST")
endif()

Expand Down
12 changes: 7 additions & 5 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ re2c_GEN_SRC = \
src/codegen/syntax_lexer.cc \
src/codegen/syntax_parser.cc \
src/parse/lex_conf.cc \
src/options/parse_opts.cc
src/default_syntax_c.cc
src/options/parse_opts.cc \
src/default_syntax_c.cc \
src/default_syntax_go.cc \
src/default_syntax_rust.cc
re2c_GEN_HDR = \
src/codegen/syntax_parser.h \
src/parse/lex.h \
$(re2c_GEN_PARSER_HDR)
re2c_GEN = \
$(re2c_GEN_SRC) \
$(re2c_GEN_HDR)
nodist_re2c_SOURCES = $(re2c_GEN) src/default_syntax_c.cc
nodist_re2c_SOURCES = $(re2c_GEN)

# custom rules create headers and must go before normal rules
BUILT_SOURCES = $(re2c_GEN_SRC)
Expand Down Expand Up @@ -528,13 +530,13 @@ if WITH_GOLANG
bin_PROGRAMS += re2go
re2go_CXXFLAGS = $(AM_CXXFLAGS) -DRE2C_LANG=Lang::GO
re2go_SOURCES = $(re2c_SOURCES)
nodist_re2go_SOURCES = $(re2c_GEN) src/default_syntax_go.cc
nodist_re2go_SOURCES = $(nodist_re2c_SOURCES)
endif

# re2rust
if WITH_RUST
bin_PROGRAMS += re2rust
re2rust_CXXFLAGS = $(AM_CXXFLAGS) -DRE2C_LANG=Lang::RUST
re2rust_SOURCES = $(re2c_SOURCES)
nodist_re2rust_SOURCES = $(re2c_GEN) src/default_syntax_rust.cc
nodist_re2rust_SOURCES = $(nodist_re2c_SOURCES)
endif
2 changes: 1 addition & 1 deletion bootstrap/src/default_syntax_c.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const char* DEFAULT_SYNTAX =
const char* DEFAULT_SYNTAX_C =
"api = [pointers, generic];\n"
"api_style = [functions, freeform];\n"
"jump_model = [goto_label, loop_switch];\n"
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/src/default_syntax_go.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const char* DEFAULT_SYNTAX =
const char* DEFAULT_SYNTAX_GO =
"api = [generic];\n"
"api_style = [freeform, functions];\n"
"jump_model = [goto_label, loop_switch];\n"
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/src/default_syntax_rust.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const char* DEFAULT_SYNTAX =
const char* DEFAULT_SYNTAX_RUST =
"api = [generic];\n"
"api_style = [freeform, functions];\n"
"jump_model = [loop_switch];\n"
Expand Down
3 changes: 2 additions & 1 deletion build/stx2cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Encodes the contents of a syntax file as C++ source code.
"""

import os
import sys

if len(sys.argv) != 3:
Expand All @@ -14,7 +15,7 @@
output = sys.argv[2]

with open(output, 'w') as output_file:
output_file.write("const char* DEFAULT_SYNTAX =\n")
output_file.write("const char* DEFAULT_SYNTAX_" + os.path.basename(input).upper() + " =\n")

# write input file line by line as a string, escaping characters as needed
with open(input, 'r') as input_file:
Expand Down

0 comments on commit aeb4f7a

Please sign in to comment.