#
#  Copyright 2021 Google, Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at:
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

# Flex (fast lexical analyzer generator) is used for parsing languages. It is
# commonly used with a parser generator (like Bison).

# Generate source files using Flex.
#
# Note: We generate .cc files for Flex. However, you must set %option c++ if you
# want to generate a C++ scanner class. We don't use --c++ in case you have
# %option reentrant set, which is mutually exclusive with %option c++.
#
# Parameters:
#   sources: Files used to generate the lexers.
template("flex_source") {
  action_name = "${target_name}_gen"
  action_foreach(action_name) {
    forward_variables_from(invoker, [ "sources" ])
    assert(defined(sources), "sources must be set")
    foreach(s, sources) {
      assert(get_path_info(s, "extension") == "ll",
             "Expecting Flex extension ll: ${s}")
    }

    script = "//common-mk/file_generator_wrapper.py"
    outformat = "${target_gen_dir}/{{source_name_part}}.cc"
    args = [
      "flex",
      "-o",
      outformat,
      "{{source}}",
    ]
    outputs = [ outformat ]
  }

  all_dependent_config_name = "_${target_name}_all_dependent_config"
  config(all_dependent_config_name) {
    include_dirs = [ "${target_gen_dir}" ]
  }

  source_set(target_name) {
    sources = get_target_outputs(":${action_name}")
    all_dependent_configs = [ ":${all_dependent_config_name}" ]
    deps = [ ":${action_name}" ]

    if (defined(invoker.configs)) {
      configs += invoker.configs
    }

    # Silence some warnings. The autogenerated code includes parts that have
    # fallthroughs and unreachable code. This may silence legitimate issues in
    # user written code in the lexer as well. User beware!
    cflags_cc = [
      "-Wno-implicit-fallthrough",
      "-Wno-unreachable-code",
    ]
  }
}