grammar.py and friends: Make test/grammar compile and start

Doesn't successfully parse grammartest.code, yet, it errors out with a
syntax error on whitespace. But at least it compiles and starts.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2017-10-30 13:05:22 +01:00
commit 16ce7abd93
5 changed files with 129 additions and 77 deletions

View file

@ -1096,10 +1096,6 @@ def grammar_create_y(grammar, opts):
for f in opts['includes']:
out += '#include "' + f + '"' + '\n'
#include "include/defs.h"
#include "include/vhdl2017.h"
#include "include/lex.vhdl2017.h"
#include "include/vhdl2017.tab.h"
out += "\nusing namespace " + opts['namespace'] + ';\n'
@ -1261,30 +1257,28 @@ def grammar_create_l(grammar, opts):
for f in opts['includes']:
out += '#include "' + f + '"' + '\n'
#include "include/defs.h"
#include "include/vhdl2017.h"
#// #include "include/lex.vhdl2017.h"
#include "include/vhdl2017.tab.h"
out += "\nusing namespace " + opts['namespace'] + ';\n'
#out += textwrap.dedent("""\
# /* This is too late in the Flex generated file to work. Still lots of
# * prototypes are spat into it above it, which end up with C++ linkage, of
# * course, causing the linkages to be inconsistent to the functions below this
# * extern "C". Only way I found was to use C++ is to use it on Bison only, and
# * have Flex use C instead. */
# #ifdef __cplusplus
# // extern "C" {
# #endif
# #ifdef _REMOVE_ME
# static void get_string(YYSTYPE *yylval_param, yyscan_t yyscanner, int skip);
# static void get_based_string(YYSTYPE *yylval_param, yyscan_t yyscanner, int skip);
# #endif
# %}
out += textwrap.dedent("""\
/* This is too late in the Flex generated file to work. Still lots of
* prototypes are spat into it above it, which end up with C++ linkage, of
* course, causing the linkages to be inconsistent to the functions below this
* extern "C". Only way I found was to use C++ is to use it on Bison only, and
* have Flex use C instead. */
#ifdef __cplusplus
// extern "C" {
#endif
#ifdef _REMOVE_ME
static void get_string(YYSTYPE *yylval_param, yyscan_t yyscanner, int skip);
static void get_based_string(YYSTYPE *yylval_param, yyscan_t yyscanner, int skip);
#endif
%}
%%
@ -1300,22 +1294,22 @@ def grammar_create_l(grammar, opts):
assert p.term[-1] in [ '"', "'" ], p.term
out += re.escape(p.term[1:-1]) + ' { return ' + p.sym + '; }\n'
out += textwrap.dedent("""\
%{/* basic_identifier */%}
%{/* extended_identifier */%}
%{/* based_integer */%}
%{/* bit_value */%}
%{/* numeric_literal */%}
%{/* enumeration_literal */%}
%{/* string_literal */%}
%{/* bit_string_literal */%}
%{/* character_literal */%}
%{/* graphic_character */%}
%{/* basic_character */%}
%{/* integer */%}
""")
#out += textwrap.dedent("""\
#
# %{/* basic_identifier */%}
# %{/* extended_identifier */%}
# %{/* based_integer */%}
# %{/* bit_value */%}
# %{/* numeric_literal */%}
# %{/* enumeration_literal */%}
# %{/* string_literal */%}
# %{/* bit_string_literal */%}
# %{/* character_literal */%}
# %{/* graphic_character */%}
# %{/* basic_character */%}
# %{/* integer */%}
#
# """)
ignore += textwrap.dedent("""\
@ -1378,8 +1372,7 @@ def grammar_create_l(grammar, opts):
void FB_SYM(error)(struct context *context, void *scanner, const char *msg)
{
struct yyguts_t *yyg =(struct yyguts_t*)scanner;
// vp_log(context->vp, VP_LOG_ERR, "%s at \\"%s\\" in line %d.\\n\\n", msg, yytext, context->lineno);
slog(PRI_ERR, "%s at \\"%s\\" in line %d.\\n\\n", msg, yytext, context->line);
set_error(PRI_ERR, EINVAL, "%s at \\"%s\\" in line %d", msg, yytext, context->line);
}
int FB_SYM(wrap)(void *scanner)
@ -1395,7 +1388,7 @@ def grammar_create_l(grammar, opts):
/* utilities which need to be placed here, because I can't find
* yylex_init() / _destroy() in any generated header file (??) */
struct vp_scanner *vhdl_default_init_scanner(const char *str)
struct vp_scanner *FB_SYM(init_scanner)(const char *str)
{
struct vp_scanner *r = (struct vp_scanner *)calloc(1, sizeof(*r));
@ -1408,12 +1401,12 @@ def grammar_create_l(grammar, opts):
return r;
}
void *vhdl_default_scanner_get_data(const struct vp_scanner *scanner)
void *FB_SYM(scanner_get_data)(const struct vp_scanner *scanner)
{
return scanner->scanner;
}
void vhdl_default_cleanup_scanner(struct vp_scanner *scanner)
void FB_SYM(cleanup_scanner)(struct vp_scanner *scanner)
{
free(scanner->str);
yy_delete_buffer(scanner->buf, scanner->scanner);
@ -1421,12 +1414,14 @@ def grammar_create_l(grammar, opts):
free(scanner);
}
#ifdef __cplusplus
// } // extern "C"
#endif
""")
# #ifdef __cplusplus
# // } // extern "C"
# #endif
#
# """)
return out
def grammar_create_h(grammar, opts):
@ -1497,7 +1492,18 @@ def grammar_create_h(grammar, opts):
if ns is not None:
out += '\n} /* namespace ' + ns + '*/'
out += '\n#endif /* #ifndef + ' + opts['mip'] + ' */'
out += textwrap.dedent("""\
struct vp_scanner;
struct vp_scanner *FB_SYM(init_scanner)(const char *str);
void *FB_SYM(scanner_get_data)(const struct vp_scanner *scanner);
void FB_SYM(cleanup_scanner)(struct vp_scanner *scanner);
""")
out += '\n\n#endif /* #ifndef + ' + opts['mip'] + ' */'
return out