From 767bf81b8549ec0c0913f584090f92c47613ffc5 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 13 Sep 2022 16:31:08 -0400 Subject: [PATCH] Lobbotomized the codebase --- parser/data.go | 37 ++----- parser/default-values.go | 180 ++++++++++++++++++++++++++++++++ parser/enum.go | 23 ++-- parser/func.go | 31 +----- parser/initialization-values.go | 179 ------------------------------- parser/misc.go | 68 ------------ parser/phrase.go | 12 --- parser/tree-tostring.go | 67 ++---------- parser/tree.go | 9 +- parser/type-notation.go | 72 +++++++++++++ parser/type.go | 21 +--- 11 files changed, 286 insertions(+), 413 deletions(-) create mode 100644 parser/default-values.go delete mode 100644 parser/initialization-values.go create mode 100644 parser/type-notation.go diff --git a/parser/data.go b/parser/data.go index 4a228e6..22fcdfb 100644 --- a/parser/data.go +++ b/parser/data.go @@ -35,39 +35,16 @@ func (parser *ParsingOperation) parseDataSection () ( return } - if parser.token.Is(lexer.TokenKindNewline) { - err = parser.nextToken() - if err != nil { return } - - // check if external - if !parser.token.Is(lexer.TokenKindIndent) { return } - if parser.token.Value().(int) != 1 { return } - - err = parser.nextToken() - if err != nil { return } - if parser.token.Is(lexer.TokenKindName) && - parser.token.Value().(string) == "external" { - - section.external = true - err = parser.nextToken(lexer.TokenKindNewline) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } - return - } - - // otherwise, parse initialization values - parser.previousToken() - section.value, err = parser.parseDefaultValues(0) - if err != nil { return } - } else { - section.value, err = parser.parseArgument() - if err != nil { return } - - err = parser.expect(lexer.TokenKindNewline) + // check if data is external + if parser.token.Is(lexer.TokenKindName) && + parser.token.Value().(string) == "external" { + + section.external = true + err = parser.nextToken(lexer.TokenKindNewline) if err != nil { return } err = parser.nextToken() if err != nil { return } + return } return } diff --git a/parser/default-values.go b/parser/default-values.go new file mode 100644 index 0000000..cc8b1df --- /dev/null +++ b/parser/default-values.go @@ -0,0 +1,180 @@ +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 +// +// // parsedefaultValues starts on the line after a = phrase, a data section, a +// // type section, or anywhere else where there can be a default value. It returns +// // a default value in the form of an argument, as well as any defined members +// // that it finds. +// func (parser *ParsingOperation) parseDefaultValues ( + // baseIndent int, +// ) ( + // argument Argument, + // members []TypeMember, + // err error, +// ) { + // // check if line is indented one more than baseIndent + // if !parser.token.Is(lexer.TokenKindIndent) { return } + // if parser.token.Value().(int) != baseIndent + 1 { return } +// + // argument.location = parser.token.Location() + // + // err = parser.nextToken() + // if err != nil { return } +// + // if parser.token.Is(lexer.TokenKindDot) { +// + // // object initialization + // parser.previousToken() + // var values ObjectDefaultValues + // values, err = parser.parseObjectdefaultValues() + // argument.kind = ArgumentKindObjectDefaultValues + // argument.value = values + // + // } else { + // + // // array initialization + // parser.previousToken() + // var values ArrayDefaultValues + // values, err = parser.parseArrayDefaultValues() + // argument.kind = ArgumentKindArrayDefaultValues + // argument.value = values + // } + // + // return +// } +// +// // parseObjectdefaultValues parses a list of object initialization +// // values until the indentation level drops. +// func (parser *ParsingOperation) parseObjectdefaultValues () ( + // defaultValues ObjectDefaultValues, + // err error, +// ) { + // defaultValues.attributes = make(map[string] Argument) +// + // baseIndent := 0 + // begin := true + // + // for { + // // if there is no indent we can just stop parsing + // if !parser.token.Is(lexer.TokenKindIndent) { break} + // indent := parser.token.Value().(int) + // + // if begin == true { + // defaultValues.location = parser.token.Location() + // baseIndent = indent + // begin = false + // } +// + // // do not parse any further if the indent has changed + // if indent != baseIndent { break } +// + // // move on to the beginning of the line, which must contain + // // a member initialization value + // err = parser.nextToken(lexer.TokenKindDot) + // if err != nil { return } + // err = parser.nextToken(lexer.TokenKindName) + // if err != nil { return } + // name := parser.token.Value().(string) +// + // // if the member has already been listed, throw an error + // _, exists := defaultValues.attributes[name] + // if exists { + // err = parser.token.NewError ( + // "duplicate member \"" + name + "\" in object " + + // "member initialization", + // infoerr.ErrorKindError) + // return + // } +// + // // parse the argument determining the member initialization + // // value + // err = parser.nextToken() + // if err != nil { return } + // var value Argument + // if parser.token.Is(lexer.TokenKindNewline) { + // + // // recurse + // err = parser.nextToken(lexer.TokenKindIndent) + // if err != nil { return } + // + // value, err = parser.parseDefaultValues(baseIndent) + // defaultValues.attributes[name] = value + // if err != nil { return } + // + // } else { +// + // // parse as normal argument + // value, err = parser.parseArgument() + // defaultValues.attributes[name] = value + // if err != nil { return } + // + // err = parser.expect(lexer.TokenKindNewline) + // if err != nil { return } + // err = parser.nextToken() + // if err != nil { return } + // } + // } + // + // return +// } +// +// // parseArrayDefaultValues parses a list of array initialization values until +// // the indentation lexel drops. +// func (parser *ParsingOperation) parseArrayDefaultValues () ( + // defaultValues ArrayDefaultValues, + // err error, +// ) { + // baseIndent := 0 + // begin := true + // + // for { + // // if there is no indent we can just stop parsing + // if !parser.token.Is(lexer.TokenKindIndent) { break} + // indent := parser.token.Value().(int) + // + // if begin == true { + // defaultValues.location = parser.token.Location() + // baseIndent = indent + // begin = false + // } +// + // // do not parse any further if the indent has changed + // if indent != baseIndent { break } +// + // // move on to the beginning of the line, which must contain + // // arguments + // err = parser.nextToken(validArgumentStartTokens...) + // if err != nil { return } +// + // for { + // // stop parsing this line and go on to the next if a + // // newline token is encountered + // if parser.token.Is(lexer.TokenKindNewline) { + // err = parser.nextToken() + // if err != nil { return } + // break + // } +// + // // otherwise, parse the argument + // var argument Argument + // argument, err = parser.parseArgument() + // if err != nil { return } + // defaultValues.values = append ( + // defaultValues.values, + // argument) + // } + // } +// + // return +// } diff --git a/parser/enum.go b/parser/enum.go index ea45b09..8c71e0c 100644 --- a/parser/enum.go +++ b/parser/enum.go @@ -73,22 +73,13 @@ func (parser *ParsingOperation) parseEnumMembers ( if err != nil { return } // parse default value - if parser.token.Is(lexer.TokenKindNewline) { - err = parser.nextToken() - if err != nil { return } + member.value, err = parser.parseArgument() + into.members = append(into.members, member) + if err != nil { return } - member.value, err = parser.parseDefaultValues(1) - into.members = append(into.members, member) - if err != nil { return } - } else { - member.value, err = parser.parseArgument() - into.members = append(into.members, member) - if err != nil { return } - - err = parser.expect(lexer.TokenKindNewline) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } - } + err = parser.expect(lexer.TokenKindNewline) + if err != nil { return } + err = parser.nextToken() + if err != nil { return } } } diff --git a/parser/func.go b/parser/func.go index 8619cc7..cc486a3 100644 --- a/parser/func.go +++ b/parser/func.go @@ -187,33 +187,10 @@ func (parser *ParsingOperation) parseFuncArguments ( output.what, err = parser.parseType() if err != nil { return } - // skip the default value if we are skimming - if parser.skimming { - err = parser.skipIndentLevel(2) - into.outputs = append(into.outputs, output) - return - } - - // parse default value - if parser.token.Is(lexer.TokenKindNewline) { - err = parser.nextToken() - if err != nil { return } - - output.value, err = - parser.parseDefaultValues(1) - into.outputs = append(into.outputs, output) - if err != nil { return } - } else { - output.value, err = - parser.parseArgument() - into.outputs = append(into.outputs, output) - if err != nil { return } - - err = parser.expect(lexer.TokenKindNewline) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } - } + parser.expect(lexer.TokenKindNewline) + if err != nil { return } + err = parser.nextToken() + if err != nil { return } } } } diff --git a/parser/initialization-values.go b/parser/initialization-values.go deleted file mode 100644 index 983f2ca..0000000 --- a/parser/initialization-values.go +++ /dev/null @@ -1,179 +0,0 @@ -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 - -// parsedefaultValues 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, -// it parses initialization values. -func (parser *ParsingOperation) parseDefaultValues ( - baseIndent int, -) ( - argument Argument, - err error, -) { - // check if line is indented one more than baseIndent - if !parser.token.Is(lexer.TokenKindIndent) { return } - if parser.token.Value().(int) != baseIndent + 1 { return } - - argument.location = parser.token.Location() - - err = parser.nextToken() - if err != nil { return } - - if parser.token.Is(lexer.TokenKindDot) { - - // object initialization - parser.previousToken() - var values ObjectDefaultValues - values, err = parser.parseObjectdefaultValues() - argument.kind = ArgumentKindObjectDefaultValues - argument.value = values - - } else { - - // array initialization - parser.previousToken() - var values ArrayDefaultValues - values, err = parser.parseArrayDefaultValues() - argument.kind = ArgumentKindArrayDefaultValues - argument.value = values - } - - return -} - -// parseObjectdefaultValues parses a list of object initialization -// values until the indentation level drops. -func (parser *ParsingOperation) parseObjectdefaultValues () ( - defaultValues ObjectDefaultValues, - err error, -) { - defaultValues.attributes = make(map[string] Argument) - - baseIndent := 0 - begin := true - - for { - // if there is no indent we can just stop parsing - if !parser.token.Is(lexer.TokenKindIndent) { break} - indent := parser.token.Value().(int) - - if begin == true { - defaultValues.location = parser.token.Location() - baseIndent = indent - begin = false - } - - // do not parse any further if the indent has changed - if indent != baseIndent { break } - - // move on to the beginning of the line, which must contain - // a member initialization value - err = parser.nextToken(lexer.TokenKindDot) - if err != nil { return } - err = parser.nextToken(lexer.TokenKindName) - if err != nil { return } - name := parser.token.Value().(string) - - // if the member has already been listed, throw an error - _, exists := defaultValues.attributes[name] - if exists { - err = parser.token.NewError ( - "duplicate member \"" + name + "\" in object " + - "member initialization", - infoerr.ErrorKindError) - return - } - - // parse the argument determining the member initialization - // value - err = parser.nextToken() - if err != nil { return } - var value Argument - if parser.token.Is(lexer.TokenKindNewline) { - - // recurse - err = parser.nextToken(lexer.TokenKindIndent) - if err != nil { return } - - value, err = parser.parseDefaultValues(baseIndent) - defaultValues.attributes[name] = value - if err != nil { return } - - } else { - - // parse as normal argument - value, err = parser.parseArgument() - defaultValues.attributes[name] = value - if err != nil { return } - - err = parser.expect(lexer.TokenKindNewline) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } - } - } - - return -} - -// parseArrayDefaultValues parses a list of array initialization values until -// the indentation lexel drops. -func (parser *ParsingOperation) parseArrayDefaultValues () ( - defaultValues ArrayDefaultValues, - err error, -) { - baseIndent := 0 - begin := true - - for { - // if there is no indent we can just stop parsing - if !parser.token.Is(lexer.TokenKindIndent) { break} - indent := parser.token.Value().(int) - - if begin == true { - defaultValues.location = parser.token.Location() - baseIndent = indent - begin = false - } - - // do not parse any further if the indent has changed - if indent != baseIndent { break } - - // move on to the beginning of the line, which must contain - // arguments - err = parser.nextToken(validArgumentStartTokens...) - if err != nil { return } - - for { - // stop parsing this line and go on to the next if a - // newline token is encountered - if parser.token.Is(lexer.TokenKindNewline) { - err = parser.nextToken() - if err != nil { return } - break - } - - // otherwise, parse the argument - var argument Argument - argument, err = parser.parseArgument() - if err != nil { return } - defaultValues.values = append ( - defaultValues.values, - argument) - } - } - - return -} diff --git a/parser/misc.go b/parser/misc.go index 52b3879..9d54782 100644 --- a/parser/misc.go +++ b/parser/misc.go @@ -1,74 +1,6 @@ package parser import "git.tebibyte.media/arf/arf/lexer" -import "git.tebibyte.media/arf/arf/infoerr" - -// parseType parses a type notation of the form Name, {Name}, etc. -func (parser *ParsingOperation) parseType () (what Type, err error) { - err = parser.expect(lexer.TokenKindName, lexer.TokenKindLBrace) - if err != nil { return } - what.location = parser.token.Location() - - if parser.token.Is(lexer.TokenKindLBrace) { - what.kind = TypeKindPointer - - err = parser.nextToken() - if err != nil { return } - - var points Type - points, err = parser.parseType() - if err != nil { return } - what.points = &points - - err = parser.expect ( - lexer.TokenKindRBrace, - lexer.TokenKindElipsis) - if err != nil { return } - - if parser.token.Is(lexer.TokenKindElipsis) { - what.kind = TypeKindVariableArray - - err = parser.nextToken(lexer.TokenKindRBrace) - if err != nil { return } - } - - err = parser.nextToken() - if err != nil { return } - } else { - what.name, err = parser.parseIdentifier() - if err != nil { return } - } - - for { - if !parser.token.Is(lexer.TokenKindColon) { break } - - err = parser.nextToken(lexer.TokenKindName, lexer.TokenKindUInt) - if err != nil { return } - - if parser.token.Is(lexer.TokenKindName) { - // parse type qualifier - qualifier := parser.token.Value().(string) - switch qualifier { - case "mut": - what.mutable = true - default: - err = parser.token.NewError ( - "unknown type qualifier \"" + - qualifier + "\"", - infoerr.ErrorKindError) - return - } - } else { - // parse fixed array length - what.length = parser.token.Value().(uint64) - } - - err = parser.nextToken() - if err != nil { return } - } - - return -} // parseIdentifier parses an identifier made out of dot separated names. func (parser *ParsingOperation) parseIdentifier () ( diff --git a/parser/phrase.go b/parser/phrase.go index 829ca49..f1c882f 100644 --- a/parser/phrase.go +++ b/parser/phrase.go @@ -213,18 +213,6 @@ func (parser *ParsingOperation) parseBlockLevelPhrase ( err = parser.nextToken() if err != nil { return } - // if this is a set phrase, parse initialization values under it - if phrase.kind == PhraseKindAssign { - var values Argument - values, err = parser.parseDefaultValues(indent) - - if values.kind != ArgumentKindNil { - phrase.arguments = append(phrase.arguments, values) - } - - return - } - // if this is a control flow phrase, parse block under it isControlFlow := false for _, kind := range controlFlowKinds { diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index 77c4885..6fa93b9 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -87,7 +87,8 @@ func (what Type) ToString () (output string) { if what.mutable { output += ":mut" } - + + // TODO: print out default value return } @@ -260,29 +261,7 @@ func (section DataSection) ToString (indent int) (output string) { "type ", section.permission.ToString(), " ", section.name, ":", - section.what.ToString()) - - isComplexInitialization := - section.value.kind == ArgumentKindObjectDefaultValues || - section.value.kind == ArgumentKindArrayDefaultValues - - 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") - } + section.what.ToString(), "\n") return } @@ -308,24 +287,7 @@ func (section TypeSection) ToString (indent int) (output string) { "type ", section.permission.ToString(), " ", section.name, ":", - section.what.ToString()) - - isComplexInitialization := - section.value.kind == ArgumentKindObjectDefaultValues || - section.value.kind == ArgumentKindArrayDefaultValues - - 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) - } + section.what.ToString(), "\n") return } @@ -443,24 +405,9 @@ func (block Block) ToString (indent int) (output string) { } func (funcOutput FuncOutput) ToString (indent int) (output string) { - output += doIndent(indent + 1, "< ", funcOutput.Declaration.ToString()) - - isComplexInitialization := - funcOutput.value.kind == ArgumentKindObjectDefaultValues || - funcOutput.value.kind == ArgumentKindArrayDefaultValues - - if !isComplexInitialization && funcOutput.value.value != nil { - output += " " + funcOutput.value.ToString(0, false) - } - output += "\n" - - for _, member := range funcOutput.what.members { - output += member.ToString(indent + 1) - } - - if isComplexInitialization { - output += funcOutput.value.ToString(indent + 1, true) - } + output += doIndent ( + indent + 1, + "< ", funcOutput.Declaration.ToString(), "\n") return } diff --git a/parser/tree.go b/parser/tree.go index b1548ea..c4f30bb 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -64,7 +64,6 @@ type TypeMember struct { nameable typeable permissionable - valuable bitWidth uint64 } @@ -84,7 +83,10 @@ type Type struct { points *Type // if non-nil, this type defines new members. - members []TypeMember + members []TypeMember + + // the default value of the type. + defaultValue Argument } // Declaration represents a variable declaration. @@ -180,7 +182,6 @@ type DataSection struct { nameable typeable permissionable - valuable external bool } @@ -191,7 +192,6 @@ type TypeSection struct { nameable typeable permissionable - valuable } // EnumMember represents a member of an enum section. @@ -270,7 +270,6 @@ type Block []Phrase // that it can have a default value. type FuncOutput struct { Declaration - valuable } // FuncSection represents a function section. diff --git a/parser/type-notation.go b/parser/type-notation.go new file mode 100644 index 0000000..c28bbe7 --- /dev/null +++ b/parser/type-notation.go @@ -0,0 +1,72 @@ +package parser + +import "git.tebibyte.media/arf/arf/lexer" +import "git.tebibyte.media/arf/arf/infoerr" + +// parseType parses a type notation of the form Name, {Name}, etc. +func (parser *ParsingOperation) parseType () (what Type, err error) { + err = parser.expect(lexer.TokenKindName, lexer.TokenKindLBrace) + if err != nil { return } + what.location = parser.token.Location() + + if parser.token.Is(lexer.TokenKindLBrace) { + what.kind = TypeKindPointer + + err = parser.nextToken() + if err != nil { return } + + var points Type + points, err = parser.parseType() + if err != nil { return } + what.points = &points + + err = parser.expect ( + lexer.TokenKindRBrace, + lexer.TokenKindElipsis) + if err != nil { return } + + if parser.token.Is(lexer.TokenKindElipsis) { + what.kind = TypeKindVariableArray + + err = parser.nextToken(lexer.TokenKindRBrace) + if err != nil { return } + } + + err = parser.nextToken() + if err != nil { return } + } else { + what.name, err = parser.parseIdentifier() + if err != nil { return } + } + + for { + if !parser.token.Is(lexer.TokenKindColon) { break } + + err = parser.nextToken(lexer.TokenKindName, lexer.TokenKindUInt) + if err != nil { return } + + if parser.token.Is(lexer.TokenKindName) { + // parse type qualifier + qualifier := parser.token.Value().(string) + switch qualifier { + case "mut": + what.mutable = true + default: + err = parser.token.NewError ( + "unknown type qualifier \"" + + qualifier + "\"", + infoerr.ErrorKindError) + return + } + } else { + // parse fixed array length + what.length = parser.token.Value().(uint64) + } + + err = parser.nextToken() + if err != nil { return } + } + + return +} + diff --git a/parser/type.go b/parser/type.go index 34ed380..14c8aa6 100644 --- a/parser/type.go +++ b/parser/type.go @@ -32,21 +32,10 @@ func (parser *ParsingOperation) parseTypeSection () ( section.what, err = parser.parseType() if err != nil { return } - // parse default values - if parser.token.Is(lexer.TokenKindNewline) { - err = parser.nextToken() - if err != nil { return } - - section.value, err = parser.parseDefaultValues(0) - if err != nil { return } - } else { - section.value, err = parser.parseArgument() - if err != nil { return } - - err = parser.expect(lexer.TokenKindNewline) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } - } + parser.expect(lexer.TokenKindNewline) + if err != nil { return } + err = parser.nextToken() + if err != nil { return } + return }