Skip to content

Commit

Permalink
lexer: Keep track of line numbers
Browse files Browse the repository at this point in the history
This adds the new field "line" to p11_lexer so any errors are printed
with the line numbers where they happen.  Also make the header to use
appropriate types for sizes and token types.

Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Apr 6, 2023
1 parent b83fb0a commit 4d9f9b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
18 changes: 14 additions & 4 deletions common/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ p11_lexer_next (p11_lexer *lexer,
end += 1;
else
end = lexer->at + lexer->remaining;
/* Count newlines in the PEM block */
pos = lexer->at;
for (; pos < end; pos++) {
pos = memchr (pos, '\n', end - pos);
if (!pos)
break;
lexer->line++;
}
lexer->tok_type = TOK_PEM;
lexer->tok.pem.begin = lexer->at;
lexer->tok.pem.length = end - lexer->at;
Expand All @@ -141,6 +149,7 @@ p11_lexer_next (p11_lexer *lexer,
lexer->at = end;
} else {
assert ((end - lexer->at) + 1 <= lexer->remaining);
lexer->line++;
lexer->remaining -= (end - lexer->at) + 1;
lexer->at = end + 1;
}
Expand Down Expand Up @@ -220,18 +229,19 @@ p11_lexer_msg (p11_lexer *lexer,

switch (lexer->tok_type) {
case TOK_FIELD:
p11_message ("%s: %s: %s", lexer->filename,
p11_message ("%s:%zu: %s: %s", lexer->filename, lexer->line,
lexer->tok.field.name, msg);
break;
case TOK_SECTION:
p11_message ("%s: [%s]: %s", lexer->filename,
p11_message ("%s:%zu: [%s]: %s", lexer->filename, lexer->line,
lexer->tok.section.name, msg);
break;
case TOK_PEM:
p11_message ("%s: BEGIN ...: %s", lexer->filename, msg);
p11_message ("%s:%zu: BEGIN ...: %s", lexer->filename,
lexer->line, msg);
break;
default:
p11_message ("%s: %s", lexer->filename, msg);
p11_message ("%s:%zu: %s", lexer->filename, lexer->line, msg);
break;
}

Expand Down
11 changes: 6 additions & 5 deletions common/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@

#include "compat.h"

enum {
typedef enum _p11_lexer_token_type {
TOK_EOF = 0,
TOK_SECTION = 1,
TOK_FIELD,
TOK_PEM,
};
} p11_lexer_token_type;

typedef struct {
char *filename;
size_t line;
const char *at;
int remaining;
int complained;
size_t remaining;
bool complained;

int tok_type;
p11_lexer_token_type tok_type;
union {
struct {
char *name;
Expand Down
4 changes: 4 additions & 0 deletions trust/persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ p11_persist_read (p11_persist *persist,
failed = !pem_to_attributes (&lexer, &attrs);
}
break;
default:
assert_not_reached ();
}

if (failed)
Expand Down Expand Up @@ -837,6 +839,8 @@ p11_persist_check (p11_persist *persist,
failed = true;
}
break;
default:
assert_not_reached ();
}
}

Expand Down

0 comments on commit 4d9f9b5

Please sign in to comment.