Skip to content
Snippets Groups Projects
ISRContentAssistantRuleProcessor.java 4.95 KiB
package hterhors.editor;

import hterhors.editor.markers.IsrRuleValidator;

import java.util.ArrayList;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;

/**
 * @author  Hendrik
 */
class ISRContentAssistantRuleProcessor implements IContentAssistProcessor {
	private final IContextInformation[] NO_CONTEXTS = {};
	private ICompletionProposal[] NO_COMPLETIONS = {};
	private int override = 0;
	private String preSpace = "";
	protected static final String lineSeparator = System
			.getProperty("line.separator");
	/**
	 * Use this method to set the size for overriding by using content-assistant.
	 * @param override  The size to be override.
	 * @uml.property  name="override"
	 */
	public void setOverride(int override) {
		this.override = override;
	}

	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
			int offset) {
		try {
			IDocument document = viewer.getDocument();
			ArrayList<CompletionProposal> result = new ArrayList<CompletionProposal>();
			for (String missing : IsrRuleValidator
					.getUnusedNonterminals(document.get())) {
				result.add(new CompletionProposal(preSpace + "$" + missing
						+ " ", offset, override, 2 + missing.length()
						+ preSpace.length(), null, missing + " - Unused!",
						null, "Add the declared but not used nonterminal ("
								+ missing + ")to the rule."));
			}
			result.add(new CompletionProposal(preSpace + "| ", offset,
					override, 2 + preSpace.length(), null, "Or", null,
					"Add the or-symbol to the rule."));
			result.add(new CompletionProposal(preSpace + "$ ", offset,
					override, 1 + preSpace.length(), null, "Nonterminal", null,
					"Add New nonterminal-symbol to the current rule."));
			result.add(new CompletionProposal(
					preSpace + "!*",
					offset,
					override,
					2 + preSpace.length(),
					null,
					"New joker-symbol",
					null,
					"\"TERMINAL\" symbols are sequences of letters from the set [a-zA-Z_\\-\\\"0-9].\n"
							+ "They must be started by a letter from the subset [a-zA-Z_\\\"].\n"
							+ "The joker symbol is denoted by '!*'.\n\n"
							+ "The \" joker symbol\"  is a special terminal symbol of the grammar which\n"
							+ "must not be defined in the lexicon.\n"
							+ "If the parser is called with a terminal symbol which is defined\n"
							+ "in the lexicon and the corresponding action table entry is empty\n"
							+ "it can use this joker symbol in order to get a defined parser\n"
							+ "action. The former terminal symbol has to be parsed afterwards.\n"
							+ "The joker symbol can be used in the grammar description in order\n"
							+ "to define such parser actions. It is handled in the same way\n"
							+ "as other terminal symbols defined in the lexicon but does not\n"
							+ "correspond to an input symbol. Therefore, it can be called\n"
							+ "a virtuell symbol. A special action 'VSHIFT' (virtual shift)\n"
							+ "is introduced when generating the action table.\n"
							+ "A parser used in a speech recognizer can add a penalty to the \n"
							+ "score of the word sequence when shifting the joker symbol.  "));
			result.add(new CompletionProposal(
					preSpace + "newTerminal ",
					offset,
					override,
					preSpace.length(),
					null,
					"New terminal",
					null,
					"\"TERMINAL\" symbols are sequences of letters from the set [a-zA-Z_\\-\\\"0-9].\n"
							+ "They must be started by a letter from the subset [a-zA-Z_\\\"].\n"
							+ "The joker symbol is denoted by '!*'."));
			for (String nonterminalDec : IsrRuleValidator
					.getNonterminalsDeclarations(document.get())) {
				result.add(new CompletionProposal(

				preSpace + "$" + nonterminalDec + " ", offset, override, 2
						+ nonterminalDec.length() + preSpace.length(), null,
						nonterminalDec + " - Existing!", null,
						"Add the existing nonterminal ($" + nonterminalDec
								+ ") to the rule."));
			}
			return (ICompletionProposal[]) result
					.toArray(new ICompletionProposal[result.size()]);
		} catch (Exception e) {
			e.printStackTrace();
			return NO_COMPLETIONS;
		}
	}

	@Override
	public IContextInformation[] computeContextInformation(ITextViewer viewer,
			int offset) {
		return NO_CONTEXTS;
	}

	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		return null;
	}

	@Override
	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	@Override
	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

	@Override
	public String getErrorMessage() {
		return "ERR";
	}
}