]> git.xonotic.org Git - xonotic/darkplaces.git/blob - parser.h
Parser is now tail recursive. Implemented parse tree, self-cleaning on failure
[xonotic/darkplaces.git] / parser.h
1 /*
2 Copyright (C) 2021 David Knapp (Cloudwalk)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "qtypes.h"
22 #include <setjmp.h>
23
24 #define PARSER_MAX_DEPTH 256
25
26 typedef enum qparser_err_e
27 {
28         PARSE_ERR_SUCCESS = 0,
29         PARSE_ERR_INVAL = 1,
30         PARSE_ERR_EOF = 2,
31         PARSE_ERR_DEPTH = 3,
32         PARSE_ERR_EMPTY = 4
33 } qparser_err_t;
34
35 typedef struct qparser_state_s
36 {
37         const char *name;
38         const char *buf;
39         const char *pos;
40         int line, col, depth;
41
42         struct
43         {
44                 qbool (*CheckComment_SingleLine)(struct qparser_state_s *);
45                 qbool (*CheckComment_Multiline_Start)(struct qparser_state_s *);
46                 qbool (*CheckComment_Multiline_End)(struct qparser_state_s *);
47         } callback;
48 } qparser_state_t;
49
50 extern jmp_buf parse_error;
51
52 void Parse_Error(struct qparser_state_s *state, qparser_err_t error, const char *expected);
53 void Parse_Next(struct qparser_state_s *state, int count);
54 char Parse_NextToken(struct qparser_state_s *state, int skip);
55 qparser_state_t *Parse_New(const unsigned char *in);
56 qparser_state_t *Parse_LoadFile(const char *file);
57
58 static inline void Parse_Indent(struct qparser_state_s *state)
59 {
60         if(state->depth >= PARSER_MAX_DEPTH)
61                 Parse_Error(state, PARSE_ERR_DEPTH, NULL);
62         state->depth++;
63 }
64
65 static inline void Parse_Dedent(struct qparser_state_s *state)
66 {
67         state->depth--;
68 }