Skip to content

Commit

Permalink
Implement default API for V and switch examples to it.
Browse files Browse the repository at this point in the history
  • Loading branch information
skvadrik committed Jun 26, 2024
1 parent d741f53 commit c208cea
Show file tree
Hide file tree
Showing 30 changed files with 1,455 additions and 1,496 deletions.
133 changes: 71 additions & 62 deletions bootstrap/src/default_syntax_v.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_V =
"supported_apis = [\"generic\", \"record\"];\n"
"supported_apis = [\"default\", \"generic\", \"record\"];\n"
"supported_api_styles = [\"free-form\"];\n"
"supported_code_models = [\"goto_label\", \"loop_switch\", \"recursive_functions\"];\n"
"supported_targets = [\"code\", \"dot\"];\n"
Expand All @@ -13,7 +13,7 @@ const char* DEFAULT_SYNTAX_V =
"indentation_sensitive = 0;\n"
"wrap_blocks_in_braces = 0;\n"
"\n"
"conf:api = custom;\n"
"conf:api = default;\n"
"conf:api:style = free-form;\n"
"conf:api:sigil = \"@@\";\n"
"conf:define:YYGETCOND:naked = 0;\n"
Expand Down Expand Up @@ -214,128 +214,137 @@ const char* DEFAULT_SYNTAX_V =
"code:abort = topindent \"panic(\\\"internal lexer error\\\")\" nl;\n"
"\n"
"code:yydebug =\n"
" topindent (api.record\n"
" topindent (api.generic\n"
" ? debug (api_style.functions ? \"()\")\n"
" : (api.record\n"
" ? debug \"(\" record \")\"\n"
" : debug\n"
" ) nl;\n"
" : debug \"(\" state \", \" char \")\"\n"
" )) nl;\n"
"\n"
"code:yypeek =\n"
" topindent char (code_model.recursive_functions ? \" := \" : \" = \") (api.record\n"
" ? input \"[\" cursor \"]\"\n"
" : peek\n"
" topindent char (code_model.recursive_functions ? \" := \" : \" = \") (api.generic\n"
" ? peek\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 \" != \" neg \" {\" nl indent)\n"
" topindent (api.record\n"
" ? tag \"-= \" offset\n"
" : shiftstag\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 \" = -1\"\n"
" : stagn\n"
" topindent (api.generic\n"
" ? stagn\n"
" : tag \" = -1\"\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"
;
6 changes: 2 additions & 4 deletions examples/v/01_basic.re
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// re2v $INPUT -o $OUTPUT -i

fn lex(str string) {
mut cursor := 0
fn lex(yyinput string) {
mut yycursor := 0
/*!re2c
re2c:define:YYCTYPE = u8;
re2c:define:YYPEEK = "str[cursor]";
re2c:define:YYSKIP = "cursor += 1";
re2c:yyfill:enable = 0;
number = [1-9][0-9]*;
Expand Down
12 changes: 6 additions & 6 deletions examples/v/01_basic.v
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// Code generated by re2c, DO NOT EDIT.
// re2v $INPUT -o $OUTPUT -i

fn lex(str string) {
mut cursor := 0
fn lex(yyinput string) {
mut yycursor := 0

mut yych := 0
yych = str[cursor]
yych = yyinput[yycursor]
match yych {
0x31...0x39 { unsafe { goto yy2 } }
else { unsafe { goto yy1 } }
}
yy1:
cursor += 1
yycursor += 1
panic("error!")
yy2:
cursor += 1
yych = str[cursor]
yycursor += 1
yych = yyinput[yycursor]
match yych {
0x30...0x39 { unsafe { goto yy2 } }
else { unsafe { goto yy3 } }
Expand Down
23 changes: 9 additions & 14 deletions examples/v/conditions/parse_u32_blocks.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const u32_lim = u64(1) << 32

fn parse_u32(str string) ?u32 {
mut cur, mut mar := 0, 0
fn parse_u32(yyinput string) ?u32 {
mut yycursor, mut yymarker := 0, 0
mut n := u64(0)
mut yych := 0

Expand All @@ -13,12 +13,7 @@ fn parse_u32(str string) ?u32 {
}
/*!re2c
re2c:yyfill:enable = 0;
re2c:define:YYCTYPE = u8;
re2c:define:YYPEEK = "str[cur]";
re2c:define:YYSKIP = "cur += 1";
re2c:define:YYSHIFT = "cur += @@{shift}";
re2c:define:YYBACKUP = "mar = cur";
re2c:define:YYRESTORE = "cur = mar";
re2c:define:YYCTYPE = u8;
re2c:variable:yych:emit = 0;
end = "\x00";
Expand All @@ -32,27 +27,27 @@ fn parse_u32(str string) ?u32 {
bin:
/*!re2c
end { unsafe{ goto end } }
[01] { n = adddgt(n, 2, str[cur-1] - 48); unsafe{ goto bin } }
[01] { n = adddgt(n, 2, yyinput[yycursor-1] - 48); unsafe{ goto bin } }
* { return none }
*/
oct:
/*!re2c
end { unsafe{ goto end } }
[0-7] { n = adddgt(n, 8, str[cur-1] - 48); unsafe{ goto oct } }
[0-7] { n = adddgt(n, 8, yyinput[yycursor-1] - 48); unsafe{ goto oct } }
* { return none }
*/
dec:
/*!re2c
end { unsafe{ goto end } }
[0-9] { n = adddgt(n, 10, str[cur-1] - 48); unsafe{ goto dec } }
[0-9] { n = adddgt(n, 10, yyinput[yycursor-1] - 48); unsafe{ goto dec } }
* { return none }
*/
hex:
/*!re2c
end { unsafe{ goto end } }
[0-9] { n = adddgt(n, 16, str[cur-1] - 48); unsafe{ goto hex } }
[a-f] { n = adddgt(n, 16, str[cur-1] - 87); unsafe{ goto hex } }
[A-F] { n = adddgt(n, 16, str[cur-1] - 55); unsafe{ goto hex } }
[0-9] { n = adddgt(n, 16, yyinput[yycursor-1] - 48); unsafe{ goto hex } }
[a-f] { n = adddgt(n, 16, yyinput[yycursor-1] - 87); unsafe{ goto hex } }
[A-F] { n = adddgt(n, 16, yyinput[yycursor-1] - 55); unsafe{ goto hex } }
* { return none }
*/
end:
Expand Down
Loading

0 comments on commit c208cea

Please sign in to comment.