type-section-rework #6
@ -28,6 +28,7 @@ data ro jObject:thing.Thing.thing.thing
|
||||
-- that 2139
|
||||
-- this 324
|
||||
data ro kNestedObject:Obj
|
||||
ro newMember:Int 9023
|
||||
-- that
|
||||
-- bird2 123.8439
|
||||
-- bird3 9328.21348239
|
||||
|
@ -3,6 +3,15 @@ package parser
|
||||
import "git.tebibyte.media/arf/arf/lexer"
|
||||
import "git.tebibyte.media/arf/arf/infoerr"
|
||||
|
||||
// TODO:
|
||||
// (parser *ParsingOperation) parseDefaultValues
|
||||
|
||||
// (parser *ParsingOperation) parseDefaultMemberValues (return tree of new members and a tree of member values)
|
||||
// (parser *ParsingOperation) parseDefaultArrayValues
|
||||
|
||||
// (parser *ParsingOperation) parseDefaultMemberValue
|
||||
// (parser *ParsingOperation) parseMemberDeclaration
|
||||
|
||||
// parseInitializationValues starts on the line after a data section, or a set
|
||||
// phrase. It checks for an indent greater than the indent of the aforementioned
|
||||
// data section or set phrase (passed through baseIndent), and if there is,
|
||||
|
248
parser/objt.go
248
parser/objt.go
@ -1,125 +1,125 @@
|
||||
package parser
|
||||
|
||||
import "git.tebibyte.media/arf/arf/types"
|
||||
import "git.tebibyte.media/arf/arf/lexer"
|
||||
import "git.tebibyte.media/arf/arf/infoerr"
|
||||
|
||||
// parseObjtSection parses an object type definition. This allows for structured
|
||||
// types to be defined, and for member variables to be added and overridden.
|
||||
func (parser *ParsingOperation) parseObjtSection () (
|
||||
section ObjtSection,
|
||||
err error,
|
||||
) {
|
||||
err = parser.expect(lexer.TokenKindName)
|
||||
if err != nil { return }
|
||||
|
||||
section.location = parser.token.Location()
|
||||
|
||||
// get permission
|
||||
err = parser.nextToken(lexer.TokenKindPermission)
|
||||
if err != nil { return }
|
||||
section.permission = parser.token.Value().(types.Permission)
|
||||
|
||||
// get name
|
||||
err = parser.nextToken(lexer.TokenKindName)
|
||||
if err != nil { return }
|
||||
section.name = parser.token.Value().(string)
|
||||
|
||||
// parse inherited type
|
||||
err = parser.nextToken(lexer.TokenKindColon)
|
||||
if err != nil { return }
|
||||
err = parser.nextToken()
|
||||
if err != nil { return }
|
||||
section.inherits, err = parser.parseIdentifier()
|
||||
if err != nil { return }
|
||||
err = parser.expect(lexer.TokenKindNewline)
|
||||
if err != nil { return }
|
||||
err = parser.nextToken()
|
||||
if err != nil { return }
|
||||
|
||||
// parse members
|
||||
err = parser.parseObjtMembers(§ion)
|
||||
if err != nil { return }
|
||||
|
||||
if len(section.members) == 0 {
|
||||
infoerr.NewError (
|
||||
section.location,
|
||||
"defining an object with no members",
|
||||
infoerr.ErrorKindWarn).Print()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// parseObjtMembers parses a list of members for an object section. Indentation
|
||||
// level is assumed.
|
||||
func (parser *ParsingOperation) parseObjtMembers (
|
||||
into *ObjtSection,
|
||||
) (
|
||||
err error,
|
||||
) {
|
||||
for {
|
||||
// if we've left the block, stop parsing
|
||||
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
||||
if parser.token.Value().(int) != 1 { return }
|
||||
|
||||
// add member to object section
|
||||
var member ObjtMember
|
||||
member, err = parser.parseObjtMember()
|
||||
into.members = append(into.members, member)
|
||||
if err != nil { return }
|
||||
}
|
||||
}
|
||||
|
||||
// parseObjtMember parses a single member of an object section. Indentation
|
||||
// level is assumed.
|
||||
func (parser *ParsingOperation) parseObjtMember () (
|
||||
member ObjtMember,
|
||||
err error,
|
||||
) {
|
||||
// get permission
|
||||
err = parser.nextToken(lexer.TokenKindPermission)
|
||||
if err != nil { return }
|
||||
member.permission = parser.token.Value().(types.Permission)
|
||||
|
||||
// get name
|
||||
err = parser.nextToken(lexer.TokenKindName)
|
||||
if err != nil { return }
|
||||
member.name = parser.token.Value().(string)
|
||||
|
||||
// get type
|
||||
err = parser.nextToken(lexer.TokenKindColon)
|
||||
if err != nil { return }
|
||||
err = parser.nextToken()
|
||||
if err != nil { return }
|
||||
member.what, err = parser.parseType()
|
||||
if err != nil { return }
|
||||
|
||||
// if there is a bit width, get it
|
||||
if parser.token.Is(lexer.TokenKindBinaryAnd) {
|
||||
err = parser.nextToken(lexer.TokenKindUInt)
|
||||
if err != nil { return }
|
||||
member.bitWidth = parser.token.Value().(uint64)
|
||||
err = parser.nextToken()
|
||||
if err != nil { return }
|
||||
}
|
||||
|
||||
// parse default value
|
||||
if parser.token.Is(lexer.TokenKindNewline) {
|
||||
err = parser.nextToken()
|
||||
if err != nil { return }
|
||||
|
||||
member.value,
|
||||
err = parser.parseInitializationValues(1)
|
||||
if err != nil { return }
|
||||
} else {
|
||||
member.value, err = parser.parseArgument()
|
||||
if err != nil { return }
|
||||
|
||||
err = parser.expect(lexer.TokenKindNewline)
|
||||
if err != nil { return }
|
||||
err = parser.nextToken()
|
||||
if err != nil { return }
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
//
|
||||
// import "git.tebibyte.media/arf/arf/types"
|
||||
// import "git.tebibyte.media/arf/arf/lexer"
|
||||
// import "git.tebibyte.media/arf/arf/infoerr"
|
||||
//
|
||||
// // parseObjtSection parses an object type definition. This allows for structured
|
||||
// // types to be defined, and for member variables to be added and overridden.
|
||||
// func (parser *ParsingOperation) parseObjtSection () (
|
||||
// section ObjtSection,
|
||||
// err error,
|
||||
// ) {
|
||||
// err = parser.expect(lexer.TokenKindName)
|
||||
// if err != nil { return }
|
||||
//
|
||||
// section.location = parser.token.Location()
|
||||
//
|
||||
// // get permission
|
||||
// err = parser.nextToken(lexer.TokenKindPermission)
|
||||
// if err != nil { return }
|
||||
// section.permission = parser.token.Value().(types.Permission)
|
||||
//
|
||||
// // get name
|
||||
// err = parser.nextToken(lexer.TokenKindName)
|
||||
// if err != nil { return }
|
||||
// section.name = parser.token.Value().(string)
|
||||
//
|
||||
// // parse inherited type
|
||||
// err = parser.nextToken(lexer.TokenKindColon)
|
||||
// if err != nil { return }
|
||||
// err = parser.nextToken()
|
||||
// if err != nil { return }
|
||||
// section.inherits, err = parser.parseIdentifier()
|
||||
// if err != nil { return }
|
||||
// err = parser.expect(lexer.TokenKindNewline)
|
||||
// if err != nil { return }
|
||||
// err = parser.nextToken()
|
||||
// if err != nil { return }
|
||||
//
|
||||
// // parse members
|
||||
// err = parser.parseObjtMembers(§ion)
|
||||
// if err != nil { return }
|
||||
//
|
||||
// if len(section.members) == 0 {
|
||||
// infoerr.NewError (
|
||||
// section.location,
|
||||
// "defining an object with no members",
|
||||
// infoerr.ErrorKindWarn).Print()
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // parseObjtMembers parses a list of members for an object section. Indentation
|
||||
// // level is assumed.
|
||||
// func (parser *ParsingOperation) parseObjtMembers (
|
||||
// into *ObjtSection,
|
||||
// ) (
|
||||
// err error,
|
||||
// ) {
|
||||
// for {
|
||||
// // if we've left the block, stop parsing
|
||||
// if !parser.token.Is(lexer.TokenKindIndent) { return }
|
||||
// if parser.token.Value().(int) != 1 { return }
|
||||
//
|
||||
// // add member to object section
|
||||
// var member ObjtMember
|
||||
// member, err = parser.parseObjtMember()
|
||||
// into.members = append(into.members, member)
|
||||
// if err != nil { return }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // parseObjtMember parses a single member of an object section. Indentation
|
||||
// // level is assumed.
|
||||
// func (parser *ParsingOperation) parseObjtMember () (
|
||||
// member ObjtMember,
|
||||
// err error,
|
||||
// ) {
|
||||
// // get permission
|
||||
// err = parser.nextToken(lexer.TokenKindPermission)
|
||||
// if err != nil { return }
|
||||
// member.permission = parser.token.Value().(types.Permission)
|
||||
//
|
||||
// // get name
|
||||
// err = parser.nextToken(lexer.TokenKindName)
|
||||
// if err != nil { return }
|
||||
// member.name = parser.token.Value().(string)
|
||||
//
|
||||
// // get type
|
||||
// err = parser.nextToken(lexer.TokenKindColon)
|
||||
// if err != nil { return }
|
||||
// err = parser.nextToken()
|
||||
// if err != nil { return }
|
||||
// member.what, err = parser.parseType()
|
||||
// if err != nil { return }
|
||||
//
|
||||
// // if there is a bit width, get it
|
||||
// if parser.token.Is(lexer.TokenKindBinaryAnd) {
|
||||
// err = parser.nextToken(lexer.TokenKindUInt)
|
||||
// if err != nil { return }
|
||||
// member.bitWidth = parser.token.Value().(uint64)
|
||||
// err = parser.nextToken()
|
||||
// if err != nil { return }
|
||||
// }
|
||||
//
|
||||
// // parse default value
|
||||
// if parser.token.Is(lexer.TokenKindNewline) {
|
||||
// err = parser.nextToken()
|
||||
// if err != nil { return }
|
||||
//
|
||||
// member.value,
|
||||
// err = parser.parseInitializationValues(1)
|
||||
// if err != nil { return }
|
||||
// } else {
|
||||
// member.value, err = parser.parseArgument()
|
||||
// if err != nil { return }
|
||||
//
|
||||
// err = parser.expect(lexer.TokenKindNewline)
|
||||
// if err != nil { return }
|
||||
// err = parser.nextToken()
|
||||
// if err != nil { return }
|
||||
// }
|
||||
//
|
||||
// return
|
||||
// }
|
||||
|
@ -257,7 +257,7 @@ func (argument Argument) ToString (indent int, breakLine bool) (output string) {
|
||||
func (section DataSection) ToString (indent int) (output string) {
|
||||
output += doIndent (
|
||||
indent,
|
||||
"data ",
|
||||
"type ",
|
||||
section.permission.ToString(), " ",
|
||||
section.name, ":",
|
||||
section.what.ToString())
|
||||
@ -265,22 +265,43 @@ func (section DataSection) ToString (indent int) (output string) {
|
||||
isComplexInitialization :=
|
||||
section.value.kind == ArgumentKindObjectInitializationValues ||
|
||||
section.value.kind == ArgumentKindArrayInitializationValues
|
||||
|
||||
if !isComplexInitialization && section.value.value != nil {
|
||||
output += " " + section.value.ToString(0, false)
|
||||
}
|
||||
output += "\n"
|
||||
|
||||
for _, member := range section.what.members {
|
||||
output += member.ToString(indent + 1)
|
||||
}
|
||||
|
||||
if isComplexInitialization {
|
||||
output += section.value.ToString(indent + 1, true)
|
||||
}
|
||||
|
||||
if section.external {
|
||||
output += "\n"
|
||||
output += doIndent(indent + 1, "external\n")
|
||||
} else if section.value.value == nil {
|
||||
output += "\n"
|
||||
} else if isComplexInitialization {
|
||||
output += "\n"
|
||||
output += section.value.ToString(indent + 1, true)
|
||||
} else {
|
||||
output += " " + section.value.ToString(0, false)
|
||||
output += "\n"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (member TypeMember) ToString (indent int) (output string) {
|
||||
output += doIndent(indent)
|
||||
|
||||
output += member.permission.ToString() + " "
|
||||
output += member.name + ":"
|
||||
output += member.what.ToString()
|
||||
|
||||
if member.bitWidth > 0 {
|
||||
output += fmt.Sprint(" & ", member.bitWidth)
|
||||
}
|
||||
|
||||
output += "\n"
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (section TypeSection) ToString (indent int) (output string) {
|
||||
output += doIndent (
|
||||
indent,
|
||||
@ -292,61 +313,23 @@ func (section TypeSection) ToString (indent int) (output string) {
|
||||
isComplexInitialization :=
|
||||
section.value.kind == ArgumentKindObjectInitializationValues ||
|
||||
section.value.kind == ArgumentKindArrayInitializationValues
|
||||
|
||||
if section.value.value == nil {
|
||||
output += "\n"
|
||||
} else if isComplexInitialization {
|
||||
output += "\n"
|
||||
output += section.value.ToString(indent + 1, true)
|
||||
} else {
|
||||
|
||||
if !isComplexInitialization && section.value.value != nil {
|
||||
output += " " + section.value.ToString(0, false)
|
||||
output += "\n"
|
||||
}
|
||||
return
|
||||
}
|
||||
output += "\n"
|
||||
|
||||
func (member ObjtMember) ToString (indent int) (output string) {
|
||||
output += doIndent(indent)
|
||||
|
||||
output += member.permission.ToString() + " "
|
||||
output += member.name + ":"
|
||||
output += member.what.ToString()
|
||||
|
||||
if member.bitWidth > 0 {
|
||||
output += fmt.Sprint(" & ", member.bitWidth)
|
||||
}
|
||||
|
||||
isComplexInitialization :=
|
||||
member.value.kind == ArgumentKindObjectInitializationValues ||
|
||||
member.value.kind == ArgumentKindArrayInitializationValues
|
||||
|
||||
if member.value.value == nil {
|
||||
output += "\n"
|
||||
} else if isComplexInitialization {
|
||||
output += "\n"
|
||||
output += member.value.ToString(indent + 1, true)
|
||||
} else {
|
||||
output += " " + member.value.ToString(0, false)
|
||||
output += "\n"
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (section ObjtSection) ToString (indent int) (output string) {
|
||||
output += doIndent (
|
||||
indent,
|
||||
"objt ",
|
||||
section.permission.ToString(), " ",
|
||||
section.name, ":",
|
||||
section.inherits.ToString(), "\n")
|
||||
|
||||
for _, member := range section.members {
|
||||
for _, member := range section.what.members {
|
||||
output += member.ToString(indent + 1)
|
||||
}
|
||||
|
||||
if isComplexInitialization {
|
||||
output += section.value.ToString(indent + 1, true)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
func (section EnumSection) ToString (indent int) (output string) {
|
||||
output += doIndent (
|
||||
indent,
|
||||
@ -410,6 +393,7 @@ func (phrase Phrase) ToString (indent int, ownLine bool) (output string) {
|
||||
}
|
||||
|
||||
var initializationValues Argument
|
||||
var declaration Argument
|
||||
|
||||
output += "[" + phrase.command.ToString(0, false)
|
||||
for _, argument := range phrase.arguments {
|
||||
@ -418,6 +402,8 @@ func (phrase Phrase) ToString (indent int, ownLine bool) (output string) {
|
||||
argument.kind == ArgumentKindArrayInitializationValues
|
||||
if isInitializationValue {
|
||||
initializationValues = argument
|
||||
} else if argument.kind == ArgumentKindDeclaration {
|
||||
declaration = argument
|
||||
} else {
|
||||
output += " " + argument.ToString(0, false)
|
||||
}
|
||||
@ -433,6 +419,13 @@ func (phrase Phrase) ToString (indent int, ownLine bool) (output string) {
|
||||
|
||||
if ownLine {
|
||||
output += "\n"
|
||||
|
||||
// TODO: make = phrases special, have them carry a declaration
|
||||
// and argument and nothing else. somehow.
|
||||
for _, member := range declaration.value.(Declaration).what.members {
|
||||
output += member.ToString(indent + 1)
|
||||
}
|
||||
|
||||
if initializationValues.kind != ArgumentKindNil {
|
||||
output += initializationValues.ToString(indent + 1, true)
|
||||
}
|
||||
|
@ -47,11 +47,11 @@ type Identifier struct {
|
||||
type TypeKind int
|
||||
|
||||
const (
|
||||
// TypeKindBasic either means it's a primitive, or it inherits from
|
||||
// something.
|
||||
// TypeKindBasic means its a normal type and inherits from something.
|
||||
// Basic types can define new members on their parent types.
|
||||
TypeKindBasic TypeKind = iota
|
||||
|
||||
// TypeKindPointer means it's a pointer
|
||||
// TypeKindPointer means it's a pointer.
|
||||
TypeKindPointer
|
||||
|
||||
// TypeKindVariableArray means it's an array of variable length.
|
||||
|
@ -19,7 +19,6 @@ type ro dInitInherit:aBasic
|
||||
-- that 9384
|
||||
-- this 389
|
||||
type ro eInitAndDefine:aBasic
|
||||
-- this 389
|
||||
ro these:aBasic
|
||||
ro born:Int 4
|
||||
ro in:Int
|
||||
@ -27,6 +26,8 @@ type ro eInitAndDefine:aBasic
|
||||
9348
|
||||
92384
|
||||
92834
|
||||
-- this 389
|
||||
-- these
|
||||
-- this 98
|
||||
-- that 9384
|
||||
|
||||
|
Reference in New Issue
Block a user