Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add struct tags support for compiler and importer #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ pub struct StructFieldDef {
pub name: Ident,
pub ann: TypeAst,
pub ty: BoundedType,
pub tags: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,11 @@ if {is_matching} != 2 {{
def.fields.iter().for_each(|f| {
let field = &f.name;
let ty = self.to_type(&f.ty.ty);
out.emit(format!(" {field} {ty}"));
let tags = &f.tags;
match tags {
Some(tg) => out.emit(format!(" {field} {ty} `{tg}`")),
None => out.emit(format!(" {field} {ty}")),
}
});

out.emit("}".to_string());
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,7 @@ has no field or method:
name,
ann: f.ann.clone(),
ty: typ.to_bounded(),
tags: None
}
})
.collect();
Expand Down Expand Up @@ -3169,6 +3170,7 @@ has no field or method:
name: sym.name.clone(),
ann: TypeAst::Unknown,
ty: ty.clone(),
tags: None
});
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,15 +527,15 @@ impl Lexer {
}
}

fn scan_string(&mut self) -> Token {
fn scan_string(&mut self, delimiter: char) -> Token {
let mut text = String::new();

let start = self.next();
let mut seen_closing = false;

// TODO validate escape \"
loop {
if self.ch == '"' {
if self.ch == delimiter {
self.next();
seen_closing = true;
break;
Expand Down Expand Up @@ -681,7 +681,7 @@ impl Lexer {
match self.ch {
'0'..='9' => self.scan_number(),
'a'..='z' | 'A'..='Z' | '_' => self.scan_ident(),
'"' => self.scan_string(),
'"' | '`' => self.scan_string(self.ch),
'\'' => self.scan_char(),
'/' => self.scan_slash_comment(),
'\\' => self.scan_multi_string(),
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,11 +1768,24 @@ impl Parser {
let name = self.parse_one_ident();
self.expect(TokenKind::Colon);
let ann = self.parse_type();
let tags = self.parse_struct_field_tags();

StructFieldDef {
name,
ann,
ty: Type::dummy().to_bounded(),
tags
}
}

fn parse_struct_field_tags(&mut self) -> Option<String> {
match self.tok.kind {
TokenKind::String => {
let tags = Some(self.tok.text.clone());
self.next();
tags
}
_ => None,
}
}

Expand Down
22 changes: 16 additions & 6 deletions importer/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type Struct struct {
type StructField struct {
Name string
Type Type
Tags string
}

type Variable struct {
Expand Down Expand Up @@ -232,7 +233,16 @@ func (p *Package) AddStruct(name string, bounds []Bound, list []*ast.Field, kind
name = f.Names[0].Name
}

fields = append(fields, StructField{Name: name, Type: p.parseTypeExpr(f.Type)})
sf := StructField{
Name: name,
Type: p.parseTypeExpr(f.Type),
}

if f.Tag != nil {
sf.Tags = f.Tag.Value
}

fields = append(fields, sf)
}

s := Struct{Name: name, Bounds: bounds, Fields: fields, Kind: kind}
Expand Down Expand Up @@ -337,7 +347,6 @@ func (p *Package) String() string {
}

for _, s := range p.Types {

switch s.Kind {
case TypeStruct:
bounds := boundsToString(s.Bounds)
Expand All @@ -356,7 +365,6 @@ func (p *Package) String() string {
def := "interface " + s.Name + generics + " {\n" + newBounds + "\n" + newFields + "\n}\n\n"
fmt.Fprint(&w, def)
}

}

return w.String()
Expand Down Expand Up @@ -560,7 +568,6 @@ func functionArgsToString(fargs []FuncArg) string {
}

return strings.Join(args, ", ")

}

func structFieldsToString(list []StructField) string {
Expand All @@ -573,7 +580,11 @@ func structFieldsToString(list []StructField) string {
continue
}

fields = append(fields, " "+f.Name+": "+f.Type.String())
fieldStr := " " + f.Name + ": " + f.Type.String()
if f.Tags != "" {
fieldStr = fieldStr + " " + f.Tags
}
fields = append(fields, fieldStr)
}

return strings.Join(fields, ",\n")
Expand Down Expand Up @@ -728,7 +739,6 @@ func main() {
}

for _, decl := range t.Decl.Specs {

if spec, ok := decl.(*ast.TypeSpec); ok {

bounds := p.parseBounds(spec.TypeParams)
Expand Down
4 changes: 2 additions & 2 deletions importer/testpkg/testpkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
const SomeConst = 1

type Person struct {
Name string
Hobbies []Hobby
Name string `json:"name"`
Hobbies []Hobby `json:"hobbies"`
}

type Hobby struct {
Expand Down