init lexer

This commit is contained in:
_N3m0 2023-12-31 14:54:14 +01:00
parent 7b0682ad18
commit b03364e3d9
4 changed files with 103 additions and 1 deletions

View File

@ -1,5 +1,5 @@
BIN=webpage
SRCS=main.c config.c page.c
SRCS=main.c config.c page.c lexer.c
INC=.
LIB=curl
FLAGS=-Wall -Wextra -Og -g

64
lexer.c Normal file
View File

@ -0,0 +1,64 @@
#include "lexer.h"
#include "page.h"
#include <string.h>
Cursor cursor = {
.chunk = 0,
.offset = 0,
};
char* nextchar(void){
if (page.chunks[cursor.chunk][cursor.offset+1] == '\0'){
cursor.chunk++;
cursor.offset = 0;
} else {
cursor.offset++;
}
return &page.chunks[cursor.chunk][cursor.offset];
}
Token* nexttoken(void){
Token* token = malloc(sizeof(Token));
token->type = NO_TYPE;
/*
while (nextchar() == '<');
char c = '\0';
do {
c = nextchar();
printf("%c", c);;
} while (c != '>');
puts("");
*/
puts("");
for (int i=0; i<200; i++){
printf("%c", *nextchar());
}
puts("");
return token;
}
void printtoken(Token* token){
switch (token->type) {
case END_BODY:
printf("END_BODY: ");
break;
case NO_TYPE:
printf("NO_TYPE: ");
break;
default:
printf("OTHER_TOKEN: ");
break;
}
if (token->value == NULL){
puts("NO VALUE FOUND");
return;
}
printf("%s\n", token->value);
}

30
lexer.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef LEXER_H
#define LEXER_H
#include <stdio.h>
#include <stdlib.h>
typedef enum TokenType {
NO_TYPE,
TEXT,
BODY,
END_BODY,
UL, LI,
H1, H2, H3, H4, H5, H6,
} TokenType;
typedef struct Token {
TokenType type;
char* value;
} Token;
typedef struct Cursor {
int chunk;
int offset;
} Cursor;
Token* nexttoken(void);
void printtoken(Token* token);
#endif // LEXER_H

8
main.c
View File

@ -1,5 +1,6 @@
#include "config.h"
#include "page.h"
#include "lexer.h"
int main(int argc, char* argv[]){
@ -8,5 +9,12 @@ int main(int argc, char* argv[]){
printPage();
Token* token;
do{
token = nexttoken();
printtoken(token);
//parse(token);
} while (token->type != NO_TYPE);
return 0;
}