Skip to content

Commit

Permalink
Implement default API for Rust, switch of the examples to it.
Browse files Browse the repository at this point in the history
Note that is cannot be enabled by default due to backwards compatibility.
  • Loading branch information
skvadrik committed Jun 26, 2024
1 parent aea726e commit d741f53
Show file tree
Hide file tree
Showing 26 changed files with 1,992 additions and 2,017 deletions.
149 changes: 79 additions & 70 deletions bootstrap/src/default_syntax_rust.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "src/parse/conf_parser.h"
const char* DEFAULT_SYNTAX_RUST =
"supported_apis = [\"generic\", \"record\"];\n"
"supported_apis = [\"default\", \"generic\", \"record\"];\n"
"supported_api_styles = [\"free-form\"];\n"
"supported_code_models = [\"loop_switch\", \"recursive_functions\"];\n"
"supported_targets = [\"code\", \"dot\"];\n"
Expand Down Expand Up @@ -91,22 +91,22 @@ const char* DEFAULT_SYNTAX_RUST =
"conf:define:YYCOPYMTAG = sigil \"{lhs} = \" sigil \"{rhs};\";\n"
"conf:define:YYCOPYSTAG = sigil \"{lhs} = \" sigil \"{rhs};\";\n"
"conf:define:YYCTYPE = \"YYCTYPE\";\n"
"conf:define:YYCTXMARKER = (api.record ? record \".yyctxmarker\" : \"YYCTXMARKER\");\n"
"conf:define:YYCURSOR = (api.record ? record \".yycursor\" : \"YYCURSOR\");\n"
"conf:define:YYCTXMARKER = (api.generic ? \"YYCTXMARKER\" : (api.record ? record \".\") \"yyctxmarker\");\n"
"conf:define:YYCURSOR = (api.generic ? \"YYCURSOR\" : (api.record ? record \".\") \"yycursor\");\n"
"conf:define:YYDEBUG = \"YYDEBUG\";\n"
"conf:define:YYFILL = \"YYFILL\";\n"
"conf:define:YYGETACCEPT = sigil \"{var}\";\n"
"conf:define:YYGETCOND = \"YYGETCONDITION\"; // historic default\n"
"conf:define:YYGETSTATE = \"YYGETSTATE\";\n"
"conf:define:YYINPUT = (api.record ? record \".yyinput\" : \"YYINPUT\");\n"
"conf:define:YYINPUT = (api.generic ? \"YYINPUT\" : (api.record ? record \".\") \"yyinput\");\n"
"conf:define:YYLESSTHAN = \"YYLESSTHAN\";\n"
"conf:define:YYLIMIT = (api.record ? record \".yylimit\" : \"YYLIMIT\");\n"
"conf:define:YYMARKER = (api.record ? record \".yymarker\" : \"YYMARKER\");\n"
"conf:define:YYLIMIT = (api.generic ? \"YYLIMIT\" : (api.record ? record \".\") \"yylimit\");\n"
"conf:define:YYMARKER = (api.generic ? \"YYMARKER\" : (api.record ? record \".\") \"yymarker\");\n"
"conf:define:YYMAXFILL = \"YYMAXFILL\";\n"
"conf:define:YYMAXNMATCH = \"YYMAXNMATCH\";\n"
"conf:define:YYMTAGN = \"YYMTAGN\";\n"
"conf:define:YYMTAGP = \"YYMTAGP\";\n"
"conf:define:YYPEEK = (api.record ? \"get_unchecked\" : \"YYPEEK\");\n"
"conf:define:YYPEEK = (api.generic ? \"YYPEEK\" : \"get_unchecked\");\n"
"conf:define:YYRESTORE = \"YYRESTORE\";\n"
"conf:define:YYRESTORECTX = \"YYRESTORECTX\";\n"
"conf:define:YYRESTORETAG = \"YYRESTORETAG\";\n"
Expand Down Expand Up @@ -203,132 +203,141 @@ const char* DEFAULT_SYNTAX_RUST =
"code:abort = topindent \"panic!(\\\"internal lexer error\\\")\" nl;\n"
"\n"
"code:yydebug =\n"
" topindent (api.record\n"
" ? debug \"(\" record \")\"\n"
" : debug\n"
" ) nl;\n"
" topindent (api.generic\n"
" ? debug\n"
" : (api.record\n"
" ? debug \"(\" record \");\"\n"
" : debug \"(\" state \", \" char \");\"\n"
" )) nl;\n"
"\n"
"code:yypeek =\n"
" topindent (code_model.recursive_functions ? \"let \") char \" = \" (api.record\n"
" topindent (code_model.recursive_functions ? \"let \") char \" = \" (api.generic\n"
" ? (unsafe\n"
" ? \"unsafe {*\" input \".\" peek \"(\" cursor \")}\"\n"
" : input \"[\" cursor \"]\")\n"
" : (unsafe\n"
" ? \"unsafe {\" peek \"}\"\n"
" : peek)\n"
" : (unsafe\n"
" ? \"unsafe {*\" input \".\" peek \"(\" cursor \")}\"\n"
" : input \"[\" cursor \"]\")\n"
" ) \";\" nl;\n"
"\n"
"code:yyskip =\n"
" topindent (api.record\n"
" ? cursor \" += 1;\"\n"
" : skip\n"
" topindent (api.generic\n"
" ? skip\n"
" : cursor \" += 1;\"\n"
" ) nl;\n"
"\n"
"code:yybackup =\n"
" topindent (api.record\n"
" ? marker \" = \" cursor \";\"\n"
" : backup\n"
" topindent (api.generic\n"
" ? backup\n"
" : marker \" = \" cursor \";\"\n"
" ) nl;\n"
"\n"
"code:yybackupctx =\n"
" topindent (api.record\n"
" ? ctxmarker \" = \" cursor \";\"\n"
" : backupctx\n"
" topindent (api.generic\n"
" ? backupctx\n"
" : ctxmarker \" = \" cursor \";\"\n"
" ) nl;\n"
"\n"
"code:yyrestore =\n"
" topindent (api.record\n"
" ? cursor \" = \" marker \";\"\n"
" : restore\n"
" topindent (api.generic\n"
" ? restore\n"
" : cursor \" = \" marker \";\"\n"
" ) nl;\n"
"\n"
"code:yyrestorectx =\n"
" topindent (api.record\n"
" ? cursor \" = \" ctxmarker \";\"\n"
" : restorectx\n"
" topindent (api.generic\n"
" ? restorectx\n"
" : cursor \" = \" ctxmarker \";\"\n"
" ) nl;\n"
"\n"
"code:yyrestoretag =\n"
" topindent (api.record\n"
" ? cursor \" = \" tag \";\"\n"
" : restoretag\n"
" topindent (api.generic\n"
" ? restoretag\n"
" : cursor \" = \" tag \";\"\n"
" ) nl;\n"
"\n"
"code:yyshift =\n"
" topindent (api.record\n"
" ? cursor \" -= \" offset \";\"\n"
" : shift\n"
" topindent (api.generic\n"
" ? shift\n"
" : cursor \" -= \" offset \";\"\n"
" ) nl;\n"
"\n"
"code:yyshiftstag =\n"
" (nested ? topindent \"if \" tag \" != \" (api.record ? \"usize::MAX\" : neg) \" {\" nl indent)\n"
" topindent (api.record\n"
" ? tag \" -= \" offset \";\"\n"
" : shiftstag\n"
" (nested ? topindent \"if \" tag \" != \" (api.generic ? neg : \"usize::MAX\") \" {\" nl indent)\n"
" topindent (api.generic\n"
" ? shiftstag\n"
" : tag \" -= \" offset \";\"\n"
" ) nl\n"
" (nested ? dedent topindent \"}\" nl);\n"
"\n"
"code:yyshiftmtag = topindent shiftmtag nl;\n"
"\n"
"code:yystagp =\n"
" topindent (api.record\n"
" ? tag \" = \" cursor \";\"\n"
" : stagp\n"
" topindent (api.generic\n"
" ? stagp\n"
" : tag \" = \" cursor \";\"\n"
" ) nl;\n"
"\n"
"code:yymtagp = topindent mtagp nl;\n"
"\n"
"code:yystagn =\n"
" topindent (api.record\n"
" ? tag \" = usize::MAX;\"\n"
" : stagn\n"
" topindent (api.generic\n"
" ? stagn\n"
" : tag \" = usize::MAX;\"\n"
" ) nl;\n"
"\n"
"code:yymtagn = topindent mtagn nl;\n"
"\n"
"code:yycopystag =\n"
" topindent (api.record\n"
" ? lhs \" = \" rhs \";\"\n"
" : copystag\n"
" topindent (api.generic\n"
" ? copystag\n"
" : lhs \" = \" rhs \";\"\n"
" ) nl;\n"
"\n"
"code:yycopymtag =\n"
" topindent (api.record\n"
" ? lhs \" = \" rhs \";\"\n"
" : copymtag\n"
" topindent (api.generic\n"
" ? copymtag\n"
" : lhs \" = \" rhs \";\"\n"
" ) nl;\n"
"\n"
"code:yygetaccept =\n"
" (api.record\n"
" ? (storable_state | code_model.recursive_functions ? record \".\") var\n"
" : getaccept);\n"
" (api.generic\n"
" ? getaccept\n"
" : (api.record & (storable_state | code_model.recursive_functions) ? record \".\") var);\n"
"\n"
"code:yysetaccept =\n"
" topindent (api.record\n"
" ? (storable_state | code_model.recursive_functions ? record \".\") var \" = \" val \";\"\n"
" : setaccept\n"
" topindent (api.generic\n"
" ? setaccept\n"
" : (api.record & (storable_state | code_model.recursive_functions) ? record \".\")\n"
" var \" = \" val \";\"\n"
" ) nl;\n"
"\n"
"code:yygetcond = (api.record ? record \".\" var : getcond);\n"
"code:yygetcond =\n"
" (api.generic\n"
" ? getcond\n"
" : (api.record ? record \".\") var);\n"
"\n"
"code:yysetcond =\n"
" topindent (api.record\n"
" ? record \".\" var \" = \" val \";\"\n"
" : setcond\n"
" topindent (api.generic\n"
" ? setcond\n"
" : (api.record ? record \".\") var \" = \" val \";\"\n"
" ) nl;\n"
"\n"
"code:yygetstate = (api.record ? record \".\" var : getstate);\n"
"code:yygetstate =\n"
" (api.generic\n"
" ? getstate\n"
" : (api.record ? record \".\") var);\n"
"\n"
"code:yysetstate =\n"
" topindent (api.record\n"
" ? record \".\" var \" = \" val \";\"\n"
" : setstate\n"
" topindent (api.generic\n"
" ? setstate\n"
" : (api.record ? record \".\" var) \" = \" val \";\"\n"
" ) nl;\n"
"\n"
"code:yylessthan =\n"
" (api.record\n"
" ? (many\n"
" (api.generic\n"
" ? lessthan\n"
" : (many\n"
" ? \"(\" limit \" - \" cursor \") < \" need\n"
" : limit \" <= \" cursor)\n"
" : lessthan);\n"
" : limit \" <= \" cursor));\n"
;
7 changes: 3 additions & 4 deletions examples/rust/01_basic.re
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// re2rust $INPUT -o $OUTPUT --no-unsafe

