forked from Telodendria/Telodendria
[ADD/WIP] Add corresponding source code for parser
This commit is contained in:
parent
a493f3de85
commit
20a44a0664
2 changed files with 140 additions and 0 deletions
133
src/Parser.c
Normal file
133
src/Parser.c
Normal file
|
@ -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 <Parser.h>
|
||||||
|
|
||||||
|
#include <Cytoplasm/Memory.h>
|
||||||
|
#include <Cytoplasm/Str.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
|
@ -52,6 +52,13 @@ extern Parser * ParserCreate(char *);
|
||||||
*/
|
*/
|
||||||
extern int ParserExcept(Parser *, 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
|
* Starts a clipping session. This stores an index into the
|
||||||
* parser, until
|
* parser, until
|
||||||
|
|
Loading…
Reference in a new issue