Skip to content

Commit 654daf4

Browse files
Added code : Go (#445)
* Added Go * minor changes
1 parent ec2162a commit 654daf4

File tree

4 files changed

+1295
-0
lines changed

4 files changed

+1295
-0
lines changed

packages/keybr-code/lib/syntax.ts

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { generate } from "./generate.ts";
88
import { Output } from "./output.ts";
99
import {
1010
grammar_cpp,
11+
grammar_go,
1112
grammar_html_css,
1213
grammar_java,
1314
grammar_javascript,
@@ -52,6 +53,8 @@ export class Syntax implements EnumItem {
5253

5354
static readonly JAVA = new Syntax("java", "Java", grammar_java);
5455

56+
static readonly GO = new Syntax("go", "Go", grammar_go);
57+
5558
static readonly ALL = new Enum<Syntax>(
5659
Syntax.HTML,
5760
Syntax.CSS,
@@ -65,6 +68,7 @@ export class Syntax implements EnumItem {
6568
Syntax.SHELL,
6669
Syntax.REGEX,
6770
Syntax.JAVA,
71+
Syntax.GO,
6872
);
6973

7074
readonly id: string;

packages/keybr-code/lib/syntax/grammars.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { compose } from "../ast.ts";
22
import _ident from "./_ident.ts";
33
import _keywords from "./_keywords.ts";
44
import lang_cpp from "./lang_cpp.ts";
5+
import lang_go from "./lang_go.ts";
56
import lang_html_css from "./lang_html_css.ts";
67
import lang_java from "./lang_java.ts";
78
import lang_javascript from "./lang_javascript.ts";
@@ -20,3 +21,4 @@ export const grammar_rust = compose(lang_rust, _keywords, _ident);
2021
export const grammar_shell = compose(lang_shell);
2122
export const grammar_regex = compose(lang_regex);
2223
export const grammar_java = compose(lang_java);
24+
export const grammar_go = compose(lang_go, _keywords, _ident);
+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
start -> golang_statement;
2+
3+
golang_statement ->
4+
package_declaration
5+
| import_declaration
6+
| function_declaration
7+
| struct_declaration
8+
| interface_declaration
9+
| variable_declaration
10+
| constant_declaration
11+
| type_declaration
12+
| comment
13+
;
14+
15+
package_declaration -> kw_package _ golang_package_name;
16+
17+
import_declaration ->
18+
kw_import _ "(" _ import_list _ ")"
19+
| kw_import _ import_spec
20+
;
21+
22+
import_list -> import_spec [_ import_spec];
23+
24+
import_spec ->
25+
golang_string_literal
26+
| golang_import_alias _ golang_string_literal
27+
;
28+
29+
golang_import_alias -> golang_identifier;
30+
31+
function_declaration -> kw_func _ [receiver _] golang_function_name _ "(" _ parameter_list _ ")" _ [return_spec _] function_body;
32+
33+
receiver -> "(" _ [golang_identifier _] golang_type _ ")";
34+
35+
return_spec ->
36+
golang_type
37+
| "(" _ return_params _ ")"
38+
;
39+
40+
return_params -> golang_type [_ "," _ golang_type];
41+
42+
function_body -> "{" _ golang_statement_list _ "}";
43+
44+
golang_statement_list -> [golang_statement [_ golang_statement]];
45+
46+
struct_declaration -> kw_type _ golang_type_name _ kw_struct _ "{" _ struct_field_list _ "}";
47+
48+
struct_field_list -> [struct_field [_ struct_field]];
49+
50+
struct_field -> golang_identifier_list _ golang_type [_ struct_tag];
51+
52+
struct_tag -> golang_string_literal;
53+
54+
golang_identifier_list -> golang_identifier [_ "," _ golang_identifier];
55+
56+
interface_declaration -> kw_type _ golang_type_name _ kw_interface _ "{" _ interface_method_list _ "}";
57+
58+
interface_method_list -> [interface_method_spec [_ interface_method_spec]];
59+
60+
interface_method_spec -> golang_method_name _ "(" _ parameter_list _ ")" _ [return_spec];
61+
62+
variable_declaration ->
63+
kw_var _ variable_spec
64+
| short_variable_declaration
65+
;
66+
67+
variable_spec -> golang_identifier_list [_ golang_type] [_ "=" _ expression_list];
68+
69+
short_variable_declaration -> golang_identifier_list _ ":=" _ expression_list;
70+
71+
expression_list -> golang_expression [_ "," _ golang_expression];
72+
73+
constant_declaration -> kw_const _ constant_spec;
74+
75+
constant_spec -> golang_identifier_list [_ golang_type] _ "=" _ expression_list;
76+
77+
type_declaration -> kw_type _ type_spec;
78+
79+
type_spec -> golang_type_name _ golang_type;
80+
81+
parameter_list -> [parameter [_ "," _ parameter]];
82+
83+
parameter -> [golang_identifier_list _] golang_type;
84+
85+
golang_type ->
86+
golang_basic_type
87+
| golang_reference_type
88+
| golang_struct_type
89+
| golang_interface_type
90+
| golang_array_type
91+
| golang_slice_type
92+
| golang_map_type
93+
| golang_channel_type
94+
| golang_function_type
95+
;
96+
97+
golang_basic_type ->
98+
kw_bool
99+
| kw_int
100+
| kw_int8
101+
| kw_int16
102+
| kw_int32
103+
| kw_int64
104+
| kw_uint
105+
| kw_uint8
106+
| kw_uint16
107+
| kw_uint32
108+
| kw_uint64
109+
| kw_float32
110+
| kw_float64
111+
| kw_complex64
112+
| kw_complex128
113+
| kw_string
114+
| kw_rune
115+
| kw_byte
116+
;
117+
118+
golang_reference_type -> "*" _ golang_type;
119+
golang_struct_type -> kw_struct _ "{" _ struct_field_list _ "}";
120+
golang_interface_type -> kw_interface _ "{" _ interface_method_list _ "}";
121+
golang_array_type -> "[" _ golang_expression _ "]" _ golang_type;
122+
golang_slice_type -> "[]" _ golang_type;
123+
golang_map_type -> kw_map _ "[" _ golang_type _ "]" _ golang_type;
124+
golang_channel_type ->
125+
kw_chan _ golang_type
126+
| "<-" _ kw_chan _ golang_type
127+
| kw_chan _ "<-" _ golang_type
128+
;
129+
golang_function_type -> kw_func _ "(" _ parameter_list _ ")" _ [return_spec];
130+
131+
golang_expression ->
132+
golang_identifier
133+
| golang_literal
134+
| golang_function_call
135+
| unary_expression
136+
| binary_expression
137+
;
138+
139+
unary_expression -> unary_operator _ golang_expression;
140+
unary_operator -> "&" | "*" | "+" | "-" | "!" | "^" | "<-";
141+
142+
binary_expression -> golang_expression _ binary_operator _ golang_expression;
143+
binary_operator -> "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "<<" | ">>" | "&&" | "||" | "==" | "!=" | "<" | "<=" | ">" | ">=";
144+
145+
golang_literal ->
146+
golang_number_literal
147+
| golang_string_literal
148+
| golang_boolean_literal
149+
| golang_nil_literal
150+
;
151+
152+
golang_number_literal -> "0" | "1" | "2" | "42" | "100" | "200" | "404";
153+
golang_string_literal -> "\"" "\"" | "`" "`";
154+
golang_boolean_literal -> "true" | "false";
155+
golang_nil_literal -> "nil";
156+
157+
golang_function_call -> golang_identifier _ "(" _ argument_list _ ")";
158+
argument_list -> [golang_expression [_ "," _ golang_expression]];
159+
160+
golang_identifier -> "x" | "y" | "z" | "result" | "data" | "input" | "err" | "val";
161+
golang_function_name -> "main" | "calculate" | "setValue" | "getResult" | "init" | "String" | "Error";
162+
golang_type_name -> "string" | "int" | "bool" | "float64" | "CustomType" | "Error" | "Reader" | "Writer";
163+
golang_package_name -> "main" | "fmt" | "strings" | "math" | "os" | "io" | "net/http" | "encoding/json";
164+
golang_method_name -> "Read" | "Write" | "String" | "Error" | "Close" | "Parse";
165+
166+
comment ->
167+
single_line_comment
168+
| multi_line_comment
169+
;
170+
171+
single_line_comment -> "//" _ comment_text;
172+
multi_line_comment -> "/*" _ comment_text _ "*/";
173+
174+
comment_text ->
175+
"Function to calculate the result"
176+
| "TODO: Improve performance"
177+
| "This is a constructor"
178+
| "Package documentation"
179+
| "Returns value with error"
180+
| "Implements interface"
181+
| "Deprecated: Use alternative function"
182+
;
183+
184+
kw_package -> "package";
185+
kw_import -> "import";
186+
kw_func -> "func";
187+
kw_type -> "type";
188+
kw_struct -> "struct";
189+
kw_interface -> "interface";
190+
kw_var -> "var";
191+
kw_const -> "const";
192+
kw_map -> "map";
193+
kw_chan -> "chan";
194+
kw_bool -> "bool";
195+
kw_int -> "int";
196+
kw_int8 -> "int8";
197+
kw_int16 -> "int16";
198+
kw_int32 -> "int32";
199+
kw_int64 -> "int64";
200+
kw_uint -> "uint";
201+
kw_uint8 -> "uint8";
202+
kw_uint16 -> "uint16";
203+
kw_uint32 -> "uint32";
204+
kw_uint64 -> "uint64";
205+
kw_float32 -> "float32";
206+
kw_float64 -> "float64";
207+
kw_complex64 -> "complex64";
208+
kw_complex128 -> "complex128";
209+
kw_string -> "string";
210+
kw_rune -> "rune";
211+
kw_byte -> "byte";
212+

0 commit comments

Comments
 (0)