Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cf4ed568a9237220bbb4429891258c393e81795c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************
 * Copyright (c) 2013, 2013 Andrew Gvozdev and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Andrew Gvozdev - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.internal.ui.text.makefile;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WordRule;

public class FunctionReferenceRule extends WordRule {
	/** Buffer used for pattern detection. */
	private StringBuffer fBuffer= new StringBuffer();

	@SuppressWarnings("nls")
	private final static String[] functions = {
		"subst", "patsubst", "strip", "findstring",
		"filter", "filter-out", "sort",
		"word", "words", "wordlist", "firstword", "lastword",
		"dir", "notdir",
		"suffix", "basename", "addsuffix", "addprefix",
		"join", "wildcard", "realpath", "abspath",
		"if", "or", "and", "foreach",
		"call", "value", "eval", "origin", "flavor",
		"shell", "error", "warning", "info",
	};

	static class TagDetector implements IWordDetector {
		private boolean isClosedBracket = false;
		private int bracketNesting = 0;
		@Override
		public boolean isWordStart(char c) {
			isClosedBracket = c == ')';
			return isClosedBracket || c == '$';
		}
		@Override
		public boolean isWordPart(char c) {
			return !isClosedBracket && (c == '$' || c == '(' || Character.isJavaIdentifierPart(c) || c == '-');
		}
	}

	public FunctionReferenceRule(IToken token) {
		super(new TagDetector());
		for (String f : functions) {
			addWord("$(" + f, token); //$NON-NLS-1$
			addWord("$$(" + f, token); //$NON-NLS-1$
		}
		addWord(")", token); //$NON-NLS-1$
	}

	@Override
	public IToken evaluate(ICharacterScanner scanner) {
		int c= scanner.read();
		if (c == ')') {
			if (((TagDetector)fDetector).bracketNesting > 0) {
				((TagDetector)fDetector).bracketNesting--;
				return (IToken)fWords.get(")"); //$NON-NLS-1$
			}
			return fDefaultToken;
		}

		if (c != ICharacterScanner.EOF && fDetector.isWordStart((char) c)) {
			if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {

				fBuffer.setLength(0);
				do {
					fBuffer.append((char) c);
					c= scanner.read();
				} while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
				scanner.unread();

				String buffer= fBuffer.toString();

				IToken token= (IToken)fWords.get(buffer);

				if (token != null) {
					if (buffer.equals("$(call") || buffer.equals("$$(call")) {
						if ((char)scanner.read() == ' ') {
							do {
								c= scanner.read();
							} while (c == '(' || c == ')' || fDetector.isWordPart((char) c));
						}
						scanner.unread();
					}
					((TagDetector)fDetector).bracketNesting++;
					return token;
				}

				if (fDefaultToken.isUndefined())
					unreadBuffer(scanner);

				return fDefaultToken;
			}
		}

		scanner.unread();
		return Token.UNDEFINED;
	}

	@Override
	protected void unreadBuffer(ICharacterScanner scanner) {
		for (int i= fBuffer.length() - 1; i >= 0; i--)
			scanner.unread();
	}
}

Back to the top