Browse Source

fix: ECAL parser can better decide between composition structure access and lists

Matthias Ladkau 3 years ago
parent
commit
684c151b05
2 changed files with 39 additions and 1 deletions
  1. 5 1
      lang/ecal/parser/parser.go
  2. 34 0
      lang/ecal/parser/parser_statement_test.go

+ 5 - 1
lang/ecal/parser/parser.go

@@ -513,7 +513,11 @@ func ndIdentifier(p *parser, self *ASTNode) (*ASTNode, error) {
 			err = parseSegment(current)
 		} else if p.node.Token.ID == TokenLPAREN {
 			err = parseFuncCall(current)
-		} else if p.node.Token.ID == TokenLBRACK {
+		} else if p.node.Token.ID == TokenLBRACK && p.node.Token.Lline == self.Token.Lline {
+
+			// Composition access needs to be on the same line as the identifier
+			// as we might otherwise have a list
+
 			err = parseCompositionAccess(current)
 		}
 

+ 34 - 0
lang/ecal/parser/parser_statement_test.go

@@ -14,6 +14,40 @@ import (
 	"testing"
 )
 
+func TestAssignmentParsing(t *testing.T) {
+
+	input := `
+z := a.b[1].c["3"]["test"]
+[x, y] := a.b
+`
+	expectedOutput := `
+statements
+  :=
+    identifier: z
+    identifier: a
+      identifier: b
+        compaccess
+          number: 1
+        identifier: c
+          compaccess
+            string: '3'
+          compaccess
+            string: 'test'
+  :=
+    list
+      identifier: x
+      identifier: y
+    identifier: a
+      identifier: b
+`[1:]
+
+	if res, err := UnitTestParse("mytest", input); err != nil || fmt.Sprint(res) != expectedOutput {
+		t.Error("Unexpected parser output:\n", res, "expected was:\n", expectedOutput, "Error:", err)
+		return
+	}
+
+}
+
 func TestLoopParsing(t *testing.T) {
 
 	input := `