parser: add support for comments

Add the ability for input files to have comments in them.  A comment
begins with a '#' character and continues through the end of the line.

Signed-off-by: Alex Elder <elder@linaro.org>
Message-Id: <20211001232338.769309-21-elder@linaro.org>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
This commit is contained in:
Alex Elder 2021-10-01 18:23:24 -05:00 committed by Bjorn Andersson
parent c172d15f2b
commit 69f032f380
2 changed files with 49 additions and 1 deletions

View File

@ -145,6 +145,26 @@ static void symbol_add(const char *name, enum token_id token_id, ...)
va_end(ap);
}
/* Skip over white space and comments (which start with '#', end with '\n') */
static bool skip(char ch)
{
static bool in_comment = false;
if (in_comment) {
if (ch == '\n')
in_comment = false;
return true;
}
if (isspace(ch))
return true;
if (ch == '#')
in_comment = true;
return in_comment;
}
static struct token yylex()
{
struct symbol *sym;
@ -154,7 +174,7 @@ static struct token yylex()
int base;
char ch;
while ((ch = input()) && isspace(ch))
while ((ch = input()) && skip(ch))
;
if (isalpha(ch)) {

28
tests/comments.qmi Normal file
View File

@ -0,0 +1,28 @@
# This is a comment at the beginning of the file
#
#############
package test;
#############
const TEST_REQUEST_RESPONSE = 35; # Skip me!!! # Skip me!!!
struct qmi_result {
u16 result;
# No limit on
u16# where comments
# can go
error;
};
request test_request {
required u8 test_number = 0x12;
} = 0x23; # Skip me!!! # Skip me!!!
response test_response {
required qmi_result r = 2;
} = 043;
indication test_indication {
optional u64 value = 0x99;
} = 0x7;
######## End the file with a comment