Skip to content

Commit

Permalink
hyperscript/analysis: store location of install features and behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
GraphR00t committed May 15, 2024
1 parent b714744 commit 3643047
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 25 deletions.
10 changes: 8 additions & 2 deletions internal/hyperscript/hsanalysis/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/inoxlang/inox/internal/hyperscript/hsanalysis/text"
"github.com/inoxlang/inox/internal/hyperscript/hscode"
"github.com/inoxlang/inox/internal/sourcecode"
)

var analyzerPool = sync.Pool{
Expand Down Expand Up @@ -124,7 +125,7 @@ func (a *analyzer) preVisitHyperscriptNode(
case hscode.DefFeature:
a.functionDefinitions = append(a.functionDefinitions, MakeFunctionDefinitionFromNode(node))
case hscode.BehaviorFeature:
behavior := MakeBehaviorFromNode(node)
behavior := MakeBehaviorFromNode(node, a.getNodeLocation(node))
a.behaviors = append(a.behaviors, behavior)
a.behaviorStack = append(a.behaviorStack, behavior)

Expand All @@ -134,6 +135,8 @@ func (a *analyzer) preVisitHyperscriptNode(
&behavior.HandledEvents,
&behavior.Installs,
behavior.Features,
a.parameters.CodeStartIndex,
a.parameters.Chunk,
)
}

Expand Down Expand Up @@ -167,6 +170,8 @@ func preAnalyzeFeaturesOfBehaviorOrComponent(
handledEvents *[]DOMEvent,
installs *[]*InstallFeature,
features []any,
codeStartIndex int32,
chunk sourcecode.ParsedChunkSource,
) {
walk := func(node hscode.JSONMap, inInit bool) {
hscode.Walk(node, func(node hscode.JSONMap, nodeType hscode.NodeType, _ hscode.JSONMap, _ hscode.NodeType, _ []hscode.JSONMap, _ bool) (hscode.AstTraversalAction, error) {
Expand Down Expand Up @@ -206,7 +211,8 @@ func preAnalyzeFeaturesOfBehaviorOrComponent(
}
walk(feature, false)
case hscode.InstallFeature:
installfeature := MakeInstallFeatureFromNode(feature)
location := getNodeLocation(feature, codeStartIndex, chunk)
installfeature := MakeInstallFeatureFromNode(feature, location)
*installs = append(*installs, installfeature)
}
}
Expand Down
24 changes: 19 additions & 5 deletions internal/hyperscript/hsanalysis/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,15 @@ func TestAnalyzeHyperscripFile(t *testing.T) {
assert.Equal(t, "A", behavior.FullName)
assert.Empty(t, behavior.Namespace)
assert.Empty(t, behavior.Features)

assert.Equal(t, sourcecode.PositionRange{
SourceName: "/a._hs",
StartLine: 1,
StartColumn: 1,
EndLine: 1,
EndColumn: 11,
Span: sourcecode.NodeSpan{Start: 0, End: 10},
}, behavior.Location)
})

t.Run("namespaced named", func(t *testing.T) {
Expand Down Expand Up @@ -717,11 +726,7 @@ func TestAnalyzeHyperscripFile(t *testing.T) {
NameString: "/a._hs",
Resource: "/a._hs",
ResourceDir: "/",
CodeString: `
behavior A
install B(x: 1)
end
`,
CodeString: "behavior A\ninstall B(x: 1) end",
}, nil))

result, err := Analyze(Parameters{
Expand Down Expand Up @@ -754,6 +759,15 @@ func TestAnalyzeHyperscripFile(t *testing.T) {

assert.Equal(t, "x", field.Name)
assert.True(t, hscode.IsNodeOfType(field.Value, hscode.NumberLiteral))

assert.Equal(t, sourcecode.PositionRange{
SourceName: "/a._hs",
StartLine: 2,
StartColumn: 1,
EndLine: 2,
EndColumn: 16,
Span: sourcecode.NodeSpan{Start: 11, End: 26},
}, install.Location)
})
})

Expand Down
20 changes: 13 additions & 7 deletions internal/hyperscript/hsanalysis/behavior.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package hsanalysis

import "github.com/inoxlang/inox/internal/hyperscript/hscode"
import (
"github.com/inoxlang/inox/internal/hyperscript/hscode"
"github.com/inoxlang/inox/internal/sourcecode"
)

type Behavior struct {
Name string
FullName string
Namespace []string
Features []any
Location sourcecode.PositionRange

Features []any

HandledEvents []DOMEvent
InitialElementScopeVarNames []string // example: {":a", ":b"}
Expand All @@ -17,13 +22,14 @@ type Behavior struct {
//Note: applying an install updates InitialElementScopeVarNames and InitializedDataAttributeNames.
}

func MakeBehaviorFromNode(node hscode.JSONMap) *Behavior {
func MakeBehaviorFromNode(node hscode.JSONMap, location sourcecode.PositionRange) *Behavior {
hscode.AssertIsNodeOfType(node, hscode.BehaviorFeature)

var behavior Behavior

behavior.Name = node["name"].(string)
behavior.FullName = node["fullName"].(string)
behavior := Behavior{
Name: node["name"].(string),
FullName: node["fullName"].(string),
Location: location,
}

namespace, ok := node["nameSpace"].([]any)
if ok {
Expand Down
4 changes: 4 additions & 0 deletions internal/hyperscript/hsanalysis/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ func PreanalyzeHyperscriptComponent(
return
}

codeStartIndex := attribute.Span.Start + 1

preAnalyzeFeaturesOfBehaviorOrComponent(
&component.InitialElementScopeVarNames,
&component.InitializedDataAttributeNames,
&component.HandledEvents,
&component.Installs,
features,
codeStartIndex,
chunkSource,
)

return
Expand Down
18 changes: 12 additions & 6 deletions internal/hyperscript/hsanalysis/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/inoxlang/inox/internal/hyperscript/hscode"
"github.com/inoxlang/inox/internal/parse"
"github.com/inoxlang/inox/internal/sourcecode"
)

type Error struct {
Expand Down Expand Up @@ -41,14 +42,19 @@ type Warning struct {
LocatedMessage string
}

func (c *analyzer) addError(node hscode.JSONMap, msg string) {
relativeNodeStart, relativeNodeEnd := hscode.GetNodeSpan(node)
codeStartIndex := c.parameters.CodeStartIndex
func (a *analyzer) getNodeLocation(node hscode.JSONMap) sourcecode.PositionRange {
return getNodeLocation(node, a.parameters.CodeStartIndex, a.parameters.Chunk)
}

func (a *analyzer) addError(node hscode.JSONMap, msg string) {
location := a.getNodeLocation(node)
a.errors = append(a.errors, MakeError(msg, location))
}

func getNodeLocation(node hscode.JSONMap, codeStartIndex int32, chunk sourcecode.ParsedChunkSource) sourcecode.PositionRange {
relativeNodeStart, relativeNodeEnd := hscode.GetNodeSpan(node)
absoluteNodeStart := codeStartIndex + relativeNodeStart
absoluteNodeEnd := codeStartIndex + relativeNodeEnd

location := c.parameters.Chunk.GetSourcePosition(parse.NodeSpan{Start: absoluteNodeStart, End: absoluteNodeEnd})

c.errors = append(c.errors, MakeError(msg, location))
return chunk.GetSourcePosition(parse.NodeSpan{Start: absoluteNodeStart, End: absoluteNodeEnd})
}
16 changes: 11 additions & 5 deletions internal/hyperscript/hsanalysis/install_feature.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package hsanalysis

import "github.com/inoxlang/inox/internal/hyperscript/hscode"
import (
"github.com/inoxlang/inox/internal/hyperscript/hscode"
"github.com/inoxlang/inox/internal/sourcecode"
)

type InstallFeature struct {
BehaviorFullName string
Fields []ArgListField

Location sourcecode.PositionRange
}

func MakeInstallFeatureFromNode(node hscode.JSONMap) *InstallFeature {
func MakeInstallFeatureFromNode(node hscode.JSONMap, location sourcecode.PositionRange) *InstallFeature {
hscode.AssertIsNodeOfType(node, hscode.InstallFeature)

var feature InstallFeature

feature.BehaviorFullName = node["fullName"].(string)
feature := InstallFeature{
BehaviorFullName: node["fullName"].(string),
Location: location,
}

for _, field := range node["fields"].([]any) {
fieldMap := field.(hscode.JSONMap)
Expand Down

0 comments on commit 3643047

Please sign in to comment.