s2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,321 @@
1
+
2
+ #include <iostream>
3
+ #include <memory>
4
+ #include "S2.hpp"
5
+
6
+ /*
7
+ WARNING: This file is generated using ruco. Please modify the .ruco file if you wish to change anything
8
+ https://github.com/davidsiaw/ruco
9
+ */
10
+
11
+ COMPILER S2
12
+
13
+ S2Ptr s2;
14
+
15
+ CHARACTERS
16
+ bigletter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
17
+ letter = "abcdefghijklmnopqrstuvwxyz".
18
+ underscore = "_".
19
+ digit = "0123456789".
20
+ cr = '\r'.
21
+ lf = '\n'.
22
+ tab = '\t'.
23
+ stringCh = ANY - '"' - '\\' - cr - lf.
24
+ charCh = ANY - '\'' - '\\' - cr - lf.
25
+ printable = '\u0020' .. '\u007e'.
26
+ hex = "0123456789abcdef".
27
+
28
+ TOKENS
29
+ pascalcase = bigletter { bigletter | letter | digit }.
30
+ camelcase = letter { bigletter | letter | digit }.
31
+
32
+ number = digit { digit } [ '.' digit { digit } ].
33
+ hexinteger = '0' 'x' hex { hex }.
34
+
35
+ string = '"' { stringCh | '\\' printable } '"'.
36
+ badString = '"' { stringCh | '\\' printable } (cr | lf).
37
+ char = '\'' ( charCh | '\\' printable { hex } ) '\''.
38
+ endOfLine = cr | lf.
39
+
40
+ customTokenTypeVariable = '$' bigletter { bigletter | letter | digit }.
41
+
42
+ PRAGMAS
43
+ ddtSym = '$' { digit | letter }.
44
+ optionSym = '$' letter { letter } '='
45
+ { digit | letter
46
+ | '-' | '.' | ':'
47
+ }.
48
+
49
+
50
+ COMMENTS FROM "/*" TO "*/" NESTED
51
+ COMMENTS FROM "//" TO lf
52
+
53
+ IGNORE tab + cr + lf
54
+
55
+ /*-------------------------------------------------------------------------*/
56
+
57
+ PRODUCTIONS
58
+
59
+ S2 = (. s2 = std::make_shared<class S2>(); .)
60
+ (. unsigned curline = la->line, curcol = la->col; .)
61
+ (. StatementPtr statement; .)
62
+ (
63
+ {
64
+ Statement<statement> (. s2->statements.push_back(statement); .)
65
+ }
66
+ )
67
+ (. s2->_line = curline; s2->_col = curcol; .)
68
+ .
69
+
70
+ TypeVariable<TypeVariablePtr& production> = (. production = std::make_shared<class TypeVariable>(); .)
71
+ (. unsigned curline = la->line, curcol = la->col; .)
72
+
73
+ (
74
+ customTokenTypeVariable (. production->content = t->val; .)
75
+ )
76
+ (. production->_line = curline; production->_col = curcol; .)
77
+ .
78
+
79
+ StructName<StructNamePtr& production> = (. production = std::make_shared<class StructName>(); .)
80
+ (. unsigned curline = la->line, curcol = la->col; .)
81
+
82
+ (
83
+ pascalcase (. production->content = t->val; .)
84
+ )
85
+ (. production->_line = curline; production->_col = curcol; .)
86
+ .
87
+
88
+ MemberName<MemberNamePtr& production> = (. production = std::make_shared<class MemberName>(); .)
89
+ (. unsigned curline = la->line, curcol = la->col; .)
90
+
91
+ (
92
+ camelcase (. production->content = t->val; .)
93
+ )
94
+ (. production->_line = curline; production->_col = curcol; .)
95
+ .
96
+
97
+ NumberLiteral<NumberLiteralPtr& production> = (. production = std::make_shared<class NumberLiteral>(); .)
98
+ (. unsigned curline = la->line, curcol = la->col; .)
99
+
100
+ (
101
+ number (. production->content = t->val; .)
102
+ )
103
+ (. production->_line = curline; production->_col = curcol; .)
104
+ .
105
+
106
+ StringLiteral<StringLiteralPtr& production> = (. production = std::make_shared<class StringLiteral>(); .)
107
+ (. unsigned curline = la->line, curcol = la->col; .)
108
+
109
+ (
110
+ string (. production->content = t->val; .)
111
+ )
112
+ (. production->_line = curline; production->_col = curcol; .)
113
+ .
114
+
115
+ TypeIdentifier<TypeIdentifierPtr& production> = (. production = std::make_shared<class TypeIdentifier>(); .)
116
+ (. unsigned curline = la->line, curcol = la->col; .)
117
+ (. StructNamePtr structname; .)
118
+ (. TypeVariablePtr typevariable; .)
119
+ (. TypeParameterArgumentsPtr typeparameterarguments; .)
120
+ (
121
+ (
122
+ StructName<structname> (. production->structname = structname; .)|
123
+ TypeVariable<typevariable> (. production->typevariable = typevariable; .)
124
+ )
125
+ [
126
+ TypeParameterArguments<typeparameterarguments> (. production->typeparameterarguments.push_back(typeparameterarguments); .)
127
+ ]
128
+ )
129
+ (. production->_line = curline; production->_col = curcol; .)
130
+ .
131
+
132
+ TypeExpression<TypeExpressionPtr& production> =
133
+ (. unsigned curline = la->line, curcol = la->col; .)
134
+ (. TypeIdentifierPtr typeidentifier; .)
135
+ (
136
+ (
137
+ TypeIdentifier<typeidentifier> (. production = typeidentifier; .)
138
+ )
139
+ )
140
+ (. production->_line = curline; production->_col = curcol; .)
141
+ .
142
+
143
+ TypeParameterArguments<TypeParameterArgumentsPtr& production> = (. production = std::make_shared<class TypeParameterArguments>(); .)
144
+ (. unsigned curline = la->line, curcol = la->col; .)
145
+ (. TypeExpressionPtr typeexpression; .)
146
+ (
147
+ "<"
148
+ TypeExpression<typeexpression> (. production->typeexpressions.push_back(typeexpression); .)
149
+ {
150
+ (
151
+ ","
152
+ TypeExpression<typeexpression> (. production->typeexpressions.push_back(typeexpression); .)
153
+ )
154
+ }
155
+ ">"
156
+ )
157
+ (. production->_line = curline; production->_col = curcol; .)
158
+ .
159
+
160
+ TypeDeclaration<TypeDeclarationPtr& production> = (. production = std::make_shared<class TypeDeclaration>(); .)
161
+ (. unsigned curline = la->line, curcol = la->col; .)
162
+ (. StructNamePtr structname; .)
163
+ (. TypeParametersPtr typeparameters; .)
164
+ (
165
+ StructName<structname> (. production->structname = structname; .)
166
+ [
167
+ TypeParameters<typeparameters> (. production->typeparameters.push_back(typeparameters); .)
168
+ ]
169
+ )
170
+ (. production->_line = curline; production->_col = curcol; .)
171
+ .
172
+
173
+ TypeParameters<TypeParametersPtr& production> = (. production = std::make_shared<class TypeParameters>(); .)
174
+ (. unsigned curline = la->line, curcol = la->col; .)
175
+ (. TypeVariablePtr typevariable; .)
176
+ (
177
+ "<"
178
+ TypeVariable<typevariable> (. production->typevariables.push_back(typevariable); .)
179
+ {
180
+ (
181
+ ","
182
+ TypeVariable<typevariable> (. production->typevariables.push_back(typevariable); .)
183
+ )
184
+ }
185
+ ">"
186
+ )
187
+ (. production->_line = curline; production->_col = curcol; .)
188
+ .
189
+
190
+ NumberLit<NumberLitPtr& production> = (. production = std::make_shared<class NumberLit>(); .)
191
+ (. unsigned curline = la->line, curcol = la->col; .)
192
+ (. NumberLiteralPtr numberliteral; .)
193
+ (
194
+ NumberLiteral<numberliteral> (. production->numberliteral = numberliteral; .)
195
+ )
196
+ (. production->_line = curline; production->_col = curcol; .)
197
+ .
198
+
199
+ Expression<ExpressionPtr& production> =
200
+ (. unsigned curline = la->line, curcol = la->col; .)
201
+ (. NumberLitPtr numberlit; .)
202
+ (
203
+ (
204
+ NumberLit<numberlit> (. production = numberlit; .)
205
+ )
206
+ )
207
+ (. production->_line = curline; production->_col = curcol; .)
208
+ .
209
+
210
+ AttributeParam<AttributeParamPtr& production> = (. production = std::make_shared<class AttributeParam>(); .)
211
+ (. unsigned curline = la->line, curcol = la->col; .)
212
+ (. MemberNamePtr membername; .)
213
+ (. ExpressionPtr expression; .)
214
+ (
215
+ MemberName<membername> (. production->membername = membername; .)
216
+ "="
217
+ Expression<expression> (. production->expression = expression; .)
218
+ )
219
+ (. production->_line = curline; production->_col = curcol; .)
220
+ .
221
+
222
+ AttributeParamList<AttributeParamListPtr& production> = (. production = std::make_shared<class AttributeParamList>(); .)
223
+ (. unsigned curline = la->line, curcol = la->col; .)
224
+ (. AttributeParamPtr attributeparam; .)
225
+ (
226
+ ":"
227
+ AttributeParam<attributeparam> (. production->attributeparams.push_back(attributeparam); .)
228
+ {
229
+ (
230
+ ","
231
+ AttributeParam<attributeparam> (. production->attributeparams.push_back(attributeparam); .)
232
+ )
233
+ }
234
+ )
235
+ (. production->_line = curline; production->_col = curcol; .)
236
+ .
237
+
238
+ Attribute<AttributePtr& production> = (. production = std::make_shared<class Attribute>(); .)
239
+ (. unsigned curline = la->line, curcol = la->col; .)
240
+ (. TypeExpressionPtr typeexpression; .)
241
+ (. AttributeParamListPtr attributeparamlist; .)
242
+ (
243
+ "["
244
+ TypeExpression<typeexpression> (. production->typeexpression = typeexpression; .)
245
+ [
246
+ AttributeParamList<attributeparamlist> (. production->attributeparamlists.push_back(attributeparamlist); .)
247
+ ]
248
+ "]"
249
+ )
250
+ (. production->_line = curline; production->_col = curcol; .)
251
+ .
252
+
253
+ Member<MemberPtr& production> = (. production = std::make_shared<class Member>(); .)
254
+ (. unsigned curline = la->line, curcol = la->col; .)
255
+ (. AttributePtr attribute; .)
256
+ (. TypeIdentifierPtr typeidentifier; .)
257
+ (. MemberNamePtr membername; .)
258
+ (
259
+ {
260
+ Attribute<attribute> (. production->attributes.push_back(attribute); .)
261
+ }
262
+ TypeIdentifier<typeidentifier> (. production->typeidentifier = typeidentifier; .)
263
+ MemberName<membername> (. production->membernames.push_back(membername); .)
264
+ {
265
+ (
266
+ ","
267
+ MemberName<membername> (. production->membernames.push_back(membername); .)
268
+ )
269
+ }
270
+ )
271
+ (. production->_line = curline; production->_col = curcol; .)
272
+ .
273
+
274
+ Structure<StructurePtr& production> = (. production = std::make_shared<class Structure>(); .)
275
+ (. unsigned curline = la->line, curcol = la->col; .)
276
+ (. AttributePtr attribute; .)
277
+ (. TypeDeclarationPtr typedeclaration; .)
278
+ (. MemberPtr member; .)
279
+ (
280
+ {
281
+ Attribute<attribute> (. production->attributes.push_back(attribute); .)
282
+ }
283
+ "struct"
284
+ TypeDeclaration<typedeclaration> (. production->typedeclaration = typedeclaration; .)
285
+ "{"
286
+ Member<member> (. production->members.push_back(member); .)
287
+ {
288
+ Member<member> (. production->members.push_back(member); .)
289
+ }
290
+ "}"
291
+ )
292
+ (. production->_line = curline; production->_col = curcol; .)
293
+ .
294
+
295
+ Import<ImportPtr& production> = (. production = std::make_shared<class Import>(); .)
296
+ (. unsigned curline = la->line, curcol = la->col; .)
297
+ (. StringLiteralPtr stringliteral; .)
298
+ (
299
+ "import"
300
+ StringLiteral<stringliteral> (. production->stringliteral = stringliteral; .)
301
+ )
302
+ (. production->_line = curline; production->_col = curcol; .)
303
+ .
304
+
305
+ Statement<StatementPtr& production> =
306
+ (. unsigned curline = la->line, curcol = la->col; .)
307
+ (. StructurePtr structure; .)
308
+ (. ImportPtr import; .)
309
+ (
310
+ (
311
+ Structure<structure> (. production = structure; .)|
312
+ Import<import> (. production = import; .)
313
+ )
314
+ )
315
+ (. production->_line = curline; production->_col = curcol; .)
316
+ .
317
+
318
+
319
+
320
+ END S2.
321
+
@@ -0,0 +1,93 @@
1
+ token "TypeVariable", :custom, "'$' bigletter { bigletter | letter | digit }"
2
+ token "StructName", :pascal_case, nil
3
+ token "MemberName", :camel_case, nil
4
+ token "NumberLiteral", :number
5
+ token "StringLiteral", :string
6
+
7
+ grammar "TypeIdentifier" do
8
+ either StructName, TypeVariable
9
+ maybe TypeParameterArguments
10
+ end
11
+
12
+ variation "TypeExpression", TypeIdentifier
13
+
14
+ grammar "TypeParameterArguments" do
15
+ one "<"
16
+ one TypeExpression
17
+ maybemany group {
18
+ one ","
19
+ one TypeExpression
20
+ }
21
+ one ">"
22
+ end
23
+
24
+ grammar "TypeDeclaration" do
25
+ one StructName
26
+ maybe TypeParameters
27
+ end
28
+
29
+ grammar "TypeParameters" do
30
+ one "<"
31
+ one TypeVariable
32
+ maybemany group {
33
+ one ","
34
+ one TypeVariable
35
+ }
36
+ one ">"
37
+ end
38
+
39
+ grammar "NumberLit" do
40
+ one NumberLiteral
41
+ end
42
+
43
+ variation "Expression", NumberLit
44
+
45
+ grammar "AttributeParam" do
46
+ one MemberName
47
+ one "="
48
+ one Expression
49
+ end
50
+
51
+ grammar "AttributeParamList" do
52
+ one ":"
53
+ one AttributeParam
54
+ maybemany group {
55
+ one ","
56
+ one AttributeParam
57
+ }
58
+ end
59
+
60
+ grammar "Attribute" do
61
+ one "["
62
+ one TypeExpression
63
+ maybe AttributeParamList
64
+ one "]"
65
+ end
66
+
67
+ grammar "Member" do
68
+ maybemany Attribute
69
+ one TypeIdentifier
70
+ one MemberName
71
+ maybemany group {
72
+ one ","
73
+ one MemberName
74
+ }
75
+ end
76
+
77
+ grammar "Structure" do
78
+ maybemany Attribute
79
+ one "struct"
80
+ one TypeDeclaration
81
+ one "{"
82
+ many Member
83
+ one "}"
84
+ end
85
+
86
+ grammar "Import" do
87
+ one "import"
88
+ one StringLiteral
89
+ end
90
+
91
+ variation "Statement", Structure, Import
92
+
93
+ maybemany Statement
@@ -0,0 +1,70 @@
1
+ #include <ruby.h>
2
+ #include "Parser.h"
3
+ #include "parse_s2.hpp"
4
+ #include "sstream"
5
+
6
+ picojson::value JsonifyParserError(S2::ParserException e)
7
+ {
8
+ picojson::object error;
9
+ error[L"_error"] = picojson::value(e.GetMessage());
10
+ error[L"_line"] = picojson::value((double)e.LineNumber());
11
+ error[L"_col"] = picojson::value((double)e.ColumnNumber());
12
+ error[L"_type"] = picojson::value(L"parser");
13
+ return picojson::value(error);
14
+ }
15
+
16
+ picojson::value JsonifyOtherError(std::wstring message)
17
+ {
18
+ picojson::object error;
19
+ error[L"_error"] = picojson::value(message);
20
+ error[L"_type"] = picojson::value(L"other");
21
+ return picojson::value(error);
22
+ }
23
+
24
+
25
+ std::wstring RSTRING_PTR_WIDE(VALUE rubyString)
26
+ {
27
+ std::wstring wc( RSTRING_LEN(rubyString), L'#' );
28
+ mbstowcs( &wc[0], RSTRING_PTR(rubyString), RSTRING_LEN(rubyString) );
29
+ return wc;
30
+ }
31
+
32
+ VALUE rb_str_new_wide(std::wstring str)
33
+ {
34
+ std::string s( str.begin(), str.end() );
35
+ return rb_str_new(s.c_str(), s.length());
36
+ }
37
+
38
+ VALUE method_test(VALUE rubySelf, VALUE rubyString)
39
+ {
40
+ std::wstring s(RSTRING_PTR_WIDE(rubyString));
41
+ s = L"test-" + s;
42
+
43
+ return rb_str_new_wide(s);
44
+ }
45
+
46
+ VALUE method_compile_to_ast(VALUE rubySelf, VALUE rubyString)
47
+ {
48
+ try
49
+ {
50
+ S2::Scanner scanner(RSTRING_PTR(rubyString), RSTRING_LEN(rubyString));
51
+ S2::Parser parser(&scanner);
52
+ parser.Parse();
53
+ auto json = S2::Jsonify(parser.s2);
54
+ return rb_str_new_wide(json.serialize().c_str());
55
+ }
56
+ catch (S2::ParserException e)
57
+ {
58
+ auto str = JsonifyParserError(e).serialize();
59
+ return rb_str_new_wide(str.c_str());
60
+ }
61
+
62
+ return rb_str_new_wide(L"{}");
63
+ }
64
+
65
+ extern "C" void Init_s2_parse()
66
+ {
67
+ VALUE s2_parse = rb_define_module("S2_parse");
68
+ rb_define_method(s2_parse, "s2_parse_test", (VALUE(*)(ANYARGS))method_test,1);
69
+ rb_define_method(s2_parse, "s2_parse_compile_to_ast", (VALUE(*)(ANYARGS))method_compile_to_ast,1);
70
+ }