attempting to parse lists

This commit is contained in:
Dana Jansens 2003-04-04 18:59:25 +00:00
parent 8dc158ac2d
commit 5da148530e
3 changed files with 67 additions and 27 deletions

View file

@ -65,7 +65,7 @@ void parse_token(ParseTokenType type, union ParseToken token)
func(type, token); func(type, token);
} }
static void parse_rc_token(ParseTokenType type, union ParseToken token) static void parse_rc_token(ParseToken *token)
{ {
static int got_eq = FALSE; static int got_eq = FALSE;
static ParseTokenType got_val = 0; static ParseTokenType got_val = 0;
@ -74,35 +74,35 @@ static void parse_rc_token(ParseTokenType type, union ParseToken token)
static gboolean b; static gboolean b;
if (id == NULL) { if (id == NULL) {
if (type == TOKEN_IDENTIFIER) { if (token->type == TOKEN_IDENTIFIER) {
id = token.identifier; id = token.identifier;
return; return;
} else { } else {
yyerror("syntax error"); yyerror("syntax error");
} }
} else if (!got_eq) { } else if (!got_eq) {
if (type == TOKEN_EQUALS) { if (token->type == TOKEN_EQUALS) {
got_eq = TRUE; got_eq = TRUE;
return; return;
} else { } else {
yyerror("syntax error"); yyerror("syntax error");
} }
} else if (!got_val) { } else if (!got_val) {
if (type == TOKEN_STRING) { if (token->type == TOKEN_STRING) {
s = token.string; s = token.string;
got_val = type; got_val = type;
return; return;
} else if (type == TOKEN_BOOL) { } else if (token->type == TOKEN_BOOL) {
b = token.bool; b = token.bool;
got_val = type; got_val = type;
return; return;
} else if (type == TOKEN_INTEGER) { } else if (token->type == TOKEN_INTEGER) {
i = token.integer; i = token.integer;
got_val = type; got_val = type;
return; return;
} else } else
yyerror("syntax error"); yyerror("syntax error");
} else if (type != TOKEN_NEWLINE) { } else if (token->type != TOKEN_NEWLINE) {
yyerror("syntax error"); yyerror("syntax error");
} else { } else {
ConfigValue v; ConfigValue v;
@ -133,5 +133,5 @@ static void parse_rc_token(ParseTokenType type, union ParseToken token)
id = s = NULL; id = s = NULL;
got_eq = FALSE; got_eq = FALSE;
got_val = 0; got_val = 0;
parse_free_token(type, token); parse_free_token(token->type, token);
} }

View file

@ -10,8 +10,7 @@ typedef enum {
TOKEN_STRING = STRING, TOKEN_STRING = STRING,
TOKEN_IDENTIFIER = IDENTIFIER, TOKEN_IDENTIFIER = IDENTIFIER,
TOKEN_BOOL = BOOL, TOKEN_BOOL = BOOL,
TOKEN_LBRACKET = '(', TOKEN_LIST,
TOKEN_RBRACKET = ')',
TOKEN_LBRACE = '{', TOKEN_LBRACE = '{',
TOKEN_RBRACE = '}', TOKEN_RBRACE = '}',
TOKEN_EQUALS = '=', TOKEN_EQUALS = '=',
@ -19,7 +18,12 @@ typedef enum {
TOKEN_NEWLINE = '\n' TOKEN_NEWLINE = '\n'
} ParseTokenType; } ParseTokenType;
typedef void (*ParseFunc)(ParseTokenType type, union ParseToken token); typedef struct {
ParseTokenType type;
union ParseTokenData data;
} ParseToken;
typedef void (*ParseFunc)(ParseToken *token);
void parse_startup(); void parse_startup();
void parse_shutdown(); void parse_shutdown();
@ -33,7 +37,7 @@ void parse_reg_section(char *section, ParseFunc func);
/* Free a parsed token's allocated memory */ /* Free a parsed token's allocated memory */
void parse_free_token(ParseTokenType type, union ParseToken token); void parse_free_token(ParseToken *token);
/* Display an error message while parsing. /* Display an error message while parsing.
found in parse.yacc */ found in parse.yacc */

View file

@ -6,13 +6,14 @@
%} %}
%union ParseToken { %union ParseTokenData {
float real; float real;
int integer; int integer;
char *string; char *string;
char *identifier; char *identifier;
gboolean bool; gboolean bool;
char character; char character;
GSList *list;
} }
%{ %{
@ -26,10 +27,10 @@ extern int yylineno;
extern FILE *yyin; extern FILE *yyin;
static char *path; static char *path;
static union ParseToken t; static ParseToken t;
/* in parse.c */ /* in parse.c */
void parse_token(ParseTokenType type, union ParseToken token); void parse_token(ParseToken *token);
void parse_set_section(char *section); void parse_set_section(char *section);
%} %}
@ -47,6 +48,9 @@ void parse_set_section(char *section);
%token <character> '\n' %token <character> '\n'
%token INVALID %token INVALID
%type <list> list
%type <list> listtokens
%% %%
sections: sections:
@ -54,7 +58,7 @@ sections:
; ;
lines: lines:
| lines tokens '\n' { t.character = $3; parse_token($3, t); } | lines tokens '\n' { t.type = $3; t.data.character = $3; parse_token(&t); }
; ;
tokens: tokens:
@ -63,19 +67,51 @@ tokens:
; ;
token: token:
REAL { t.real = $1; parse_token(REAL, t); } REAL { t.type = TOKEN_REAL; t.data.real = $1; parse_token(&t); }
| INTEGER { t.integer = $1; parse_token(INTEGER, t); } | INTEGER { t.type = TOKEN_INTEGER; t.data.integer = $1;
| STRING { t.string = $1; parse_token(STRING, t); } parse_token(&t); }
| IDENTIFIER { t.identifier = $1; parse_token(IDENTIFIER, t); } | STRING { t.type = TOKEN_STRING; t.data.string = $1; parse_token(&t); }
| BOOL { t.bool = $1; parse_token(BOOL, t); } | IDENTIFIER { t.type = TOKEN_IDENTIFIER; t.data.identifier = $1;
| '(' { t.character = $1; parse_token($1, t); } parse_token(&t);}
| ')' { t.character = $1; parse_token($1, t); } | BOOL { t.type = TOKEN_BOOL; t.data.bool = $1; parse_token(&t); }
| '{' { t.character = $1; parse_token($1, t); } | list { t.type = TOKEN_LIST; t.data.list = $1; parse_token(&t); }
| '}' { t.character = $1; parse_token($1, t); } | '{' { t.type = $1; t.data.character = $1; parse_token(&t); }
| '=' { t.character = $1; parse_token($1, t); } | '}' { t.type = $1; t.data.character = $1; parse_token(&t); }
| ',' { t.character = $1; parse_token($1, t); } | '=' { t.type = $1; t.data.character = $1; parse_token(&t); }
| ',' { t.type = $1; t.data.character = $1; parse_token(&t); }
; ;
list:
'(' listtokens ')' { $$ = $2; }
;
listtokens:
listtokens listtoken { ParseToken *nt = g_new(ParseToken, 1);
nt->type = t.type;
nt->data = t.data;
$$ = g_slist_append($1, nt);
}
| token { ParseToken *nt = g_new(ParseToken, 1);
nt->type = t.type;
nt->data = t.data;
$$ = g_slist_append(NULL, nt);
}
;
listtoken:
REAL { t.type = TOKEN_REAL; t.data.real = $1; }
| INTEGER { t.type = TOKEN_INTEGER; t.data.integer = $1; }
| STRING { t.type = TOKEN_STRING; t.data.string = $1; }
| IDENTIFIER { t.type = TOKEN_IDENTIFIER; t.data.identifier = $1; }
| BOOL { t.type = TOKEN_BOOL; t.data.bool = $1; }
| list { t.type = TOKEN_LIST; t.data.list = $1; }
| '{' { t.type = $1; t.data.character = $1; }
| '}' { t.type = $1; t.data.character = $1; }
| '=' { t.type = $1; t.data.character = $1; }
| ',' { t.type = $1; t.data.character = $1; }
;
%% %%
void yyerror(char *err) { void yyerror(char *err) {