/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.lib.json;

import foam.lib.parse.*;

public class MapParser
  extends ObjectNullParser
{
  private final static Parser instance__ = new MapParser();

  public static Parser instance() { return instance__; }
  private MapParser() {
    super(new Seq1(3,
      Whitespace.instance(),
      Literal.create("{"),
      Whitespace.instance(),
      new Repeat(new Seq2(1, 5,
                 Whitespace.instance(),
                 new AnyKeyParser(),
                 Whitespace.instance(),
                 Literal.create(":"),
                 Whitespace.instance(),
                 AnyParser.instance()),
        new Seq0(Whitespace.instance(), Literal.create(","))),
      Whitespace.instance(),
      Literal.create("}")));
  }

  public PStream parse(PStream ps, ParserContext x) {
    ps = super.parse(ps, x);
    if ( ps == null ) return ps;

    Object[] values = (Object[]) ps.value();
    java.util.Map map = new java.util.HashMap(values.length);

    for ( int i = 0 ; i < values.length ; i++ ) {
      Object[] item = (Object[]) values[i];

      map.put(item[0], item[1]);
    }

    return ps.setValue(map);
  }
}

@@ -500,25 +500,16 @@
 2(1, 5,%0A
-         
         
@@ -539,25 +539,16 @@
         
-     
-    
 new AnyK
@@ -555,33 +555,24 @@
 eyParser(),%0A
-         
         Whit
@@ -586,33 +586,24 @@
 instance(),%0A
-         
         Lite
@@ -627,25 +627,16 @@
         
-     
-    
 Whitespa
@@ -650,25 +650,16 @@
 ance(),%0A
-         
         

Fix indentation.