working on csv data, parser works but needs more testing, might add bool to ParserData_DestroyFn callback for clearing

This commit is contained in:
herbglitch 2024-12-04 02:43:58 -07:00
parent 4c3d357cb9
commit ca6a9c118f
13 changed files with 384 additions and 279 deletions

View file

@ -5,28 +5,54 @@
extern "C" {
#endif
#include "arc/std/bool.h"
#include "arc/std/parser.h"
#include "arc/std/string.h"
#include <stdint.h>
/*
<line> -> <body> NEWLINE <line> | <body> | NEWLINE <line> | LAMBDA
<body> -> <tag> WHITESPACE ARROW WHITESPACE <arguments>
<arguments> -> <argument> WHITESPACE OR WHITESPACE <arguments> | <argument>
<argument> -> <tagOrConstant> WHITESPACE <argument> | <tagOrConstant>
<tagOrConstant> -> <tag> | <constant>
<constant> -> ALPHA_UPPER_CHAR <constantBody>
<constantBody> -> <constantChar> <constantBody> | LAMBDA
<constantChar> -> ALPHA_UPPER_CHAR | UNDERSCORE
<tag> -> LESS_THAN <variable> GREATER_THAN
<variable> -> <alphaChar> <variableBody> | UNDERSCORE <variableBody>
<variableBody> -> <variableChar> <variableBody> | LAMBDA
<variableChar> -> <alphaChar> | NUMBER | UNDERSCORE
<alphaChar> -> ALPHA_LOWER_CHAR | ALPHA_UPPER_CHAR
<line> -> <data> NEWLINE <line> | <data> | NEWLINE <line> | LAMBDA
<data> -> <string> COMMA <data> | <string>
<string> -> COMMON_CHAR <string> | COMON_CHAR
*/
/*
/**
* @brief a callback for the csv parser to use to cast that data the stored data
*
* @note this callback will only be called on non-header data
*
* @param[in/out] data the csv data casted into the users type
* @param[in] string an value of the csv as a string
*/
typedef uint32_t (* ARC_ParserCSV_CastTypeFn)(void **data, ARC_String *string);
/**
* @brief a callback for the csv parser to use to free csv data
*
* @note this callback will only be called on non-header data
*
* @param[in] data the csv data to free
*/
typedef uint32_t (* ARC_ParserCSV_DestroyTypeFn)(void *data);
/**
* @brief defines a csv data type, data is set by the callback passed in when createing a parserCSV as parser
*
* @note this data can be retieved after parsing by calling get data, check arc/std/parser.h for more information
* @note destroyTypeFn is stored here (for clearing as userdata is not passed in then) but should not be used by anything but the parser
*/
typedef struct ARC_ParserCSVData {
ARC_Bool hasHeader;
ARC_String **headers;
uint32_t width;
uint32_t height;
void **data;
ARC_ParserCSV_DestroyTypeFn destroyTypeFn;
} ARC_ParserCSVData;
/**
* @brief creates a parser for the Parser Lang
*
* @note the rules will be inited for the parser lang
@ -34,42 +60,7 @@ extern "C" {
*
* @param[out] parser the parser to create
*/
void ARC_ParserCSV_CreateAsParser(ARC_Parser **parser, ARC_Parser_GetStringIdFn getStringIdFn);
#define ARC_PARSERLANG_TOKEN_NULL 0
#define ARC_PARSERLANG_TOKEN_NUMBER 1
#define ARC_PARSERLANG_TOKEN_ALPHA_LOWER_CHAR 2
#define ARC_PARSERLANG_TOKEN_ALPHA_UPPER_CHAR 3
#define ARC_PARSERLANG_TOKEN_WHITESPACE 4
#define ARC_PARSERLANG_TOKEN_NEWLINE_ID 5
#define ARC_PARSERLANG_TOKEN_NEWLINE_CHAR '\n'
#define ARC_PARSERLANG_TOKEN_LESS_THAN_ID 6
#define ARC_PARSERLANG_TOKEN_LESS_THAN_CHAR '<'
#define ARC_PARSERLANG_TOKEN_GREATER_THAN_ID 7
#define ARC_PARSERLANG_TOKEN_GREATER_THAN_CHAR '>'
#define ARC_PARSERLANG_TOKEN_OR_ID 8
#define ARC_PARSERLANG_TOKEN_OR_CHAR '|'
#define ARC_PARSERLANG_TOKEN_UNDERSCORE_ID 9
#define ARC_PARSERLANG_TOKEN_UNDERSCORE_CHAR '_'
#define ARC_PARSERLANG_TOKEN_ARROW_ID 10
#define ARC_PARSERLANG_TOKEN_ARROW_CSTRING "->"
#define ARC_PARSERLANG_LAMBDA ARC_PARSER_TAG_LAMBDA
#define ARC_PARSERLANG_LINE 11
#define ARC_PARSERLANG_BODY 12
#define ARC_PARSERLANG_ARGUMENTS 13
#define ARC_PARSERLANG_ARGUMENT 14
#define ARC_PARSERLANG_TAG_OR_CONSTANT 15
#define ARC_PARSERLANG_CONSTANT 16
#define ARC_PARSERLANG_CONSTANT_BODY 17
#define ARC_PARSERLANG_CONSTANT_CHAR 18
#define ARC_PARSERLANG_TAG 19
#define ARC_PARSERLANG_VARIABLE 20
#define ARC_PARSERLANG_VARIABLE_BODY 21
#define ARC_PARSERLANG_VARIABLE_CHAR 22
#define ARC_PARSERLANG_ALPHA_CHAR 23
void ARC_ParserCSV_CreateAsParser(ARC_Parser **parser, ARC_Bool header, ARC_ParserCSV_CastTypeFn castTypeFn, ARC_ParserCSV_DestroyTypeFn destroyTypeFn);
#ifdef __cplusplus
}

