Package com.google.caja.parser.quasiliteral

Source Code of com.google.caja.parser.quasiliteral.NonAsciiCheckVisitor

// Copyright (C) 2008 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.caja.parser.quasiliteral;

import com.google.caja.parser.ParseTreeNodeVisitor;
import com.google.caja.parser.ParseTreeNode;
import com.google.caja.parser.js.Identifier;
import com.google.caja.parser.js.SyntheticNodes;
import com.google.caja.reporting.MessageQueue;

/**
* Non-ASCII characters can change the semantics of a program depending on how
* they're rendered, so we forbid them outside of strings.
*
* @author metaweta@gmail.com (Mike Stay)
*/
public final class NonAsciiCheckVisitor implements ParseTreeNodeVisitor {
  private final MessageQueue mq;

  public NonAsciiCheckVisitor(MessageQueue mq) {
    this.mq = mq;
  }

  /**
   * Add an error to the queue if an identifier contains non-ASCII characters.
   */
  public boolean visit(ParseTreeNode node) {
    if (!(node instanceof Identifier)) { return true; }
    Identifier ident = (Identifier) node;
    String name = ident.getName();
    if (!SyntheticNodes.is(ident)
        && name != null && !name.matches("^[a-zA-Z_$][a-zA-Z0-9_$]*$")) {
      mq.addMessage(
          RewriterMessageType.NONASCII_IDENTIFIER,
          node.getFilePosition(), node);
    }
    return true;
  }
}
TOP

Related Classes of com.google.caja.parser.quasiliteral.NonAsciiCheckVisitor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.