main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/GenericLexer.h>
  7. #include <AK/HashTable.h>
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/SourceGenerator.h>
  10. #include <AK/String.h>
  11. #include <AK/StringBuilder.h>
  12. #include <AK/Types.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/File.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. struct Range {
  18. int begin;
  19. int end;
  20. };
  21. struct StateTransition {
  22. Optional<String> new_state;
  23. Optional<String> action;
  24. };
  25. struct MatchedAction {
  26. Range range;
  27. StateTransition action;
  28. };
  29. struct State {
  30. String name;
  31. Vector<MatchedAction> actions;
  32. Optional<String> entry_action;
  33. Optional<String> exit_action;
  34. };
  35. struct StateMachine {
  36. String name;
  37. String initial_state;
  38. Vector<State> states;
  39. Optional<State> anywhere;
  40. Optional<String> namespaces;
  41. };
  42. static OwnPtr<StateMachine>
  43. parse_state_machine(StringView input)
  44. {
  45. auto state_machine = make<StateMachine>();
  46. GenericLexer lexer(input);
  47. auto consume_whitespace = [&] {
  48. bool consumed = true;
  49. while (consumed) {
  50. consumed = lexer.consume_while(isspace).length() > 0;
  51. if (lexer.consume_specific("//")) {
  52. lexer.consume_line();
  53. consumed = true;
  54. }
  55. }
  56. };
  57. auto consume_identifier = [&] {
  58. consume_whitespace();
  59. return lexer.consume_while([](char c) { return isalnum(c) || c == '_'; });
  60. };
  61. auto get_hex_value = [&](char c) {
  62. if (isdigit(c))
  63. return c - '0';
  64. else
  65. return c - 'a' + 10;
  66. };
  67. auto consume_number = [&] {
  68. int num = 0;
  69. consume_whitespace();
  70. if (lexer.consume_specific("0x")) {
  71. auto hex_digits = lexer.consume_while([](char c) {
  72. if (isdigit(c)) return true;
  73. else {
  74. c = tolower(c);
  75. return (c >= 'a' && c <= 'f');
  76. } });
  77. for (auto c : hex_digits)
  78. num = 16 * num + get_hex_value(c);
  79. } else {
  80. lexer.consume_specific('\'');
  81. if (lexer.next_is('\\'))
  82. num = (int)lexer.consume_escaped_character('\\');
  83. else
  84. num = lexer.consume_until('\'').to_int().value();
  85. lexer.consume_specific('\'');
  86. }
  87. return num;
  88. };
  89. auto consume_condition = [&] {
  90. Range condition;
  91. consume_whitespace();
  92. if (lexer.consume_specific('[')) {
  93. consume_whitespace();
  94. condition.begin = consume_number();
  95. consume_whitespace();
  96. lexer.consume_specific("..");
  97. consume_whitespace();
  98. condition.end = consume_number();
  99. consume_whitespace();
  100. lexer.consume_specific(']');
  101. } else {
  102. auto num = consume_number();
  103. condition.begin = num;
  104. condition.end = num;
  105. }
  106. return condition;
  107. };
  108. auto consume_action = [&]() {
  109. StateTransition action;
  110. consume_whitespace();
  111. lexer.consume_specific("=>");
  112. consume_whitespace();
  113. lexer.consume_specific('(');
  114. consume_whitespace();
  115. if (!lexer.consume_specific("_"))
  116. action.new_state = consume_identifier();
  117. consume_whitespace();
  118. lexer.consume_specific(',');
  119. consume_whitespace();
  120. if (!lexer.consume_specific("_"))
  121. action.action = consume_identifier();
  122. consume_whitespace();
  123. lexer.consume_specific(')');
  124. return action;
  125. };
  126. auto consume_state_description
  127. = [&] {
  128. State state;
  129. consume_whitespace();
  130. state.name = consume_identifier();
  131. consume_whitespace();
  132. consume_whitespace();
  133. lexer.consume_specific('{');
  134. for (;;) {
  135. consume_whitespace();
  136. if (lexer.consume_specific('}')) {
  137. break;
  138. }
  139. if (lexer.consume_specific("@entry")) {
  140. consume_whitespace();
  141. state.entry_action = consume_identifier();
  142. } else if (lexer.consume_specific("@exit")) {
  143. consume_whitespace();
  144. state.exit_action = consume_identifier();
  145. } else if (lexer.next_is('@')) {
  146. auto directive = consume_identifier().to_string();
  147. fprintf(stderr, "Unimplemented @ directive %s\n", directive.characters());
  148. exit(1);
  149. } else {
  150. MatchedAction matched_action;
  151. matched_action.range = consume_condition();
  152. matched_action.action = consume_action();
  153. state.actions.append(matched_action);
  154. }
  155. }
  156. return state;
  157. };
  158. while (!lexer.is_eof()) {
  159. consume_whitespace();
  160. if (lexer.is_eof())
  161. break;
  162. if (lexer.consume_specific("@namespace")) {
  163. consume_whitespace();
  164. state_machine->namespaces = lexer.consume_while([](char c) { return isalpha(c) || c == ':'; });
  165. } else if (lexer.consume_specific("@begin")) {
  166. consume_whitespace();
  167. state_machine->initial_state = consume_identifier();
  168. } else if (lexer.consume_specific("@name")) {
  169. consume_whitespace();
  170. state_machine->name = consume_identifier();
  171. } else if (lexer.next_is("@anywhere")) {
  172. lexer.consume_specific('@');
  173. state_machine->anywhere = consume_state_description();
  174. } else if (lexer.consume_specific('@')) {
  175. auto directive = consume_identifier().to_string();
  176. fprintf(stderr, "Unimplemented @ directive %s\n", directive.characters());
  177. exit(1);
  178. } else {
  179. auto description = consume_state_description();
  180. state_machine->states.append(description);
  181. }
  182. }
  183. if (state_machine->initial_state.is_empty()) {
  184. fprintf(stderr, "Missing @begin directive\n");
  185. exit(1);
  186. } else if (state_machine->name.is_empty()) {
  187. fprintf(stderr, "Missing @name directive\n");
  188. exit(1);
  189. }
  190. if (state_machine->anywhere.has_value()) {
  191. state_machine->anywhere.value().name = "_Anywhere";
  192. }
  193. return state_machine;
  194. }
  195. void output_header(const StateMachine&, SourceGenerator&);
  196. void output_cpp(const StateMachine&, SourceGenerator&);
  197. int main(int argc, char** argv)
  198. {
  199. Core::ArgsParser args_parser;
  200. const char* path = nullptr;
  201. bool header_mode = false;
  202. args_parser.add_option(header_mode, "Generate .h file", "header", 'H');
  203. args_parser.add_positional_argument(path, "Path to parser description", "input", Core::ArgsParser::Required::Yes);
  204. args_parser.parse(argc, argv);
  205. auto file_or_error = Core::File::open(path, Core::IODevice::ReadOnly);
  206. if (file_or_error.is_error()) {
  207. fprintf(stderr, "Cannot open %s\n", path);
  208. }
  209. auto content = file_or_error.value()->read_all();
  210. auto state_machine = parse_state_machine(content);
  211. StringBuilder builder;
  212. SourceGenerator generator { builder };
  213. if (header_mode)
  214. output_header(*state_machine, generator);
  215. else
  216. output_cpp(*state_machine, generator);
  217. outln("{}", generator.as_string_view());
  218. return 0;
  219. }
  220. HashTable<String> actions(const StateMachine& machine)
  221. {
  222. HashTable<String> table;
  223. auto do_state = [&](const State& state) {
  224. if (state.entry_action.has_value())
  225. table.set(state.entry_action.value());
  226. if (state.exit_action.has_value())
  227. table.set(state.exit_action.value());
  228. for (auto action : state.actions) {
  229. if (action.action.action.has_value())
  230. table.set(action.action.action.value());
  231. }
  232. };
  233. for (auto state : machine.states) {
  234. do_state(state);
  235. }
  236. if (machine.anywhere.has_value())
  237. do_state(machine.anywhere.value());
  238. return move(table);
  239. }
  240. void generate_lookup_table(const StateMachine& machine, SourceGenerator& generator)
  241. {
  242. generator.append(R"~~~(
  243. static constexpr StateTransition STATE_TRANSITION_TABLE[][256] = {
  244. )~~~");
  245. auto generate_for_state = [&](const State& s) {
  246. auto table_generator = generator.fork();
  247. table_generator.set("active_state", s.name);
  248. table_generator.append("/* @active_state@ */ { ");
  249. VERIFY(!s.name.is_empty());
  250. Vector<StateTransition> row;
  251. for (int i = 0; i < 256; i++)
  252. row.append({ s.name, "_Ignore" });
  253. for (auto action : s.actions) {
  254. for (int range_element = action.range.begin; range_element <= action.range.end; range_element++) {
  255. row[range_element] = { action.action.new_state, action.action.action };
  256. }
  257. }
  258. for (int i = 0; i < 256; ++i) {
  259. auto cell_generator = table_generator.fork();
  260. cell_generator.set("cell_new_state", row[i].new_state.value_or(s.name));
  261. cell_generator.set("cell_action", row[i].action.value_or("_Ignore"));
  262. cell_generator.append(" {State::@cell_new_state@, Action::@cell_action@}, ");
  263. }
  264. table_generator.append("},\n");
  265. };
  266. if (machine.anywhere.has_value()) {
  267. generate_for_state(machine.anywhere.value());
  268. }
  269. for (auto s : machine.states) {
  270. generate_for_state(s);
  271. }
  272. generator.append(R"~~~(
  273. };
  274. )~~~");
  275. }
  276. void output_header(const StateMachine& machine, SourceGenerator& generator)
  277. {
  278. generator.set("class_name", machine.name);
  279. generator.set("initial_state", machine.initial_state);
  280. generator.set("state_count", String::number(machine.states.size() + 1));
  281. generator.append(R"~~~(
  282. #pragma once
  283. #include <AK/Function.h>
  284. #include <AK/Platform.h>
  285. #include <AK/Types.h>
  286. )~~~");
  287. if (machine.namespaces.has_value()) {
  288. generator.set("namespace", machine.namespaces.value());
  289. generator.append(R"~~~(
  290. namespace @namespace@ {
  291. )~~~");
  292. }
  293. generator.append(R"~~~(
  294. class @class_name@ {
  295. public:
  296. enum class Action : u8 {
  297. _Ignore,
  298. )~~~");
  299. for (auto a : actions(machine)) {
  300. if (a.is_empty())
  301. continue;
  302. auto action_generator = generator.fork();
  303. action_generator.set("action.name", a);
  304. action_generator.append(R"~~~(
  305. @action.name@,
  306. )~~~");
  307. }
  308. generator.append(R"~~~(
  309. }; // end Action
  310. typedef Function<void(Action, u8)> Handler;
  311. @class_name@(Handler);
  312. void advance(u8);
  313. private:
  314. enum class State : u8 {
  315. _Anywhere,
  316. )~~~");
  317. int largest_state_value = 0;
  318. for (auto s : machine.states) {
  319. auto state_generator = generator.fork();
  320. state_generator.set("state.name", s.name);
  321. largest_state_value++;
  322. state_generator.append(R"~~~(
  323. @state.name@,
  324. )~~~");
  325. }
  326. generator.append(R"~~~(
  327. }; // end State
  328. struct StateTransition {
  329. State new_state;
  330. Action action;
  331. };
  332. State m_state { State::@initial_state@ };
  333. Handler m_handler;
  334. StateTransition lookup_state_transition(u8);
  335. )~~~");
  336. auto table_generator = generator.fork();
  337. generate_lookup_table(machine, table_generator);
  338. generator.append(R"~~~(
  339. }; // end @class_name@
  340. )~~~");
  341. if (machine.namespaces.has_value()) {
  342. generator.append(R"~~~(
  343. } // end namespace
  344. )~~~");
  345. }
  346. }
  347. void output_cpp(const StateMachine& machine, SourceGenerator& generator)
  348. {
  349. VERIFY(!machine.name.is_empty());
  350. generator.set("class_name", machine.name);
  351. generator.set("state_count", String::number(machine.states.size() + 1));
  352. generator.append(R"~~~(
  353. #include "@class_name@.h"
  354. #include <AK/Function.h>
  355. #include <AK/Types.h>
  356. )~~~");
  357. if (machine.namespaces.has_value()) {
  358. generator.set("namespace", machine.namespaces.value());
  359. generator.append(R"~~~(
  360. namespace @namespace@ {
  361. )~~~");
  362. }
  363. generator.append(R"~~~(
  364. @class_name@::@class_name@(Handler handler)
  365. : m_handler(move(handler))
  366. {
  367. }
  368. ALWAYS_INLINE @class_name@::StateTransition @class_name@::lookup_state_transition(u8 byte)
  369. {
  370. VERIFY((u8)m_state < @state_count@);
  371. )~~~");
  372. if (machine.anywhere.has_value()) {
  373. generator.append(R"~~~(
  374. auto anywhere_state = STATE_TRANSITION_TABLE[0][byte];
  375. if (anywhere_state.new_state != @class_name@::State::_Anywhere || anywhere_state.action != @class_name@::Action::_Ignore)
  376. return anywhere_state;
  377. else
  378. )~~~");
  379. }
  380. generator.append(R"~~~(
  381. return STATE_TRANSITION_TABLE[(u8)m_state][byte];
  382. }
  383. )~~~");
  384. generator.append(R"~~~(
  385. void @class_name@::advance(u8 byte)
  386. {
  387. auto next_state = lookup_state_transition(byte);
  388. bool state_will_change = next_state.new_state != m_state && next_state.new_state != @class_name@::State::_Anywhere;
  389. // only run exit directive if state is being changed
  390. if (state_will_change)
  391. {
  392. switch (m_state)
  393. {
  394. )~~~");
  395. for (auto s : machine.states) {
  396. auto state_generator = generator.fork();
  397. if (s.exit_action.has_value()) {
  398. state_generator.set("state_name", s.name);
  399. state_generator.set("action", s.exit_action.value());
  400. state_generator.append(R"~~~(
  401. case @class_name@::State::@state_name@:
  402. m_handler(Action::@action@, byte);
  403. break;
  404. )~~~");
  405. }
  406. }
  407. generator.append(R"~~~(
  408. default:
  409. break;
  410. }
  411. }
  412. if (next_state.action != @class_name@::Action::_Ignore)
  413. m_handler(next_state.action, byte);
  414. m_state = next_state.new_state;
  415. // only run entry directive if state is being changed
  416. if (state_will_change)
  417. {
  418. switch (next_state.new_state)
  419. {
  420. )~~~");
  421. for (auto state : machine.states) {
  422. auto state_generator = generator.fork();
  423. if (state.entry_action.has_value()) {
  424. state_generator.set("state_name", state.name);
  425. state_generator.set("action", state.entry_action.value());
  426. state_generator.append(R"~~~(
  427. case @class_name@::State::@state_name@:
  428. m_handler(Action::@action@, byte);
  429. break;
  430. )~~~");
  431. }
  432. }
  433. generator.append(R"~~~(
  434. default:
  435. break;
  436. }
  437. }
  438. }
  439. )~~~");
  440. if (machine.namespaces.has_value()) {
  441. generator.append(R"~~~(
  442. } // end namespace
  443. )~~~");
  444. }
  445. }