aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/DictionaryLiteral.java
blob: 8f79739d0272e4a301effc8b8d1293e2d0dff06d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.syntax;

import com.google.common.collect.ImmutableList;

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

/**
 * Syntax node for dictionary literals. 
 */
public class DictionaryLiteral extends Expression {

  static final class DictionaryEntryLiteral extends ASTNode {

    private final Expression key;
    private final Expression value;

    public DictionaryEntryLiteral(Expression key, Expression value) {
      this.key = key;
      this.value = value;
    }

    Expression getKey() {
      return key;
    }

    Expression getValue() {
      return value;
    }

    @Override
    public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append(key);
      sb.append(": ");
      sb.append(value);
      return sb.toString();
    }

    @Override
    public void accept(SyntaxTreeVisitor visitor) {
      visitor.visit(this);
    }
  }

  private final ImmutableList<DictionaryEntryLiteral> entries;

  public DictionaryLiteral(List<DictionaryEntryLiteral> exprs) {
    this.entries = ImmutableList.copyOf(exprs);
  }

  @Override
  Object eval(Environment env) throws EvalException, InterruptedException {
    // We need LinkedHashMap to maintain the order during iteration (e.g. for loops)
    Map<Object, Object> map = new LinkedHashMap<>();
    for (DictionaryEntryLiteral entry : entries) {
      if (entry == null) {
        throw new EvalException(getLocation(), "null expression in " + this);
      }
      map.put(entry.key.eval(env), entry.value.eval(env));
      
    }
    return map;
  }

  @Override
  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("{");
    String sep = "";
    for (DictionaryEntryLiteral e : entries) {
      sb.append(sep);
      sb.append(e);
      sep = ", ";
    }
    sb.append("}");
    return sb.toString();
  }

  @Override
  public void accept(SyntaxTreeVisitor visitor) {
    visitor.visit(this);
  }

  public ImmutableList<DictionaryEntryLiteral> getEntries() {
    return entries;
  }

  @Override
  SkylarkType validate(ValidationEnvironment env) throws EvalException {
    SkylarkType type = SkylarkType.UNKNOWN;
    for (DictionaryEntryLiteral entry : entries) {
      SkylarkType nextType = entry.key.validate(env);
      entry.value.validate(env);
      if (!nextType.isSimple()) {
        throw new EvalException(getLocation(),
            String.format("Dict cannot contain composite type '%s' as key", nextType));
      }
      type = type.infer(nextType, "dict literal", entry.getLocation(), getLocation());
    }
    return SkylarkType.of(Map.class, type.getType());
  }
}