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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
#![deny(unconditional_recursion)] #![feature(test)] #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] #![warn(missing_docs)] #![warn(trivial_casts)] #![warn(trivial_numeric_casts)] #![warn(unsafe_code)] #![warn(unused_import_braces)] //! This module exports the API for defining a simple lexer based on a deterministic finite state //! automaton. //! //! Lexers defined using the Flexer are capable of lexing languages of significant complexity, and //! while they're defined in a simple way (akin to regular grammars), they can work even with //! context-sensitive languages. //! //! The process of defining a lexer involves the user doing the following: //! //! 1. Creating a `Lexer` type that wraps the [`Flexer`]. //! 2. Creating a `State` type, to hold the user-defined lexing state. //! 3. Implementing [`State`] for the `State` type. //! 4. Implementing [`Definition`] for the `Lexer`, along with lexing transition rules to lex the //! language. //! //! The result of defining a lexer using the flexer is a hybrid of the code written using this //! library, and also the code that this library generates to specialize your lexer. //! //! # Writing a Lexer //! //! As the Flexer is a library for writing lexers, it would be remiss of us not to include a worked //! example for how to define a lexer. The following example defines a lexer for a small language, //! and shows you how to integrate the flexer code generation step with your project's build. //! //! ## The Language //! //! We're going to define a lexer for a very simple language, represented by the following //! [EBNF](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) grammar. //! //! ```plain //! a-word = 'a'+; //! b-word = 'b'+; //! word = a-word | b-word; //! space = ' '; //! spaced-word = space, word; //! language = word, spaced-word*; //! ``` //! //! ## The Lexer's Output //! //! Every lexer needs the ability to write a stream of tokens as its output. A flexer-based lexer //! can use any type that it wants as its output type, but this language is going to use a very //! simple `Token` type, wrapped into a `TokenStream`. //! //! ``` //! #[derive(Clone)] //! pub enum Token { //! /// A word from the input, consisting of a sequence of all `a` or all `b`. //! Word(String), //! /// A token that the lexer is unable to recognise. //! Unrecognized(String) //! } //! //! #[derive(Clone,Default)] //! pub struct TokenStream { //! tokens:Vec<Token> //! } //! //! impl TokenStream { //! pub fn push(&mut self,token:Token) { //! self.tokens.push(token) //! } //! } //! ``` //! //! These tokens will be inserted into the token stream by our lexer as it recognises valid portions //! of our language. //! //! Whatever you choose as the `Output` type of your lexer, it will need to implement both //! [`std::clone::Clone`] and [`std::default::Default`]. //! //! ## The Lexer's State //! //! Every Flexer-based lexer operates over a state that holds all of the user-defined state //! information required to define the particular lexer. This state type must conform to the //! [`State`] trait, which defines important functionality that it must provide to the flexer. //! //! In our language, we want to only be able to match words with a preceding space character once //! we've seen an initial word that doesn't have one. To this end, we need a state in our lexer to //! record that we've 'seen' the first word. As required by the [`State`] trait, we also need to //! provide the flexer with an initial state, the state registry, and the bookmarks we use. //! //! ``` //! use enso_flexer::group; //! use enso_flexer::prelude::reader::BookmarkManager; //! use enso_flexer::State; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! //! //! // === LexerState === //! //! #[derive(Debug)] //! pub struct LexerState { //! /// The registry for groups in the lexer. //! lexer_states:group::Registry, //! /// The initial state of the lexer. //! initial_state:group::Identifier, //! /// The state entered when the first word has been seen. //! seen_first_word_state:group::Identifier, //! /// The bookmarks for this lexer. //! bookmarks:BookmarkManager //! } //! ``` //! //! The flexer library provides useful functionality to help with defining your lexer state, such as //! [`group::Registry`] for containing the various states through which your lexer may transition, //! amd [`prelude::reader::BookmarkManager`] for storing bookmarks. //! //! > ### Bookmarks //! > In order to enable arbitrary lookahead, the flexer provides a system for "bookmarking" a point //! > in the input stream so that the lexer may return to it later. In fact, this mechanism is used //! > _by default_ in the implementation to deal with overlapping rules, and so the //! > [`prelude::reader::BookmarkManager`] provides some bookmarks for you by default. //! > //! > As a user, however, you can define additional bookmarks as part of your state, and mark or //! > return to them as part of your lexer's transition functions (more on this below). //! //! Now that we have our state type, we need to define an implementation of [`State`] for it. This //! is a mostly trivial exercise, but two functions ([`State::new()`] and [`State::specialize`]) //! require special attention. We'll look at both below. //! //! ``` //! use enso_flexer::generate; //! # use enso_flexer::group; //! use enso_flexer::generate::GenError; //! use enso_flexer::prelude::AnyLogger; //! # use enso_flexer::prelude::reader::BookmarkManager; //! # use enso_flexer::State; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! # //! # //! # // === LexerState === //! # //! # #[derive(Debug)] //! # pub struct LexerState { //! # /// The registry for groups in the lexer. //! # lexer_states:group::Registry, //! # /// The initial state of the lexer. //! # initial_state:group::Identifier, //! # /// The state entered when the first word has been seen. //! # seen_first_word_state:group::Identifier, //! # /// The bookmarks for this lexer. //! # bookmarks:BookmarkManager //! # } //! //! impl enso_flexer::State for LexerState { //! fn new(_logger:&impl AnyLogger) -> Self { //! // Here we construct all of the elements needed for our lexer state. This function can //! // contain arbitrarily complex logic and is only called once at initialization time. //! let mut lexer_states = group::Registry::default(); //! let initial_state = lexer_states.define_group("ROOT",None); //! let seen_first_word_state = lexer_states.define_group("SEEN FIRST WORD",None); //! let bookmarks = BookmarkManager::new(); //! Self{lexer_states,initial_state,seen_first_word_state,bookmarks} //! } //! //! fn initial_state(&self) -> group::Identifier { //! self.initial_state //! } //! //! fn groups(&self) -> &group::Registry { //! &self.lexer_states //! } //! //! fn groups_mut(&mut self) -> &mut group::Registry { //! &mut self.lexer_states //! } //! //! fn bookmarks(&self) -> &BookmarkManager { //! &self.bookmarks //! } //! //! fn bookmarks_mut(&mut self) -> &mut BookmarkManager { //! &mut self.bookmarks //! } //! //! fn specialize(&self) -> Result<String,GenError> { //! // It is very important to pass both the type name of your lexer and your output //! // correctly here. This function should always be implemented as a call to the //! // below-used function. //! generate::specialize(self,"TestLexer","Token") //! } //! } //! ``` //! //! ## Defining the Lexer Type //! //! With our state type defined, we now have the prerequisites for defining the lexer itself! //! //! The notion behind the way we define lexers in the flexer is to use a chain of //! [`std::ops::Deref`] implementations to make the disparate parts feel like a cohesive whole. //! The [`Flexer`] itself already implements deref to your state type, so all that remains is to do //! the following: //! //! 1. Define your lexer struct itself, containing an instance of the [`Flexer`], parametrised by //! your state and output types. //! //! ``` //! use enso_flexer::Flexer; //! # use enso_flexer::generate; //! # use enso_flexer::group; //! # use enso_flexer::prelude::GenError; //! # use enso_flexer::prelude::AnyLogger; //! use enso_flexer::prelude::logger::Disabled; //! # use enso_flexer::prelude::reader::BookmarkManager; //! # use enso_flexer::State; //! //! type Logger = Disabled; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! # //! # //! # // === LexerState === //! # //! # #[derive(Debug)] //! # pub struct LexerState { //! # /// The registry for groups in the lexer. //! # lexer_states:group::Registry, //! # /// The initial state of the lexer. //! # initial_state:group::Identifier, //! # /// The state entered when the first word has been seen. //! # seen_first_word_state:group::Identifier, //! # /// The bookmarks for this lexer. //! # bookmarks:BookmarkManager //! # } //! # //! # impl enso_flexer::State for LexerState { //! # fn new(_logger:&impl AnyLogger) -> Self { //! # // Here we construct all of the elements needed for our lexer state. This function can //! # // contain arbitrarily complex logic and is only called once at initialization time. //! # let mut lexer_states = group::Registry::default(); //! # let initial_state = lexer_states.define_group("ROOT",None); //! # let seen_first_word_state = lexer_states.define_group("SEEN FIRST WORD",None); //! # let bookmarks = BookmarkManager::new(); //! # Self{lexer_states,initial_state,seen_first_word_state,bookmarks} //! # } //! # //! # fn initial_state(&self) -> group::Identifier { //! # self.initial_state //! # } //! # //! # fn groups(&self) -> &group::Registry { //! # &self.lexer_states //! # } //! # //! # fn groups_mut(&mut self) -> &mut group::Registry { //! # &mut self.lexer_states //! # } //! # //! # fn bookmarks(&self) -> &BookmarkManager { //! # &self.bookmarks //! # } //! # //! # fn bookmarks_mut(&mut self) -> &mut BookmarkManager { //! # &mut self.bookmarks //! # } //! # //! # fn specialize(&self) -> Result<String,GenError> { //! # // It is very important to pass both the type name of your lexer and your output //! # // correctly here. This function should always be implemented as a call to the //! # // below-used function. //! # generate::specialize(self,"TestLexer","Token") //! # } //! # } //! //! //! // === Lexer === //! //! pub struct Lexer { //! lexer:Flexer<LexerState,TokenStream,Logger> //! } //! ``` //! //! You'll note that the `Flexer` also takes a logging implementation from the Enso logging library //! as a type parameter. This lets the client of the library configure the behaviour of logging in //! their lexer. We recommend aliasing the current logger type (as shown above) for ease of use. //! //! 2. Implement a `new()` function for your lexer. //! //! ``` //! # use enso_flexer::Flexer; //! # use enso_flexer::generate; //! # use enso_flexer::group; //! # use enso_flexer::prelude::AnyLogger; //! # use enso_flexer::prelude::GenError; //! # use enso_flexer::prelude::logger::Disabled; //! # use enso_flexer::prelude::reader::BookmarkManager; //! # use enso_flexer::State; //! # //! # type Logger = Disabled; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! # //! # //! # // === LexerState === //! # //! # #[derive(Debug)] //! # pub struct LexerState { //! # /// The registry for groups in the lexer. //! # lexer_states:group::Registry, //! # /// The initial state of the lexer. //! # initial_state:group::Identifier, //! # /// The state entered when the first word has been seen. //! # seen_first_word_state:group::Identifier, //! # /// The bookmarks for this lexer. //! # bookmarks:BookmarkManager //! # } //! # //! # impl enso_flexer::State for LexerState { //! # fn new(_logger:&impl AnyLogger) -> Self { //! # // Here we construct all of the elements needed for our lexer state. This function can //! # // contain arbitrarily complex logic and is only called once at initialization time. //! # let mut lexer_states = group::Registry::default(); //! # let initial_state = lexer_states.define_group("ROOT",None); //! # let seen_first_word_state = lexer_states.define_group("SEEN FIRST WORD",None); //! # let bookmarks = BookmarkManager::new(); //! # Self{lexer_states,initial_state,seen_first_word_state,bookmarks} //! # } //! # //! # fn initial_state(&self) -> group::Identifier { //! # self.initial_state //! # } //! # //! # fn groups(&self) -> &group::Registry { //! # &self.lexer_states //! # } //! # //! # fn groups_mut(&mut self) -> &mut group::Registry { //! # &mut self.lexer_states //! # } //! # //! # fn bookmarks(&self) -> &BookmarkManager { //! # &self.bookmarks //! # } //! # //! # fn bookmarks_mut(&mut self) -> &mut BookmarkManager { //! # &mut self.bookmarks //! # } //! # //! # fn specialize(&self) -> Result<String,GenError> { //! # // It is very important to pass both the type name of your lexer and your output //! # // correctly here. This function should always be implemented as a call to the //! # // below-used function. //! # generate::specialize(self,"TestLexer","Token") //! # } //! # } //! # //! # //! # // === Lexer === //! # //! # pub struct Lexer { //! # lexer:Flexer<LexerState,TokenStream,Logger> //! # } //! //! impl Lexer { //! pub fn new() -> Self { //! let lexer = Flexer::new(Logger::new("Lexer")); //! Lexer{lexer} //! } //! } //! ``` //! //! 3. Define [`std::ops::Deref`] and [`std::ops::DerefMut`] for your lexer. //! //! ``` //! # use enso_flexer::Flexer; //! # use enso_flexer::generate; //! # use enso_flexer::group; //! # use enso_flexer::prelude::AnyLogger; //! # use enso_flexer::prelude::GenError; //! # use enso_flexer::prelude::logger::Disabled; //! # use enso_flexer::prelude::reader::BookmarkManager; //! # use enso_flexer::State; //! use std::ops::Deref; //! use std::ops::DerefMut; //! # //! # type Logger = Disabled; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! # //! # //! # // === LexerState === //! # //! # #[derive(Debug)] //! # pub struct LexerState { //! # /// The registry for groups in the lexer. //! # lexer_states:group::Registry, //! # /// The initial state of the lexer. //! # initial_state:group::Identifier, //! # /// The state entered when the first word has been seen. //! # seen_first_word_state:group::Identifier, //! # /// The bookmarks for this lexer. //! # bookmarks:BookmarkManager //! # } //! # //! # impl enso_flexer::State for LexerState { //! # fn new(_logger:&impl AnyLogger) -> Self { //! # // Here we construct all of the elements needed for our lexer state. This function can //! # // contain arbitrarily complex logic and is only called once at initialization time. //! # let mut lexer_states = group::Registry::default(); //! # let initial_state = lexer_states.define_group("ROOT",None); //! # let seen_first_word_state = lexer_states.define_group("SEEN FIRST WORD",None); //! # let bookmarks = BookmarkManager::new(); //! # Self{lexer_states,initial_state,seen_first_word_state,bookmarks} //! # } //! # //! # fn initial_state(&self) -> group::Identifier { //! # self.initial_state //! # } //! # //! # fn groups(&self) -> &group::Registry { //! # &self.lexer_states //! # } //! # //! # fn groups_mut(&mut self) -> &mut group::Registry { //! # &mut self.lexer_states //! # } //! # //! # fn bookmarks(&self) -> &BookmarkManager { //! # &self.bookmarks //! # } //! # //! # fn bookmarks_mut(&mut self) -> &mut BookmarkManager { //! # &mut self.bookmarks //! # } //! # //! # fn specialize(&self) -> Result<String,GenError> { //! # // It is very important to pass both the type name of your lexer and your output //! # // correctly here. This function should always be implemented as a call to the //! # // below-used function. //! # generate::specialize(self,"TestLexer","Token") //! # } //! # } //! # //! # //! # // === Lexer === //! # //! # pub struct Lexer { //! # lexer:Flexer<LexerState,TokenStream,Logger> //! # } //! # //! # impl Lexer { //! # pub fn new() -> Self { //! # let lexer = Flexer::new(Logger::new("Lexer")); //! # Lexer{lexer} //! # } //! # } //! //! impl Deref for Lexer { //! type Target = Flexer<LexerState,TokenStream,Logger> ; //! fn deref(&self) -> &Self::Target { //! &self.lexer //! } //! } //! impl DerefMut for Lexer { //! fn deref_mut(&mut self) -> &mut Self::Target { //! &mut self.lexer //! } //! } //! ``` //! //! You'll note that here we've instantiated the flexer with a `Logger`. This is used for providing //! debug information during development, and can be accessed from all scopes of your lexer. In //! release mode, however, logging calls at the "trace", "debug", and "info" levels are optimised //! away. //! //! ## Defining the Lexing Rules //! //! Flexer-based lexers operate by matching on a series of [`automata::pattern::Pattern`]s that //! describe the language that it is trying to lex. It combines these patterns with "transition //! functions" that may execute arbitrary code when a pattern matches on the lexer's input. //! //! In order to define the lexing rules, we need to implement [`Definition`] for our lexer, //! particularly the [`Definition::define()`] function. //! //! ``` //! use enso_flexer::automata::pattern::Pattern; //! # use enso_flexer::Flexer; //! # use enso_flexer::generate; //! use enso_flexer::group::Registry; //! # use enso_flexer::group; //! # use enso_flexer::prelude::AnyLogger; //! # use enso_flexer::prelude::GenError; //! # use enso_flexer::prelude::logger::Disabled; //! # use enso_flexer::prelude::reader::BookmarkManager; //! # use enso_flexer::State; //! use enso_flexer; //! # use std::ops::Deref; //! # use std::ops::DerefMut; //! # //! # type Logger = Disabled; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! # //! # //! # // === LexerState === //! # //! # #[derive(Debug)] //! # pub struct LexerState { //! # /// The registry for groups in the lexer. //! # lexer_states:group::Registry, //! # /// The initial state of the lexer. //! # initial_state:group::Identifier, //! # /// The state entered when the first word has been seen. //! # seen_first_word_state:group::Identifier, //! # /// The bookmarks for this lexer. //! # bookmarks:BookmarkManager //! # } //! # //! # impl enso_flexer::State for LexerState { //! # fn new(_logger:&impl AnyLogger) -> Self { //! # // Here we construct all of the elements needed for our lexer state. This function can //! # // contain arbitrarily complex logic and is only called once at initialization time. //! # let mut lexer_states = group::Registry::default(); //! # let initial_state = lexer_states.define_group("ROOT",None); //! # let seen_first_word_state = lexer_states.define_group("SEEN FIRST WORD",None); //! # let bookmarks = BookmarkManager::new(); //! # Self{lexer_states,initial_state,seen_first_word_state,bookmarks} //! # } //! # //! # fn initial_state(&self) -> group::Identifier { //! # self.initial_state //! # } //! # //! # fn groups(&self) -> &group::Registry { //! # &self.lexer_states //! # } //! # //! # fn groups_mut(&mut self) -> &mut group::Registry { //! # &mut self.lexer_states //! # } //! # //! # fn bookmarks(&self) -> &BookmarkManager { //! # &self.bookmarks //! # } //! # //! # fn bookmarks_mut(&mut self) -> &mut BookmarkManager { //! # &mut self.bookmarks //! # } //! # //! # fn specialize(&self) -> Result<String,GenError> { //! # // It is very important to pass both the type name of your lexer and your output //! # // correctly here. This function should always be implemented as a call to the //! # // below-used function. //! # generate::specialize(self,"TestLexer","Token") //! # } //! # } //! # //! # //! # // === Lexer === //! # //! # pub struct Lexer { //! # lexer:Flexer<LexerState,TokenStream,Logger> //! # } //! # //! # impl Lexer { //! # pub fn new() -> Self { //! # let lexer = Flexer::new(Logger::new("Lexer")); //! # Lexer{lexer} //! # } //! # } //! # //! # impl Deref for Lexer { //! # type Target = Flexer<LexerState,TokenStream,Logger> ; //! # fn deref(&self) -> &Self::Target { //! # &self.lexer //! # } //! # } //! # impl DerefMut for Lexer { //! # fn deref_mut(&mut self) -> &mut Self::Target { //! # &mut self.lexer //! # } //! # } //! //! impl enso_flexer::Definition for Lexer { //! fn define() -> Self { //! // First we instantiate our lexer. Definitions take place _directly_ on the lexer, and //! // manipulate runtime state. //! let mut lexer = Self::new(); //! //! // Then, we define the patterns that we're going to use. For an overview of the p //! let a_word = Pattern::char('a').many1(); //! let b_word = Pattern::char('b').many1(); //! let space = Pattern::char(' '); //! let spaced_a_word = &space >> &a_word; //! let spaced_b_word = &space >> &b_word; //! let any = Pattern::any(); //! let end = Pattern::eof(); //! //! // Next, we define groups of lexer rules. This uses the groups that we've defined in our //! // lexer's state, and the patterns we've defined above. //! let root_group_id = lexer.initial_state; //! let root_group = lexer.groups_mut().group_mut(root_group_id); //! root_group.create_rule(&a_word,"self.on_first_word(reader)"); //! root_group.create_rule(&b_word,"self.on_first_word(reader)"); //! root_group.create_rule(&end, "self.on_no_err_suffix_first_word(reader)"); //! root_group.create_rule(&any, "self.on_err_suffix_first_word(reader)"); //! //! let seen_first_word_group_id = lexer.seen_first_word_state; //! let seen_first_word_group = lexer.groups_mut().group_mut(seen_first_word_group_id); //! seen_first_word_group.create_rule(&spaced_a_word,"self.on_spaced_word(reader)"); //! seen_first_word_group.create_rule(&spaced_b_word,"self.on_spaced_word(reader)"); //! seen_first_word_group.create_rule(&end, "self.on_no_err_suffix(reader)"); //! seen_first_word_group.create_rule(&any, "self.on_err_suffix(reader)"); //! //! lexer //! } //! //! /// This function just returns the lexer's groups. //! fn groups(&self) -> &Registry { //! self.lexer.groups() //! } //! //! /// Code you want to run before lexing begins. //! fn set_up(&mut self) {} //! //! /// Code you want to run after lexing finishes. //! fn tear_down(&mut self) {} //! } //! ``` //! //! > ### Transition Functions //! > You may be wondering why the transition functions are specified as strings. This allows us to //! > generate highly-efficient, specialized code for your lexer once you define it. More on this //! > later. //! //! A [`group::Group`] in the lexer is like a state that operates on a stack. A transition function //! can arbitrarily activate or deactivate a group on the flexer's stack, allowing you to perform //! context-sensitive lexing behaviour. For more information (including on how to use parent groups //! to inherit rules), see the relevant module. //! //! For more information on the [`automata::pattern::Pattern`] APIs used above, please see the //! relevant module in this crate. //! //! ## Defining the Transition Functions //! //! You'll have noticed that, up above, we told the rules to use a bunch of transition functions //! that we've not yet talked about. These functions can be defined anywhere you like, as long as //! they are in scope in the file in which you are defining your lexer. We do, however, recommend //! defining them on your lexer itself, so they can access and manipulate lexer state, so that's //! what we're going to do here. //! //! ``` //! # use enso_flexer::automata::pattern::Pattern; //! # use enso_flexer::Flexer; //! # use enso_flexer::generate; //! # use enso_flexer::group::Registry; //! # use enso_flexer::group; //! # use enso_flexer::prelude::AnyLogger; //! use enso_flexer::prelude::ReaderOps; //! # use enso_flexer::prelude::GenError; //! # use enso_flexer::prelude::logger::Disabled; //! # use enso_flexer::prelude::reader::BookmarkManager; //! # use enso_flexer::State; //! # use enso_flexer; //! # use std::ops::Deref; //! # use std::ops::DerefMut; //! # //! # type Logger = Disabled; //! # //! # //! # // === Token === //! # //! # #[derive(Clone)] //! # pub enum Token { //! # /// A word from the input, consisting of a sequence of all `a` or all `b`. //! # Word(String), //! # /// A token that the lexer is unable to recognise. //! # Unrecognized(String) //! # } //! # //! # #[derive(Clone,Default)] //! # pub struct TokenStream { //! # tokens:Vec<Token> //! # } //! # //! # impl TokenStream { //! # pub fn push(&mut self,token:Token) { //! # self.tokens.push(token) //! # } //! # } //! # //! # //! # // === LexerState === //! # //! # #[derive(Debug)] //! # pub struct LexerState { //! # /// The registry for groups in the lexer. //! # lexer_states:group::Registry, //! # /// The initial state of the lexer. //! # initial_state:group::Identifier, //! # /// The state entered when the first word has been seen. //! # seen_first_word_state:group::Identifier, //! # /// The bookmarks for this lexer. //! # bookmarks:BookmarkManager //! # } //! # //! # impl enso_flexer::State for LexerState { //! # fn new(_logger:&impl AnyLogger) -> Self { //! # // Here we construct all of the elements needed for our lexer state. This function can //! # // contain arbitrarily complex logic and is only called once at initialization time. //! # let mut lexer_states = group::Registry::default(); //! # let initial_state = lexer_states.define_group("ROOT",None); //! # let seen_first_word_state = lexer_states.define_group("SEEN FIRST WORD",None); //! # let bookmarks = BookmarkManager::new(); //! # Self{lexer_states,initial_state,seen_first_word_state,bookmarks} //! # } //! # //! # fn initial_state(&self) -> group::Identifier { //! # self.initial_state //! # } //! # //! # fn groups(&self) -> &group::Registry { //! # &self.lexer_states //! # } //! # //! # fn groups_mut(&mut self) -> &mut group::Registry { //! # &mut self.lexer_states //! # } //! # //! # fn bookmarks(&self) -> &BookmarkManager { //! # &self.bookmarks //! # } //! # //! # fn bookmarks_mut(&mut self) -> &mut BookmarkManager { //! # &mut self.bookmarks //! # } //! # //! # fn specialize(&self) -> Result<String,GenError> { //! # // It is very important to pass both the type name of your lexer and your output //! # // correctly here. This function should always be implemented as a call to the //! # // below-used function. //! # generate::specialize(self,"TestLexer","Token") //! # } //! # } //! # //! # //! # // === Lexer === //! # //! # pub struct Lexer { //! # lexer:Flexer<LexerState,TokenStream,Logger> //! # } //! # //! # impl Lexer { //! # pub fn new() -> Self { //! # let lexer = Flexer::new(Logger::new("Lexer")); //! # Lexer{lexer} //! # } //! # } //! # //! # impl Deref for Lexer { //! # type Target = Flexer<LexerState,TokenStream,Logger> ; //! # fn deref(&self) -> &Self::Target { //! # &self.lexer //! # } //! # } //! # impl DerefMut for Lexer { //! # fn deref_mut(&mut self) -> &mut Self::Target { //! # &mut self.lexer //! # } //! # } //! # //! # impl enso_flexer::Definition for Lexer { //! # fn define() -> Self { //! # // First we instantiate our lexer. Definitions take place _directly_ on the lexer, and //! # // manipulate runtime state. //! # let mut lexer = Self::new(); //! # //! # // Then, we define the patterns that we're going to use. For an overview of the p //! # let a_word = Pattern::char('a').many1(); //! # let b_word = Pattern::char('b').many1(); //! # let space = Pattern::char(' '); //! # let spaced_a_word = &space >> &a_word; //! # let spaced_b_word = &space >> &b_word; //! # let any = Pattern::any(); //! # let end = Pattern::eof(); //! # //! # // Next, we define groups of lexer rules. This uses the groups that we've defined in our //! # // lexer's state, and the patterns we've defined above. //! # let root_group_id = lexer.initial_state; //! # let root_group = lexer.groups_mut().group_mut(root_group_id); //! # root_group.create_rule(&a_word,"self.on_first_word(reader)"); //! # root_group.create_rule(&b_word,"self.on_first_word(reader)"); //! # root_group.create_rule(&end, "self.on_no_err_suffix_first_word(reader)"); //! # root_group.create_rule(&any, "self.on_err_suffix_first_word(reader)"); //! # //! # let seen_first_word_group_id = lexer.seen_first_word_state; //! # let seen_first_word_group = lexer.groups_mut().group_mut(seen_first_word_group_id); //! # seen_first_word_group.create_rule(&spaced_a_word,"self.on_spaced_word(reader)"); //! # seen_first_word_group.create_rule(&spaced_b_word,"self.on_spaced_word(reader)"); //! # seen_first_word_group.create_rule(&end, "self.on_no_err_suffix(reader)"); //! # seen_first_word_group.create_rule(&any, "self.on_err_suffix(reader)"); //! # //! # lexer //! # } //! # //! # /// This function just returns the lexer's groups. //! # fn groups(&self) -> &Registry { //! # self.lexer.groups() //! # } //! # //! # /// Code you want to run before lexing begins. //! # fn set_up(&mut self) {} //! # //! # /// Code you want to run after lexing finishes. //! # fn tear_down(&mut self) {} //! # } //! //! impl Lexer { //! pub fn on_first_word<R:ReaderOps>(&mut self, _reader:&mut R) { //! let str = self.current_match.clone(); //! let ast = Token::Word(str); //! self.output.push(ast); //! let id = self.seen_first_word_state; //! self.push_state(id); //! } //! //! pub fn on_spaced_word<R:ReaderOps>(&mut self, _reader:&mut R) { //! let str = self.current_match.clone(); //! let ast = Token::Word(String::from(str.trim())); //! self.output.push(ast); //! } //! //! pub fn on_err_suffix_first_word<R:ReaderOps>(&mut self, _reader:&mut R) { //! let ast = Token::Unrecognized(self.current_match.clone()); //! self.output.push(ast); //! } //! //! pub fn on_err_suffix<R:ReaderOps>(&mut self, reader:&mut R) { //! self.on_err_suffix_first_word(reader); //! self.pop_state(); //! } //! //! pub fn on_no_err_suffix_first_word<R:ReaderOps>(&mut self, _reader:&mut R) {} //! //! pub fn on_no_err_suffix<R:ReaderOps>(&mut self, reader:&mut R) { //! self.on_no_err_suffix_first_word(reader); //! self.pop_state(); //! } //! } //! ``` //! //! > ### Magic Transition Functions //! > The transition functions are the 'secret sauce', so to speak, of the Flexer. They are called //! > when a rule matches, and allow arbitrary code to manipulate the lexer. This means that the //! > flexer can be used to define very complex grammars while still keeping a simple interface and //! > ensuring performant execution. //! //! You'll note that all of these functions have a couple of things in common: //! //! 1. They have a type parameter `R` that conforms to the [`prelude::LazyReader`] trait. //! 2. They take an argument of type `R`, that is the reader over which the lexer is running as the //! _first_ non-`self` argument to the function. //! 3. Any additional arguments must be valid in the scope in which the specialisation rules are //! going to be generated. //! //! Both of these, combined, allow the transition functions to manipulate the text being read by the //! lexer. //! //! ## Specializing the Lexer //! //! In order to actually _use_ the lexer that you've defined, you need to specialize it to the rules //! that you define. Unfortunately, `cargo` doesn't have support for post-build hooks, and so this //! is a little more involved than we'd like it to be. //! //! 1. Create a file that performs the definition of the lexer as above. It can use multiple files //! in its crate as long as they are publicly exposed. //! 2. Create a separate cargo project that has a prebuild hook in its `build.rs`. //! 3. In that build.rs, you need to: //! 1. Import the lexer definition and instantiate it using `::define()`. //! 2. Call [`State::specialize()`] on the resultant lexer. This will generate a string that //! contains the optimised lexer implementation. //! 3. Write both the generated code and the code from the original lexer definition into an //! output file. //! 4. Re-export this output file from your cargo project's `lib.rs`. //! //! The process of specialization will generate quite a bit of code, but most importantly it will //! generate `pub fn run<R:LazyReader>(&mut self, mut reader:R) -> Result<Output>`, where `Output` //! is your lexer's token type. All of these functions are defined on your lexer type (the one whose //! name is provided to `specialize()`. //! //! ## In Summary //! //! The flexer allows its clients to define highly optimised lexer implementations that are capable //! of lexing languages of a high complexity. use crate::prelude::*; use prelude::logger::*; use crate::generate::GenError; use prelude::logger::AnyLogger; use prelude::reader::BookmarkManager; pub use enso_automata as automata; pub use enso_automata::char; pub use enso_automata::literal; pub mod generate; pub mod group; /// Useful libraries for working with the flexer. pub mod prelude { pub use crate::generate::GenError; pub use enso_prelude::*; pub use lazy_reader::ReaderOps; pub use lazy_reader::Reader; pub use logger::AnyLogger; pub use logger::macros::*; /// The lazy reader library. pub mod reader { pub use lazy_reader::*; } /// The Enso logging library. pub mod logger { pub use enso_logger::*; pub use enso_logger::WarningLogger as Disabled; pub use enso_logger::TraceLogger as Enabled; } } // ================= // === Constants === // ================= mod constants { /// The number of 'frames' to reserve in the state stack, aiming to avoid re-allocation in hot /// code paths. pub const STATE_STACK_RESERVATION:usize = 1024; } // ============== // === Flexer === // ============== /// The flexer is an engine for generating lexers. /// /// Akin to flex and other lexer generators, it is given a definition as a series of rules from /// which it then generates code for a highly optimised lexer implemented on top of a /// [DFA](https://en.wikipedia.org/wiki/Deterministic_finite_automaton). /// /// Lexers defined using the flexer work on a stack of _states_, where a state is represented by a /// [`crate::group::Group`]. Being in a given state (represented below by the top of the /// `state_stack`) means that the flexer can match a certain set of rules associated with that /// state. The user may cause the lexer to transition between states by pushing and popping states /// on the stack, thus allowing a much more flexible lexing engine than pure regular grammars. #[derive(Clone,Debug)] pub struct Flexer<Definition,Output,Logger> { /// The stack of states that are active during lexer execution. pub state_stack:NonEmptyVec<group::Identifier>, /// The result of the current stage of the DFA. pub status:StageStatus, /// The tokens that have been lexed. pub output:Output, /// The text of the current match of the lexer. pub current_match:String, /// A logger for the flexer, accessible in user definitions. pub logger:Logger, /// The definition of the user-provided state for the lexer. definition:Definition, } impl<Definition,Output,Logger> Flexer<Definition,Output,Logger> where Definition : State, Logger : AnyLogger<Owned=Logger>, Output : Default { /// Create a new lexer instance. pub fn new(parent_logger:impl AnyLogger) -> Flexer<Definition,Output,Logger> { let logger = <Logger>::sub(&parent_logger,"Flexer"); let status = default(); let output = default(); let definition = Definition::new(&logger); let initial_state_id = definition.initial_state(); let mut state_stack = NonEmptyVec::singleton(initial_state_id); let current_match = default(); state_stack.reserve(constants::STATE_STACK_RESERVATION); Flexer{state_stack,status,output,current_match,logger,definition} } } impl<Definition,Output,Logger> Flexer<Definition,Output,Logger> where Definition : State, Output : Clone, Logger : AnyLogger<Owned=Logger> + LoggerOps<logger::entry::level::Debug> { /// Get the lexer result. pub fn result(&mut self) -> &Output { &self.output } /// Get the lexer's initial state. pub fn initial_state(&self) -> group::Identifier { self.definition.initial_state() } /// Get the state that the lexer is currently in. pub fn current_state(&self) -> group::Identifier { *self.state_stack.last() } /// Tell the lexer to enter the state described by `state`. pub fn push_state(&mut self, state:group::Identifier) { self.logger.group_begin(logger::entry::level::Debug,false, ||format!("Enter State: {}",self.groups().group(state).name.as_str()) ); self.state_stack.push(state); } /// End the current state, returning the popped state identifier if one was ended. /// /// It will never end the initial state of the lexer. pub fn pop_state(&mut self) -> Option<group::Identifier> { let result = self.state_stack.pop(); match result { None => (), Some(ident) => debug!(self.logger,"Leave State: {self.groups().group(ident).name}"), }; self.logger.group_end(logger::entry::level::Debug); result } /// End states until the specified `state` is reached, leaving the lexer in `state`. /// /// If `state` does not exist on the lexer's stack, then the lexer will be left in the root /// state. Additionally, this function cannot pop the final occurrence of the root state. pub fn pop_states_until(&mut self, state:group::Identifier) -> group::Identifier { while self.current_state() != state && self.current_state() != self.initial_state() { self.pop_state(); } *self.state_stack.last() } /// End states up to and including the first instance of `state`, returning the identifier of /// the new state the lexer is in. /// /// If `state` does not exist on the lexer's stack, the lexer will be left in the root state. /// Additionally, this function cannot pop the final occurrence of the root state. pub fn pop_states_including(&mut self, state:group::Identifier) -> group::Identifier { while self.current_state() != state && self.current_state() != self.initial_state() { self.pop_state(); } self.pop_state(); *self.state_stack.last() } /// Check if the lexer is currently in the state described by `state`. pub fn is_in_state(&self, state:group::Identifier) -> bool { self.current_state() == state } /// Check if the lexer is currently inside `state` at some point in the state stack. pub fn is_inside_state(&self, state:group::Identifier) -> bool { self.state_stack.iter().rev().any(|s| *s == state) } } // === Trait Impls === impl<Definition,Output,Logger> Deref for Flexer<Definition,Output,Logger> { type Target = Definition; fn deref(&self) -> &Self::Target { &self.definition } } impl<Definition,Output,Logger> DerefMut for Flexer<Definition,Output,Logger> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.definition } } // ================== // === SubStateId === // ================== /// An identifier for a sub-state of the lexer to transition to. #[derive(Copy,Clone,Debug,Default,PartialEq)] pub struct SubStateId(usize); impl SubStateId { /// Create a new `SubStateId` with the specified value. pub fn new(val:usize) -> SubStateId { SubStateId(val) } } // === Trait Impls === impl From<usize> for SubStateId { fn from(val:usize) -> Self { SubStateId::new(val) } } impl From<&usize> for SubStateId { fn from(val:&usize) -> Self { SubStateId::new(*val) } } impl From<SubStateId> for usize { fn from(value:SubStateId) -> Self { value.0 } } // =================== // === StageStatus === // =================== /// The result of executing a single step of the DFA. #[derive(Clone,Copy,Debug,PartialEq)] pub enum StageStatus { /// The initial state of a lexer stage. Initial, /// The stage exits successfully, having consumed a complete token. ExitSuccess, /// The stage exits unsuccessfully. ExitFail, /// A single step of the DFA has executed successfully. ExitFinished, /// The lexer should continue, transitioning to the included state. ContinueWith(SubStateId) } impl StageStatus { /// Check if the lexer stage should continue. pub fn should_continue(&self) -> bool { self.continue_as().is_some() } /// Obtain the state to which the lexer should transition, iff the lexer should continue. pub fn continue_as(&self) -> Option<SubStateId> { match self { StageStatus::Initial => Some(SubStateId::new(0)), StageStatus::ContinueWith(val) => Some(*val), _ => None } } } // === Trait Impls === impl Default for StageStatus { fn default() -> Self { StageStatus::Initial } } // ============== // === Result === // ============== /// The result of executing the lexer on a given input. #[derive(Clone,Debug)] pub struct LexingResult<T> { /// The kind of the result, representing _how_ the lexer completed. pub kind:ResultKind, /// The tokens that the lexer was able to process. pub tokens:T } impl<T> LexingResult<T> { /// Create a new lexer result using the provided `kind` and `tokens`. pub fn new(kind:ResultKind,tokens:T) -> LexingResult<T> { LexingResult {kind,tokens} } /// Create a new success result, with the provided `tokens`. pub fn success(tokens:T) -> LexingResult<T> { LexingResult::new(ResultKind::Success, tokens) } /// Create a new partial lex result, with the provided `tokens`. pub fn partial(tokens:T) -> LexingResult<T> { LexingResult::new(ResultKind::Partial, tokens) } /// Create a failure result, with the `tokens` it _did_ manage to consume. pub fn failure(tokens:T) -> LexingResult<T> { LexingResult::new(ResultKind::Failure, tokens) } } /// The kind of lexer result. #[derive(Copy,Clone,Debug)] pub enum ResultKind { /// The lexer succeeded, returning the contained token stream. Success, /// The lexer succeeded on part of the input, returning the contained token stream. Partial, /// The lexer failed on the input, returning any tokens it _did_ manage to consume. Failure } // ============= // === State === // ============= /// Contains the state needed by the flexer from a lexer implementation. /// /// The types for which this trait is implemented will normally also contain the user-defined state /// for that lexer. pub trait State { /// Create a new instance of the lexer's state. /// /// This function is guaranteed to be called at most once per run of the lexer. fn new(parent_logger:&impl AnyLogger) -> Self; /// Return the _initial_ lexing state. fn initial_state(&self) -> group::Identifier; /// Return a reference to the group registry for a given lexer. fn groups(&self) -> &group::Registry; /// Return a mutable reference to the group registry for a given lexer. fn groups_mut(&mut self) -> &mut group::Registry; /// Get an immutable reference to the bookmark manager for this state. fn bookmarks(&self) -> &BookmarkManager; /// Get a mutable reference to the bookmark manager for this state. fn bookmarks_mut(&mut self) -> &mut BookmarkManager; /// Generate code to specialize the flexer for the user's particular lexer definition. /// /// This function should be implemented as a call to [`generate::specialize`], passing /// the name of your lexer, and the name of your lexer's output type as a string. fn specialize(&self) -> Result<String,GenError>; } // ================== // === Definition === // ================== /// Allows for the definition of flexer-based lexers. pub trait Definition { /// Define the custom lexer. fn define() -> Self; /// Obtain the registry of groups for the lexer. fn groups(&self) -> &group::Registry; /// Run before any lexing takes place. fn set_up(&mut self); /// Run after lexing has completed. fn tear_down(&mut self); }