Program Listing for File lexer.h

Return to documentation for file (src/lovejoy/lexer.h)

#pragma once

#include "common.h"
#include "operators.h"

enum TokenType {
    TT_NUMBER = 0,
    TT_STRING,
    TT_CHAR,
    TT_OPERATOR,
    TT_IDENT,
    TT_LPAREN,
    TT_RPAREN,
    TT_LBRACKET,
    TT_RBRACKET,
    TT_LCURLY,
    TT_RCURLY,
    TT_TERM,
    TT_NONE
};
unqualify(enum, TokenType);

struct LexerContext {
    const byte *filename;
    const OperatorTable operator_table;
    const byte *lineptr;
    usize lineno;
    TokenType last_token_type;
};
unqualify(struct, LexerContext);

LexerContext NewLexer();

struct Lexeme {
    TokenType type;
    const byte *start;
    const byte *end;
    const byte *line;
    usize lineno;
};
unqualify(struct, Lexeme);

enum LexerError {
    LEXER_ERROR_UNEXPECTED = 11,
    LEXER_ERROR_EOS        = 12,
};
unqualify(enum, LexerError);

static inline
usize lexeme_span(const Lexeme *lexeme)
    { return lexeme->end - lexeme->start; }

static inline
usize lexeme_col(const Lexeme *lexeme)
    { return lexeme->start - lexeme->line + 1; }

string lexeme_substring(const Lexeme *);

TokenType character_type(byte);

Lexeme *lex(LexerContext *, const byte *source);

Lexeme *peek(LexerContext *, u16 count, const byte *source);

u0 lexeme_free(Lexeme *);