From cd9de16338a1d816c426683b1c88eda148fe2bc3 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 27 Sep 2022 15:48:47 -0400 Subject: [PATCH] Removed previous code from type-notation.go --- parser/tree.go | 4 +- parser/type-notation.go | 240 +------------------------------------ tests/parser/data/main.arf | 6 + tests/parser/type/main.arf | 2 +- 4 files changed, 11 insertions(+), 241 deletions(-) diff --git a/parser/tree.go b/parser/tree.go index cb3a8a2..0ef5fd3 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -230,7 +230,9 @@ type Phrase struct { multiValuable kind PhraseKind - + + // TODO: do not have this be an argument. make a string version, and + // and identifier version. command Argument // only applicable for PhraseKindOperator diff --git a/parser/type-notation.go b/parser/type-notation.go index 945a08f..a97213a 100644 --- a/parser/type-notation.go +++ b/parser/type-notation.go @@ -48,11 +48,7 @@ func (parser *ParsingOperation) parseType () (what Type, err error) { err = parser.skipWhitespace() if err != nil { return } - err = parser.expect( - lexer.TokenKindName, - lexer.TokenKindUInt, - lexer.TokenKindLParen, - lexer.TokenKindLessThan) + err = parser.expect(lexer.TokenKindName, lexer.TokenKindUInt) if err != nil { return } if parser.token.Is(lexer.TokenKindName) { @@ -77,242 +73,8 @@ func (parser *ParsingOperation) parseType () (what Type, err error) { err = parser.nextToken() if err != nil { return } - } else if parser.token.Is(lexer.TokenKindLessThan) { - // parse default value - what.defaultValue, err = parser.parseBasicDefaultValue() - if err != nil { return } - - } else if parser.token.Is(lexer.TokenKindLParen) { - // parse members and member default values - what.defaultValue, - what.members, - err = parser.parseObjectDefaultValueAndMembers() - if err != nil { return } } } return } - -// parseBasicDefaultValue parses a default value of a non-object type. -func (parser *ParsingOperation) parseBasicDefaultValue () ( - value Argument, - err error, -) { - value.location = parser.token.Location() - - err = parser.expect(lexer.TokenKindLessThan) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } - - var attributes []Argument - - defer func () { - // if we have multiple values, we need to return the full array - // instead. - if len(attributes) > 1 { - value.kind = ArgumentKindArrayDefaultValues - value.value = ArrayDefaultValues(attributes) - } - } () - - for { - err = parser.skipWhitespace() - if err != nil { return } - if parser.token.Is(lexer.TokenKindGreaterThan) { break } - - value, err = parser.parseArgument() - if err != nil { return } - attributes = append(attributes, value) - } - - err = parser.nextToken() - if err != nil { return } - - return -} - -// parseObjectDefaultValueAndMembers parses default values and new members of an -// object type. -func (parser *ParsingOperation) parseObjectDefaultValueAndMembers () ( - value Argument, - members []TypeMember, - err error, -) { - value.location = parser.token.Location() - - err = parser.expect(lexer.TokenKindLParen) - if err != nil { return } - parser.nextToken() - if err != nil { return } - - var attributes ObjectDefaultValues - - for { - err = parser.skipWhitespace() - if err != nil { return } - if parser.token.Is(lexer.TokenKindRParen) { break } - - err = parser.expect(lexer.TokenKindDot) - if err != nil { return } - parser.nextToken(lexer.TokenKindName, lexer.TokenKindPermission) - if err != nil { return } - - if parser.token.Is(lexer.TokenKindName) { - // parsing a defalut value for an inherited member - var memberName string - var memberValue Argument - - memberName, - memberValue, err = parser.parseObjectInheritedMember() - if err != nil { return } - - if value.kind == ArgumentKindNil { - // create default value map if it doesn't - // already exist - value.kind = ArgumentKindObjectDefaultValues - attributes = make(ObjectDefaultValues) - value.value = attributes - } - - // TODO: error on duplicate - if memberValue.kind != ArgumentKindNil { - attributes[memberName] = memberValue - } - - } else if parser.token.Is(lexer.TokenKindPermission) { - // parsing a member declaration - var member TypeMember - member, err = parser.parseObjectNewMember() - - // TODO: error on duplicate - members = append(members, member) - if err != nil { return } - } - } - - err = parser.nextToken() - if err != nil { return } - - return -} - -// parseObjectDefaultValue parses member default values only, and will throw an -// error when it encounteres a new member definition. -func (parser *ParsingOperation) parseObjectDefaultValue () ( - value Argument, - err error, -) { - value.location = parser.token.Location() - - err = parser.expect(lexer.TokenKindLParen) - if err != nil { return } - parser.nextToken() - if err != nil { return } - - var attributes ObjectDefaultValues - - for { - err = parser.skipWhitespace() - if err != nil { return } - if parser.token.Is(lexer.TokenKindRParen) { break } - - err = parser.expect(lexer.TokenKindDot) - if err != nil { return } - parser.nextToken(lexer.TokenKindName) - if err != nil { return } - - if value.kind == ArgumentKindNil { - value.kind = ArgumentKindObjectDefaultValues - attributes = make(ObjectDefaultValues) - value.value = attributes - } - - var memberName string - var memberValue Argument - memberName, - memberValue, err = parser.parseObjectInheritedMember() - - attributes[memberName] = memberValue - } - - err = parser.nextToken() - if err != nil { return } - - return -} - -// .name: - -// parseObjectInheritedMember parses a new default value for an inherited -// member. -func (parser *ParsingOperation) parseObjectInheritedMember () ( - name string, - value Argument, - err error, -) { - // get the name of the inherited member - err = parser.expect(lexer.TokenKindName) - value.location = parser.token.Location() - if err != nil { return } - name = parser.token.Value().(string) - - // we require a default value to be present - err = parser.nextToken(lexer.TokenKindColon) - if err != nil { return } - err = parser.nextToken(lexer.TokenKindLParen, lexer.TokenKindLessThan) - if err != nil { return } - - if parser.token.Is(lexer.TokenKindLessThan) { - // parse default value - value, err = parser.parseBasicDefaultValue() - if err != nil { return } - - } else if parser.token.Is(lexer.TokenKindLParen) { - // parse member default values - value, err = parser.parseObjectDefaultValue() - if err != nil { return } - } - - return -} - -// .ro name:Type:qualifier: - -// parseObjectNewMember parses an object member declaration, and its -// default value if it exists. -func (parser *ParsingOperation) parseObjectNewMember () ( - member TypeMember, - err error, -) { - // get member permission - err = parser.expect(lexer.TokenKindPermission) - member.location = parser.token.Location() - if err != nil { return } - member.permission = parser.token.Value().(types.Permission) - - // get member 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(lexer.TokenKindName, lexer.TokenKindLBrace) - if err != nil { return } - member.what, err = parser.parseType() - if err != nil { return } - - // get bit width - 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 } - } - - return -} diff --git a/tests/parser/data/main.arf b/tests/parser/data/main.arf index bd204ba..7f6c767 100644 --- a/tests/parser/data/main.arf +++ b/tests/parser/data/main.arf @@ -26,6 +26,12 @@ data ro jObject:Obj (324 438) +# TODO: at some point, have this syntax for object literals. terminate members +# with newlines. +# data ro jObject:Bird ( + # .this 324 + # .that 438) + data ro lMutIntegerArray16:Int:16:mut data ro mExternalData:Int:8 diff --git a/tests/parser/type/main.arf b/tests/parser/type/main.arf index 611adf9..7236678 100644 --- a/tests/parser/type/main.arf +++ b/tests/parser/type/main.arf @@ -8,7 +8,7 @@ type ro bBitFields:Obj ro that:Int & 1 ro this:Int 298 & 24 -type ro cInit:Obj:( +type ro cInit:Obj ro that:String "hello world" ro this:Int 23