Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/gravity
Path: blob/master/src/compiler/gravity_lexer.h
1214 views
1
//
2
// gravity_lexer.h
3
// gravity
4
//
5
// Created by Marco Bambini on 30/08/14.
6
// Copyright (c) 2014 CreoLabs. All rights reserved.
7
//
8
9
#ifndef __GRAVITY_LEXER__
10
#define __GRAVITY_LEXER__
11
12
#include "gravity_delegate.h"
13
#include "gravity_token.h"
14
#include "debug_macros.h"
15
16
/*
17
Lexer is built in such a way that no memory allocations are necessary during usage
18
(except for the gravity_lexer_t opaque datatype allocated within gravity_lexer_create).
19
20
Example:
21
gravity_lexer *lexer = gravity_lexer_create(...);
22
while (gravity_lexer_next(lexer)) {
23
// do something here
24
}
25
gravity_lexer_free(lexer);
26
27
gravity_lexer_next (and gravity_lexer_peek) returns an int token (gtoken_t)
28
which represents what has been currently scanned. When EOF is reached TOK_EOF is
29
returned (with value 0) and the while loop exits.
30
31
In order to have token details, gravity_lexer_token must be called.
32
In case of a scan error TOK_ERROR is returned and error details can be extracted
33
from the token itself. In order to be able to not allocate any memory during
34
tokenization STRINGs and NUMBERs are just sanity checked but not converted.
35
It is parser responsability to perform the right conversion.
36
37
*/
38
39
// opaque datatype
40
typedef struct gravity_lexer_t gravity_lexer_t;
41
42
// public functions
43
gravity_lexer_t *gravity_lexer_create (const char *source, size_t len, uint32_t fileid, bool is_static);
44
void gravity_lexer_setdelegate (gravity_lexer_t *lexer, gravity_delegate_t *delegate);
45
void gravity_lexer_free (gravity_lexer_t *lexer);
46
47
gtoken_t gravity_lexer_peek (gravity_lexer_t *lexer);
48
gtoken_t gravity_lexer_next (gravity_lexer_t *lexer);
49
gtoken_s gravity_lexer_token (gravity_lexer_t *lexer);
50
gtoken_s gravity_lexer_token_next (gravity_lexer_t *lexer);
51
gtoken_t gravity_lexer_token_type (gravity_lexer_t *lexer);
52
void gravity_lexer_token_dump (gtoken_s token);
53
54
#if GRAVITY_LEXER_DEGUB
55
void gravity_lexer_debug (gravity_lexer_t *lexer);
56
#endif
57
58
#endif
59
60