package org.yinwang.yin.ast;


import org.yinwang.yin.Constants;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Parameter {
    public List<Node> elements;
    public List<String> positional = new ArrayList<>();
    public Map<String, Node> keywords = new LinkedHashMap<>();


    public Parameter(List<Node> elements) throws ParseError {
        this.elements = elements;

        for (int i = 0; i < elements.size(); i++) {
            Node n1 = elements.get(i);
            if (n1 instanceof Name) {
                positional.add(((Name) n1).id);
            } else if (n1 instanceof Keyword) {
                positional.add(((Keyword) n1).id);
                if (i >= elements.size()) {
                    throw new ParseError(n1, "missing value for keyword: " + ((Keyword) n1).id);
                } else {
                    Node n2 = elements.get(i + 1);
                    keywords.put(((Keyword) n1).id, n2);
                }
            }
        }
    }


    public String toString() {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Node e : elements) {
            if (!first) {
                sb.append(" ");
            }
            sb.append(e);
            first = false;
        }
        return sb.toString();
    }

}

@@ -28,43 +28,8 @@
 ;%0A%0A%0A
-import org.yinwang.yin.Constants;%0A%0A
 impo
@@ -721,16 +721,20 @@
 s.size()
+ - 1
 ) %7B%0A    
@@ -806,33 +806,18 @@
 rd: %22 + 
-((Keyword) n1).id
+n1
 );%0A     
@@ -952,33 +952,154 @@
                 
-%7D
+    i++;%0A                %7D%0A            %7D else %7B%0A                throw new ParseError(n1, %22illegal parameter form: %22 + n1);
 %0A            %7D%0A 

improve error reporting for parameters