View file

@ -0,0 +1,26 @@
#ifndef ARC_STD_PARSER_HELPERS_H_
#define ARC_STD_PARSER_HELPERS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "arc/std/parser.h"
#include <stddef.h>
/**
* @brief recurses through a tag token adding token strings to a main string
*
* @note the string needs to be created otherwise this will break
* @note this will only add tokens that have data
*
* @param[in/out] data
* @param[in] tagToken the tag token to recurse through
*/
void ARC_ParserData_HelperRecurseStringAdd(ARC_String **data, ARC_ParserTagToken *tagToken);
#ifdef __cplusplus
}
#endif
#endif //ARC_STD_PARSER_HELPERS_H_

View file

@ -9,10 +9,10 @@ extern "C" {
/*
<line> -> <body> NEWLINE <line> | <body> | NEWLINE <line> | LAMBDA
<body> -> <tag> WHITESPACE ARROW WHITESPACE <arguments>
<body> -> <tag> <whitespace> ARROW <whitespace> <arguments>
<arguments> -> <argument> WHITESPACE OR WHITESPACE <arguments> | <argument>
<argument> -> <tagOrConstant> WHITESPACE <argument> | <tagOrConstant>
<arguments> -> <argument> <whitespace> OR <whitespace> <arguments> | <argument>
<argument> -> <tagOrConstant> <whitespace> <argument> | <tagOrConstant>
<tagOrConstant> -> <tag> | <constant>
<constant> -> ALPHA_UPPER_CHAR <constantBody>
@ -24,9 +24,11 @@ extern "C" {
<variableBody> -> <variableChar> <variableBody> | LAMBDA
<variableChar> -> <alphaChar> | NUMBER | UNDERSCORE
<alphaChar> -> ALPHA_LOWER_CHAR | ALPHA_UPPER_CHAR
<whitespace> -> WHITESPACE <whitespace> | WHITESPACE
*/
/*
/**
* @brief creates a parser for the Parser Lang
*
* @note the rules will be inited for the parser lang
@ -70,6 +72,7 @@ void ARC_ParserLang_CreateAsParser(ARC_Parser **parser, ARC_Parser_GetStringIdFn
#define ARC_PARSERLANG_VARIABLE_BODY 21
#define ARC_PARSERLANG_VARIABLE_CHAR 22
#define ARC_PARSERLANG_ALPHA_CHAR 23
#define ARC_PARSERLANG_WHITESPACE 24
#ifdef __cplusplus
}