mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 18:03:31 +01:00
grammar.py and friends: Improve line numbers and headers
Line numbers of parsed input are now automatically maintained. More needed data structures are now automatically created in the API header file. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
a3a8313ce8
commit
3cba245b77
5 changed files with 76 additions and 71 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
[white_space[
|
||||
type = token
|
||||
lex_extra_action = "if memchr(yytext, '\n', yyleng) context->line++;"
|
||||
regex = "[ \n\t\r]+"
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,33 +0,0 @@
|
|||
#ifndef _JW_PYTHON_GRAMMARTEST_PARSER_DEFS_H
|
||||
#define _JW_PYTHON_GRAMMARTEST_PARSER_DEFS_H
|
||||
|
||||
#define YY_NO_INPUT
|
||||
#define YY_NO_UNPUT
|
||||
// #define YY_NO_UNISTD_H
|
||||
|
||||
struct context {
|
||||
int line;
|
||||
int column;
|
||||
};
|
||||
|
||||
union YYSTYPE;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* defined in grammartest-parser.l */
|
||||
struct vp_scanner;
|
||||
struct vp_scanner *grammartest_default_init_scanner(const char *str);
|
||||
void *grammartest_default_scanner_get_data(const struct vp_scanner *scanner);
|
||||
void grammartest_default_cleanup_scanner(struct vp_scanner *scanner);
|
||||
|
||||
void FB_SYM(error)(struct context *context, void *scanner, const char *s);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
|
||||
#define YY_DECL int FB_SYM(lex)(YYSTYPE *yylval_param, struct context *context, void *yyscanner)
|
||||
|
||||
#endif /* #ifndef _JW_PYTHON_GRAMMARTEST_PARSER_DEFS_H */
|
||||
|
|
@ -1,19 +1,10 @@
|
|||
#include <string>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <misc.h>
|
||||
#include <slog.h>
|
||||
#include <YMisc.h>
|
||||
|
||||
// TODO: this should be included from grammartest.tab.h automatically
|
||||
#include "include/grammartest.h"
|
||||
|
||||
#include "include/defs.h"
|
||||
#include "include/grammartest.tab.h"
|
||||
|
||||
extern int FB_SYM(debug);
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
|
|
@ -30,20 +21,9 @@ int main(int argc, const char *argv[])
|
|||
return 1;
|
||||
}
|
||||
|
||||
// TODO: Initialize this in a generated function
|
||||
struct context context = {
|
||||
line: 1,
|
||||
column: 0
|
||||
};
|
||||
|
||||
FB_SYM(debug) = 1;
|
||||
|
||||
struct vp_scanner *scanner = FB_SYM(init_scanner)(content.c_str());
|
||||
int status = FB_SYM(parse)(&context, FB_SYM(scanner_get_data)(scanner));
|
||||
FB_SYM(cleanup_scanner)(scanner);
|
||||
if (status) {
|
||||
slog(PRI_ERR, "failed to parse [%s] (%s)", path, err());
|
||||
return -1;
|
||||
if (FB_SYM(create_ast)(content.c_str())<0) {
|
||||
slog(PRI_ERR, "failed to create AST from [%s] (%s)", path, err());
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue