parser: Move message parsing to parser.c

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson 2018-01-30 16:10:54 -08:00
parent a3aa10545b
commit 63f0bedbda
5 changed files with 110 additions and 111 deletions

View File

@ -10,7 +10,6 @@
#include <unistd.h>
#include "list.h"
#include "parser.h"
#include "qmic.h"
#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
@ -252,6 +251,79 @@ static void qmi_const_parse()
list_add(&qmi_consts, &qc->node);
}
struct list_head qmi_messages = LIST_INIT(qmi_messages);
void qmi_message_parse(enum message_type message_type)
{
struct qmi_message_member *qmm;
struct qmi_message *qm;
struct token msg_id_tok;
struct token type_tok;
struct token num_tok;
struct token id_tok;
unsigned array;
bool required;
token_expect(TOK_ID, &msg_id_tok);
token_expect('{', NULL);
qm = malloc(sizeof(struct qmi_message));
qm->name = msg_id_tok.str;
qm->type = message_type;
list_init(&qm->members);
while (!token_accept('}', NULL)) {
array = 0;
if (token_accept(TOK_REQUIRED, NULL))
required = true;
else if (token_accept(TOK_OPTIONAL, NULL))
required = false;
else
yyerror("expected required, optional or '}'");
token_expect(TOK_TYPE, &type_tok);
token_expect(TOK_ID, &id_tok);
if (token_accept('[', NULL)) {
array = 1;
if (token_accept(TOK_NUM, &num_tok)) {
if (num_tok.num & 0xffff0000)
array = 4;
else if (num_tok.num & 0xff00)
array = 2;
}
token_expect(']', NULL);
}
token_expect('=', NULL);
token_expect(TOK_NUM, &num_tok);
token_expect(';', NULL);
qmm = malloc(sizeof(struct qmi_message_member));
qmm->name = id_tok.str;
qmm->type = type_tok.num;
qmm->qmi_struct = type_tok.qmi_struct;
qmm->id = num_tok.num;
qmm->required = required;
qmm->array = array;
list_add(&qm->members, &qmm->node);
}
if (token_accept('=', NULL)) {
token_expect(TOK_NUM, &num_tok);
qm->msg_id = num_tok.num;
}
token_expect(';', NULL);
list_add(&qmi_messages, &qm->node);
}
const char *qmi_package;
void qmi_parse(void)

View File

@ -1,16 +0,0 @@
#ifndef __PARSER_H__
#define __PARSER_H__
void qmi_parse(void);
extern const char *qmi_package;
struct qmi_const {
const char *name;
unsigned value;
struct list_head node;
};
extern struct list_head qmi_consts;
#endif

View File

@ -6,99 +6,6 @@
#include "list.h"
#include "qmic.h"
struct qmi_message_member {
const char *name;
int type;
struct qmi_struct *qmi_struct;
int id;
bool required;
unsigned array;
struct list_head node;
};
struct qmi_message {
enum message_type type;
const char *name;
unsigned msg_id;
struct list_head node;
struct list_head members;
};
static struct list_head qmi_messages = LIST_INIT(qmi_messages);
void qmi_message_parse(enum message_type message_type)
{
struct qmi_message_member *qmm;
struct qmi_message *qm;
struct token msg_id_tok;
struct token type_tok;
struct token num_tok;
struct token id_tok;
unsigned array;
bool required;
token_expect(TOK_ID, &msg_id_tok);
token_expect('{', NULL);
qm = malloc(sizeof(struct qmi_message));
qm->name = msg_id_tok.str;
qm->type = message_type;
list_init(&qm->members);
while (!token_accept('}', NULL)) {
array = 0;
if (token_accept(TOK_REQUIRED, NULL))
required = true;
else if (token_accept(TOK_OPTIONAL, NULL))
required = false;
else
yyerror("expected required, optional or '}'");
token_expect(TOK_TYPE, &type_tok);
token_expect(TOK_ID, &id_tok);
if (token_accept('[', NULL)) {
array = 1;
if (token_accept(TOK_NUM, &num_tok)) {
if (num_tok.num & 0xffff0000)
array = 4;
else if (num_tok.num & 0xff00)
array = 2;
}
token_expect(']', NULL);
}
token_expect('=', NULL);
token_expect(TOK_NUM, &num_tok);
token_expect(';', NULL);
qmm = malloc(sizeof(struct qmi_message_member));
qmm->name = id_tok.str;
qmm->type = type_tok.num;
qmm->qmi_struct = type_tok.qmi_struct;
qmm->id = num_tok.num;
qmm->required = required;
qmm->array = array;
list_add(&qm->members, &qmm->node);
}
if (token_accept('=', NULL)) {
token_expect(TOK_NUM, &num_tok);
qm->msg_id = num_tok.num;
}
token_expect(';', NULL);
list_add(&qmi_messages, &qm->node);
}
static void qmi_message_emit_message_type(FILE *fp,
const char *package,
const char *message)

1
qmic.c
View File

@ -10,7 +10,6 @@
#include <unistd.h>
#include "list.h"
#include "parser.h"
#include "qmic.h"
const char *sz_simple_types[] = {

37
qmic.h
View File

@ -39,6 +39,40 @@ struct token {
extern const char *sz_simple_types[];
extern const char *qmi_package;
struct qmi_const {
const char *name;
unsigned value;
struct list_head node;
};
struct qmi_message_member {
const char *name;
int type;
struct qmi_struct *qmi_struct;
int id;
bool required;
unsigned array;
struct list_head node;
};
struct qmi_message {
enum message_type type;
const char *name;
unsigned msg_id;
struct list_head node;
struct list_head members;
};
extern struct list_head qmi_consts;
extern struct list_head qmi_messages;
void yyerror(const char *fmt, ...) __attribute__((noreturn));
void symbol_add(const char *name, int token, ...);
@ -55,4 +89,7 @@ void qmi_struct_header(FILE *fp, const char *package);
void qmi_struct_emit_prototype(FILE *fp, const char *package, const char *message, const char *member, unsigned array_size, struct qmi_struct *qs);
void qmi_struct_emit_accessors(FILE *fp, const char *package, const char *message, const char *member, int member_id, unsigned array_size, struct qmi_struct *qs);
void qmi_parse(void);
#endif