File: json_lexer.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (272 lines) | stat: -rw-r--r-- 7,121 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
# include <cctype>
# include <cppad/local/graph/json_lexer.hpp>
# include <cppad/local/graph/cpp_graph_op.hpp>
# include <cppad/utility/error_handler.hpp>
# include <cppad/utility/to_string.hpp>
# include <cppad/utility/thread_alloc.hpp>


// BEGIN_CPPAD_LOCAL_GRAPH_NAMESPACE
namespace CppAD { namespace local { namespace graph {

// report_error
void json_lexer::report_error(
   const std::string& expected ,
   const std::string& found    )
{  size_t pos = index_;
   size_t count_newline = 0;
   while(0 < pos && count_newline < 2 )
   {  --pos;
      count_newline += json_[pos] == '\n';
   }
   std::string recent_input = json_.substr( pos, index_ - pos + 1);

   std::string msg = "Error occurred while parsing Json AD graph";
   if( function_name_ != "" )
      msg += " for the function " + function_name_;
   msg += ".\n";
   msg += "Expected a " + expected + " token but found " + found + "\n";
   msg += "Detected at end of following input:";
   msg += recent_input + "\n";
   msg += "This end is character " + to_string(char_number_);
   msg += " in line " + to_string(line_number_) + " of the json.\n";
   msg += "See https://coin-or.github.io/CppAD/doc/json_ad_graph.htm.";
   //
   // use this source code as point of detection
   bool known       = true;
   int  line        = __LINE__;
   const char* file = __FILE__;
   const char* exp  = "false";
   //
   // CppAD error handler
   ErrorHandler::Call(known, line, file, exp, msg.c_str());
}

// next_index
void json_lexer::next_index(void)
{  CPPAD_ASSERT_UNKNOWN( index_ < json_.size() );
   if( json_[index_] == '\n' )
   {  ++line_number_;
      char_number_ = 0;
   }
   ++index_;
   ++char_number_;
}

// skip_white_space
void json_lexer::skip_white_space(void)
{  while( index_ < json_.size() && isspace( json_[index_] ) )
      next_index();
}

// constructor
json_lexer::json_lexer(const std::string& json)
:
json_(json),
index_(0),
line_number_(1),
char_number_(1),
token_(""),
function_name_("")
{
   skip_white_space();
   if( index_ < json_.size() )
      token_ = json_[index_];
   if( token_ != "{" )
   {  std::string expected = "'{'";
      std::string found    = "'";
      if( index_ < json_.size() )
         found += json_[index_];
      found += "'";
      report_error(expected, found);
   }
   return;
}


// token
const std::string& json_lexer::token(void) const
{  return token_; }

// line_number
size_t json_lexer::line_number(void) const
{  return line_number_; }

// char_number
size_t json_lexer::char_number(void) const
{  return char_number_; }

// set_function_name
void json_lexer::set_function_name(const std::string& function_name)
{  function_name_ = function_name; }

// token2size_t
size_t json_lexer::token2size_t(void) const
{  return size_t( std::atoi( token_.c_str() ) ); }

// token2double
double json_lexer::token2double(void) const
{  return std::atof( token_.c_str() ); }

// check_next_char
void json_lexer::check_next_char(char ch)
{  // advance to next character
   if( index_ < json_.size() )
      next_index();
   skip_white_space();
   //
   bool ok = false;
   if( index_ < json_.size() )
   {  token_.resize(1);
      token_[0] = json_[index_];
      ok = (token_[0] == ch) || (ch == '\0');
   }
   if( ! ok )
   {  std::string expected = "a character that is not white space";
      if( ch != '\0' )
      {  expected  = "'";
         expected += ch;
         expected += "'";
      }
      //
      std::string found = "'";
      if( index_ < json_.size() )
         found += json_[index_];;
      found += "'";
      report_error(expected, found);
   }
}

// check_next_string
void json_lexer::check_next_string(const std::string& expected)
{  // advance to next character
   bool found_first_quote = index_ < json_.size();
   if( found_first_quote )
   {  next_index();
      skip_white_space();
      found_first_quote = index_ < json_.size();
   }
   // check for "
   if( found_first_quote )
      found_first_quote = json_[index_] == '"';
   //
   // set value of token
   token_.resize(0);
   if( found_first_quote )
   {  next_index();
      while( index_ < json_.size() && json_[index_] != '"' )
      {  token_.push_back( json_[index_] );
         next_index();
      }
   }
   // check for "
   bool found_second_quote = false;
   if( found_first_quote && index_ < json_.size() )
      found_second_quote = json_[index_] == '"';
   //
   bool ok = found_first_quote & found_second_quote;
   if( ok & (expected != "" ) )
      ok = expected == token_;
   if( ! ok )
   {  std::string expected_token;
      if( expected == "" )
         expected_token = "string";
      else
      {  expected_token = '"';
         expected_token += expected;
         expected_token += '"';
      }
      //
      std::string found;
      if( ! found_first_quote )
      {  found = "'";
         if( index_ < json_.size() )
            found += json_[index_];
         found += "'";
      }
      else
      {  found += '"';
         found += token_;
         if( found_second_quote )
            found += '"';
      }
      report_error(expected_token, found);
   }
}

// next_non_neg_int
void json_lexer::next_non_neg_int(void)
{  // advance to next character
   bool ok = index_ < json_.size();
   if( ok )
   {  next_index();
      skip_white_space();
      ok = index_ < json_.size();
   }
   if( ok )
      ok = std::isdigit( json_[index_] );
   if( ! ok )
   {  std::string expected_token = "non-negative integer";
      std::string found = "'";
      if( index_ < json_.size() )
         found += json_[index_];
      found += "'";
      report_error(expected_token, found);
   }
   //
   token_.resize(0);
   while( ok )
   {  token_.push_back( json_[index_] );
      ok = index_ + 1 < json_.size();
      if( ok )
         ok = isdigit( json_[index_ + 1] );
      if( ok )
         next_index();
   }
}

// next_float
void json_lexer::next_float(void)
{  // advance to next character
   bool ok = index_ < json_.size();
   if( ok )
   {  next_index();
      skip_white_space();
      ok = index_ < json_.size();
   }
   if( ok )
   {  char ch = json_[index_];
      ok = std::isdigit(ch);
      ok |= (ch == '.') || (ch == '+') || (ch == '-');
      ok |= (ch == 'e') || (ch == 'E');
   }
   if( ! ok )
   {  std::string expected_token = "floating point number";
      std::string found = "'";
      if( index_ < json_.size() )
         found += json_[index_];
      found += "'";
      report_error(expected_token, found);
   }
   //
   token_.resize(0);
   while( ok )
   {  token_.push_back( json_[index_] );
      ok = index_ + 1 < json_.size();
      if( ok )
      {  char ch  = json_[index_ + 1];
         ok  = isdigit(ch);
         ok |= (ch == '.') || (ch == '+') || (ch == '-');
         ok |= (ch == 'e') || (ch == 'E');
      }
      if( ok )
         next_index();
   }
   return;
}

} } } // END_CPPAD_LOCAL_GRAPH_NAMESPACE