diff --git a/parser.c b/parser.c index d5fe5e5..2f3aa5c 100644 --- a/parser.c +++ b/parser.c @@ -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)) { diff --git a/tests/comments.qmi b/tests/comments.qmi new file mode 100644 index 0000000..461072e --- /dev/null +++ b/tests/comments.qmi @@ -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