fn lex(s: &[u8]) -> bool {
let mut cursor = 0;
fn lex(yyinput: &[u8]) -> bool {
let mut yycursor = 0;
/*!re2c
re2c:api = default;
re2c:define:YYCTYPE = u8;
re2c:define:YYPEEK = "s[cursor]";
re2c:define:YYSKIP = "cursor += 1;";
re2c:yyfill:enable = 0;
number = [1-9][0-9]*;
Expand Down
12 changes: 6 additions & 6 deletions examples/rust/01_basic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Generated by re2c */
// re2rust $INPUT -o $OUTPUT --no-unsafe

fn lex(s: &[u8]) -> bool {
let mut cursor = 0;
fn lex(yyinput: &[u8]) -> bool {
let mut yycursor = 0;

{
#[allow(unused_assignments)]
Expand All @@ -11,8 +11,8 @@ fn lex(s: &[u8]) -> bool {
'yyl: loop {
match yystate {
0 => {
yych = s[cursor];
cursor += 1;
yych = yyinput[yycursor];
yycursor += 1;
match yych {
0x31 ..= 0x39 => {
yystate = 2;
Expand All @@ -26,10 +26,10 @@ fn lex(s: &[u8]) -> bool {
}
1 => { return false; },
2 => {
yych = s[cursor];
yych = yyinput[yycursor];
match yych {
0x30 ..= 0x39 => {
cursor += 1;
yycursor += 1;
yystate = 2;
continue 'yyl;
}
Expand Down
32 changes: 13 additions & 19 deletions examples/rust/conditions/parse_u32_conditions.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,16 @@ fn add(num: &mut u64, str: &[u8], cur: usize, offs: u8, base: u64) {
*num = std::cmp::min(*num * base + digit as u64, ERROR);
}

fn parse_u32(str: &[u8]) -> Option<u32> {
assert_eq!(str.last(), Some(&0)); // expect null-terminated input
fn parse_u32(yyinput: &[u8]) -> Option<u32> {
assert_eq!(yyinput.last(), Some(&0)); // expect null-terminated input

let (mut cur, mut mar) = (0, 0);
let mut cond = YYC_INIT;
let (mut yycursor, mut yymarker) = (0, 0);
let mut yycond = YYC_INIT;
let mut num = 0u64; // Store number in u64 to simplify overflow checks.

'lex: loop {/*!re2c
re2c:define:YYCTYPE = u8;
re2c:define:YYPEEK = "*str.get_unchecked(cur)";
re2c:define:YYSKIP = "cur += 1;";
re2c:define:YYBACKUP = "mar = cur;";
re2c:define:YYRESTORE = "cur = mar;";
re2c:define:YYSHIFT = "cur = (cur as isize + @@) as usize;";
re2c:define:YYGETCOND = "cond";
re2c:define:YYSETCOND = "cond = @@;";
'lex: loop { /*!re2c
re2c:api = default;
re2c:define:YYCTYPE = u8;
re2c:yyfill:enable = 0;
<INIT> '0b' / [01] :=> BIN
Expand All @@ -34,12 +28,12 @@ fn parse_u32(str: &[u8]) -> Option<u32> {
<INIT> '0x' / [0-9a-fA-F] :=> HEX
<INIT> * { return None; }

<BIN> [01] { add(&mut num, str, cur, 48, 2); continue 'lex; }
<OCT> [0-7] { add(&mut num, str, cur, 48, 8); continue 'lex; }
<DEC> [0-9] { add(&mut num, str, cur, 48, 10); continue 'lex; }
<HEX> [0-9] { add(&mut num, str, cur, 48, 16); continue 'lex; }
<HEX> [a-f] { add(&mut num, str, cur, 87, 16); continue 'lex; }
<HEX> [A-F] { add(&mut num, str, cur, 55, 16); continue 'lex; }
<BIN> [01] { add(&mut num, yyinput, yycursor, 48, 2); continue 'lex; }
<OCT> [0-7] { add(&mut num, yyinput, yycursor, 48, 8); continue 'lex; }
<DEC> [0-9] { add(&mut num, yyinput, yycursor, 48, 10); continue 'lex; }
<HEX> [0-9] { add(&mut num, yyinput, yycursor, 48, 16); continue 'lex; }
<HEX> [a-f] { add(&mut num, yyinput, yycursor, 87, 16); continue 'lex; }
<HEX> [A-F] { add(&mut num, yyinput, yycursor, 55, 16); continue 'lex; }

<BIN, OCT, DEC, HEX> * {
return if num < ERROR { Some(num as u32) } else { None };
Expand Down
Loading

0 comments on commit d741f53

Please sign in to comment.