[MOD/FIX] Make the Parser API usable

It should now be good enough for parsing simple grammars(user IDs
for example)
This commit is contained in:
lda 2023-11-24 17:45:07 +01:00
parent 20a44a0664
commit b378d443c0
2 changed files with 59 additions and 5 deletions

View file

@ -26,6 +26,7 @@
#include <Cytoplasm/Memory.h> #include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
@ -72,7 +73,7 @@ ParserEndCopy(Parser * p)
{ {
char *ret; char *ret;
size_t len; size_t len;
if (!p || p->mark >= 0) if (!p || p->mark < 0)
{ {
return NULL; return NULL;
} }
@ -118,7 +119,44 @@ ParserExceptOneOf(Parser * p, const char * str)
p->eof = 1; p->eof = 1;
p->idx--; /* Avoid overflows */ 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 void
@ -131,3 +169,4 @@ ParserEnd(Parser * p)
Free(p->string); Free(p->string);
Free(p); Free(p);
} }

View file

@ -53,9 +53,23 @@ extern Parser * ParserCreate(char *);
extern int ParserExcept(Parser *, char); extern int ParserExcept(Parser *, char);
/** /**
* Returns true if the character at the current position in * Gets the currently pointed at character in the parser, then
* the parsing process is one of the characters in the inputted * 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. * string, and advances by one character eitherway.
*
* If non-zero, the return value is the actual character.
*/ */
extern int ParserExceptOneOf(Parser *, const char *); extern int ParserExceptOneOf(Parser *, const char *);
@ -80,4 +94,5 @@ extern char * ParserEndCopy(Parser *);
* .Fn ParserCreate . * .Fn ParserCreate .
*/ */
extern void ParserEnd(Parser *); extern void ParserEnd(Parser *);
#endif /* TELODENDRIA_PARSER_H */ #endif /* TELODENDRIA_PARSER_H */