diff --git a/lang/ast/ast.go b/lang/ast/ast.go index f75935f6..2e2e587f 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -106,8 +106,14 @@ const ( // - FlagsSuspendible is "ID1" vs "ID1?" KFunc + // KStruct is "struct ID1(List0)": + // - ID1: name + // - List0: fields + // - FlagsSuspendible is "ID1" vs "ID1?" + KStruct + // KFile is a file of source code: - // - List0: top-level declarations + // - List0: top-level declarations KFile ) @@ -131,6 +137,7 @@ var kindStrings = [...]string{ KInvalid: "KInvalid", KParam: "KParam", KReturn: "KReturn", + KStruct: "KStruct", KType: "KType", } diff --git a/lang/parse/parse.go b/lang/parse/parse.go index 88441eb3..d56c3f5f 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -99,6 +99,33 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { List1: outParams, List2: block, }, nil + + case t.IDStruct: + flags := a.Flags(0) + p.src = p.src[1:] + id1, err := p.parseIdent() + if err != nil { + return nil, err + } + if p.peekID() == t.IDQuestion { + flags |= a.FlagsSuspendible + p.src = p.src[1:] + } + list0, err := p.parseList((*parser).parseParam) + if err != nil { + return nil, err + } + if x := p.peekID(); x != t.IDSemicolon { + got := p.m.ByKey(x.Key()) + return nil, fmt.Errorf("parse: expected (implicit) \";\", got %q at %s:%d", got, p.filename, p.line()) + } + p.src = p.src[1:] + return &a.Node{ + Kind: a.KStruct, + Flags: flags, + ID1: id1, + List0: list0, + }, nil } return nil, fmt.Errorf("parse: unrecognized top level declaration at %s:%d", p.filename, p.src[0].Line) } diff --git a/lang/render/render.go b/lang/render/render.go index ad113f1d..4de8c8e9 100644 --- a/lang/render/render.go +++ b/lang/render/render.go @@ -7,8 +7,6 @@ package render // allow automated refactoring tools, not just automated formatting. For now, // rendering tokens is easier, especially with respect to tracking comments. -// TODO: handle comments. - import ( "errors" "io" @@ -43,9 +41,13 @@ func RenderFile(w io.Writer, src []token.Token, comments []string, m *token.IDMa src = src[i:] // Print any previous comments. + commentIndent := indent + if prevLineHanging { + commentIndent++ + } for ; commentLine < line; commentLine++ { buf = buf[:0] - buf = appendComment(buf, comments, commentLine, indent, true) + buf = appendComment(buf, comments, commentLine, commentIndent, true) if len(buf) == 0 { continue } @@ -80,11 +82,11 @@ func RenderFile(w io.Writer, src []token.Token, comments []string, m *token.IDMa } // Render any leading indentation. If this line starts with a close - // curly, outdent it so that it hopefully aligns vertically with the - // line containing the matching open curly. + // token, outdent it so that it hopefully aligns vertically with the + // line containing the matching open token. buf = buf[:0] indentAdjustment := 0 - if lineTokens[0].ID == token.IDCloseCurly { + if lineTokens[0].ID.IsClose() { indentAdjustment-- } else if hanging { indentAdjustment++ diff --git a/lang/token/list.go b/lang/token/list.go index 759dffb6..a93c7445 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -152,6 +152,7 @@ const ( IDReturn = ID(0xA6<> idShift: {"return", IDReturn}, IDBreak >> idShift: {"break", IDBreak}, IDContinue >> idShift: {"continue", IDContinue}, + IDStruct >> idShift: {"struct", IDStruct}, IDFalse >> idShift: {"false", IDFalse}, IDTrue >> idShift: {"true", IDTrue},