From 20a44a0664dcaf2bc26090472764e3e301acf7e5 Mon Sep 17 00:00:00 2001 From: lda Date: Fri, 24 Nov 2023 04:16:15 +0100 Subject: [PATCH] [ADD/WIP] Add corresponding source code for parser --- src/Parser.c | 133 +++++++++++++++++++++++++++++++++++++++++++ src/include/Parser.h | 7 +++ 2 files changed, 140 insertions(+) create mode 100644 src/Parser.c diff --git a/src/Parser.c b/src/Parser.c new file mode 100644 index 0000000..7ddcbf7 --- /dev/null +++ b/src/Parser.c @@ -0,0 +1,133 @@ +/* + * Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net> + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include +#include + +#include +#include + +struct Parser { + char *string; + size_t idx; + + ssize_t mark; + + int eof; +}; + +Parser * +ParserCreate(char * str) +{ + Parser * ret; + if (!str) + { + return NULL; + } + + ret = Malloc(sizeof(Parser)); + + ret->string = StrDuplicate(str); + ret->idx = 0; + ret->eof = 0; + ret->mark = -1; + + return ret; +} + +void +ParserStartCopy(Parser * p) +{ + if (!p) + { + return; + } + p->mark = p->idx; +} +char * +ParserEndCopy(Parser * p) +{ + char *ret; + size_t len; + if (!p || p->mark >= 0) + { + return NULL; + } + + len = p->idx - p->mark; + + ret = Malloc(len + 1); + memcpy(ret, p->string + p->mark, len); + ret[len] = '\0'; + + return ret; +} + +int +ParserExcept(Parser * p, char c) +{ + char strc; + if (!p || p->eof) + { + return 0; + } + + strc = p->string[p->idx++]; + if (!strc) + { + p->eof = 1; + p->idx--; /* Avoid overflows */ + } + return strc == c; +} +int +ParserExceptOneOf(Parser * p, const char * str) +{ + char strc; + if (!p || !str || p->eof) + { + return 0; + } + + strc = p->string[p->idx++]; + if (!strc) + { + p->eof = 1; + p->idx--; /* Avoid overflows */ + } + return !!strchr(str, strc); +} + +void +ParserEnd(Parser * p) +{ + if (!p) + { + return; + } + Free(p->string); + Free(p); +} diff --git a/src/include/Parser.h b/src/include/Parser.h index 0f0d826..538f774 100644 --- a/src/include/Parser.h +++ b/src/include/Parser.h @@ -52,6 +52,13 @@ 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. + */ +extern int ParserExceptOneOf(Parser *, const char *); + /** * Starts a clipping session. This stores an index into the * parser, until