From b378d443c07463a2fef02d83eb2b4def11a444f4 Mon Sep 17 00:00:00 2001 From: lda Date: Fri, 24 Nov 2023 17:45:07 +0100 Subject: [PATCH] [MOD/FIX] Make the Parser API usable It should now be good enough for parsing simple grammars(user IDs for example) --- src/Parser.c | 43 +++++++++++++++++++++++++++++++++++++++++-- src/include/Parser.h | 21 ++++++++++++++++++--- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/Parser.c b/src/Parser.c index 7ddcbf7..5801634 100644 --- a/src/Parser.c +++ b/src/Parser.c @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -72,7 +73,7 @@ ParserEndCopy(Parser * p) { char *ret; size_t len; - if (!p || p->mark >= 0) + if (!p || p->mark < 0) { return NULL; } @@ -118,7 +119,44 @@ ParserExceptOneOf(Parser * p, const char * str) p->eof = 1; p->idx--; /* Avoid overflows */ } - return !!strchr(str, strc); + if (strchr(str, strc)) + { + return strc; + } + return 0; +} + +int +ParserGetc(Parser * p) +{ + int c; + if (!p || p->eof) + { + return EOF; + } + c = p->string[p->idx++]; + if (!c) + { + p->eof = 1; + p->idx--; /* Avoid overflows */ + c = EOF; + } + return c; +} +void +ParserUndo(Parser * p) +{ + if (!p) + { + return; + } + + if (!p->idx) + { + return; + } + p->eof = 0; + p->idx--; } void @@ -131,3 +169,4 @@ ParserEnd(Parser * p) Free(p->string); Free(p); } + diff --git a/src/include/Parser.h b/src/include/Parser.h index 538f774..6c9c281 100644 --- a/src/include/Parser.h +++ b/src/include/Parser.h @@ -53,9 +53,23 @@ extern Parser * ParserCreate(char *); extern int ParserExcept(Parser *, char); /** - * Returns true if the character at the current position in - * the parsing process is one of the characters in the inputted - * string, and advances by one character eitherway. + * Gets the currently pointed at character in the parser, then + * advances it by one, unless if hit by an EOF. + */ +extern int ParserGetc(Parser *); + +/** + * Rolls back the currently pointed character by one(unless already + * at the start.) + */ +extern void ParserUndo(Parser *); + +/** + * Returns a non-zero value if the character at the current position + * in the parsing process is one of the characters in the inputted + * string, and advances by one character eitherway. + * + * If non-zero, the return value is the actual character. */ extern int ParserExceptOneOf(Parser *, const char *); @@ -80,4 +94,5 @@ extern char * ParserEndCopy(Parser *); * .Fn ParserCreate . */ extern void ParserEnd(Parser *); + #endif /* TELODENDRIA_PARSER_H */