From 2295e3bd32ca7ef24b6a0b4233fb622e5f588663 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 14:09:47 -0400 Subject: [PATCH 1/6] Data sections can now be marked as external --- parser/accessors.go | 6 ++++++ parser/data.go | 19 +++++++++++++++++++ parser/data_test.go | 4 +++- parser/tree-tostring.go | 5 ++++- parser/tree.go | 2 ++ tests/parser/data/main.arf | 5 ++++- 6 files changed, 38 insertions(+), 3 deletions(-) diff --git a/parser/accessors.go b/parser/accessors.go index 42b378b..776a387 100644 --- a/parser/accessors.go +++ b/parser/accessors.go @@ -197,6 +197,12 @@ func (section FaceSection) Behaviors () (iterator types.Iterator[FaceBehavior]) return } +// External returns whether or not the data section is external. +func (section DataSection) External () (external bool) { + external = section.external + return +} + // Kind returns what kind of phrase it is. func (phrase Phrase) Kind () (kind PhraseKind) { kind = phrase.kind diff --git a/parser/data.go b/parser/data.go index fa19ff0..0643697 100644 --- a/parser/data.go +++ b/parser/data.go @@ -32,6 +32,25 @@ func (parser *ParsingOperation) parseDataSection () ( 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.parseInitializationValues(0) if err != nil { return } } else { diff --git a/parser/data_test.go b/parser/data_test.go index 08c77d9..aa01b6b 100644 --- a/parser/data_test.go +++ b/parser/data_test.go @@ -35,7 +35,9 @@ data ro kNestedObject:Obj .bird0 324 .bird1 "hello world" data ro lMutIntegerArray16:Int:16:mut -data ro mIntegerArrayInitialized:Int:16:mut +data ro mExternalData:Int:8 + external +data ro nIntegerArrayInitialized:Int:16:mut 3948 293 293049 diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index 220cd7b..28c09ff 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -266,7 +266,10 @@ func (section DataSection) ToString (indent int) (output string) { section.value.kind == ArgumentKindObjectInitializationValues || section.value.kind == ArgumentKindArrayInitializationValues - if section.value.value == nil { + if section.external { + output += "\n" + output += doIndent(indent + 1, "external\n") + } else if section.value.value == nil { output += "\n" } else if isComplexInitialization { output += "\n" diff --git a/parser/tree.go b/parser/tree.go index 3b5f77c..aace30b 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -169,6 +169,8 @@ type DataSection struct { typeable permissionable valuable + + external bool } // TypeSection represents a blind type definition. diff --git a/tests/parser/data/main.arf b/tests/parser/data/main.arf index b850361..55833af 100644 --- a/tests/parser/data/main.arf +++ b/tests/parser/data/main.arf @@ -38,6 +38,9 @@ data ro kNestedObject:Obj data ro lMutIntegerArray16:Int:16:mut -data ro mIntegerArrayInitialized:Int:16:mut +data ro mExternalData:Int:8 + external + +data ro nIntegerArrayInitialized:Int:16:mut 3948 293 293049 948 912 340 0 2304 0 4785 92 From cc1eaa2c111d3f7b5e72f5c03793c017ff7aa3a2 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 14:52:37 -0400 Subject: [PATCH 2/6] Add test case for skimming --- parser/data_test.go | 2 +- parser/enum_test.go | 2 +- parser/func_test.go | 2 +- parser/meta_test.go | 2 +- parser/objt_test.go | 2 +- parser/skim_test.go | 27 +++++++++++++++++++++++++++ parser/test-common.go | 4 ++-- parser/type_test.go | 2 +- tests/parser/skim/main.arf | 38 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 parser/skim_test.go create mode 100644 tests/parser/skim/main.arf diff --git a/parser/data_test.go b/parser/data_test.go index aa01b6b..f6b7df3 100644 --- a/parser/data_test.go +++ b/parser/data_test.go @@ -3,7 +3,7 @@ package parser import "testing" func TestData (test *testing.T) { - checkTree ("../tests/parser/data", + checkTree ("../tests/parser/data", false, `:arf --- data ro aInteger:Int 3202 diff --git a/parser/enum_test.go b/parser/enum_test.go index a695dfc..bdc57e5 100644 --- a/parser/enum_test.go +++ b/parser/enum_test.go @@ -3,7 +3,7 @@ package parser import "testing" func TestEnum (test *testing.T) { - checkTree ("../tests/parser/enum", + checkTree ("../tests/parser/enum", false, `:arf --- enum ro AffrontToGod:Int:4 diff --git a/parser/func_test.go b/parser/func_test.go index d7137b3..99facdf 100644 --- a/parser/func_test.go +++ b/parser/func_test.go @@ -3,7 +3,7 @@ package parser import "testing" func TestFunc (test *testing.T) { - checkTree ("../tests/parser/func", + checkTree ("../tests/parser/func", false, `:arf --- func ro aBasicExternal diff --git a/parser/meta_test.go b/parser/meta_test.go index 36cc1f4..6e6466f 100644 --- a/parser/meta_test.go +++ b/parser/meta_test.go @@ -3,7 +3,7 @@ package parser import "testing" func TestMeta (test *testing.T) { - checkTree ("../tests/parser/meta", + checkTree ("../tests/parser/meta", false, `:arf author "Sasha Koshka" license "GPLv3" diff --git a/parser/objt_test.go b/parser/objt_test.go index 3a6775a..eda582d 100644 --- a/parser/objt_test.go +++ b/parser/objt_test.go @@ -3,7 +3,7 @@ package parser import "testing" func TestObjt (test *testing.T) { - checkTree ("../tests/parser/objt", + checkTree ("../tests/parser/objt", false, `:arf --- objt ro Basic:Obj diff --git a/parser/skim_test.go b/parser/skim_test.go new file mode 100644 index 0000000..e00d48e --- /dev/null +++ b/parser/skim_test.go @@ -0,0 +1,27 @@ +package parser + +import "testing" + +func TestSkim (test *testing.T) { + checkTree ("../tests/parser/skim", true, +`:arf +--- +data ro aExternalData:Int + external +data ro bSingleValue:Int + external +data ro cNestedObject:Obj + external +data ro dUninitialized:Int:16:mut + external +data ro eIntegerArrayInitialized:Int:16:mut + external +func ro fComplexFunction + --- + external +func ro gExternalFunction + > x:Int + --- + external +`, test) +} diff --git a/parser/test-common.go b/parser/test-common.go index 8a8e918..918a86d 100644 --- a/parser/test-common.go +++ b/parser/test-common.go @@ -6,11 +6,11 @@ import "strings" import "testing" import "path/filepath" -func checkTree (modulePath string, correct string, test *testing.T) { +func checkTree (modulePath string, skim bool, correct string, test *testing.T) { cwd, _ := os.Getwd() modulePath = filepath.Join(cwd, modulePath) println(modulePath) - tree, err := Fetch(modulePath, false) + tree, err := Fetch(modulePath, skim) treeString := tree.ToString(0) treeRunes := []rune(treeString) diff --git a/parser/type_test.go b/parser/type_test.go index 44219bc..db05b06 100644 --- a/parser/type_test.go +++ b/parser/type_test.go @@ -3,7 +3,7 @@ package parser import "testing" func TestType (test *testing.T) { - checkTree ("../tests/parser/type", + checkTree ("../tests/parser/type", false, `:arf --- type ro Basic:Int diff --git a/tests/parser/skim/main.arf b/tests/parser/skim/main.arf new file mode 100644 index 0000000..d5d389f --- /dev/null +++ b/tests/parser/skim/main.arf @@ -0,0 +1,38 @@ +:arf +--- + +data ro aExternalData:Int + external + +data ro bSingleValue:Int 342 + +data ro cNestedObject:Obj + .this + .bird0 324 + .bird1 "hello world" + .that + .bird2 123.8439 + .bird3 9328.21348239 + +data ro dUninitialized:Int:16:mut + +data ro eIntegerArrayInitialized:Int:16:mut + 3948 293 293049 948 912 + 340 0 2304 0 4785 92 + +func ro fComplexFunction + --- + = x:Int 3 + = y:{Int} [loc x] + = z:Int:8 + 398 9 2309 983 -2387 + 478 555 123 + = bird:Bird + .that + .whenYou 99999 + .this 324 + +func ro gExternalFunction + > x:Int + --- + external From 613ccc3fba5316b4357e2a90f40ad2e22fd9ebd1 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 14:56:35 -0400 Subject: [PATCH 3/6] Add skipIndentLevel function --- parser/parser.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/parser/parser.go b/parser/parser.go index 43f344a..bb51097 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -150,3 +150,18 @@ func (parser *ParsingOperation) previousToken () { parser.token = parser.tokens[parser.tokenIndex] return } + +// skipIndentLevel advances the parser, ignoring every line with an indentation +// equal to or greater than the specified indent. +func (parser *ParsingOperation) skipIndentLevel (indent int) (err error) { + for { + err = parser.nextToken() + if err != nil { return } + + if parser.token.Is(lexer.TokenKindIndent) && + parser.token.Value().(int) < indent { + + return + } + } +} From ae0166b509fadb672593a2404ca7d1d708a966c4 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 15:04:39 -0400 Subject: [PATCH 4/6] Data section initialization values are now skimmed over --- parser/data.go | 7 +++++++ parser/parser.go | 19 +++++++++++++------ parser/test-common.go | 1 - 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/parser/data.go b/parser/data.go index 0643697..52cb5e3 100644 --- a/parser/data.go +++ b/parser/data.go @@ -28,6 +28,13 @@ func (parser *ParsingOperation) parseDataSection () ( section.what, err = parser.parseType() if err != nil { return } + // skip the rest of the section if we are only skimming it + if parser.skimming { + section.external = true + err = parser.skipIndentLevel(1) + return + } + if parser.token.Is(lexer.TokenKindNewline) { err = parser.nextToken() if err != nil { return } diff --git a/parser/parser.go b/parser/parser.go index bb51097..672efea 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -13,6 +13,7 @@ type ParsingOperation struct { token lexer.Token tokens []lexer.Token tokenIndex int + skimming bool tree SyntaxTree } @@ -42,6 +43,7 @@ func Fetch (modulePath string, skim bool) (tree SyntaxTree, err error) { // miss, so parse the module. parser := ParsingOperation { modulePath: modulePath, + skimming: skim, tree: SyntaxTree { sections: make(map[string] Section), }, @@ -155,13 +157,18 @@ func (parser *ParsingOperation) previousToken () { // equal to or greater than the specified indent. func (parser *ParsingOperation) skipIndentLevel (indent int) (err error) { for { + if parser.token.Is(lexer.TokenKindNewline) { + err = parser.nextToken() + if err != nil { return } + + if !parser.token.Is(lexer.TokenKindIndent) || + parser.token.Value().(int) < indent { + + return + } + } + err = parser.nextToken() if err != nil { return } - - if parser.token.Is(lexer.TokenKindIndent) && - parser.token.Value().(int) < indent { - - return - } } } diff --git a/parser/test-common.go b/parser/test-common.go index 918a86d..80ee2fd 100644 --- a/parser/test-common.go +++ b/parser/test-common.go @@ -9,7 +9,6 @@ import "path/filepath" func checkTree (modulePath string, skim bool, correct string, test *testing.T) { cwd, _ := os.Getwd() modulePath = filepath.Join(cwd, modulePath) - println(modulePath) tree, err := Fetch(modulePath, skim) treeString := tree.ToString(0) From d6db27ccb622f023408d220041483ec2955ce3b1 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 15:06:22 -0400 Subject: [PATCH 5/6] Function root blocks are now skimmed over --- parser/func.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/parser/func.go b/parser/func.go index f6288f5..6b5f5c2 100644 --- a/parser/func.go +++ b/parser/func.go @@ -32,6 +32,13 @@ func (parser *ParsingOperation) parseFuncSection () ( err = parser.parseFuncArguments(§ion) if err != nil { return } + // skip the rest of the section if we are only skimming it + if parser.skimming { + section.external = true + err = parser.skipIndentLevel(1) + return + } + // check to see if the function is external if !parser.token.Is(lexer.TokenKindIndent) { return } if parser.token.Value().(int) != 1 { return } From d4d7c038302b130860cb5088c4df3ef017325564 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 5 Sep 2022 15:13:27 -0400 Subject: [PATCH 6/6] Function output default values are skimmed over --- parser/func.go | 7 +++++++ parser/skim_test.go | 1 + tests/parser/skim/main.arf | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/parser/func.go b/parser/func.go index 6b5f5c2..1722d9f 100644 --- a/parser/func.go +++ b/parser/func.go @@ -186,6 +186,13 @@ func (parser *ParsingOperation) parseFuncArguments ( if err != nil { return } 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) { diff --git a/parser/skim_test.go b/parser/skim_test.go index e00d48e..44eed97 100644 --- a/parser/skim_test.go +++ b/parser/skim_test.go @@ -21,6 +21,7 @@ func ro fComplexFunction external func ro gExternalFunction > x:Int + < arr:Int --- external `, test) diff --git a/tests/parser/skim/main.arf b/tests/parser/skim/main.arf index d5d389f..eefef04 100644 --- a/tests/parser/skim/main.arf +++ b/tests/parser/skim/main.arf @@ -34,5 +34,9 @@ func ro fComplexFunction func ro gExternalFunction > x:Int + < arr:Int 5 + 34908 + 39 3498 + 38 219 --- external