Skip to content

Commit d3c150c

Browse files
committed
feat: update the source code grammars
1 parent 29a3078 commit d3c150c

16 files changed

+646
-197
lines changed

packages/keybr-code/lib/syntax.ts

+57-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,64 @@ import { type RNG } from "@keybr/rand";
33
import { type Rules } from "./ast.ts";
44
import { generate } from "./generate.ts";
55
import { Output } from "./output.ts";
6-
import lang_c from "./syntax/lang_c.ts";
7-
import lang_html from "./syntax/lang_html.ts";
8-
import lang_noise from "./syntax/lang_noise.ts";
6+
import lang_cpp from "./syntax/lang_cpp.ts";
7+
import lang_html_css from "./syntax/lang_html_css.ts";
8+
import lang_javascript from "./syntax/lang_javascript.ts";
9+
import lang_php from "./syntax/lang_php.ts";
10+
import lang_rust from "./syntax/lang_rust.ts";
911

1012
export class Syntax implements EnumItem {
11-
static readonly HTML = new Syntax("html", "HTML", lang_html);
12-
static readonly C = new Syntax("cpp", "C/C++", lang_c);
13-
static readonly NOISE = new Syntax("noise", "Noise", lang_noise);
14-
static readonly ALL = new Enum<Syntax>(Syntax.HTML, Syntax.C, Syntax.NOISE);
13+
static readonly CPP = new Syntax(
14+
"cpp", //
15+
"C/C++",
16+
lang_cpp,
17+
);
18+
static readonly CPP_FPROTO = new Syntax(
19+
"cpp_fproto", //
20+
"C/C++ Function Prototypes",
21+
lang_cpp,
22+
"start_fproto",
23+
);
24+
static readonly CPP_STMT = new Syntax(
25+
"cpp_stmt", //
26+
"C/C++ Statements",
27+
lang_cpp,
28+
"start_stmt",
29+
);
30+
static readonly HTML = new Syntax(
31+
"html", //
32+
"HTML",
33+
lang_html_css,
34+
"html",
35+
);
36+
static readonly CSS = new Syntax(
37+
"css", //
38+
"CSS",
39+
lang_html_css,
40+
"css",
41+
);
42+
static readonly JAVASCRIPT = new Syntax(
43+
"javascript", //
44+
"Java Script",
45+
lang_javascript,
46+
);
47+
static readonly PHP = new Syntax(
48+
"php", //
49+
"PHP",
50+
lang_php,
51+
);
52+
static readonly RUST = new Syntax(
53+
"rust", //
54+
"Rust",
55+
lang_rust,
56+
);
57+
static readonly ALL = new Enum<Syntax>(
58+
Syntax.HTML,
59+
Syntax.CSS,
60+
Syntax.CPP,
61+
Syntax.CPP_FPROTO,
62+
Syntax.CPP_STMT,
63+
);
1564

1665
private constructor(
1766
readonly id: string,
@@ -31,7 +80,7 @@ export class Syntax implements EnumItem {
3180
throw err;
3281
}
3382
}
34-
return String(output);
83+
return String(output).trim();
3584
}
3685

