⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 directive.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2003-2005 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal.preprocessor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;

import eclipseme.core.internal.EclipseMECorePlugin;

/**
 * Represents a preprocessor directive within the source code.
 * <p />
 * Copyright (c) 2003-2005 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.5 $
 * <br>
 * $Date: 2006/11/12 01:11:00 $
 * <br>
 * @author Craig Setera
 */
public abstract class Directive {
	public static final int TYPE_DEFINE = 1;
	public static final int TYPE_UNDEFINE = 2;
	public static final int TYPE_IFDEF = 3;
	public static final int TYPE_IFNDEF = 4;
	public static final int TYPE_IF = 5;
	public static final int TYPE_ELIF = 6;
	public static final int TYPE_ELIFDEF = 7;
	public static final int TYPE_ELIFNDEF = 8;
	public static final int TYPE_ELSE = 9;
	public static final int TYPE_ENDIF = 10;
	public static final int TYPE_INCLUDE = 11;
	public static final int TYPE_ENDINCLUDE = 12;
	
	private static final String ARGUMENT_REGEX = "(\\S*)\\s+(.*)";
	private static final Pattern ARGUMENT_PATTERN = Pattern.compile(ARGUMENT_REGEX);
	
	protected IResource resource;
	protected Comment comment;
	protected String commentText;
	
	/**
	 * Construct a new fully-formed directive object.
	 * 
	 * @param compilationUnit
	 * @param comment
	 * @param commentText
	 */
	protected Directive(IResource resource, Comment comment, String commentText) 
	{
		super();
		this.resource = resource;
		this.comment = comment;
		this.commentText = commentText;
	}

	/**
	 * Execute the function of this directive.
	 * @param processingState TODO
	 * @param monitor TODO
	 * @throws CoreException 
	 */
	public void execute(ProcessingState processingState, IProgressMonitor monitor) 
		throws CoreException 
	{
		markAsUnsupported();
	}

	/**
	 * @return Returns the comment.
	 */
	public Comment getComment() {
		return comment;
	}

	/**
	 * @return Returns the commentText.
	 */
	public String getCommentText() {
		return commentText;
	}

	/**
	 * Return the arguments to the directive.  This
	 * may be an empty string if there are no arguments
	 * to be returned.
	 * 
	 * @return
	 */
	public String getDirectiveArguments() {
		String arguments = "";
		
		Matcher matcher = ARGUMENT_PATTERN.matcher(getCommentText());
		if (matcher.find()) {
			arguments = matcher.group(2).trim();
		}
		
		return arguments;
	}
	
	/**
	 * Return the region covered by this directive.
	 * 
	 * @return
	 * @throws CoreException
	 */
	public IRegion getRegion()
		throws CoreException
	{
		// By default, return the region covered by the comment
		return new Region(
			comment.getStartPosition(),
			comment.getLength());
	}
	
	/**
	 * Return the type of this directive.
	 * 
	 * @return
	 */
	public abstract int getType();
	
	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return commentText;
	}

	/**
	 * Return a boolean indicating whether or not this
	 * directive is a conditional block directive.
	 * 
	 * @return
	 */
	public boolean isConditionalDirective() {
		return false;
	}
	
	/**
	 * Return a boolean indicating whether this block directive is the 
	 * start of a block.
	 * 
	 * @return
	 */
	public boolean isContainerEnd() {
		return false;
	}

	/**
	 * Return a boolean indicating whether this block directive is the 
	 * start of a new block.
	 * 
	 * @return
	 */
	public boolean isContainerStart() {
		return false;
	}
	
	/**
	 * Create a new preprocessor problem marker for this directive
	 * with the specified information.
	 * 
	 * @param severity
	 * @param text
	 * @throws CoreException
	 */
	protected void createProblemMarker(int severity, String text) 
		throws CoreException
	{
		IMarker marker = 
			resource.createMarker(Preprocessor.PROBLEM_MARKER);
		marker.setAttribute(IMarker.MESSAGE, text);
		marker.setAttribute(IMarker.SEVERITY, severity);
		
		int start = comment.getStartPosition();
		int end = start + comment.getLength();
		marker.setAttribute(IMarker.CHAR_START, start);
		marker.setAttribute(IMarker.CHAR_END, end);
	}
	
	/**
	 * Return the line on which this directive currently resides.  This
	 * method accounts for the movement that may have occurred due to
	 * insertions of comments and includes.
	 * 
	 * @param state
	 * @return
	 * @throws CoreException
	 */
	protected int getLine(ProcessingState state)
		throws CoreException
	{
		return getLineOfOffset(state, getRegion().getOffset());
	}
	
	/**
	 * Return the line on which the specified offset currently resides.  This
	 * method accounts for the movement that may have occurred due to
	 * insertions of comments and includes.
	 * 
	 * @param state
	 * @return
	 * @throws CoreException
	 */
	protected int getLineOfOffset(ProcessingState state, int offset)
		throws CoreException
	{
		int line = -1;
		int startOffset = offset + state.insertedCharacterCount;
		
		try {
			line = state.document.getLineOfOffset(startOffset);
		} catch (BadLocationException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
		}
		
		return line;
	}
	
	/**
	 * Insert the specified text into the document being processed at the
	 * specified offset.
	 * 
	 * @param state
	 * @param offset
	 * @param text
	 * @throws CoreException
	 */
	protected void insertText(ProcessingState state, int offset, String text) 
		throws CoreException 
	{
		try {
			state.document.replace(offset, 0, text);
			state.insertedCharacterCount += text.length();
		} catch (BadLocationException e) {
			EclipseMECorePlugin.throwCoreException(IStatus.ERROR, -999, e);
		}
	}
	
	/**
	 * Mark this particular directive as being unsupported at this time.
	 * 
	 * @throws CoreException 
	 */
	protected void markAsUnsupported() 
		throws CoreException
	{
		createProblemMarker(IMarker.SEVERITY_WARNING, "Unsupported preprocessing directive");
	}

	/**
	 * Validate an expected identifier.  Return a boolean indicating
	 * whether or not the identifier appears valid.
	 * 
	 * @param arguments
	 * @throws CoreException
	 */
	protected boolean validateIdentifier(String arguments) throws CoreException {
		boolean valid = true;
		
		String[] split = arguments.split("\\s");
		if (split.length != 1) {
			createProblemMarker(IMarker.SEVERITY_ERROR, "Identifier expected");
			valid = false;
		}
		
		return valid;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -