Restructure bison generation to allow multiple forntends
This commit is contained in:
parent
b83f057e49
commit
0e5ef46512
@ -7,7 +7,9 @@ add_compile_options(-Wall -Wextra)
|
|||||||
aux_source_directory("src" SRC_DIR)
|
aux_source_directory("src" SRC_DIR)
|
||||||
aux_source_directory("src/tpr" SRC_TPR_DIR)
|
aux_source_directory("src/tpr" SRC_TPR_DIR)
|
||||||
|
|
||||||
set (SRC_GENERATED "${CMAKE_CURRENT_BINARY_DIR}/tpr-parser.cpp" "${CMAKE_CURRENT_BINARY_DIR}/tpr-scanner.cpp")
|
set(TPR_PARSER_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated-tpr")
|
||||||
|
|
||||||
|
set (SRC_GENERATED "${TPR_PARSER_DIR}/tpr-parser.cpp" "${TPR_PARSER_DIR}/tpr-scanner.cpp")
|
||||||
|
|
||||||
set (SOURCES
|
set (SOURCES
|
||||||
${SRC_DIR}
|
${SRC_DIR}
|
||||||
@ -15,30 +17,30 @@ set (SOURCES
|
|||||||
${SRC_GENERATED}
|
${SRC_GENERATED}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
DEPENDS
|
DEPENDS
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l
|
${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l
|
||||||
OUTPUT
|
OUTPUT
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/tpr-scanner.cpp
|
${TPR_PARSER_DIR}/tpr-scanner.cpp
|
||||||
COMMAND
|
COMMAND
|
||||||
flex -+ -o "${CMAKE_CURRENT_BINARY_DIR}/tpr-scanner.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l"
|
mkdir -p "${TPR_PARSER_DIR}" && flex -+ -o "${TPR_PARSER_DIR}/tpr-scanner.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.l"
|
||||||
)
|
)
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
DEPENDS
|
DEPENDS
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp
|
${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp
|
||||||
OUTPUT
|
OUTPUT
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/tpr-parser.cpp
|
${TPR_PARSER_DIR}/tpr-parser.cpp
|
||||||
COMMAND
|
COMMAND
|
||||||
mkdir -p "${CMAKE_CURRENT_BINARY_DIR}/generated" && bison -o"${CMAKE_CURRENT_BINARY_DIR}/tpr-parser.cpp" --header="${CMAKE_CURRENT_BINARY_DIR}/generated/tpr-parser.hpp" "${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp"
|
mkdir -p "${TPR_PARSER_DIR}/include/tpr-parser" && ${CMAKE_CURRENT_SOURCE_DIR}/bison-wrapper.sh "${TPR_PARSER_DIR}/tpr-parser.cpp" "${TPR_PARSER_DIR}/include/tpr-parser/tpr-parser.hpp" "${TPR_PARSER_DIR}/include/tpr-parser/location.hh" --header=tpr-parser.hpp ${CMAKE_CURRENT_SOURCE_DIR}/parser/tpr.ypp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SET_SOURCE_FILES_PROPERTIES(${SRC_GENERATED} PROPERTIES GENERATED 1)
|
SET_SOURCE_FILES_PROPERTIES(${SRC_GENERATED} PROPERTIES GENERATED 1)
|
||||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
|
|
||||||
|
target_include_directories(${PROJECT_NAME} PRIVATE "${TPR_PARSER_DIR}/include" "${TPR_PARSER_DIR}/include/tpr-parser")
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||||
# TEMPORARY FIx:
|
# TEMPORARY FIx:
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
|
#target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
|
31
tprcc/bison-wrapper.sh
Executable file
31
tprcc/bison-wrapper.sh
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Usage: bison-wrapper.sh <c-file> <include-file> <location.hh> <bison_file> <bison-parameters>
|
||||||
|
if [[ $# -lt 4 ]]; then
|
||||||
|
echo "Error. Not enough parameters"
|
||||||
|
exit -1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cfile=$1
|
||||||
|
shift
|
||||||
|
include=$1
|
||||||
|
shift
|
||||||
|
location=$1
|
||||||
|
shift
|
||||||
|
|
||||||
|
tmpdir=`mktemp -d`
|
||||||
|
cd $tmpdir
|
||||||
|
echo "Using $tmpdir"
|
||||||
|
echo cp *.tab.cpp "$cfile"
|
||||||
|
echo cp *.hpp "$include"
|
||||||
|
echo cp location.hh "$location"
|
||||||
|
|
||||||
|
bison $@
|
||||||
|
cp *.tab.cpp "$cfile"
|
||||||
|
cp *.hpp "$include"
|
||||||
|
cp location.hh "$location"
|
||||||
|
|
||||||
|
|
||||||
|
rm -rfv "$tmpdir"
|
@ -6,8 +6,8 @@
|
|||||||
#include <FlexLexer.h>
|
#include <FlexLexer.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "tpr-parser.hpp"
|
#include <tpr-parser/tpr-parser.hpp>
|
||||||
#include "location.hh"
|
#include <tpr-parser/location.hh>
|
||||||
|
|
||||||
namespace tpr {
|
namespace tpr {
|
||||||
|
|
||||||
|
@ -19,26 +19,17 @@ enum class CommandType {
|
|||||||
digio_wait,
|
digio_wait,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CommandSpec {
|
|
||||||
CommandType type;
|
|
||||||
std::vector<bool> param_is_whole_num;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TprCommand {
|
class TprCommand {
|
||||||
private:
|
private:
|
||||||
static const CommandSpec m_specs[11];
|
|
||||||
|
|
||||||
CommandType m_type;
|
CommandType m_type;
|
||||||
std::vector<float> m_parameters;
|
std::vector<float> m_parameters;
|
||||||
bool is_whole_number(float num) const;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TprCommand(CommandType type);
|
TprCommand(CommandType type);
|
||||||
TprCommand(CommandType type, const std::vector<float> ¶ms);
|
TprCommand(CommandType type, const std::vector<float> ¶ms);
|
||||||
TprCommand(CommandType type, const std::vector<float> &¶ms);
|
TprCommand(CommandType type, const std::vector<float> &¶ms);
|
||||||
|
|
||||||
|
|
||||||
bool check_command(bool print_status) const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,7 +19,9 @@ using token = tpr::TempProfileParser::token;
|
|||||||
NEWLINE "\n"|"\r\n"
|
NEWLINE "\n"|"\r\n"
|
||||||
COMMENT_LINE "#".*\n
|
COMMENT_LINE "#".*\n
|
||||||
SPACE "\t"|" "
|
SPACE "\t"|" "
|
||||||
NUM [-+]?([0-9]*\.[0-9]+|[0-9]+)
|
|
||||||
|
NUM_INT [-+]?([0-9]+)
|
||||||
|
NUM_FLOAT [-+]?([0-9]*\.[0-9]+)
|
||||||
|
|
||||||
%%
|
%%
|
||||||
%{
|
%{
|
||||||
@ -29,7 +31,8 @@ NUM [-+]?([0-9]*\.[0-9]+|[0-9]+)
|
|||||||
<*>{SPACE} { /*Ignore spaces */}
|
<*>{SPACE} { /*Ignore spaces */}
|
||||||
{COMMENT_LINE} {loc->lines(); return token::lineend;}
|
{COMMENT_LINE} {loc->lines(); return token::lineend;}
|
||||||
{NEWLINE} {loc->lines(); return token::lineend;}
|
{NEWLINE} {loc->lines(); return token::lineend;}
|
||||||
{NUM} {yylval->build<float>(std::stof(std::string(yytext))); return token::number;}
|
{NUM_FLOAT} {yylval->build<float>(std::stof(std::string(yytext))); return token::number_float;}
|
||||||
|
{NUM_INT} {yylval->build<float>(std::stof(std::string(yytext))); return token::number_int;}
|
||||||
|
|
||||||
pid_conf { return token::kw_pid_conf; }
|
pid_conf { return token::kw_pid_conf; }
|
||||||
temp_set { return token::kw_temp_set; }
|
temp_set { return token::kw_temp_set; }
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
%define api.namespace {tpr}
|
%define api.namespace {tpr}
|
||||||
%define api.parser.class {TempProfileParser}
|
%define api.parser.class {TempProfileParser}
|
||||||
|
|
||||||
|
|
||||||
%define parse.error verbose
|
%define parse.error verbose
|
||||||
|
|
||||||
%code requires{
|
%code requires{
|
||||||
@ -22,6 +23,7 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <tpr/tpr-scanner.hpp>
|
#include <tpr/tpr-scanner.hpp>
|
||||||
|
#include <tpr/tpr-types.hpp>
|
||||||
#undef yylex
|
#undef yylex
|
||||||
#define yylex scanner.yylex
|
#define yylex scanner.yylex
|
||||||
}
|
}
|
||||||
@ -30,7 +32,8 @@
|
|||||||
%locations
|
%locations
|
||||||
%start tpr_file
|
%start tpr_file
|
||||||
|
|
||||||
%token<float> number
|
%token<float> number_float
|
||||||
|
%token<float> number_int
|
||||||
%token lineend
|
%token lineend
|
||||||
%token kw_pid_conf
|
%token kw_pid_conf
|
||||||
%token kw_temp_set
|
%token kw_temp_set
|
||||||
@ -45,6 +48,8 @@
|
|||||||
%token kw_digio_wait
|
%token kw_digio_wait
|
||||||
%token unexpected_input
|
%token unexpected_input
|
||||||
|
|
||||||
|
%type<float>number number_truncated
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
tpr_file: tpr_command
|
tpr_file: tpr_command
|
||||||
@ -81,17 +86,25 @@ cmd_wait_time: kw_wait_time number;
|
|||||||
|
|
||||||
cmd_temp_ramp: kw_temp_ramp number number;
|
cmd_temp_ramp: kw_temp_ramp number number;
|
||||||
|
|
||||||
cmd_beep: kw_beep number;
|
cmd_beep: kw_beep number_truncated;
|
||||||
|
|
||||||
cmd_temp_off: kw_temp_off;
|
cmd_temp_off: kw_temp_off;
|
||||||
|
|
||||||
cmd_clear_flags: kw_clear_flags;
|
cmd_clear_flags: kw_clear_flags;
|
||||||
|
|
||||||
cmd_digio_conf: kw_digio_conf number number;
|
cmd_digio_conf: kw_digio_conf number_truncated number_truncated;
|
||||||
|
|
||||||
cmd_digio_set: kw_digio_set number number;
|
cmd_digio_set: kw_digio_set number_truncated number_truncated;
|
||||||
|
|
||||||
cmd_digio_wait: kw_digio_wait number number;
|
cmd_digio_wait: kw_digio_wait number_truncated number_truncated;
|
||||||
|
|
||||||
|
number_truncated: number_float {$$ = $1; std::cerr << "[WARN] Floating point number " << $1 << " will be truncated to an integer (" << (int)$1 << ") at location (" << @1 << ")" << std::endl;}
|
||||||
|
| number_int {$$ = $1;}
|
||||||
|
;
|
||||||
|
|
||||||
|
number: number_float {$$ = $1;}
|
||||||
|
| number_int {$$ = $1;}
|
||||||
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include "tprcc/logger.hpp"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <tpr/tpr-types.hpp>
|
#include <tpr/tpr-types.hpp>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
@ -5,19 +6,6 @@
|
|||||||
|
|
||||||
namespace tpr {
|
namespace tpr {
|
||||||
|
|
||||||
const CommandSpec TprCommand::m_specs[11] = {
|
|
||||||
{.type = CommandType::beep, .param_is_whole_num = std::vector<bool>{true}},
|
|
||||||
{.type = CommandType::clear_flags, .param_is_whole_num = std::vector<bool>{}},
|
|
||||||
{.type = CommandType::digio_conf, .param_is_whole_num = std::vector<bool>{true, true}},
|
|
||||||
{.type = CommandType::digio_set, .param_is_whole_num = std::vector<bool>{true, true}},
|
|
||||||
{.type = CommandType::digio_wait, .param_is_whole_num = std::vector<bool>{true, true}},
|
|
||||||
{.type = CommandType::wait_temp, .param_is_whole_num = std::vector<bool>{false}},
|
|
||||||
{.type = CommandType::wait_time, .param_is_whole_num = std::vector<bool>{false}},
|
|
||||||
{.type = CommandType::temp_off, .param_is_whole_num = std::vector<bool>{}},
|
|
||||||
{.type = CommandType::temp_ramp, .param_is_whole_num = std::vector<bool>{true, true}},
|
|
||||||
{.type = CommandType::pid_conf, .param_is_whole_num = std::vector<bool>{false, false, false, false, false, false}},
|
|
||||||
{.type = CommandType::temp_set, .param_is_whole_num = std::vector<bool>{false}}
|
|
||||||
};
|
|
||||||
|
|
||||||
TprCommand::TprCommand(CommandType type)
|
TprCommand::TprCommand(CommandType type)
|
||||||
{
|
{
|
||||||
@ -34,37 +22,5 @@ TprCommand::TprCommand(CommandType type, const std::vector<float> &¶ms) : Tp
|
|||||||
m_parameters = std::move(params);
|
m_parameters = std::move(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TprCommand::is_whole_number(float num) const
|
|
||||||
{
|
|
||||||
if (floor(num) == num)
|
|
||||||
return true;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TprCommand::check_command(bool print_status) const
|
|
||||||
{
|
|
||||||
for (unsigned int i = 0; i < 11; i++) {
|
|
||||||
if (m_specs[i].type == m_type) {
|
|
||||||
/* Check size of param vector. This is just to be safe and should never fail */
|
|
||||||
if (m_parameters.size() != m_specs[i].param_is_whole_num.size()) {
|
|
||||||
std::cerr << "[ERR] Parameter count mismatch in command. This is a compiler bug!" << std::endl;
|
|
||||||
std::abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int param_idx = 0; i < m_parameters.size(); i++) {
|
|
||||||
if (is_whole_number(m_parameters[param_idx]) ^ m_specs[i].param_is_whole_num[param_idx]) {
|
|
||||||
if (print_status) {
|
|
||||||
// TODO:::: Continue here!!
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user