Parse "struct foo(x u32)".

Also remove an obsolete TODO comment.
This commit is contained in:
Nigel Tao committed 2017-05-05 13:51:09 +10:00
1 parent 94cb8b25cb
commit 65d6608434
4 files changed
+45 -7

No files matched your search

+8 -1
View File
@@ -106,8 +106,14 @@ const (
// - FlagsSuspendible is "ID1" vs "ID1?"
KFunc
// KStruct is "struct ID1(List0)":
// - ID1: name
// - List0: <KParam> fields
// - FlagsSuspendible is "ID1" vs "ID1?"
KStruct
// KFile is a file of source code:
// - List0: <KFunc> top-level declarations
// - List0: <KFunc|KStruct> top-level declarations
KFile
)
@@ -131,6 +137,7 @@ var kindStrings = [...]string{
KInvalid: "KInvalid",
KParam: "KParam",
KReturn: "KReturn",
KStruct: "KStruct",
KType: "KType",
}
+27
View File
@@ -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)
}
+8 -6
View File
@@ -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++
+2
View File
@@ -152,6 +152,7 @@ const (
IDReturn = ID(0xA6<<idShift | FlagsOther | FlagsImplicitSemicolon)
IDBreak = ID(0xA7<<idShift | FlagsOther | FlagsImplicitSemicolon)
IDContinue = ID(0xA8<<idShift | FlagsOther | FlagsImplicitSemicolon)
IDStruct = ID(0xA9<<idShift | FlagsOther)
IDFalse = ID(0xD0<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
IDTrue = ID(0xD1<<idShift | FlagsLiteral | FlagsImplicitSemicolon)
@@ -234,6 +235,7 @@ var builtInsByKey = [nBuiltInKeys]struct {
IDReturn >> idShift: {"return", IDReturn},
IDBreak >> idShift: {"break", IDBreak},
IDContinue >> idShift: {"continue", IDContinue},
IDStruct >> idShift: {"struct", IDStruct},
IDFalse >> idShift: {"false", IDFalse},
IDTrue >> idShift: {"true", IDTrue},