Qore Programming Language  1.12.0
ParserSupport.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  ParserSupport.h
4 
5  parsing support functions and objects
6 
7  Qore Programming language
8 
9  Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_PARSER_SUPPORT_H
35 
36 #define _QORE_PARSER_SUPPORT_H
37 
38 #define HE_TAG_CONST 1
39 #define HE_TAG_SCOPED_CONST 2
40 
41 #include <cstring>
42 
43 class QoreParserLocation {
44 public:
45  int first_line = 1;
46  int last_line = 1;
47  int first_col = 0;
48  int last_col = 0;
49 
50  int saved_first_line = 0;
51  int saved_first_col = 0;
52 
53  DLLLOCAL void saveFirst() {
54  //printd(0, "QoreParserLocation::setFirst: current: %d:%d - %d:%d\n", first_line, first_col, last_line, last_col);
55  saved_first_line = first_line;
56  saved_first_col = first_col;
57  }
58 
59  DLLLOCAL void restoreFirst() {
60  first_line = saved_first_line;
61  first_col = saved_first_col;
62  }
63 
64  DLLLOCAL void update(int lineno, int leng, const char* text) {
65  assert(leng >= 0);
66  first_line = last_line;
67  first_col = last_col;
68  if (first_line == lineno)
69  last_col += leng;
70  else {
71  unsigned int col = 1;
72  for (; ((int)col <= leng) && (text[leng - col] != '\n'); ++col) {}
73  last_col = col;
74  last_line = lineno;
75  }
76  }
77 };
78 
79 hashdecl TryModuleError {
80  char* var;
81  QoreException* ex;
82 
83  DLLLOCAL TryModuleError(char* v, QoreException* e) : var(v), ex(e) {
84  }
85 
86  DLLLOCAL ~TryModuleError() {
87  if (var)
88  free(var);
89  if (ex)
90  ex->del(0);
91  }
92 
93  // move down string to remove '$' sign from beginning
94  DLLLOCAL void fixName() {
95  size_t len = strlen(var);
96  assert(len);
97  // move string + trailing null
98  memmove(var, var + 1, len);
99  }
100 
101  DLLLOCAL QoreHashNode* takeExceptionHash() {
102  assert(ex);
103  QoreHashNode* h = ex->makeExceptionObjectAndDelete(0);
104  ex = 0;
105  return h;
106  }
107 
108  DLLLOCAL char* takeName() {
109  char* str = var;
110  var = 0;
111  return str;
112  }
113 };
114 
115 #define YYLTYPE class QoreParserLocation
116 
117 // private interface to bison/flex parser/scanner
118 typedef void* yyscan_t;
119 DLLLOCAL extern int yyparse(yyscan_t yyscanner);
120 DLLLOCAL extern hashdecl yy_buffer_state* yy_scan_string(const char*, yyscan_t scanner);
121 DLLLOCAL int yylex_init(yyscan_t* scanner);
122 DLLLOCAL void yyset_in(FILE* in_str, yyscan_t yyscanner);
123 DLLLOCAL int yylex_destroy(yyscan_t yyscanner);
124 DLLLOCAL void yyset_lineno(int line_number, yyscan_t yyscanner);
125 
126 #endif // _QORE_PARSER_SUPPORT_H
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50