Merge pull request 'parser-skim' (#5) from parser-skim into main
Reviewed-on: arf/arf#5
This commit is contained in:
commit
7f42ccac48
@ -197,6 +197,12 @@ func (section FaceSection) Behaviors () (iterator types.Iterator[FaceBehavior])
|
|||||||
return
|
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.
|
// Kind returns what kind of phrase it is.
|
||||||
func (phrase Phrase) Kind () (kind PhraseKind) {
|
func (phrase Phrase) Kind () (kind PhraseKind) {
|
||||||
kind = phrase.kind
|
kind = phrase.kind
|
||||||
|
@ -28,10 +28,36 @@ func (parser *ParsingOperation) parseDataSection () (
|
|||||||
section.what, err = parser.parseType()
|
section.what, err = parser.parseType()
|
||||||
if err != nil { return }
|
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) {
|
if parser.token.Is(lexer.TokenKindNewline) {
|
||||||
err = parser.nextToken()
|
err = parser.nextToken()
|
||||||
if err != nil { return }
|
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)
|
section.value, err = parser.parseInitializationValues(0)
|
||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
} else {
|
} else {
|
||||||
|
@ -3,7 +3,7 @@ package parser
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestData (test *testing.T) {
|
func TestData (test *testing.T) {
|
||||||
checkTree ("../tests/parser/data",
|
checkTree ("../tests/parser/data", false,
|
||||||
`:arf
|
`:arf
|
||||||
---
|
---
|
||||||
data ro aInteger:Int 3202
|
data ro aInteger:Int 3202
|
||||||
@ -35,7 +35,9 @@ data ro kNestedObject:Obj
|
|||||||
.bird0 324
|
.bird0 324
|
||||||
.bird1 "hello world"
|
.bird1 "hello world"
|
||||||
data ro lMutIntegerArray16:Int:16:mut
|
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
|
3948
|
||||||
293
|
293
|
||||||
293049
|
293049
|
||||||
|
@ -3,7 +3,7 @@ package parser
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestEnum (test *testing.T) {
|
func TestEnum (test *testing.T) {
|
||||||
checkTree ("../tests/parser/enum",
|
checkTree ("../tests/parser/enum", false,
|
||||||
`:arf
|
`:arf
|
||||||
---
|
---
|
||||||
enum ro AffrontToGod:Int:4
|
enum ro AffrontToGod:Int:4
|
||||||
|
@ -32,6 +32,13 @@ func (parser *ParsingOperation) parseFuncSection () (
|
|||||||
err = parser.parseFuncArguments(§ion)
|
err = parser.parseFuncArguments(§ion)
|
||||||
if err != nil { return }
|
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
|
// check to see if the function is external
|
||||||
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
||||||
if parser.token.Value().(int) != 1 { return }
|
if parser.token.Value().(int) != 1 { return }
|
||||||
@ -179,6 +186,13 @@ func (parser *ParsingOperation) parseFuncArguments (
|
|||||||
if err != nil { return }
|
if err != nil { return }
|
||||||
output.what, err = parser.parseType()
|
output.what, err = parser.parseType()
|
||||||
if err != nil { return }
|
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
|
// parse default value
|
||||||
if parser.token.Is(lexer.TokenKindNewline) {
|
if parser.token.Is(lexer.TokenKindNewline) {
|
||||||
|
@ -3,7 +3,7 @@ package parser
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestFunc (test *testing.T) {
|
func TestFunc (test *testing.T) {
|
||||||
checkTree ("../tests/parser/func",
|
checkTree ("../tests/parser/func", false,
|
||||||
`:arf
|
`:arf
|
||||||
---
|
---
|
||||||
func ro aBasicExternal
|
func ro aBasicExternal
|
||||||
|
@ -3,7 +3,7 @@ package parser
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestMeta (test *testing.T) {
|
func TestMeta (test *testing.T) {
|
||||||
checkTree ("../tests/parser/meta",
|
checkTree ("../tests/parser/meta", false,
|
||||||
`:arf
|
`:arf
|
||||||
author "Sasha Koshka"
|
author "Sasha Koshka"
|
||||||
license "GPLv3"
|
license "GPLv3"
|
||||||
|
@ -3,7 +3,7 @@ package parser
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestObjt (test *testing.T) {
|
func TestObjt (test *testing.T) {
|
||||||
checkTree ("../tests/parser/objt",
|
checkTree ("../tests/parser/objt", false,
|
||||||
`:arf
|
`:arf
|
||||||
---
|
---
|
||||||
objt ro Basic:Obj
|
objt ro Basic:Obj
|
||||||
|
@ -13,6 +13,7 @@ type ParsingOperation struct {
|
|||||||
token lexer.Token
|
token lexer.Token
|
||||||
tokens []lexer.Token
|
tokens []lexer.Token
|
||||||
tokenIndex int
|
tokenIndex int
|
||||||
|
skimming bool
|
||||||
|
|
||||||
tree SyntaxTree
|
tree SyntaxTree
|
||||||
}
|
}
|
||||||
@ -42,6 +43,7 @@ func Fetch (modulePath string, skim bool) (tree SyntaxTree, err error) {
|
|||||||
// miss, so parse the module.
|
// miss, so parse the module.
|
||||||
parser := ParsingOperation {
|
parser := ParsingOperation {
|
||||||
modulePath: modulePath,
|
modulePath: modulePath,
|
||||||
|
skimming: skim,
|
||||||
tree: SyntaxTree {
|
tree: SyntaxTree {
|
||||||
sections: make(map[string] Section),
|
sections: make(map[string] Section),
|
||||||
},
|
},
|
||||||
@ -150,3 +152,23 @@ func (parser *ParsingOperation) previousToken () {
|
|||||||
parser.token = parser.tokens[parser.tokenIndex]
|
parser.token = parser.tokens[parser.tokenIndex]
|
||||||
return
|
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 {
|
||||||
|
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 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
28
parser/skim_test.go
Normal file
28
parser/skim_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
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
|
||||||
|
< arr:Int
|
||||||
|
---
|
||||||
|
external
|
||||||
|
`, test)
|
||||||
|
}
|
@ -6,11 +6,10 @@ import "strings"
|
|||||||
import "testing"
|
import "testing"
|
||||||
import "path/filepath"
|
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()
|
cwd, _ := os.Getwd()
|
||||||
modulePath = filepath.Join(cwd, modulePath)
|
modulePath = filepath.Join(cwd, modulePath)
|
||||||
println(modulePath)
|
tree, err := Fetch(modulePath, skim)
|
||||||
tree, err := Fetch(modulePath, false)
|
|
||||||
|
|
||||||
treeString := tree.ToString(0)
|
treeString := tree.ToString(0)
|
||||||
treeRunes := []rune(treeString)
|
treeRunes := []rune(treeString)
|
||||||
|
@ -266,7 +266,10 @@ func (section DataSection) ToString (indent int) (output string) {
|
|||||||
section.value.kind == ArgumentKindObjectInitializationValues ||
|
section.value.kind == ArgumentKindObjectInitializationValues ||
|
||||||
section.value.kind == ArgumentKindArrayInitializationValues
|
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"
|
output += "\n"
|
||||||
} else if isComplexInitialization {
|
} else if isComplexInitialization {
|
||||||
output += "\n"
|
output += "\n"
|
||||||
|
@ -169,6 +169,8 @@ type DataSection struct {
|
|||||||
typeable
|
typeable
|
||||||
permissionable
|
permissionable
|
||||||
valuable
|
valuable
|
||||||
|
|
||||||
|
external bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// TypeSection represents a blind type definition.
|
// TypeSection represents a blind type definition.
|
||||||
|
@ -3,7 +3,7 @@ package parser
|
|||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestType (test *testing.T) {
|
func TestType (test *testing.T) {
|
||||||
checkTree ("../tests/parser/type",
|
checkTree ("../tests/parser/type", false,
|
||||||
`:arf
|
`:arf
|
||||||
---
|
---
|
||||||
type ro Basic:Int
|
type ro Basic:Int
|
||||||
|
@ -38,6 +38,9 @@ data ro kNestedObject:Obj
|
|||||||
|
|
||||||
data ro lMutIntegerArray16:Int:16:mut
|
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
|
3948 293 293049 948 912
|
||||||
340 0 2304 0 4785 92
|
340 0 2304 0 4785 92
|
||||||
|
42
tests/parser/skim/main.arf
Normal file
42
tests/parser/skim/main.arf
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
: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
|
||||||
|
< arr:Int 5
|
||||||
|
34908
|
||||||
|
39 3498
|
||||||
|
38 219
|
||||||
|
---
|
||||||
|
external
|
Reference in New Issue
Block a user