summaryrefslogtreecommitdiff
path: root/components/external/espruino/src/jslex.h
blob: 8a1aee3a9bd05e75571207ffe65cb209f3c3be3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
 *
 * Copyright (C) 2013 Gordon Williams <[email protected]>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * ----------------------------------------------------------------------------
 * Lexer (convert JsVar strings into a series of tokens)
 * ----------------------------------------------------------------------------
 */
#ifndef JSLEX_H_
#define JSLEX_H_

#include "jsutils.h"
#include "jsvar.h"

typedef struct JsLex
{
    // Actual Lexing related stuff
    char currCh, nextCh;
    short tk; ///< The type of the token that we have
    JslCharPos tokenStart; ///< Position in the data at the beginning of the token we have here
    JslCharPos tokenEnd; ///< Position in the data at the last character of the token we have here
    JslCharPos tokenLastStart; ///< Position in the data of the first character of the last token
    JslCharPos tokenLastEnd; ///< Position in the data of the last character of the last token
    char token[JSLEX_MAX_TOKEN_LENGTH]; ///< Data contained in the token we have here
    JsVar *tokenValue; ///< JsVar containing the current token - used only for strings
    unsigned char tokenl; ///< the current length of token

    /* Where we get our data from...
     *
     * This is a bit more tricky than normal because the data comes from JsVars,
     * which only have fixed length strings. If we go past this, we have to go
     * to the next jsVar...
     */
    JsVar *sourceVar; // the actual string var
    JsvStringIterator it; // Iterator for the string
} JsLex;

void jslInit(JsLex *lex, JsVar *var);
void jslKill(JsLex *lex);
void jslReset(JsLex *lex);
void jslSeekTo(JsLex *lex, JslCharPos seekToChar); // like jslSeek, but pre-fills characters

bool jslMatch(JsLex *lex, int expected_tk); ///< Match, and return true on success, false on failure
void jslTokenAsString(int token, char *str, size_t len); ///< output the given token as a string - for debugging
void jslGetTokenString(JsLex *lex, char *str, size_t len);
char *jslGetTokenValueAsString(JsLex *lex);
JsVar *jslGetTokenValueAsVar(JsLex *lex);

// Only for more 'internal' use
void jslSeek(JsLex *lex, JslCharPos seekToChar); // like jslSeekTo, but doesn't pre-fill characters
void jslGetNextCh(JsLex *lex);
void jslGetNextToken(JsLex *lex); ///< Get the text token from our text string

#endif /* JSLEX_H_ */