3786
toString() {

packages/keybr-code/lib/syntax/lang_c.g packages/keybr-code/lib/syntax/lang_cpp.g

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
1-
start -> c_func ;
1+
start -> c_func " " c_func " " c_func ;
22

3-
c_func -> c_type " " c_func_id "(" c_param_list ") " c_func_body ;
3+
start_fproto ->
4+
c_func_proto ";"
5+
" " c_func_proto ";"
6+
" " c_func_proto ";"
7+
" " c_func_proto ";"
8+
;
9+
10+
start_stmt ->
11+
c_stmt
12+
" " c_stmt
13+
" " c_stmt
14+
" " c_stmt
15+
;
416

5-
c_type -> ( "char" | "int" | "long" | "short" | "void" ) [ "*" ] ;
17+
c_func -> c_func_proto " " c_func_body ;
618

7-
c_param_list -> c_param ", " c_param ;
19+
c_func_proto -> c_type " " c_func_id "(" c_param_list ")";
20+
21+
c_type -> ( "char" | "int" | "long" | "short" | "void" ) [ "*" [ "*" ] ] ;
22+
23+
c_param_list -> c_param [ ", " c_param [ ", " c_param ] ] ;
824

925
c_param -> c_type " " [ "&" ] c_var_id [ "[]" ] ;
1026

11-
c_arg_list -> c_arg ", " c_arg ;
27+
c_arg_list -> c_arg [ ", " c_arg [ ", " c_arg ] ] ;
1228

1329
c_arg -> c_expr ;
1430

1531
c_func_body -> "{ " c_stmt " }" ;
1632

17-
c_stmt -> ( c_call_stmt | c_if_stmt | c_return_stmt ) ";" ;
33+
c_stmt -> ( c_var_stmt | c_call_stmt | c_if_stmt | c_return_stmt ) ;
34+
35+
c_var_stmt -> c_type " " c_var_id " = " c_expr ";" ;
1836

19-
c_call_stmt -> c_func_id "(" c_arg_list ")" ;
37+
c_call_stmt -> c_func_id "(" c_arg_list ")" ";" ;
2038

2139
c_if_stmt -> "if (" c_expr ") { " c_call_stmt " }" ;
2240

23-
c_return_stmt -> "return " c_expr ;
41+
c_return_stmt -> "return " c_expr ";" ;
2442

2543
c_expr -> c_unary_exp | c_binary_exp | c_ternary_exp ;
2644

packages/keybr-code/lib/syntax/lang_c.ts packages/keybr-code/lib/syntax/lang_cpp.ts

+140-22
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,71 @@ import { type Rules } from "../ast.ts";
44

55
export default {
66
start: {
7-
ref: "c_func",
7+
seq: [
8+
{
9+
ref: "c_func",
10+
},
11+
" ",
12+
{
13+
ref: "c_func",
14+
},
15+
" ",
16+
{
17+
ref: "c_func",
18+
},
19+
],
20+
},
21+
start_fproto: {
22+
seq: [
23+
{
24+
ref: "c_func_proto",
25+
},
26+
"; ",
27+
{
28+
ref: "c_func_proto",
29+
},
30+
"; ",
31+
{
32+
ref: "c_func_proto",
33+
},
34+
"; ",
35+
{
36+
ref: "c_func_proto",
37+
},
38+
";",
39+
],
40+
},
41+
start_stmt: {
42+
seq: [
43+
{
44+
ref: "c_stmt",
45+
},
46+
" ",
47+
{
48+
ref: "c_stmt",
49+
},
50+
" ",
51+
{
52+
ref: "c_stmt",
53+
},
54+
" ",
55+
{
56+
ref: "c_stmt",
57+
},
58+
],
859
},
960
c_func: {
61+
seq: [
62+
{
63+
ref: "c_func_proto",
64+
},
65+
" ",
66+
{
67+
ref: "c_func_body",
68+
},
69+
],
70+
},
71+
c_func_proto: {
1072
seq: [
1173
{
1274
ref: "c_type",
@@ -19,10 +81,7 @@ export default {
1981
{
2082
ref: "c_param_list",
2183
},
22-
") ",
23-
{
24-
ref: "c_func_body",
25-
},
84+
")",
2685
],
2786
},
2887
c_type: {
@@ -32,7 +91,15 @@ export default {
3291
},
3392
{
3493
f: 0.5,
35-
opt: "*",
94+
opt: {
95+
seq: [
96+
"*",
97+
{
98+
f: 0.5,
99+
opt: "*",
100+
},
101+
],
102+
},
36103
},
37104
],
38105
},
@@ -41,9 +108,27 @@ export default {
41108
{
42109
ref: "c_param",
43110
},
44-
", ",
45111
{
46-
ref: "c_param",
112+
f: 0.5,
113+
opt: {
114+
seq: [
115+
", ",
116+
{
117+
ref: "c_param",
118+
},
119+
{
120+
f: 0.5,
121+
opt: {
122+
seq: [
123+
", ",
124+
{
125+
ref: "c_param",
126+
},
127+
],
128+
},
129+
},
130+
],
131+
},
47132
},
48133
],
49134
},
@@ -71,9 +156,27 @@ export default {
71156
{
72157
ref: "c_arg",
73158
},
74-
", ",
75159
{
76-
ref: "c_arg",
160+
f: 0.5,
161+
opt: {
162+
seq: [
163+
", ",
164+
{
165+
ref: "c_arg",
166+
},
167+
{
168+
f: 0.5,
169+
opt: {
170+
seq: [
171+
", ",
172+
{
173+
ref: "c_arg",
174+
},
175+
],
176+
},
177+
},
178+
],
179+
},
77180
},
78181
],
79182
},
@@ -90,19 +193,33 @@ export default {
90193
],
91194
},
92195
c_stmt: {
196+
alt: [
197+
{
198+
ref: "c_var_stmt",
199+
},
200+
{
201+
ref: "c_call_stmt",
202+
},
203+
{
204+
ref: "c_if_stmt",
205+
},
206+
{
207+
ref: "c_return_stmt",
208+
},
209+
],
210+
},
211+
c_var_stmt: {
93212
seq: [
94213
{
95-
alt: [
96-
{
97-
ref: "c_call_stmt",
98-
},
99-
{
100-
ref: "c_if_stmt",
101-
},
102-
{
103-
ref: "c_return_stmt",
104-
},
105-
],
214+
ref: "c_type",
215+
},
216+
" ",
217+
{
218+
ref: "c_var_id",
219+
},
220+
" = ",
221+
{
222+
ref: "c_expr",
106223
},
107224
";",
108225
],
@@ -116,7 +233,7 @@ export default {
116233
{
117234
ref: "c_arg_list",
118235
},
119-
")",
236+
");",
120237
],
121238
},
122239
c_if_stmt: {
@@ -138,6 +255,7 @@ export default {
138255
{
139256
ref: "c_expr",
140257
},
258+
";",
141259
],
142260
},
143261
c_expr: {

packages/keybr-code/lib/syntax/lang_html.g

-24
This file was deleted.

0 commit comments

Comments
 (0)