hare::lex +linux +x86_64

hare::lex provides a lexer for Hare source code. A lexer takes an io::handle and returns a series of Hare tokens. See the Hare specification for more details:

https://harelang.org/specification

Index

Types

type _null;
type flags;
type location;
type ltok;
type token;
type value;

// Undocumented types:
type lexer;

Errors

type error;
type syntax;

Functions

fn comment(*lexer) str;
fn init(io::handle, str, flags...) lexer;
fn lex(*lexer) (token | error);
fn strerror(error) const str;
fn tokstr(token) const str;
fn unlex(*lexer, token) void;

// Undocumented functions:
fn mkloc(*lexer) location;
fn prevloc(*lexer) location;

Types

type _null[link]

type _null = void;

The value "null".

type flags[link]

type flags = enum uint {
	NONE = 0,
	COMMENTS = 1 << 0, // Enables lexing comments
};

Flags which apply to this lexer

type location[link]

type location = struct {
	path: str,
	line: uint,
	col: uint,
};

A location within a source file. The path is borrowed from the file name given to the lexer.

type ltok[link]

type ltok = enum uint {
	ATTR_FINI,
	ATTR_INIT,
	ATTR_NORETURN,
	ATTR_OFFSET,
	ATTR_SYMBOL,
	ATTR_TEST,
	UNDERSCORE,
	ABORT,
	ALLOC,
	APPEND,
	AS,
	ASSERT,
	BOOL,
	BREAK,
	CASE,
	CHAR,
	CONST,
	CONTINUE,
	DEF,
	DEFER,
	DELETE,
	ELSE,
	ENUM,
	EXPORT,
	F32,
	F64,
	FALSE,
	FN,
	FOR,
	FREE,
	I16,
	I32,
	I64,
	I8,
	IF,
	INSERT,
	INT,
	IS,
	LEN,
	LET,
	MATCH,
	NULL,
	NULLABLE,
	OFFSET,
	RETURN,
	RUNE,
	SIZE,
	STATIC,
	STR,
	STRUCT,
	SWITCH,
	TRUE,
	TYPE,
	U16,
	U32,
	U64,
	U8,
	UINT,
	UINTPTR,
	UNION,
	USE,
	VAARG,
	VAEND,
	VALIST,
	VASTART,
	VOID,
	YIELD,
	LAST_KEYWORD = YIELD,
	ARROW,
	BAND,
	BANDEQ,
	BNOT,
	BOR,
	BOREQ,
	BXOR,
	BXOREQ,
	COLON,
	COMMA,
	DIV,
	DIVEQ,
	DOT,
	DOUBLE_COLON,
	ELLIPSIS,
	EQUAL,
	GT,
	GTEQ,
	LAND,
	LANDEQ,
	LBRACE,
	LBRACKET,
	LEQUAL,
	LESS,
	LESSEQ,
	LNOT,
	LOR,
	LOREQ,
	LPAREN,
	LSHIFT,
	LSHIFTEQ,
	LXOR,
	LXOREQ,
	MINUS,
	MINUSEQ,
	MODEQ,
	MODULO,
	NEQUAL,
	PLUS,
	PLUSEQ,
	QUESTION,
	RBRACE,
	RBRACKET,
	RPAREN,
	RSHIFT,
	RSHIFTEQ,
	SEMICOLON,
	SLICE,
	TIMES,
	TIMESEQ,
	LAST_BTOK = TIMESEQ,
	LIT_U8,
	LIT_U16,
	LIT_U32,
	LIT_U64,
	LIT_UINT,
	LIT_SIZE,
	LIT_I8,
	LIT_I16,
	LIT_I32,
	LIT_I64,
	LIT_INT,
	LIT_ICONST,
	LIT_F32,
	LIT_F64,
	LIT_FCONST,
	LIT_RUNE,
	LIT_STR,
	LAST_LITERAL = LIT_STR,
	NAME,
	EOF,

};

A lexical token class.

type token[link]

type token = (ltok, value, location);

A single lexical token.

type value[link]

type value = (str | rune |
u64 | f64 | void);

A token value, used for tokens such as '1337' (an integer).

type lexer[link]

Show undocumented member
type lexer = struct {
	in: io::handle,
	path: str,
	loc: (uint, uint),
	un: (token | void),
	rb: [2]((rune, location) |
	io::EOF | void),
	// 1 more than the size of un and rb respectively
prevunlocs: [2](location, location), prevrlocs: [3]location, flags: flags, comment: str, require_int: bool, };

Errors

type error[link]

type error = !(io::error | syntax);

All possible lexer errors

type syntax[link]

type syntax = !(location, str);

A syntax error

Functions

fn comment[link]

fn comment(lex: *lexer) str;

Returns the current value of the comment buffer, or empty string if unset (or if flags::COMMENTS was not enabled for this lexer).

fn init[link]

fn init(in: io::handle, path: str, flags: flags...) lexer;

Initializes a new lexer for the given input. The path is borrowed.

fn lex[link]

fn lex(lex: *lexer) (token | error);

Returns the next token from the lexer.

fn strerror[link]

fn strerror(err: error) const str;

Returns a human-friendly string for a given error

fn tokstr[link]

fn tokstr(tok: token) const str;

Converts a token to its string representation.

fn unlex[link]

fn unlex(lex: *lexer, tok: token) void;

Unlex a single token. The next call to lex will return this token. Only one unlex is supported at a time; you must call lex before calling unlex again.

fn mkloc[link]

Show undocumented member
fn mkloc(lex: *lexer) location;

fn prevloc[link]

Show undocumented member
fn prevloc(lex: *lexer) location;