<commit_before>/*
 * Copyright 2009 Google Inc.
 *
 * 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.common.css.compiler.passes;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.css.compiler.ast.CssAtRuleNode;
import com.google.common.css.compiler.ast.CssAtRuleNode.Type;
import com.google.common.css.compiler.ast.CssBlockNode;
import com.google.common.css.compiler.ast.CssBooleanExpressionNode;
import com.google.common.css.compiler.ast.CssCompilerPass;
import com.google.common.css.compiler.ast.CssCompositeValueNode;
import com.google.common.css.compiler.ast.CssConditionalBlockNode;
import com.google.common.css.compiler.ast.CssDeclarationBlockNode;
import com.google.common.css.compiler.ast.CssFontFaceNode;
import com.google.common.css.compiler.ast.CssFunctionNode;
import com.google.common.css.compiler.ast.CssImportBlockNode;
import com.google.common.css.compiler.ast.CssImportRuleNode;
import com.google.common.css.compiler.ast.CssLiteralNode;
import com.google.common.css.compiler.ast.CssMediaRuleNode;
import com.google.common.css.compiler.ast.CssNode;
import com.google.common.css.compiler.ast.CssPageRuleNode;
import com.google.common.css.compiler.ast.CssPageSelectorNode;
import com.google.common.css.compiler.ast.CssRootNode;
import com.google.common.css.compiler.ast.CssRulesetNode;
import com.google.common.css.compiler.ast.CssStringNode;
import com.google.common.css.compiler.ast.CssUnknownAtRuleNode;
import com.google.common.css.compiler.ast.CssValueNode;
import com.google.common.css.compiler.ast.ErrorManager;
import com.google.common.css.compiler.ast.GssError;
import com.google.common.css.compiler.ast.MutatingVisitController;
import java.util.List;
import java.util.Set;

/**
 * A compiler pass that transforms standard {@link CssUnknownAtRuleNode} instances to more specific
 * at-rule nodes, or deletes them.
 *
 */
public class CreateStandardAtRuleNodes implements UniformVisitor, CssCompilerPass {

  @VisibleForTesting static final String NO_BLOCK_ERROR_MESSAGE = "This @-rule has to have a block";

  @VisibleForTesting
  static final String BLOCK_ERROR_MESSAGE = "This @-rule is not allowed to have a block";

  @VisibleForTesting
  static final String ONLY_DECLARATION_BLOCK_ERROR_MESSAGE =
      "Only declaration blocks are allowed for this @-rule";

  @VisibleForTesting
  static final String INVALID_PARAMETERS_ERROR_MESSAGE = "This @-rule has invalid parameters";

  @VisibleForTesting
  static final String MEDIA_INVALID_CHILD_ERROR_MESSAGE =
      "This is not valid inside an @media block";

  @VisibleForTesting
  static final String MEDIA_WITHOUT_PARAMETERS_ERROR_MESSAGE = "@media without parameters";

  @VisibleForTesting
  static final String PAGE_SELECTOR_PARAMETERS_ERROR_MESSAGE =
      "Page selectors are not allowed to have parameters";

  @VisibleForTesting
  static final String FONT_FACE_PARAMETERS_ERROR_MESSAGE =
      "@font-face is not allowed to have parameters";

  @VisibleForTesting
  static final String IGNORED_IMPORT_WARNING_MESSAGE =
      "@import rules should occur outside blocks and can only be preceded "
          + "by @charset and other @import rules.";

  @VisibleForTesting
  static final String IGNORE_IMPORT_WARNING_MESSAGE =
      "A node after which all @import rule nodes are ignored is here.";

  private static final List<Type> PAGE_SELECTORS =
      ImmutableList.of(
          Type.TOP_LEFT_CORNER,
          Type.TOP_LEFT,
          Type.TOP_CENTER,
          Type.TOP_RIGHT,
          Type.TOP_RIGHT_CORNER,
          Type.LEFT_TOP,
          Type.LEFT_MIDDLE,
          Type.LEFT_BOTTOM,
          Type.RIGHT_TOP,
          Type.RIGHT_MIDDLE,
          Type.RIGHT_BOTTOM,
          Type.BOTTOM_LEFT_CORNER,
          Type.BOTTOM_LEFT,
          Type.BOTTOM_CENTER,
          Type.BOTTOM_RIGHT,
          Type.BOTTOM_RIGHT_CORNER);
  private static final Set<String> PSEUDO_PAGES = ImmutableSet.of(
      ":left", ":right", ":first");
  // The @-rules are restricted because it only makes sense for them to be used
  // inside an @media block. Especially @def should never be allowed because it
  // can be misleading; @def rules are processed by the compiler but @media
  // rules are handled by the browser.
  private static final Set<String> ALLOWED_AT_RULES_IN_MEDIA = ImmutableSet.of(
      "page", "if", "elseif", "else", "for");

  private final MutatingVisitController visitController;
  private final ErrorManager errorManager;
  private final List<CssImportRuleNode> nonIgnoredImportRules =
      Lists.newArrayList();
  private CssRootNode root;

