main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/OwnPtr.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. int main(int argc, char** argv)
  197. {
  198. Core::ArgsParser args_parser;
  199. const char* path = nullptr;
  200. args_parser.add_positional_argument(path, "Path to parser description", "input", Core::ArgsParser::Required::Yes);
  201. args_parser.parse(argc, argv);
  202. auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
  203. if (file_or_error.is_error()) {
  204. fprintf(stderr, "Cannot open %s\n", path);
  205. }
  206. auto content = file_or_error.value()->read_all();
  207. auto state_machine = parse_state_machine(content);
  208. StringBuilder builder;
  209. SourceGenerator generator { builder };
  210. output_header(*state_machine, generator);
  211. outln("{}", generator.as_string_view());
  212. return 0;
  213. }
  214. HashTable<String> actions(const StateMachine& machine)
  215. {
  216. HashTable<String> table;
  217. auto do_state = [&](const State& state) {
  218. if (state.entry_action.has_value())
  219. table.set(state.entry_action.value());
  220. if (state.exit_action.has_value())
  221. table.set(state.exit_action.value());
  222. for (auto action : state.actions) {
  223. if (action.action.action.has_value())
  224. table.set(action.action.action.value());
  225. }
  226. };
  227. for (auto state : machine.states) {
  228. do_state(state);
  229. }
  230. if (machine.anywhere.has_value())
  231. do_state(machine.anywhere.value());
  232. return move(table);
  233. }
  234. void generate_lookup_table(const StateMachine& machine, SourceGenerator& generator)
  235. {
  236. generator.append(R"~~~(
  237. static constexpr StateTransition STATE_TRANSITION_TABLE[][256] = {
  238. )~~~");
  239. auto generate_for_state = [&](const State& s) {
  240. auto table_generator = generator.fork();
  241. table_generator.set("active_state", s.name);
  242. table_generator.append("/* @active_state@ */ { ");
  243. VERIFY(!s.name.is_empty());
  244. Vector<StateTransition> row;
  245. for (int i = 0; i < 256; i++)
  246. row.append({ s.name, "_Ignore" });
  247. for (auto action : s.actions) {
  248. for (int range_element = action.range.begin; range_element <= action.range.end; range_element++) {
  249. row[range_element] = { action.action.new_state, action.action.action };
  250. }
  251. }
  252. for (int i = 0; i < 256; ++i) {
  253. auto cell_generator = table_generator.fork();
  254. cell_generator.set("cell_new_state", row[i].new_state.value_or(s.name));
  255. cell_generator.set("cell_action", row[i].action.value_or("_Ignore"));
  256. cell_generator.append(" {State::@cell_new_state@, Action::@cell_action@}, ");
  257. }
  258. table_generator.append("},\n");
  259. };
  260. if (machine.anywhere.has_value()) {
  261. generate_for_state(machine.anywhere.value());
  262. }
  263. for (auto s : machine.states) {
  264. generate_for_state(s);
  265. }
  266. generator.append(R"~~~(
  267. };
  268. )~~~");
  269. }
  270. void output_header(const StateMachine& machine, SourceGenerator& generator)
  271. {
  272. generator.set("class_name", machine.name);
  273. generator.set("initial_state", machine.initial_state);
  274. generator.set("state_count", String::number(machine.states.size() + 1));
  275. generator.append(R"~~~(
  276. #pragma once
  277. #include <AK/Function.h>
  278. #include <AK/Platform.h>
  279. #include <AK/Types.h>
  280. )~~~");
  281. if (machine.namespaces.has_value()) {
  282. generator.set("namespace", machine.namespaces.value());
  283. generator.append(R"~~~(
  284. namespace @namespace@ {
  285. )~~~");
  286. }
  287. generator.append(R"~~~(
  288. class @class_name@ {
  289. public:
  290. enum class Action : u8 {
  291. _Ignore,
  292. )~~~");
  293. for (auto a : actions(machine)) {
  294. if (a.is_empty())
  295. continue;
  296. auto action_generator = generator.fork();
  297. action_generator.set("action.name", a);
  298. action_generator.append(R"~~~(
  299. @action.name@,
  300. )~~~");
  301. }
  302. generator.append(R"~~~(
  303. }; // end Action
  304. using Handler = Function<void(Action, u8)>;
  305. @class_name@(Handler handler)
  306. : m_handler(move(handler))
  307. {
  308. }
  309. void advance(u8 byte)
  310. {
  311. auto next_state = lookup_state_transition(byte);
  312. bool state_will_change = next_state.new_state != m_state && next_state.new_state != State::_Anywhere;
  313. // only run exit directive if state is being changed
  314. if (state_will_change) {
  315. switch (m_state) {
  316. )~~~");
  317. for (auto s : machine.states) {
  318. auto state_generator = generator.fork();
  319. if (s.exit_action.has_value()) {
  320. state_generator.set("state_name", s.name);
  321. state_generator.set("action", s.exit_action.value());
  322. state_generator.append(R"~~~(
  323. case State::@state_name@:
  324. m_handler(Action::@action@, byte);
  325. break;
  326. )~~~");
  327. }
  328. }
  329. generator.append(R"~~~(
  330. default:
  331. break;
  332. }
  333. }
  334. if (next_state.action != Action::_Ignore)
  335. m_handler(next_state.action, byte);
  336. m_state = next_state.new_state;
  337. // only run entry directive if state is being changed
  338. if (state_will_change)
  339. {
  340. switch (next_state.new_state)
  341. {
  342. )~~~");
  343. for (auto state : machine.states) {
  344. auto state_generator = generator.fork();
  345. if (state.entry_action.has_value()) {
  346. state_generator.set("state_name", state.name);
  347. state_generator.set("action", state.entry_action.value());
  348. state_generator.append(R"~~~(
  349. case State::@state_name@:
  350. m_handler(Action::@action@, byte);
  351. break;
  352. )~~~");
  353. }
  354. }
  355. generator.append(R"~~~(
  356. default:
  357. break;
  358. }
  359. }
  360. }
  361. private:
  362. enum class State : u8 {
  363. _Anywhere,
  364. )~~~");
  365. int largest_state_value = 0;
  366. for (auto s : machine.states) {
  367. auto state_generator = generator.fork();
  368. state_generator.set("state.name", s.name);
  369. largest_state_value++;
  370. state_generator.append(R"~~~(
  371. @state.name@,
  372. )~~~");
  373. }
  374. generator.append(R"~~~(
  375. }; // end State
  376. struct StateTransition {
  377. State new_state;
  378. Action action;
  379. };
  380. State m_state { State::@initial_state@ };
  381. Handler m_handler;
  382. ALWAYS_INLINE StateTransition lookup_state_transition(u8 byte)
  383. {
  384. VERIFY((u8)m_state < @state_count@);
  385. )~~~");
  386. if (machine.anywhere.has_value()) {
  387. generator.append(R"~~~(
  388. auto anywhere_state = STATE_TRANSITION_TABLE[0][byte];
  389. if (anywhere_state.new_state != State::_Anywhere || anywhere_state.action != Action::_Ignore)
  390. return anywhere_state;
  391. else
  392. )~~~");
  393. }
  394. generator.append(R"~~~(
  395. return STATE_TRANSITION_TABLE[(u8)m_state][byte];
  396. }
  397. )~~~");
  398. auto table_generator = generator.fork();
  399. generate_lookup_table(machine, table_generator);
  400. generator.append(R"~~~(
  401. }; // end @class_name@
  402. )~~~");
  403. if (machine.namespaces.has_value()) {
  404. generator.append(R"~~~(
  405. } // end namespace
  406. )~~~");
  407. }
  408. }