All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.json.parser.TreeFactory Maven / Gradle / Ivy

The newest version!
/*
 * SonarQube JSON Analyzer
 * Copyright (C) 2015-2017 David RACODON
 * [email protected]
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.json.parser;

import com.google.common.collect.Lists;
import com.sonar.sslr.api.typed.Optional;
import org.sonar.json.tree.impl.*;
import org.sonar.plugins.json.api.tree.*;

import java.util.List;

public class TreeFactory {

  public JsonTree json(Optional byteOrderMark, Optional value, SyntaxToken eof) {
    return new JsonTreeImpl(byteOrderMark.orNull(), value.orNull(), eof);
  }

  public ObjectTree object(InternalSyntaxToken leftBrace, Optional> pairs, InternalSyntaxToken rightBrace) {
    return new ObjectTreeImpl(leftBrace, pairs.orNull(), rightBrace);
  }

  public ArrayTree array(InternalSyntaxToken leftBracket, Optional> values, InternalSyntaxToken rightBracket) {
    return new ArrayTreeImpl(leftBracket, values.orNull(), rightBracket);
  }

  public PairTree pair(KeyTree key, SyntaxToken colon, ValueTree value) {
    return new PairTreeImpl(key, colon, value);
  }

  public KeyTree key(SyntaxToken key) {
    return new KeyTreeImpl(key);
  }

  public ValueTree value(Tree value) {
    return new ValueTreeImpl(value);
  }

  public SeparatedList valueList(ValueTree value, Optional>> subsequentValues) {
    List values = Lists.newArrayList();
    List commas = Lists.newArrayList();

    values.add(value);

    if (subsequentValues.isPresent()) {
      for (Tuple t : subsequentValues.get()) {
        commas.add(t.first());
        values.add(t.second());
      }
    }

    return new SeparatedList<>(values, commas);
  }

  public SeparatedList pairList(PairTree pair, Optional>> subsequentPairs) {
    List pairs = Lists.newArrayList();
    List commas = Lists.newArrayList();

    pairs.add(pair);

    if (subsequentPairs.isPresent()) {
      for (Tuple t : subsequentPairs.get()) {
        commas.add(t.first());
        pairs.add(t.second());
      }
    }

    return new SeparatedList<>(pairs, commas);
  }

  public StringTree string(SyntaxToken token) {
    return new StringTreeImpl(token);
  }

  public LiteralTree number(SyntaxToken token) {
    return new NumberTreeImpl(token);
  }

  public LiteralTree falsee(SyntaxToken token) {
    return new FalseTreeImpl(token);
  }

  public LiteralTree truee(SyntaxToken token) {
    return new TrueTreeImpl(token);
  }

  public LiteralTree nul(SyntaxToken token) {
    return new NullTreeImpl(token);
  }

  public static class Tuple {

    private final T first;
    private final U second;

    public Tuple(T first, U second) {
      super();

      this.first = first;
      this.second = second;
    }

    public T first() {
      return first;
    }

    public U second() {
      return second;
    }
  }

  private static  Tuple newTuple(T first, U second) {
    return new Tuple<>(first, second);
  }

  public  Tuple newTuple1(T first, U second) {
    return newTuple(first, second);
  }

  public  Tuple newTuple2(T first, U second) {
    return newTuple(first, second);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy