Commit bc8ea858 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Remove the lexer / parser

parent c6366959
Pipeline #108534 passed with stages
in 6 minutes and 33 seconds
......@@ -30,9 +30,6 @@ AM_CPPFLAGS += -I $(srcdir)/%reldir% -I %reldir%
include %reldir%/core/Makefile.am
include %reldir%/cards/Makefile.am
include %reldir%/counter/Makefile.am
include %reldir%/lexer/Makefile.am
include %reldir%/parser/Makefile.am
include %reldir%/cli/Makefile.am
include %reldir%/xml/Makefile.am
include %reldir%/layout/Makefile.am
include %reldir%/cairo/Makefile.am
......
# Makefile.am
#
# Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
noinst_LTLIBRARIES += %reldir%/libtarot-cli.la
tarotinclude_HEADERS += %reldir%/tarot/cli.h
%canon_reldir%_libtarot_cli_la_SOURCES = \
%reldir%/cli.c \
%reldir%/tarot/cli_private.h \
%reldir%/tarot/cli_private_impl.h
%canon_reldir%_libtarot_cli_la_LDFLAGS = \
-no-undefined $(AM_LDADD)
AM_CPPFLAGS += -I $(srcdir)/%reldir% -I %reldir%
INDENTED += $(%canon_reldir%_libtarot_cli_la_SOURCES) \
$(%canon_reldir%_check_SOURCES)
INTROSPECTED_SOURCES += %reldir%/cli.c
/**
* file tarot/cli.c
*
* Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <tarot/cli_private.h>
#include <stdlib.h>
TarotCli *
tarot_cli_alloc ()
{
return cli_alloc ();
}
void
tarot_cli_free (TarotCli * cli)
{
cli_free (cli);
}
TarotCli *
tarot_cli_dup (const TarotCli * cli)
{
(void) cli;
return NULL;
}
int
tarot_cli_push (TarotCli * cli, const char *data, int eof)
{
return cli_push (cli, data, eof);
}
int
tarot_cli_next (TarotCli * cli)
{
return cli_next (cli);
}
int
tarot_cli_has_command (const TarotCli * cli)
{
return cli_has_command (cli);
}
int
tarot_cli_is_continuation (const TarotCli * cli)
{
return cli_is_continuation (cli);
}
int
tarot_cli_is_help (const TarotCli * cli)
{
return cli_is_help (cli);
}
int
tarot_cli_is_version (const TarotCli * cli)
{
return cli_is_version (cli);
}
int
tarot_cli_is_license (const TarotCli * cli)
{
return cli_is_license (cli);
}
int
tarot_cli_is_error (const TarotCli * cli)
{
return cli_is_error (cli);
}
int
tarot_cli_is_status (const TarotCli * cli)
{
return cli_is_status (cli);
}
int
tarot_cli_is_load (const TarotCli * cli)
{
return cli_is_load (cli);
}
int
tarot_cli_is_save (const TarotCli * cli)
{
return cli_is_save (cli);
}
int
tarot_cli_is_check (const TarotCli * cli)
{
return cli_is_check (cli);
}
int
tarot_cli_is_event (const TarotCli * cli)
{
return cli_is_event (cli);
}
const char *
tarot_cli_get_error (const TarotCli * cli)
{
return cli_get_error (cli);
}
int
tarot_cli_get_with_imputation (const TarotCli * cli)
{
return cli_get_with_imputation (cli);
}
const char *
tarot_cli_get_filename (const TarotCli * cli)
{
return cli_get_filename (cli);
}
const TarotGameEvent *
tarot_cli_get_event (const TarotCli * cli)
{
return cli_get_event (cli);
}
#include <tarot/cli_private_impl.h>
/**
* file tarot/cli.h
*
* Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_CLI_INCLUDED
#define H_TAROT_CLI_INCLUDED
#include <stddef.h>
#include <tarot/cards.h>
#include <tarot/player.h>
#include <tarot/game_event.h>
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
struct TarotCli;
typedef struct TarotCli TarotCli;
/**
* tarot_cli_alloc: (constructor):
*/
TarotCli *tarot_cli_alloc ();
/**
* tarot_cli_free: (skip):
*/
void tarot_cli_free (TarotCli * cli);
/**
* tarot_cli_dup: (skip):
*
* This does NOT WORK yet! BUT a pointer to such a function is
* required for GObject introspection. DO NOT TRY TO COPY A CLI, A
* PARSER OR A LEXER INSTANCE!!!
*/
TarotCli *tarot_cli_dup (const TarotCli * cli);
/**
* tarot_cli_push:
* @eof: (type boolean):
*/
int tarot_cli_push (TarotCli * cli, const char *data, int eof);
int tarot_cli_next (TarotCli * cli);
/**
* tarot_cli_has_command:
* Returns: (type boolean):
*/
int tarot_cli_has_command (const TarotCli * cli);
/**
* tarot_cli_is_continuation:
* Returns: (type boolean):
*/
int tarot_cli_is_continuation (const TarotCli * cli);
/**
* tarot_cli_is_help:
* Returns: (type boolean):
*/
int tarot_cli_is_help (const TarotCli * cli);
/**
* tarot_cli_is_version:
* Returns: (type boolean):
*/
int tarot_cli_is_version (const TarotCli * cli);
/**
* tarot_cli_is_license:
* Returns: (type boolean):
*/
int tarot_cli_is_license (const TarotCli * cli);
/**
* tarot_cli_is_error:
* Returns: (type boolean):
*/
int tarot_cli_is_error (const TarotCli * cli);
const char *tarot_cli_get_error (const TarotCli * cli);
/**
* tarot_cli_is_status:
* Returns: (type boolean):
*/
int tarot_cli_is_status (const TarotCli * cli);
/**
* tarot_cli_get_with_imputation:
* Returns: (type boolean):
*/
int tarot_cli_get_with_imputation (const TarotCli * cli);
/**
* tarot_cli_is_load:
* Returns: (type boolean):
*/
int tarot_cli_is_load (const TarotCli * cli);
const char *tarot_cli_get_filename (const TarotCli * cli);
/**
* tarot_cli_is_save:
* Returns: (type boolean):
*/
int tarot_cli_is_save (const TarotCli * cli);
/**
* tarot_cli_is_check:
* Returns: (type boolean):
*/
int tarot_cli_is_check (const TarotCli * cli);
const TarotGameEvent *tarot_cli_get_event (const TarotCli * cli);
/**
* tarot_cli_is_event:
* Returns: (type boolean):
*/
int tarot_cli_is_event (const TarotCli * cli);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* not H_TAROT_CLI_INCLUDED */
/**
* file tarot/cli_private.h
*
* Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_CLI_PRIVATE_INCLUDED
#define H_TAROT_CLI_PRIVATE_INCLUDED
#include <tarot/cli.h>
#include <tarot/lexer.h>
#include <tarot/parser.h>
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
struct TarotCli
{
TarotLexer *lexer;
TarotParser *parser;
};
static TarotCli *cli_alloc ();
static void cli_free (TarotCli * cli);
static int cli_push (TarotCli * cli, const char *data, int eof);
static int cli_next (TarotCli * cli);
static int cli_has_command (const TarotCli * cli);
static int cli_is_continuation (const TarotCli * cli);
static int cli_is_help (const TarotCli * cli);
static int cli_is_version (const TarotCli * cli);
static int cli_is_license (const TarotCli * cli);
static int cli_is_error (const TarotCli * cli);
static int cli_is_status (const TarotCli * cli);
static int cli_is_load (const TarotCli * cli);
static int cli_is_save (const TarotCli * cli);
static int cli_is_check (const TarotCli * cli);
static int cli_is_event (const TarotCli * cli);
static const char *cli_get_error (const TarotCli * cli);
static int cli_get_with_imputation (const TarotCli * cli);
static const char *cli_get_filename (const TarotCli * cli);
static const TarotGameEvent *cli_get_event (const TarotCli * cli);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* not H_TAROT_CLI_PRIVATE_INCLUDED */
/**
* file tarot/cli_private_impl.h
*
* Copyright (C) 2018 Vivien Kraus <vivien@planete-kraus.eu>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <tarot/cli_private.h>
#include <assert.h>
#include <string.h>
#include <stdalign.h>
#ifndef N_
#define N_(String) (String)
#endif /* not N_ */
static inline TarotCli *
cli_alloc ()
{
TarotLexer *lexer = tarot_lexer_alloc ();
TarotParser *parser = tarot_parser_alloc ();
TarotCli *cli = malloc (sizeof (TarotCli));
if (lexer == NULL || parser == NULL || cli == NULL)
{
free (cli);
tarot_lexer_free (lexer);
tarot_parser_free (parser);
return NULL;
}
cli->lexer = lexer;
cli->parser = parser;
return cli;
}
static inline void
cli_free (TarotCli * cli)
{
if (cli)
{
tarot_lexer_free (cli->lexer);
tarot_parser_free (cli->parser);
}
free (cli);
}
static int
cli_transfer (TarotCli * cli)
{
while (tarot_parser_get_command_type (cli->parser) < 0
&& tarot_lexer_get_token_type (cli->lexer) >= 0)
{
int tt;
TarotParserTokenValue value;
TarotParserTokenPosition position;
tt = tarot_lexer_get_token_type (cli->lexer);
tarot_lexer_get_position (cli->lexer, &(position.fl), &(position.fc),
&(position.ll), &(position.lc));
if (tt == tarot_lexer_token_help ()
|| tt == tarot_lexer_token_version ()
|| tt == tarot_lexer_token_license ()
|| tt == tarot_lexer_token_eof ()
|| tt == tarot_lexer_token_status ()
|| tt == tarot_lexer_token_load ()
|| tt == tarot_lexer_token_save ()
|| tt == tarot_lexer_token_check ()
|| tt == tarot_lexer_token_setup ()
|| tt == tarot_lexer_token_with ()
|| tt == tarot_lexer_token_call ()
|| tt == tarot_lexer_token_deal ()
|| tt == tarot_lexer_token_to ()
|| tt == tarot_lexer_token_bid ()
|| tt == tarot_lexer_token_pass ()
|| tt == tarot_lexer_token_take ()
|| tt == tarot_lexer_token_push ()
|| tt == tarot_lexer_token_straight ()
|| tt == tarot_lexer_token_double ()
|| tt == tarot_lexer_token_keep ()
|| tt == tarot_lexer_token_decl ()
|| tt == tarot_lexer_token_slam ()
|| tt == tarot_lexer_token_call ()
|| tt == tarot_lexer_token_dog ()
|| tt == tarot_lexer_token_discard ()
|| tt == tarot_lexer_token_handful ()
|| tt == tarot_lexer_token_play ()
|| tt == tarot_lexer_token_newline ()
|| tt == tarot_lexer_token_imputation ())
{
/* No value attached, pass directly to the parser */
if (tarot_parser_push_token (cli->parser, tt, &value, &position) !=
0)
{
return 1;
}
if (tt == tarot_lexer_token_eof ())
{
break;
}
}
else if (tt == tarot_lexer_token_error ())
{
/* Unknown token, pass it to the parser */
if (tarot_parser_push_syntax_error (cli->parser, &position) != 0)
{
return 1;
}
}
else if (tt == tarot_lexer_token_number ())
{
value.number = tarot_lexer_get_number (cli->lexer);
if (tarot_parser_push_token (cli->parser, tt, &value, &position) !=
0)
{
return 1;
}
}
else if (tt == tarot_lexer_token_player ())
{
value.player = tarot_lexer_get_player (cli->lexer);
if (tarot_parser_push_token (cli->parser, tt, &value, &position) !=
0)
{
return 1;
}
}
else if (tt == tarot_lexer_token_card ())
{
value.card = tarot_lexer_get_card (cli->lexer);
if (tarot_parser_push_token (cli->parser, tt, &value, &position) !=
0)
{
return 1;
}
}
else if (tt == tarot_lexer_token_string ())
{
value.string = tarot_lexer_get_string (cli->lexer);
if (tarot_parser_push_token (cli->parser, tt, &value, &position) !=
0)
{
return 1;
}
}
else
{
/* You have made a new token type, but you forgot to add the
code to pass it to the parser. Add it in the first large if
(if it has no value). */
assert (0);
}
if (tarot_lexer_next (cli->lexer) != 0)
{
return 1;
}
}
return 0;
}
static inline int
cli_push (TarotCli * cli, const char *data, int eof)
{
if (tarot_lexer_push (cli->lexer, data, eof) != 0)
{
return 1;
}
if (!eof && tarot_lexer_push (cli->lexer, "\n", eof) != 0)
{
return 1;
}
return cli_transfer (cli);
}
static inline int
cli_next (TarotCli * cli)
{
tarot_parser_clear (cli->parser);
if (tarot_lexer_get_token_type (cli->lexer) != tarot_lexer_token_eof ())
{
return cli_transfer (cli);
}
return 0;
}
static inline int
cli_has_command (const TarotCli * cli)
{
return tarot_parser_get_command_type (cli->parser) >= 0;
}
static inline int
cli_is_continuation (const TarotCli * cli)
{
return tarot_parser_is_continuation (cli->parser);
}
static inline int
cli_is_help (const TarotCli * cli)
{
return tarot_parser_get_command_type (cli->parser) ==
tarot_parser_type_help ();
}
static inline int
cli_is_version (const TarotCli * cli)
{
return tarot_parser_get_command_type (cli->parser) ==
tarot_parser_type_version ();
}
static inline int
cli_is_license (const TarotCli * cli)
{