libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
lexer.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7
8#include <stdbool.h>
9#include "snobol/c23_compat.h"
10#include <stddef.h>
11#include <stdint.h>
12
20
24typedef enum {
25 TOKEN_EOF, /* End of input */
26 TOKEN_LIT, /* Literal string: 'text' */
27 TOKEN_IDENT, /* Identifier: name */
28 TOKEN_CHARCLASS, /* Character class: [abc] */
29 TOKEN_INTEGER, /* Integer literal: 5, -1 */
30 TOKEN_PIPE, /* Alternation: | */
31 TOKEN_LPAREN, /* Left paren: ( */
32 TOKEN_RPAREN, /* Right paren: ) */
33 TOKEN_STAR, /* Zero-or-more: * */
34 TOKEN_PLUS, /* One-or-more: + */
35 TOKEN_QUESTION, /* Optional: ? */
36 TOKEN_ANCHOR_START, /* Start anchor: ^ */
37 TOKEN_ANCHOR_END, /* End anchor: $ */
38 TOKEN_AT, /* Capture: @ */
39 TOKEN_COLON, /* Label/goto marker: : */
40 TOKEN_LBRACKET, /* Left bracket: [ */
41 TOKEN_RBRACKET, /* Right bracket: ] */
42 TOKEN_DOT, /* Match naming: . */
43 TOKEN_EQUALS, /* Assignment: = */
44 TOKEN_COMMA, /* Comma: , */
45 TOKEN_ERROR /* Lexical error (see snobol_lexer_get_error) */
47
51typedef struct {
53
54 union {
55 /* TOKEN_LIT, TOKEN_IDENT, TOKEN_CHARCLASS */
56 struct {
57 const char *text; /* Pointer into source (not owned) */
58 size_t len; /* Length of token text */
59 } string;
60
61 /* TOKEN_INTEGER */
62 struct {
63 int64_t value; /* Parsed integer value */
64 } integer;
65
66 /* Single-character tokens use no additional data */
67 } data;
68} token_t;
69
73typedef struct snobol_lexer snobol_lexer_t;
74
81snobol_lexer_t *snobol_lexer_create(const char *source, size_t len);
82
89
96
103
110
114typedef struct {
115 size_t pos;
116 size_t line;
117 size_t column;
120 token_type_t prev_type; /* Type of the last returned token */
122
124#define SNOBOL_LEXER_ERROR_MAX 160
125
133
140const char *snobol_lexer_get_error(const snobol_lexer_t *lexer);
141
148
155
161
168
169#ifdef __cplusplus
170}
171#endif
struct snobol_lexer snobol_lexer_t
Definition lexer.h:73
size_t snobol_lexer_get_pos(snobol_lexer_t *lexer)
const char * snobol_token_name(token_type_t type)
snobol_lexer_state_t snobol_lexer_save(snobol_lexer_t *lexer)
token_t snobol_lexer_next(snobol_lexer_t *lexer)
void snobol_lexer_destroy(snobol_lexer_t *lexer)
bool snobol_lexer_has_error(const snobol_lexer_t *lexer)
token_t snobol_lexer_peek(snobol_lexer_t *lexer)
size_t snobol_lexer_get_line(snobol_lexer_t *lexer)
snobol_lexer_t * snobol_lexer_create(const char *source, size_t len)
const char * snobol_lexer_get_error(const snobol_lexer_t *lexer)
token_type_t
Definition lexer.h:24
@ TOKEN_STAR
Definition lexer.h:33
@ TOKEN_CHARCLASS
Definition lexer.h:28
@ TOKEN_IDENT
Definition lexer.h:27
@ TOKEN_AT
Definition lexer.h:38
@ TOKEN_EQUALS
Definition lexer.h:43
@ TOKEN_PLUS
Definition lexer.h:34
@ TOKEN_ANCHOR_START
Definition lexer.h:36
@ TOKEN_COMMA
Definition lexer.h:44
@ TOKEN_COLON
Definition lexer.h:39
@ TOKEN_PIPE
Definition lexer.h:30
@ TOKEN_EOF
Definition lexer.h:25
@ TOKEN_INTEGER
Definition lexer.h:29
@ TOKEN_RBRACKET
Definition lexer.h:41
@ TOKEN_DOT
Definition lexer.h:42
@ TOKEN_ANCHOR_END
Definition lexer.h:37
@ TOKEN_LBRACKET
Definition lexer.h:40
@ TOKEN_ERROR
Definition lexer.h:45
@ TOKEN_RPAREN
Definition lexer.h:32
@ TOKEN_QUESTION
Definition lexer.h:35
@ TOKEN_LPAREN
Definition lexer.h:31
@ TOKEN_LIT
Definition lexer.h:26
void snobol_lexer_restore(snobol_lexer_t *lexer, snobol_lexer_state_t state)
token_type_t prev_type
Definition lexer.h:120
token_t peek_token
Definition lexer.h:118
size_t len
Definition lexer.h:58
token_type_t type
Definition lexer.h:52
int64_t value
Definition lexer.h:63
const char * text
Definition lexer.h:57