agent_lexer.ll 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /* Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
  2. This Source Code Form is subject to the terms of the Mozilla Public
  3. License, v. 2.0. If a copy of the MPL was not distributed with this
  4. file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. %{ /* -*- C++ -*- */
  6. #include <cerrno>
  7. #include <climits>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <agent/parser_context.h>
  11. #include <asiolink/io_address.h>
  12. #include <boost/lexical_cast.hpp>
  13. #include <exceptions/exceptions.h>
  14. #include <cc/dhcp_config_error.h>
  15. /* Please avoid C++ style comments (// ... eol) as they break flex 2.6.2 */
  16. /* Work around an incompatibility in flex (at least versions
  17. 2.5.31 through 2.5.33): it generates code that does
  18. not conform to C89. See Debian bug 333231
  19. <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
  20. # undef yywrap
  21. # define yywrap() 1
  22. namespace {
  23. bool start_token_flag = false;
  24. isc::agent::ParserContext::ParserType start_token_value;
  25. unsigned int comment_start_line = 0;
  26. using namespace isc;
  27. using isc::agent::AgentParser;
  28. };
  29. /* To avoid the call to exit... oops! */
  30. #define YY_FATAL_ERROR(msg) isc::agent::ParserContext::fatal(msg)
  31. %}
  32. /* noyywrap disables automatic rewinding for the next file to parse. Since we
  33. always parse only a single string, there's no need to do any wraps. And
  34. using yywrap requires linking with -lfl, which provides the default yywrap
  35. implementation that always returns 1 anyway. */
  36. %option noyywrap
  37. /* nounput simplifies the lexer, by removing support for putting a character
  38. back into the input stream. We never use such capability anyway. */
  39. %option nounput
  40. /* batch means that we'll never use the generated lexer interactively. */
  41. %option batch
  42. /* avoid to get static global variables to remain with C++. */
  43. /* in last resort %option reentrant */
  44. /* Enables debug mode. To see the debug messages, one needs to also set
  45. yy_flex_debug to 1, then the debug messages will be printed on stderr. */
  46. %option debug
  47. /* I have no idea what this option does, except it was specified in the bison
  48. examples and Postgres folks added it to remove gcc 4.3 warnings. Let's
  49. be on the safe side and keep it. */
  50. %option noinput
  51. %x COMMENT
  52. %x DIR_ENTER DIR_INCLUDE DIR_EXIT
  53. /* These are not token expressions yet, just convenience expressions that
  54. can be used during actual token definitions. Note some can match
  55. incorrect inputs (e.g., IP addresses) which must be checked. */
  56. int \-?[0-9]+
  57. blank [ \t\r]
  58. UnicodeEscapeSequence u[0-9A-Fa-f]{4}
  59. JSONEscapeCharacter ["\\/bfnrt]
  60. JSONEscapeSequence {JSONEscapeCharacter}|{UnicodeEscapeSequence}
  61. JSONStandardCharacter [^\x00-\x1f"\\]
  62. JSONStringCharacter {JSONStandardCharacter}|\\{JSONEscapeSequence}
  63. JSONString \"{JSONStringCharacter}*\"
  64. /* for errors */
  65. BadUnicodeEscapeSequence u[0-9A-Fa-f]{0,3}[^0-9A-Fa-f]
  66. BadJSONEscapeSequence [^"\\/bfnrtu]|{BadUnicodeEscapeSequence}
  67. ControlCharacter [\x00-\x1f]
  68. ControlCharacterFill [^"\\]|\\{JSONEscapeSequence}
  69. %{
  70. /* This code run each time a pattern is matched. It updates the location
  71. by moving it ahead by yyleng bytes. yyleng specifies the length of the
  72. currently matched token. */
  73. #define YY_USER_ACTION driver.loc_.columns(yyleng);
  74. %}
  75. %%
  76. %{
  77. /* This part of the code is copied over to the verbatim to the top
  78. of the generated yylex function. Explanation:
  79. http://www.gnu.org/software/bison/manual/html_node/Multiple-start_002dsymbols.html */
  80. /* Code run each time yylex is called. */
  81. driver.loc_.step();
  82. /* We currently have 3 points of entries defined:
  83. START_JSON - which expects any valid JSON
  84. START_AGENT - which expects full configuration (with outer map and Control-agent
  85. object in it.
  86. START_SUB_AGENT - which expects only content of the Control-agent, this is
  87. primarily useful for testing. */
  88. if (start_token_flag) {
  89. start_token_flag = false;
  90. switch (start_token_value) {
  91. case ParserContext::PARSER_JSON:
  92. default:
  93. return isc::agent::AgentParser::make_START_JSON(driver.loc_);
  94. case ParserContext::PARSER_AGENT:
  95. return isc::agent::AgentParser::make_START_AGENT(driver.loc_);
  96. case ParserContext::PARSER_SUB_AGENT:
  97. return isc::agent::AgentParser::make_START_SUB_AGENT(driver.loc_);
  98. }
  99. }
  100. %}
  101. #.* ;
  102. "//"(.*) ;
  103. "/*" {
  104. BEGIN(COMMENT);
  105. comment_start_line = driver.loc_.end.line;;
  106. }
  107. <COMMENT>"*/" BEGIN(INITIAL);
  108. <COMMENT>. ;
  109. <COMMENT><<EOF>> {
  110. isc_throw(ParseError, "Comment not closed. (/* in line " << comment_start_line);
  111. }
  112. "<?" BEGIN(DIR_ENTER);
  113. <DIR_ENTER>"include" BEGIN(DIR_INCLUDE);
  114. <DIR_INCLUDE>\"([^\"\n])+\" {
  115. /* Include directive. */
  116. /* Extract the filename. */
  117. std::string tmp(yytext+1);
  118. tmp.resize(tmp.size() - 1);
  119. driver.includeFile(tmp);
  120. }
  121. <DIR_ENTER,DIR_INCLUDE,DIR_EXIT><<EOF>> {
  122. isc_throw(ParseError, "Directive not closed.");
  123. }
  124. <DIR_EXIT>"?>" BEGIN(INITIAL);
  125. <*>{blank}+ {
  126. /* Ok, we found a with space. Let's ignore it and update loc variable. */
  127. driver.loc_.step();
  128. }
  129. <*>[\n]+ {
  130. /* Newline found. Let's update the location and continue. */
  131. driver.loc_.lines(yyleng);
  132. driver.loc_.step();
  133. }
  134. \"Control-agent\" {
  135. switch(driver.ctx_) {
  136. case ParserContext::CONFIG:
  137. return AgentParser::make_CONTROL_AGENT(driver.loc_);
  138. default:
  139. return AgentParser::make_STRING("Control-agent", driver.loc_);
  140. }
  141. }
  142. \"http-host\" {
  143. switch(driver.ctx_) {
  144. case ParserContext::AGENT:
  145. return AgentParser::make_HTTP_HOST(driver.loc_);
  146. default:
  147. return AgentParser::make_STRING("http-host", driver.loc_);
  148. }
  149. }
  150. \"http-port\" {
  151. switch(driver.ctx_) {
  152. case ParserContext::AGENT:
  153. return AgentParser::make_HTTP_PORT(driver.loc_);
  154. default:
  155. return AgentParser::make_STRING("http-port", driver.loc_);
  156. }
  157. }
  158. \"control-sockets\" {
  159. switch(driver.ctx_) {
  160. case ParserContext::AGENT:
  161. return AgentParser::make_CONTROL_SOCKETS(driver.loc_);
  162. default:
  163. return AgentParser::make_STRING("control-sockets", driver.loc_);
  164. }
  165. }
  166. \"dhcp4\" {
  167. switch(driver.ctx_) {
  168. case ParserContext::CONTROL_SOCKETS:
  169. return AgentParser::make_DHCP4_SERVER(driver.loc_);
  170. default:
  171. return AgentParser::make_STRING("dhcp4", driver.loc_);
  172. }
  173. }
  174. \"dhcp6\" {
  175. switch(driver.ctx_) {
  176. case ParserContext::CONTROL_SOCKETS:
  177. return AgentParser::make_DHCP6_SERVER(driver.loc_);
  178. default:
  179. return AgentParser::make_STRING("dhcp6", driver.loc_);
  180. }
  181. }
  182. \"d2\" {
  183. switch(driver.ctx_) {
  184. case ParserContext::CONTROL_SOCKETS:
  185. return AgentParser::make_D2_SERVER(driver.loc_);
  186. default:
  187. return AgentParser::make_STRING("d2", driver.loc_);
  188. }
  189. }
  190. \"socket-name\" {
  191. switch(driver.ctx_) {
  192. case ParserContext::SERVER:
  193. return AgentParser::make_SOCKET_NAME(driver.loc_);
  194. default:
  195. return AgentParser::make_STRING("socket-name", driver.loc_);
  196. }
  197. }
  198. \"socket-type\" {
  199. switch(driver.ctx_) {
  200. case ParserContext::SERVER:
  201. return AgentParser::make_SOCKET_TYPE(driver.loc_);
  202. default:
  203. return AgentParser::make_STRING("socket-type", driver.loc_);
  204. }
  205. }
  206. \"unix\" {
  207. switch(driver.ctx_) {
  208. case ParserContext::SOCKET_TYPE:
  209. return AgentParser::make_UNIX(driver.loc_);
  210. default:
  211. return AgentParser::make_STRING("unix", driver.loc_);
  212. }
  213. }
  214. \"hooks-libraries\" {
  215. switch(driver.ctx_) {
  216. case ParserContext::AGENT:
  217. return AgentParser::make_HOOKS_LIBRARIES(driver.loc_);
  218. default:
  219. return AgentParser::make_STRING("hooks-libraries", driver.loc_);
  220. }
  221. }
  222. \"library\" {
  223. switch(driver.ctx_) {
  224. case ParserContext::HOOKS_LIBRARIES:
  225. return AgentParser::make_LIBRARY(driver.loc_);
  226. default:
  227. return AgentParser::make_STRING("library", driver.loc_);
  228. }
  229. }
  230. \"parameters\" {
  231. switch(driver.ctx_) {
  232. case ParserContext::HOOKS_LIBRARIES:
  233. return AgentParser::make_PARAMETERS(driver.loc_);
  234. default:
  235. return AgentParser::make_STRING("parameters", driver.loc_);
  236. }
  237. }
  238. \"Logging\" {
  239. switch(driver.ctx_) {
  240. case ParserContext::CONFIG:
  241. return AgentParser::make_LOGGING(driver.loc_);
  242. default:
  243. return AgentParser::make_STRING("Logging", driver.loc_);
  244. }
  245. }
  246. \"loggers\" {
  247. switch(driver.ctx_) {
  248. case ParserContext::LOGGING:
  249. return AgentParser::make_LOGGERS(driver.loc_);
  250. default:
  251. return AgentParser::make_STRING("loggers", driver.loc_);
  252. }
  253. }
  254. \"name\" {
  255. switch(driver.ctx_) {
  256. case ParserContext::LOGGERS:
  257. return AgentParser::make_NAME(driver.loc_);
  258. default:
  259. return AgentParser::make_STRING("name", driver.loc_);
  260. }
  261. }
  262. \"output_options\" {
  263. switch(driver.ctx_) {
  264. case ParserContext::LOGGERS:
  265. return AgentParser::make_OUTPUT_OPTIONS(driver.loc_);
  266. default:
  267. return AgentParser::make_STRING("output_options", driver.loc_);
  268. }
  269. }
  270. \"output\" {
  271. switch(driver.ctx_) {
  272. case ParserContext::OUTPUT_OPTIONS:
  273. return AgentParser::make_OUTPUT(driver.loc_);
  274. default:
  275. return AgentParser::make_STRING("output", driver.loc_);
  276. }
  277. }
  278. \"flush\" {
  279. switch(driver.ctx_) {
  280. case ParserContext::OUTPUT_OPTIONS:
  281. return AgentParser::make_FLUSH(driver.loc_);
  282. default:
  283. return AgentParser::make_STRING("flush", driver.loc_);
  284. }
  285. }
  286. \"maxsize\" {
  287. switch(driver.ctx_) {
  288. case ParserContext::OUTPUT_OPTIONS:
  289. return AgentParser::make_MAXSIZE(driver.loc_);
  290. default:
  291. return AgentParser::make_STRING("maxsize", driver.loc_);
  292. }
  293. }
  294. \"maxver\" {
  295. switch(driver.ctx_) {
  296. case ParserContext::OUTPUT_OPTIONS:
  297. return AgentParser::make_MAXVER(driver.loc_);
  298. default:
  299. return AgentParser::make_STRING("maxver", driver.loc_);
  300. }
  301. }
  302. \"debuglevel\" {
  303. switch(driver.ctx_) {
  304. case ParserContext::LOGGERS:
  305. return AgentParser::make_DEBUGLEVEL(driver.loc_);
  306. default:
  307. return AgentParser::make_STRING("debuglevel", driver.loc_);
  308. }
  309. }
  310. \"severity\" {
  311. switch(driver.ctx_) {
  312. case ParserContext::LOGGERS:
  313. return AgentParser::make_SEVERITY(driver.loc_);
  314. default:
  315. return AgentParser::make_STRING("severity", driver.loc_);
  316. }
  317. }
  318. \"Dhcp4\" {
  319. switch(driver.ctx_) {
  320. case ParserContext::CONFIG:
  321. return AgentParser::make_DHCP4(driver.loc_);
  322. default:
  323. return AgentParser::make_STRING("Dhcp4", driver.loc_);
  324. }
  325. }
  326. \"Dhcp6\" {
  327. switch(driver.ctx_) {
  328. case ParserContext::CONFIG:
  329. return AgentParser::make_DHCP6(driver.loc_);
  330. default:
  331. return AgentParser::make_STRING("Dhcp6", driver.loc_);
  332. }
  333. }
  334. \"DhcpDdns\" {
  335. switch(driver.ctx_) {
  336. case ParserContext::CONFIG:
  337. return AgentParser::make_DHCPDDNS(driver.loc_);
  338. default:
  339. return AgentParser::make_STRING("DhcpDdns", driver.loc_);
  340. }
  341. }
  342. {JSONString} {
  343. /* A string has been matched. It contains the actual string and single quotes.
  344. We need to get those quotes out of the way and just use its content, e.g.
  345. for 'foo' we should get foo */
  346. std::string raw(yytext+1);
  347. size_t len = raw.size() - 1;
  348. raw.resize(len);
  349. std::string decoded;
  350. decoded.reserve(len);
  351. for (size_t pos = 0; pos < len; ++pos) {
  352. int b = 0;
  353. char c = raw[pos];
  354. switch (c) {
  355. case '"':
  356. /* impossible condition */
  357. driver.error(driver.loc_, "Bad quote in \"" + raw + "\"");
  358. case '\\':
  359. ++pos;
  360. if (pos >= len) {
  361. /* impossible condition */
  362. driver.error(driver.loc_, "Overflow escape in \"" + raw + "\"");
  363. }
  364. c = raw[pos];
  365. switch (c) {
  366. case '"':
  367. case '\\':
  368. case '/':
  369. decoded.push_back(c);
  370. break;
  371. case 'b':
  372. decoded.push_back('\b');
  373. break;
  374. case 'f':
  375. decoded.push_back('\f');
  376. break;
  377. case 'n':
  378. decoded.push_back('\n');
  379. break;
  380. case 'r':
  381. decoded.push_back('\r');
  382. break;
  383. case 't':
  384. decoded.push_back('\t');
  385. break;
  386. case 'u':
  387. /* support only \u0000 to \u00ff */
  388. ++pos;
  389. if (pos + 4 > len) {
  390. /* impossible condition */
  391. driver.error(driver.loc_,
  392. "Overflow unicode escape in \"" + raw + "\"");
  393. }
  394. if ((raw[pos] != '0') || (raw[pos + 1] != '0')) {
  395. driver.error(driver.loc_, "Unsupported unicode escape in \"" + raw + "\"");
  396. }
  397. pos += 2;
  398. c = raw[pos];
  399. if ((c >= '0') && (c <= '9')) {
  400. b = (c - '0') << 4;
  401. } else if ((c >= 'A') && (c <= 'F')) {
  402. b = (c - 'A' + 10) << 4;
  403. } else if ((c >= 'a') && (c <= 'f')) {
  404. b = (c - 'a' + 10) << 4;
  405. } else {
  406. /* impossible condition */
  407. driver.error(driver.loc_, "Not hexadecimal in unicode escape in \"" + raw + "\"");
  408. }
  409. pos++;
  410. c = raw[pos];
  411. if ((c >= '0') && (c <= '9')) {
  412. b |= c - '0';
  413. } else if ((c >= 'A') && (c <= 'F')) {
  414. b |= c - 'A' + 10;
  415. } else if ((c >= 'a') && (c <= 'f')) {
  416. b |= c - 'a' + 10;
  417. } else {
  418. /* impossible condition */
  419. driver.error(driver.loc_, "Not hexadecimal in unicode escape in \"" + raw + "\"");
  420. }
  421. decoded.push_back(static_cast<char>(b & 0xff));
  422. break;
  423. default:
  424. /* impossible condition */
  425. driver.error(driver.loc_, "Bad escape in \"" + raw + "\"");
  426. }
  427. break;
  428. default:
  429. if ((c >= 0) && (c < 0x20)) {
  430. /* impossible condition */
  431. driver.error(driver.loc_, "Invalid control in \"" + raw + "\"");
  432. }
  433. decoded.push_back(c);
  434. }
  435. }
  436. return AgentParser::make_STRING(decoded, driver.loc_);
  437. }
  438. \"{JSONStringCharacter}*{ControlCharacter}{ControlCharacterFill}*\" {
  439. /* Bad string with a forbidden control character inside */
  440. driver.error(driver.loc_, "Invalid control in " + std::string(yytext));
  441. }
  442. \"{JSONStringCharacter}*\\{BadJSONEscapeSequence}[^\x00-\x1f"]*\" {
  443. /* Bad string with a bad escape inside */
  444. driver.error(driver.loc_, "Bad escape in " + std::string(yytext));
  445. }
  446. \"{JSONStringCharacter}*\\\" {
  447. /* Bad string with an open escape at the end */
  448. driver.error(driver.loc_, "Overflow escape in " + std::string(yytext));
  449. }
  450. "[" { return AgentParser::make_LSQUARE_BRACKET(driver.loc_); }
  451. "]" { return AgentParser::make_RSQUARE_BRACKET(driver.loc_); }
  452. "{" { return AgentParser::make_LCURLY_BRACKET(driver.loc_); }
  453. "}" { return AgentParser::make_RCURLY_BRACKET(driver.loc_); }
  454. "," { return AgentParser::make_COMMA(driver.loc_); }
  455. ":" { return AgentParser::make_COLON(driver.loc_); }
  456. {int} {
  457. /* An integer was found. */
  458. std::string tmp(yytext);
  459. int64_t integer = 0;
  460. try {
  461. /* In substring we want to use negative values (e.g. -1).
  462. In enterprise-id we need to use values up to 0xffffffff.
  463. To cover both of those use cases, we need at least
  464. int64_t. */
  465. integer = boost::lexical_cast<int64_t>(tmp);
  466. } catch (const boost::bad_lexical_cast &) {
  467. driver.error(driver.loc_, "Failed to convert " + tmp + " to an integer.");
  468. }
  469. /* The parser needs the string form as double conversion is no lossless */
  470. return AgentParser::make_INTEGER(integer, driver.loc_);
  471. }
  472. [-+]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)? {
  473. /* A floating point was found. */
  474. std::string tmp(yytext);
  475. double fp = 0.0;
  476. try {
  477. fp = boost::lexical_cast<double>(tmp);
  478. } catch (const boost::bad_lexical_cast &) {
  479. driver.error(driver.loc_, "Failed to convert " + tmp + " to a floating point.");
  480. }
  481. return AgentParser::make_FLOAT(fp, driver.loc_);
  482. }
  483. true|false {
  484. string tmp(yytext);
  485. return AgentParser::make_BOOLEAN(tmp == "true", driver.loc_);
  486. }
  487. null {
  488. return AgentParser::make_NULL_TYPE(driver.loc_);
  489. }
  490. (?i:true) driver.error (driver.loc_, "JSON true reserved keyword is lower case only");
  491. (?i:false) driver.error (driver.loc_, "JSON false reserved keyword is lower case only");
  492. (?i:null) driver.error (driver.loc_, "JSON null reserved keyword is lower case only");
  493. <*>. driver.error (driver.loc_, "Invalid character: " + std::string(yytext));
  494. <<EOF>> {
  495. if (driver.states_.empty()) {
  496. return AgentParser::make_END(driver.loc_);
  497. }
  498. driver.loc_ = driver.locs_.back();
  499. driver.locs_.pop_back();
  500. driver.file_ = driver.files_.back();
  501. driver.files_.pop_back();
  502. if (driver.sfile_) {
  503. fclose(driver.sfile_);
  504. driver.sfile_ = 0;
  505. }
  506. if (!driver.sfiles_.empty()) {
  507. driver.sfile_ = driver.sfiles_.back();
  508. driver.sfiles_.pop_back();
  509. }
  510. agent__delete_buffer(YY_CURRENT_BUFFER);
  511. agent__switch_to_buffer(driver.states_.back());
  512. driver.states_.pop_back();
  513. BEGIN(DIR_EXIT);
  514. }
  515. %%
  516. using namespace isc::dhcp;
  517. void
  518. ParserContext::scanStringBegin(const std::string& str, ParserType parser_type)
  519. {
  520. start_token_flag = true;
  521. start_token_value = parser_type;
  522. file_ = "<string>";
  523. sfile_ = 0;
  524. loc_.initialize(&file_);
  525. yy_flex_debug = trace_scanning_;
  526. YY_BUFFER_STATE buffer;
  527. buffer = agent__scan_bytes(str.c_str(), str.size());
  528. if (!buffer) {
  529. fatal("cannot scan string");
  530. /* fatal() throws an exception so this can't be reached */
  531. }
  532. }
  533. void
  534. ParserContext::scanFileBegin(FILE * f,
  535. const std::string& filename,
  536. ParserType parser_type)
  537. {
  538. start_token_flag = true;
  539. start_token_value = parser_type;
  540. file_ = filename;
  541. sfile_ = f;
  542. loc_.initialize(&file_);
  543. yy_flex_debug = trace_scanning_;
  544. YY_BUFFER_STATE buffer;
  545. /* See agent_lexer.cc header for available definitions */
  546. buffer = agent__create_buffer(f, 65536 /*buffer size*/);
  547. if (!buffer) {
  548. fatal("cannot scan file " + filename);
  549. }
  550. agent__switch_to_buffer(buffer);
  551. }
  552. void
  553. ParserContext::scanEnd() {
  554. if (sfile_)
  555. fclose(sfile_);
  556. sfile_ = 0;
  557. static_cast<void>(agent_lex_destroy());
  558. /* Close files */
  559. while (!sfiles_.empty()) {
  560. FILE* f = sfiles_.back();
  561. if (f) {
  562. fclose(f);
  563. }
  564. sfiles_.pop_back();
  565. }
  566. /* Delete states */
  567. while (!states_.empty()) {
  568. agent__delete_buffer(states_.back());
  569. states_.pop_back();
  570. }
  571. }
  572. void
  573. ParserContext::includeFile(const std::string& filename) {
  574. if (states_.size() > 10) {
  575. fatal("Too many nested include.");
  576. }
  577. FILE* f = fopen(filename.c_str(), "r");
  578. if (!f) {
  579. fatal("Can't open include file " + filename);
  580. }
  581. if (sfile_) {
  582. sfiles_.push_back(sfile_);
  583. }
  584. sfile_ = f;
  585. states_.push_back(YY_CURRENT_BUFFER);
  586. YY_BUFFER_STATE buffer;
  587. buffer = agent__create_buffer(f, 65536 /*buffer size*/);
  588. if (!buffer) {
  589. fatal( "Can't scan include file " + filename);
  590. }
  591. agent__switch_to_buffer(buffer);
  592. files_.push_back(file_);
  593. file_ = filename;
  594. locs_.push_back(loc_);
  595. loc_.initialize(&file_);
  596. BEGIN(INITIAL);
  597. }
  598. namespace {
  599. /** To avoid unused function error */
  600. class Dummy {
  601. /* cppcheck-suppress unusedPrivateFunction */
  602. void dummy() { yy_fatal_error("Fix me: how to disable its definition?"); }
  603. };
  604. }