-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathStructParser.cs
More file actions
173 lines (153 loc) · 6.49 KB
/
StructParser.cs
File metadata and controls
173 lines (153 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System.Text.RegularExpressions;
using Superpower;
using Superpower.Model;
using Superpower.Parsers;
using Superpower.Tokenizers;
public class RustField
{
public string DocComment { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public bool Optional { get; set; }
public bool Vec { get; set; }
}
public class RustStruct
{
public string DocComment { get; set; }
public string Name { get; set; }
public List<RustField> Fields { get; set; }
}
public enum RustToken
{
DocComment,
Attribute,
KeywordPub,
KeywordStruct,
KeywordImpl,
Identifier,
OpenBrace, // {
CloseBrace, // }
Colon, // :
Semicolon, // ;
Comma, // ,
OtherSymbol,
}
public static class StructParser
{
// Tokenizer
private static readonly Tokenizer<RustToken> Tokenizer = new TokenizerBuilder<RustToken>()
.Ignore(Span.WhiteSpace)
.Ignore(Span.Regex(@"/\*[\s\S]*?\*/")) // Ignore multi-line comments
.Ignore(Span.Regex(@"(\s|^)\/\/[^\/].*")) // Ignore single-line comments but not doc comments
.Match(Span.Regex(@"///.*"), RustToken.DocComment)
.Match(Span.Regex(@"#\[[^\]]*\]"), RustToken.Attribute)
.Match(Span.EqualTo("pub"), RustToken.KeywordPub)
.Match(Span.EqualTo("struct"), RustToken.KeywordStruct)
.Match(Span.EqualTo("impl"), RustToken.KeywordImpl)
.Match(Span.Regex(@"[a-zA-Z_][a-zA-Z0-9_<>]*"), RustToken.Identifier)
.Match(Character.EqualTo('{'), RustToken.OpenBrace)
.Match(Character.EqualTo('}'), RustToken.CloseBrace)
.Match(Character.EqualTo(':'), RustToken.Colon)
.Match(Character.EqualTo(';'), RustToken.Semicolon)
.Match(Character.EqualTo(','), RustToken.Comma)
.Match(Character.AnyChar, RustToken.OtherSymbol)
.Ignore(Span.WhiteSpace)
.Build();
// Parsers
private static readonly TokenListParser<RustToken, string> DocComment =
Token.EqualTo(RustToken.DocComment)
.Many()
.Select(docs => string.Join("\n", docs.Select(doc =>
doc.ToStringValue().Substring(3).Trim())));
private static readonly TokenListParser<RustToken, Unit> Attribute =
Token.EqualTo(RustToken.Attribute).Many().Select(_ => Unit.Value);
private static TokenListParser<RustToken, Unit> SkipNestedBraces()
{
return
from open in Token.EqualTo(RustToken.OpenBrace)
from content in SkipNestedBracesContent()
from close in Token.EqualTo(RustToken.CloseBrace)
select Unit.Value;
}
private static TokenListParser<RustToken, Unit> SkipNestedBracesContent()
{
return
(from nested in SkipNestedBraces() select Unit.Value) // handle recursive braces
.Or(from nonBrace in Token.Matching<RustToken>(kind => kind != RustToken.OpenBrace && kind != RustToken.CloseBrace, "non-brace").AtLeastOnce() select Unit.Value).Many()
.Select(_ => Unit.Value);
}
private static readonly TokenListParser<RustToken, Unit> ImplBlock =
from implKeyword in Token.EqualTo(RustToken.KeywordImpl)
from rest in Token.Matching<RustToken>(kind => kind != RustToken.OpenBrace, "Expected tokens before '{'").AtLeastOnce()
from content in SkipNestedBraces()
select Unit.Value;
private static readonly TokenListParser<RustToken, string> TypeParser =
from rest in Token.Matching<RustToken>(kind => kind != RustToken.Comma, "Expected tokens before ','").AtLeastOnce()
from end in Token.EqualTo(RustToken.Comma)
select string.Join(" ", rest.Select(t => t.ToStringValue()));
private static readonly TokenListParser<RustToken, RustField> FieldDefinition =
from attrs1 in Attribute.Optional()
from docComments in DocComment.OptionalOrDefault()
from attrs2 in Attribute.Optional()
from pub in Token.EqualTo(RustToken.KeywordPub).Optional()
from fieldName in Token.EqualTo(RustToken.Identifier).Select(t => t.ToStringValue())
from colon in Token.EqualTo(RustToken.Colon)
from fieldType in TypeParser
select new RustField {
DocComment = docComments,
Name = fieldName,
Type = fieldType.Trim()
};
private static readonly TokenListParser<RustToken, List<RustField>> StructBody =
from openBrace in Token.EqualTo(RustToken.OpenBrace)
from fields in FieldDefinition.Many()
from closeBrace in Token.EqualTo(RustToken.CloseBrace)
select fields.ToList();
private static readonly TokenListParser<RustToken, RustStruct> StructDefinition =
from attrs1 in Attribute.Optional()
from docComments in DocComment.OptionalOrDefault()
from attrs2 in Attribute.Optional()
from pub in Token.EqualTo(RustToken.KeywordPub).Optional()
from structKeyword in Token.EqualTo(RustToken.KeywordStruct)
from structName in Token.EqualTo(RustToken.Identifier).Select(t => t.ToStringValue())
from structBody in StructBody
select new RustStruct {
DocComment = docComments,
Name = structName,
Fields = structBody
};
private static readonly TokenListParser<RustToken, RustStruct> TopLevelItem =
(from impl in ImplBlock
select (RustStruct) null)
.Or(
from structDef in StructDefinition
select structDef
);
public static IEnumerable<RustStruct> ParseStructs(string code)
{
var tokens = Tokenizer.Tokenize(code);
var parser = TopLevelItem.Many();
var result = parser(tokens);
if (!result.HasValue) {
throw new Exception(result.ToString());
}
var structs = result.Value.Where(s => s != null).ToArray();
foreach (var s in structs) {
foreach (var f in s.Fields) {
var match = Regex.Match(f.Type, @"Option<(.*)>");
// If the field type is an Option, extract the inner type and set Optional to true
if (match.Success) {
f.Type = match.Groups[1].Value;
f.Optional = true;
}
var match2 = Regex.Match(f.Type, @"Vec<(.*)>");
// If the field type is an Vec, extract the inner type and set Vec to true
if (match2.Success) {
f.Type = match2.Groups[1].Value;
f.Vec = true;
}
}
}
return structs;
}
}