  /**
   * The first node after which import rules will be ignored, or null if
   * no such nodes have been discovered.
   */
  private CssNode noMoreImportRules;
<commit_msg>Declare immutable type for immutable constants<commit_after>/*
 * Copyright 2009 Google Inc.
 *
 * 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.common.css.compiler.passes;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.css.compiler.ast.CssAtRuleNode;
import com.google.common.css.compiler.ast.CssAtRuleNode.Type;
import com.google.common.css.compiler.ast.CssBlockNode;
import com.google.common.css.compiler.ast.CssBooleanExpressionNode;
import com.google.common.css.compiler.ast.CssCompilerPass;
import com.google.common.css.compiler.ast.CssCompositeValueNode;
import com.google.common.css.compiler.ast.CssConditionalBlockNode;
import com.google.common.css.compiler.ast.CssDeclarationBlockNode;
import com.google.common.css.compiler.ast.CssFontFaceNode;
import com.google.common.css.compiler.ast.CssFunctionNode;
import com.google.common.css.compiler.ast.CssImportBlockNode;
import com.google.common.css.compiler.ast.CssImportRuleNode;
import com.google.common.css.compiler.ast.CssLiteralNode;
import com.google.common.css.compiler.ast.CssMediaRuleNode;
import com.google.common.css.compiler.ast.CssNode;
import com.google.common.css.compiler.ast.CssPageRuleNode;
import com.google.common.css.compiler.ast.CssPageSelectorNode;
import com.google.common.css.compiler.ast.CssRootNode;
import com.google.common.css.compiler.ast.CssRulesetNode;
import com.google.common.css.compiler.ast.CssStringNode;
import com.google.common.css.compiler.ast.CssUnknownAtRuleNode;
import com.google.common.css.compiler.ast.CssValueNode;
import com.google.common.css.compiler.ast.ErrorManager;
import com.google.common.css.compiler.ast.GssError;
import com.google.common.css.compiler.ast.MutatingVisitController;
import java.util.List;

/**
 * A compiler pass that transforms standard {@link CssUnknownAtRuleNode} instances to more specific
 * at-rule nodes, or deletes them.
 *
 */
public class CreateStandardAtRuleNodes implements UniformVisitor, CssCompilerPass {

  @VisibleForTesting static final String NO_BLOCK_ERROR_MESSAGE = "This @-rule has to have a block";

  @VisibleForTesting
  static final String BLOCK_ERROR_MESSAGE = "This @-rule is not allowed to have a block";

  @VisibleForTesting
  static final String ONLY_DECLARATION_BLOCK_ERROR_MESSAGE =
      "Only declaration blocks are allowed for this @-rule";

  @VisibleForTesting
  static final String INVALID_PARAMETERS_ERROR_MESSAGE = "This @-rule has invalid parameters";

  @VisibleForTesting
  static final String MEDIA_INVALID_CHILD_ERROR_MESSAGE =
      "This is not valid inside an @media block";

  @VisibleForTesting
  static final String MEDIA_WITHOUT_PARAMETERS_ERROR_MESSAGE = "@media without parameters";

  @VisibleForTesting
  static final String PAGE_SELECTOR_PARAMETERS_ERROR_MESSAGE =
      "Page selectors are not allowed to have parameters";

  @VisibleForTesting
  static final String FONT_FACE_PARAMETERS_ERROR_MESSAGE =
      "@font-face is not allowed to have parameters";

  @VisibleForTesting
  static final String IGNORED_IMPORT_WARNING_MESSAGE =
      "@import rules should occur outside blocks and can only be preceded "
          + "by @charset and other @import rules.";

  @VisibleForTesting
  static final String IGNORE_IMPORT_WARNING_MESSAGE =
      "A node after which all @import rule nodes are ignored is here.";

  private static final ImmutableList<Type> PAGE_SELECTORS =
      ImmutableList.of(
          Type.TOP_LEFT_CORNER,
          Type.TOP_LEFT,
          Type.TOP_CENTER,
          Type.TOP_RIGHT,
          Type.TOP_RIGHT_CORNER,
          Type.LEFT_TOP,
          Type.LEFT_MIDDLE,
          Type.LEFT_BOTTOM,
          Type.RIGHT_TOP,
          Type.RIGHT_MIDDLE,
          Type.RIGHT_BOTTOM,
          Type.BOTTOM_LEFT_CORNER,
          Type.BOTTOM_LEFT,
          Type.BOTTOM_CENTER,
          Type.BOTTOM_RIGHT,
          Type.BOTTOM_RIGHT_CORNER);
  private static final ImmutableSet<String> PSEUDO_PAGES =
      ImmutableSet.of(":left", ":right", ":first");
  // The @-rules are restricted because it only makes sense for them to be used
  // inside an @media block. Especially @def should never be allowed because it
  // can be misleading; @def rules are processed by the compiler but @media
  // rules are handled by the browser.
  private static final ImmutableSet<String> ALLOWED_AT_RULES_IN_MEDIA =
      ImmutableSet.of("page", "if", "elseif", "else", "for");

  private final MutatingVisitController visitController;
  private final ErrorManager errorManager;
  private final List<CssImportRuleNode> nonIgnoredImportRules =
      Lists.newArrayList();
  private CssRootNode root;

  /**
   * The first node after which import rules will be ignored, or null if
   * no such nodes have been discovered.
   */
  private CssNode noMoreImportRules;

{'fraction_non_alphanumeric': 0.08322604377414015, 'fraction_numerical': 0.0014713996689350744, 'mean_word_length': 4.183508102955195, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6347854', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3147, 'n_words': 857}