本文整理汇总了Golang中github.com/serulian/compiler/compilercommon.InputSource函数的典型用法代码示例。如果您正苦于以下问题:Golang InputSource函数的具体用法?Golang InputSource怎么用?Golang InputSource使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了InputSource函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestParser
func TestParser(t *testing.T) {
for _, test := range parserTests {
if os.Getenv("FILTER") != "" {
if !strings.Contains(test.name, os.Getenv("FILTER")) {
continue
} else {
fmt.Printf("Matched Test: %v\n", test.name)
}
}
moduleNode := createAstNode(compilercommon.InputSource(test.name), NodeTypeGlobalModule)
Parse(moduleNode, createAstNode, compilercommon.InputSource(test.name), test.input())
parseTree := getParseTree((moduleNode).(*testNode), 0)
assert := assert.New(t)
expected := strings.TrimSpace(test.tree())
found := strings.TrimSpace(parseTree)
if os.Getenv("REGEN") == "true" {
test.writeTree(found)
} else {
if !assert.Equal(expected, found, test.name) {
t.Log(parseTree)
}
}
}
}
示例2: conductParsing
// conductParsing performs parsing of a source file found at the given path.
func (p *PackageLoader) conductParsing(sourceFile pathInformation) {
inputSource := compilercommon.InputSource(sourceFile.path)
// Add the file to the package map as a package of one file.
p.packageMap.Add(sourceFile.sourceKind, sourceFile.referenceId, PackageInfo{
kind: sourceFile.sourceKind,
referenceId: sourceFile.referenceId,
modulePaths: []compilercommon.InputSource{inputSource},
})
// Ensure the file exists.
if ok, _ := exists(sourceFile.path); !ok {
p.errors <- compilercommon.SourceErrorf(sourceFile.sal, "Could not find source file '%s'", sourceFile.path)
return
}
// Load the source file's contents.
contents, err := ioutil.ReadFile(sourceFile.path)
if err != nil {
p.errors <- compilercommon.SourceErrorf(sourceFile.sal, "Could not load source file '%s'", sourceFile.path)
return
}
// Parse the source file.
handler, hasHandler := p.handlers[sourceFile.sourceKind]
if !hasHandler {
log.Fatalf("Missing handler for source file of kind: [%v]", sourceFile.sourceKind)
}
handler.Parse(inputSource, string(contents), p.handleImport)
}
示例3: scopeUnaryExpression
// scopeUnaryExpression scopes a unary expression in the SRG.
func (sb *scopeBuilder) scopeUnaryExpression(node compilergraph.GraphNode, opName string, predicate compilergraph.Predicate, context scopeContext) *scopeInfoBuilder {
// Get the scope of the sub expression.
childScope := sb.getScope(node.GetNode(predicate), context)
// Ensure that the child scope is valid.
if !childScope.GetIsValid() {
return newScope().Invalid()
}
// Ensure that the operator exists under the resolved type.
childType := childScope.ResolvedTypeRef(sb.sg.tdg)
module := compilercommon.InputSource(node.Get(parser.NodePredicateSource))
operator, rerr := childType.ResolveAccessibleMember(opName, module, typegraph.MemberResolutionOperator)
if rerr != nil {
sb.decorateWithError(node, "Operator '%v' is not defined on type '%v'", opName, childType)
return newScope().Invalid()
}
returnType, _ := operator.ReturnType()
// Check for nullable values.
if childType.NullValueAllowed() {
sb.decorateWithError(node, "Cannot invoke operator '%v' on nullable type '%v'", opName, childType)
return newScope().Invalid().CallsOperator(operator).Resolving(returnType.TransformUnder(childType))
}
return newScope().Valid().CallsOperator(operator).Resolving(returnType.TransformUnder(childType))
}
示例4: scopeInCollectionExpression
// scopeInCollectionExpression scopes an 'in' collection expression in the SRG.
func (sb *scopeBuilder) scopeInCollectionExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Get the scope of the left and right expressions.
leftScope := sb.getScope(node.GetNode(parser.NodeBinaryExpressionLeftExpr), context)
rightScope := sb.getScope(node.GetNode(parser.NodeBinaryExpressionRightExpr), context)
// Ensure that both scopes are valid.
if !leftScope.GetIsValid() || !rightScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
// Ensure that the right side has a 'contains' operator defined.
rightType := rightScope.ResolvedTypeRef(sb.sg.tdg)
module := compilercommon.InputSource(node.Get(parser.NodePredicateSource))
operator, rerr := rightType.ResolveAccessibleMember("contains", module, typegraph.MemberResolutionOperator)
if rerr != nil {
sb.decorateWithError(node, "Operator 'contains' is not defined on type '%v'", rightType)
return newScope().Invalid().GetScope()
}
// Ensure the right side is not nullable.
if rightType.NullValueAllowed() {
sb.decorateWithError(node, "Cannot invoke operator 'in' on nullable value of type '%v'", rightType)
return newScope().Invalid().GetScope()
}
// Ensure that the left side can be used as the operator's parameter.
parameterType := operator.ParameterTypes()[0].TransformUnder(rightType)
leftType := leftScope.ResolvedTypeRef(sb.sg.tdg)
if serr := leftType.CheckSubTypeOf(parameterType); serr != nil {
sb.decorateWithError(node, "Cannot invoke operator 'in' with value of type '%v': %v", leftType, serr)
return newScope().Invalid().GetScope()
}
return newScope().Valid().CallsOperator(operator).Resolving(sb.sg.tdg.BoolTypeReference()).GetScope()
}
示例5: salForValues
// salForValues returns a SourceAndLocation for the given string predicate values.
func salForValues(sourceStr string, bytePositionStr string) compilercommon.SourceAndLocation {
source := compilercommon.InputSource(sourceStr)
bytePosition, err := strconv.Atoi(bytePositionStr)
if err != nil {
panic(fmt.Sprintf("Expected int value for byte position, found: %v", bytePositionStr))
}
return compilercommon.NewSourceAndLocation(source, bytePosition)
}
示例6: conductParsing
func conductParsing(t *testing.T, test goldenTest, source []byte) (*parseTree, formatterNode) {
parseTree := newParseTree(source)
inputSource := compilercommon.InputSource(test.filename)
rootNode := parser.Parse(parseTree.createAstNode, nil, inputSource, string(source))
if !assert.Empty(t, parseTree.errors, "Expected no parse errors for test %s", test.name) {
return nil, formatterNode{}
}
return parseTree, rootNode.(formatterNode)
}
示例7: TestNameScoping
func TestNameScoping(t *testing.T) {
for _, test := range nameScopeTests {
if os.Getenv("FILTER") != "" && !strings.Contains(test.name, os.Getenv("FILTER")) {
continue
}
source := fmt.Sprintf("tests/namescope/%s.seru", test.source)
testSRG := getSRG(t, source, "tests/testlib")
_, found := testSRG.FindModuleBySource(compilercommon.InputSource(source))
if !assert.True(t, found, "Test module not found") {
continue
}
// Find the commented node.
commentedNode, commentFound := testSRG.FindCommentedNode("/* " + test.comment + " */")
if !assert.True(t, commentFound, "Comment %v for test %v not found", test.comment, test.name) {
continue
}
// Resolve the name from the node.
result, nameFound := testSRG.FindNameInScope(test.queryName, commentedNode)
if !test.result.isValid {
assert.False(t, nameFound, "Test %v expected name %v to not be found. Found: %v", test.name, test.queryName, result)
continue
}
if !assert.Equal(t, result.IsNamedScope(), !test.result.isExternal, "External scope mismatch on test %s", test.name) {
continue
}
if result.IsNamedScope() {
resolved := result.AsNamedScope()
if !assert.True(t, nameFound, "Test %v expected name %v to be found", test.name, test.queryName) {
continue
}
if !assert.Equal(t, test.result.expectedNodeType, resolved.Kind(), "Test %v expected node of kind %v", test.name, test.result.expectedNodeType) {
continue
}
if !assert.Equal(t, test.result.expectedName, resolved.Name(), "Test %v expected name %v", test.name, test.result.expectedName) {
continue
}
if !assert.Equal(t, test.result.expectedKind, resolved.ScopeKind(), "Test %v expected kind %v", test.name, test.result.expectedKind) {
continue
}
} else {
if !assert.Equal(t, test.result.expectedName, result.AsPackageImport().name, "Mismatch name on imported package for test %s", test.name) {
continue
}
}
}
}
示例8: TestComplexResolveTypePath
func TestComplexResolveTypePath(t *testing.T) {
testSRG := getSRG(t, "tests/complexresolve/entrypoint.seru")
// Lookup the entrypoint module.
entrypointModule, ok := testSRG.FindModuleBySource(compilercommon.InputSource("tests/complexresolve/entrypoint.seru"))
assert.True(t, ok, "Could not find entrypoint module")
// Lookup all the expected types.
// In module.
assertResolveTypePath(t, entrypointModule, "SomeClass", "SomeClass")
// from ... import ... on module
assertResolveTypePath(t, entrypointModule, "AnotherClass", "AnotherClass")
assertResolveTypePath(t, entrypointModule, "BestClass", "AThirdClass")
// from ... import ... on package
assertResolveTypePath(t, entrypointModule, "FirstClass", "FirstClass")
assertResolveTypePath(t, entrypointModule, "SecondClass", "SecondClass")
assertResolveTypePath(t, entrypointModule, "BusinessClass", "FirstClass")
// Direct imports.
assertResolveTypePath(t, entrypointModule, "anothermodule.AnotherClass", "AnotherClass")
assertResolveTypePath(t, entrypointModule, "subpackage.FirstClass", "FirstClass")
assertResolveTypePath(t, entrypointModule, "subpackage.SecondClass", "SecondClass")
// Ensure that an non-exported type is still accessible inside the package..
assertResolveTypePath(t, entrypointModule, "anothermodule.localClass", "localClass")
// Other package.
assertResolveTypePath(t, entrypointModule, "anotherpackage.ExportedPackageClass", "ExportedPackageClass")
// Ensure that an non-exported type is not accessible from another package.
_, found := entrypointModule.ResolveTypePath("anotherpackage.otherPackageClass")
assert.False(t, found, "Expected otherPackageClass to not be exported")
// Lookup another module.
anotherModule, ok := testSRG.FindModuleBySource(compilercommon.InputSource("tests/complexresolve/anothermodule.seru"))
assert.True(t, ok, "Could not find another module")
assertResolveTypePath(t, anotherModule, "localClass", "localClass")
}
示例9: collect
// collect gathers the emitted tokens into a slice.
func collect(t *lexerTest) (tokens []lexeme) {
l := lex(compilercommon.InputSource(t.name), t.input)
for {
token := l.nextToken()
tokens = append(tokens, token)
if token.kind == tokenTypeEOF || token.kind == tokenTypeError {
break
}
}
return
}
示例10: TestBasicResolveTypePath
func TestBasicResolveTypePath(t *testing.T) {
testSRG := getSRG(t, "tests/basic/basic.seru")
// Lookup the basic module.
basicModule, ok := testSRG.FindModuleBySource(compilercommon.InputSource("tests/basic/basic.seru"))
assert.True(t, ok, "Could not find basic module")
// Lookup both expected types.
assertResolveTypePath(t, basicModule, "SomeClass", "SomeClass")
assertResolveTypePath(t, basicModule, "AnotherClass", "AnotherClass")
}
示例11: scopeMemberAccessExpression
// scopeMemberAccessExpression scopes a member access expression in the SRG.
func (sb *scopeBuilder) scopeMemberAccessExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Get the scope of the child expression.
childScope := sb.getScope(node.GetNode(parser.NodeMemberAccessChildExpr), context)
if !childScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
memberName := node.Get(parser.NodeMemberAccessIdentifier)
module := compilercommon.InputSource(node.Get(parser.NodePredicateSource))
switch childScope.GetKind() {
case proto.ScopeKind_VALUE:
childType := childScope.ResolvedTypeRef(sb.sg.tdg)
if childType.IsNullable() {
sb.decorateWithError(node, "Cannot access name '%v' under nullable type '%v'. Please use the ?. operator to ensure type safety.", memberName, childType)
return newScope().Invalid().GetScope()
}
typeMember, rerr := childType.ResolveAccessibleMember(memberName, module, typegraph.MemberResolutionInstance)
if rerr != nil {
sb.decorateWithError(node, "%v", rerr)
return newScope().Invalid().GetScope()
}
memberScope := sb.getNamedScopeForMember(typeMember)
context.staticDependencyCollector.checkNamedScopeForDependency(memberScope)
return newScope().ForNamedScopeUnderType(memberScope, childType, context).GetScope()
case proto.ScopeKind_GENERIC:
namedScope, _ := sb.getNamedScopeForScope(childScope)
sb.decorateWithError(node, "Cannot attempt member access of '%v' under %v %v, as it is generic without specification", memberName, namedScope.Title(), namedScope.Name())
return newScope().Invalid().GetScope()
case proto.ScopeKind_STATIC:
staticType := childScope.StaticTypeRef(sb.sg.tdg)
namedScope, _ := sb.getNamedScopeForScope(childScope)
memberScope, rerr := namedScope.ResolveStaticMember(memberName, module, staticType)
if rerr != nil {
sb.decorateWithError(node, "%v", rerr)
return newScope().Invalid().GetScope()
}
return newScope().ForNamedScopeUnderType(memberScope, staticType, context).GetScope()
default:
panic("Unknown scope kind")
}
return newScope().Invalid().GetScope()
}
示例12: TestModuleMembers
func TestModuleMembers(t *testing.T) {
testSRG := getSRG(t, "tests/members/module.seru")
// Ensure both module-level members are found.
module, _ := testSRG.FindModuleBySource(compilercommon.InputSource("tests/members/module.seru"))
members := module.GetMembers()
if !assert.Equal(t, 2, len(members), "Expected 2 members found") {
return
}
assert.Equal(t, members[0].MemberKind(), VarMember)
assert.Equal(t, members[1].MemberKind(), FunctionMember)
}
示例13: scopeStreamMemberAccessExpression
// scopeStreamMemberAccessExpression scopes a stream member access expression in the SRG.
func (sb *scopeBuilder) scopeStreamMemberAccessExpression(node compilergraph.GraphNode, context scopeContext) proto.ScopeInfo {
// Get the scope of the child expression.
childScope := sb.getScope(node.GetNode(parser.NodeMemberAccessChildExpr), context)
if !childScope.GetIsValid() {
return newScope().Invalid().GetScope()
}
memberName := node.Get(parser.NodeMemberAccessIdentifier)
module := compilercommon.InputSource(node.Get(parser.NodePredicateSource))
switch childScope.GetKind() {
case proto.ScopeKind_VALUE:
childType := childScope.ResolvedTypeRef(sb.sg.tdg)
// Ensure the child type is a stream.
generics, serr := childType.CheckConcreteSubtypeOf(sb.sg.tdg.StreamType())
if serr != nil {
sb.decorateWithError(node, "Cannot attempt stream access of name '%v' under non-stream type '%v': %v", memberName, childType, serr)
return newScope().Invalid().GetScope()
}
valueType := generics[0]
typeMember, rerr := valueType.ResolveAccessibleMember(memberName, module, typegraph.MemberResolutionInstance)
if rerr != nil {
sb.decorateWithError(node, "%v", rerr)
return newScope().Invalid().GetScope()
}
memberScope := sb.getNamedScopeForMember(typeMember)
context.staticDependencyCollector.checkNamedScopeForDependency(memberScope)
return newScope().ForNamedScopeUnderModifiedType(memberScope, valueType, makeStream, context).GetScope()
case proto.ScopeKind_GENERIC:
namedScope, _ := sb.getNamedScopeForScope(childScope)
sb.decorateWithError(node, "Cannot attempt stream member access of '%v' under %v %v, as it is generic without specification", memberName, namedScope.Title(), namedScope.Name())
return newScope().Invalid().GetScope()
case proto.ScopeKind_STATIC:
namedScope, _ := sb.getNamedScopeForScope(childScope)
sb.decorateWithError(node, "Cannot attempt stream member access of '%v' under %v %v, as it is a static type", memberName, namedScope.Title(), namedScope.Name())
return newScope().Invalid().GetScope()
default:
panic("Unknown scope kind")
}
return newScope().Invalid().GetScope()
}
示例14: getModulesMap
// getModulesMap returns the modules map for the modules in the SRG.
func (g *SRG) getModulesMap() map[compilercommon.InputSource]SRGModule {
if g.modulePathMap == nil {
moduleMap := map[compilercommon.InputSource]SRGModule{}
it := g.findAllNodes(parser.NodeTypeFile).BuildNodeIterator(parser.NodePredicateSource)
for it.Next() {
moduleMap[compilercommon.InputSource(it.GetPredicate(parser.NodePredicateSource).String())] = SRGModule{it.Node(), g}
}
// Cache the map.
g.modulePathMap = moduleMap
return moduleMap
}
return g.modulePathMap
}
示例15: ResolveType
// ResolveType attempts to resolve the type path referenced by this type ref.
// Panics if this is not a RefKind of TypeRefPath.
func (t SRGTypeRef) ResolveType() (TypeResolutionResult, bool) {
// Find the parent module.
source := compilercommon.InputSource(t.GraphNode.Get(parser.NodePredicateSource))
srgModule, found := t.srg.FindModuleBySource(source)
if !found {
panic(fmt.Sprintf("Unknown parent module: %s", source))
}
// Resolve the type path under the module.
resolutionPath := t.ResolutionPath()
resolvedType, typeFound := srgModule.ResolveTypePath(resolutionPath)
if typeFound {
return resolvedType, true
}
// If not found and the path is a single name, try to resolve as a generic
// under a parent function or type.
if strings.ContainsRune(resolutionPath, '.') {
// Not a single name.
return TypeResolutionResult{}, false
}
containingFilter := func(q compilergraph.GraphQuery) compilergraph.Query {
// For this filter, we check if the defining type (or type member) if the
// generic is the same type (or type member) containing the typeref. To do so,
// we perform a check that the start rune and end rune of the definition
// contains the range of the start and end rune, respectively, of the typeref. Since
// we know both nodes are in the same module, and the SRG is a tree, this validates
// that we are in the correct scope without having to walk the tree upward.
startRune := t.GraphNode.GetValue(parser.NodePredicateStartRune).Int()
endRune := t.GraphNode.GetValue(parser.NodePredicateEndRune).Int()
return q.
In(parser.NodeTypeDefinitionGeneric, parser.NodePredicateTypeMemberGeneric).
HasWhere(parser.NodePredicateStartRune, compilergraph.WhereLTE, startRune).
HasWhere(parser.NodePredicateEndRune, compilergraph.WhereGTE, endRune)
}
resolvedGenericNode, genericFound := t.srg.layer.
StartQuery(). // Find a node...
Has(parser.NodeGenericPredicateName, resolutionPath). // With the generic name..
Has(parser.NodePredicateSource, string(source)). // That is in this module...
IsKind(parser.NodeTypeGeneric). // That is a generic...
FilterBy(containingFilter). // Filter by whether its defining type or member contains this typeref.
TryGetNode()
return resultForTypeOrGeneric(SRGTypeOrGeneric{resolvedGenericNode, t.srg}), genericFound
}