2021-07-14 08:15:46 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "node.h"
|
|
|
|
|
|
|
|
#define STRING_BUFF_SIZE 1024
|
|
|
|
|
|
|
|
static char stringBuff[STRING_BUFF_SIZE];
|
|
|
|
|
2023-10-15 15:30:59 -07:00
|
|
|
HeaderNode* parseHeader(TokenList** list);
|
2021-07-14 08:15:46 -07:00
|
|
|
|
|
|
|
NodeList*
|
|
|
|
ParseNodes(TokenList* list)
|
|
|
|
{
|
2023-10-15 15:30:59 -07:00
|
|
|
printf("ParseNodes() start\n");
|
2021-07-14 08:15:46 -07:00
|
|
|
NodeList* nl = malloc(sizeof(NodeList));
|
2023-10-15 15:30:59 -07:00
|
|
|
nl->first = NULL;
|
2021-07-14 08:15:46 -07:00
|
|
|
|
2023-10-15 15:30:59 -07:00
|
|
|
/*currentNode->next = NULL;*/
|
|
|
|
/*currentNode->node = NULL;*/
|
2021-07-14 08:15:46 -07:00
|
|
|
|
2023-10-15 15:30:59 -07:00
|
|
|
TokenList* currentToken = list;
|
|
|
|
Node* prevNode = NULL;
|
|
|
|
|
|
|
|
printf("ParseNodes() loop\n");
|
2021-07-14 08:15:46 -07:00
|
|
|
|
|
|
|
while (1) {
|
2023-10-15 15:30:59 -07:00
|
|
|
Node* currentNode = NULL;
|
|
|
|
|
|
|
|
switch (currentToken->token->type) {
|
2021-07-14 08:15:46 -07:00
|
|
|
case TT_NEWLINE:
|
|
|
|
break;
|
|
|
|
case TT_HASH:
|
|
|
|
// start of header
|
2023-10-15 15:30:59 -07:00
|
|
|
currentNode = (Node*)parseHeader(¤tToken);
|
2021-07-14 08:15:46 -07:00
|
|
|
break;
|
2023-10-15 15:30:59 -07:00
|
|
|
|
|
|
|
case TT_EOF:
|
|
|
|
printf("EOF found\n");
|
|
|
|
return nl;
|
2021-07-14 08:15:46 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-15 15:30:59 -07:00
|
|
|
if (currentToken->next == NULL) {
|
|
|
|
printf("currentToken->next == NULL\n");
|
2021-07-14 08:15:46 -07:00
|
|
|
break;
|
|
|
|
}
|
2023-10-15 15:30:59 -07:00
|
|
|
|
|
|
|
currentToken = currentToken->next;
|
|
|
|
if (currentNode == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (prevNode != NULL) {
|
|
|
|
prevNode->next = currentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nl->first == NULL) {
|
|
|
|
nl->first = currentNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevNode = currentNode;
|
2021-07-14 08:15:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nl;
|
|
|
|
}
|
|
|
|
|
2023-10-15 15:30:59 -07:00
|
|
|
HeaderNode*
|
2021-07-14 08:15:46 -07:00
|
|
|
parseHeader(TokenList** list)
|
|
|
|
{
|
|
|
|
TokenList* l = *list;
|
|
|
|
// Count the number of TT_HASH tokens
|
|
|
|
int count = 1;
|
|
|
|
while (l->next != NULL && l->next->token->type == TT_HASH)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
l = l->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->next == NULL)
|
|
|
|
{
|
|
|
|
printf("Header missing text");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
l = l->next;
|
|
|
|
|
|
|
|
// Trim leading whitespace
|
|
|
|
while (l->next != NULL && l->token->type == TT_WHITESPACE)
|
|
|
|
{
|
|
|
|
l = l->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->next == NULL)
|
|
|
|
{
|
|
|
|
printf("Header missing text");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringBuff[0] = '\0';
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int bufSize = strlen(stringBuff);
|
|
|
|
int litSize = strlen(l->token->literal);
|
|
|
|
if (bufSize + litSize + 1 > STRING_BUFF_SIZE)
|
|
|
|
{
|
|
|
|
printf("Buffer not big enough!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strncat(stringBuff, l->token->literal, strlen(l->token->literal));
|
|
|
|
|
|
|
|
if (l->next == NULL || l->next->token->type == TT_NEWLINE)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = l->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*list = l;
|
|
|
|
printf("header hash count: %d\ntext: '%s'\n", count, stringBuff);
|
2023-10-15 15:30:59 -07:00
|
|
|
|
|
|
|
HeaderNode* retval = malloc(sizeof(HeaderNode));
|
|
|
|
switch(count) {
|
|
|
|
case 1:
|
|
|
|
retval->type = NT_Header1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
retval->type = NT_Header2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
retval->type = NT_Header3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval->type = NT_Header4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval->next = NULL;
|
|
|
|
retval->rawText = stringBuff;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
NodeTypeString(NodeType t)
|
|
|
|
{
|
|
|
|
switch(t) {
|
|
|
|
case NT_Header1:
|
|
|
|
return "NT_Header1";
|
|
|
|
case NT_Header2:
|
|
|
|
return "NT_Header2";
|
|
|
|
case NT_Header3:
|
|
|
|
return "NT_Header3";
|
|
|
|
case NT_Header4:
|
|
|
|
return "NT_Header4";
|
|
|
|
case NT_Paragraph:
|
|
|
|
return "NT_Paragraph";
|
|
|
|
case NT_UnorderedList:
|
|
|
|
return "NT_UnorderedList";
|
|
|
|
case NT_OrderedList:
|
|
|
|
return "NT_OrderedList";
|
|
|
|
case NT_InlineCode:
|
|
|
|
return "NT_InlineCode";
|
|
|
|
case NT_BlockCode:
|
|
|
|
return "NT_BlockCode";
|
|
|
|
case NT_BlockQuote:
|
|
|
|
return "NT_BlockQuote";
|
|
|
|
case NT_Bold:
|
|
|
|
return "NT_Bold";
|
|
|
|
case NT_Underline:
|
|
|
|
return "NT_Underline";
|
|
|
|
|
|
|
|
default:
|
|
|
|
snprintf(stringBuff, 1000, "unknown NodeType: %d", t);
|
|
|
|
return stringBuff;
|
|
|
|
}
|
2021-07-14 08:15:46 -07:00
|
|
|
}
|
|
|
|
|