1 #ifndef Ta3_TOKENIZER_H
2 #define Ta3_TOKENIZER_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #include "object.h"
8 
9 /* Tokenizer interface */
10 
11 #include "../Include/token.h"
12 
13 #define MAXINDENT 100   /* Max indentation level */
14 
15 enum decoding_state {
16     STATE_INIT,
17     STATE_RAW,
18     STATE_NORMAL        /* have a codec associated with input */
19 };
20 
21 /* Tokenizer state */
22 struct tok_state {
23     /* Input state; buf <= cur <= inp <= end */
24     /* NB an entire line is held in the buffer */
25     char *buf;          /* Input buffer, or NULL; malloc'ed if fp != NULL */
26     char *cur;          /* Next character in buffer */
27     char *inp;          /* End of data in buffer */
28     char *end;          /* End of input buffer if buf != NULL */
29     char *start;        /* Start of current token if not NULL */
30     int done;           /* E_OK normally, E_EOF at EOF, otherwise error code */
31     /* NB If done != E_OK, cur must be == inp!!! */
32     FILE *fp;           /* Rest of input; NULL if tokenizing a string */
33     int tabsize;        /* Tab spacing */
34     int indent;         /* Current indentation index */
35     int indstack[MAXINDENT];            /* Stack of indents */
36     int atbol;          /* Nonzero if at begin of new line */
37     int pendin;         /* Pending indents (if > 0) or dedents (if < 0) */
38     const char *prompt, *nextprompt;          /* For interactive prompting */
39     int lineno;         /* Current line number */
40     int level;          /* () [] {} Parentheses nesting level */
41             /* Used to allow free continuations inside them */
42     /* Stuff for checking on different tab sizes */
43 #ifndef PGEN
44     /* pgen doesn't have access to Python codecs, it cannot decode the input
45        filename. The bytes filename might be kept, but it is only used by
46        indenterror() and it is not really needed: pgen only compiles one file
47        (Grammar/Grammar). */
48     PyObject *filename;
49 #endif
50     int altindstack[MAXINDENT];         /* Stack of alternate indents */
51     /* Stuff for PEP 0263 */
52     enum decoding_state decoding_state;
53     int decoding_erred;         /* whether erred in decoding  */
54     int read_coding_spec;       /* whether 'coding:...' has been read  */
55     char *encoding;         /* Source encoding. */
56     int cont_line;          /* whether we are in a continuation line. */
57     const char* line_start;     /* pointer to start of current line */
58 #ifndef PGEN
59     PyObject *decoding_readline; /* open(...).readline */
60     PyObject *decoding_buffer;
61 #endif
62     const char* enc;        /* Encoding for the current str. */
63     const char* str;
64     const char* input; /* Tokenizer's newline translated copy of the string. */
65 
66     /* async/await related fields; can be removed in 3.7 when async and await
67        become normal keywords. */
68     int async_def;        /* =1 if tokens are inside an 'async def' body. */
69     int async_def_indent; /* Indentation level of the outermost 'async def'. */
70     int async_def_nl;     /* =1 if the outermost 'async def' had at least one
71                              NEWLINE token after it. */
72     int async_always;     /* =1 if async/await are always keywords */
73 };
74 
75 extern struct tok_state *Ta3Tokenizer_FromString(const char *, int);
76 extern struct tok_state *Ta3Tokenizer_FromUTF8(const char *, int);
77 extern struct tok_state *Ta3Tokenizer_FromFile(FILE *, const char*,
78                                               const char *, const char *);
79 extern void Ta3Tokenizer_Free(struct tok_state *);
80 extern int Ta3Tokenizer_Get(struct tok_state *, char **, char **);
81 
82 #ifdef __cplusplus
83 }
84 #endif
85 #endif /* !Ta3_TOKENIZER_H */
86