Использование Утверждено из библиотеки Cats в Scala

сначала: направить все комментарии на определенный канал (только комментарии)

COMMENT
    : '/*' .*? '*/' -> channel(2)
    ;

LINE_COMMENT
    : '//' ~[\r\n]* -> channel(2)
    ;

second: распечатать все комментарии

      CommonTokenStream tokens = new CommonTokenStream(lexer);
      tokens.fill();
      for (int index = 0; index < tokens.size(); index++)
      {
         Token token = tokens.get(index);
         // substitute whatever parser you have
         if (token.getType() != Parser.WS) 
         {
            String out = "";
            // Comments will be printed as channel 2 (configured in .g4 grammar file)
            out += "Channel: " + token.getChannel();
            out += " Type: " + token.getType();
            out += " Hidden: ";
            List<Token> hiddenTokensToLeft = tokens.getHiddenTokensToLeft(index);
            for (int i = 0; hiddenTokensToLeft != null && i < hiddenTokensToLeft.size(); i++)
            {
               if (hiddenTokensToLeft.get(i).getType() != IDLParser.WS)
               {
                  out += "\n\t" + i + ":";
                  out += "\n\tChannel: " + hiddenTokensToLeft.get(i).getChannel() + "  Type: " + hiddenTokensToLeft.get(i).getType();
                  out += hiddenTokensToLeft.get(i).getText().replaceAll("\\s", "");
               }
            }
            out += token.getText().replaceAll("\\s", "");
            System.out.println(out);
         }
      }
1
задан Alexandr 11 March 2019 в 14:22
поделиться

1 ответ

Просто некоторые дженерики не были выведены. Попробуйте указать их явно

  type AllErrorsOr[A] = Validated[List[String], A]

  def bothInvalid: AllErrorsOr[(Int, Int)] = {
    Semigroupal[AllErrorsOr].product[Int, Int](
      Validated.invalid(List("Error 1")),
      Validated.invalid(List("Error 2"))
    )
  }

  def bothInvalidTuple: AllErrorsOr[(Int, Int)] = {
    Semigroupal.tuple2[AllErrorsOr, Int, Int](
      Validated.invalid(List("Error 1")),
      Validated.invalid(List("Error 2"))
    )
  }

  def bothValid: AllErrorsOr[(Int, Int)] = {
    Semigroupal[AllErrorsOr].product[Int, Int](
      Validated.valid(10),
      Validated.valid(20)
    )
  }

  def bothValidTuple: AllErrorsOr[(Int, Int)] = {
    Semigroupal.tuple2[AllErrorsOr, Int, Int](
      Validated.valid(10),
      Validated.valid(20)
    )
  }
0
ответ дан Dmytro Mitin 11 March 2019 в 14:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: