init lexer
This commit is contained in:
parent
7b0682ad18
commit
b03364e3d9
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
||||||
BIN=webpage
|
BIN=webpage
|
||||||
SRCS=main.c config.c page.c
|
SRCS=main.c config.c page.c lexer.c
|
||||||
INC=.
|
INC=.
|
||||||
LIB=curl
|
LIB=curl
|
||||||
FLAGS=-Wall -Wextra -Og -g
|
FLAGS=-Wall -Wextra -Og -g
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -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
8
main.c
|
@ -1,5 +1,6 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "page.h"
|
#include "page.h"
|
||||||
|
#include "lexer.h"
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
|
@ -8,5 +9,12 @@ int main(int argc, char* argv[]){
|
||||||
|
|
||||||
printPage();
|
printPage();
|
||||||
|
|
||||||
|
Token* token;
|
||||||
|
do{
|
||||||
|
token = nexttoken();
|
||||||
|
printtoken(token);
|
||||||
|
//parse(token);
|
||||||
|
} while (token->type != NO_TYPE);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue