aboutsummaryrefslogtreecommitdiffstats
path: root/org.openembedded.bc.ui/src/org/openembedded/bc/ui/editors/bitbake/CustomFunctionRule.java
blob: 68bf0761bab30c9add63baa216821cf1e54d1edd (plain)
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
/*****************************************************************************
 * Copyright (c) 2009 Ken Gilmer
 * 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:
 *     Ken Gilmer - initial API and implementation
 *******************************************************************************/
package org.openembedded.bc.ui.editors.bitbake;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;

/**
 * Rule for def_ BB Recipe functions
 * @author kgilmer
 *
 */
final class CustomFunctionRule implements IRule {

	/** Token to return for this rule */
	private final IToken fToken;

	/**
	 * Creates a new operator rule.
	 * 
	 * @param token
	 *            Token to use for this rule
	 */
	public CustomFunctionRule(IToken token) {
		fToken = token;
	}

	public IToken evaluate(ICharacterScanner scanner) {
		if (scanner.getColumn() > 0) {
			return Token.UNDEFINED;
		}

		int i = scanner.read();
		int c = 1;
		
		if (!Character.isLetter(i) && i != 10) {
			scanner.unread();
			return Token.UNDEFINED;
		}
		
		if (i == 'd' && scanAhead(scanner, "o_".toCharArray())) {
			scanner.unread();
			return Token.UNDEFINED;
		}

		while (i != ICharacterScanner.EOF && i != 10) {
			i = scanner.read();
			c++;
			
			if (i == '(') {
				readUntil(scanner, ')');
				
				return fToken;
			}
		}

		for (int t = 0; t < c; t++) {
			scanner.unread();
		}
		
		return Token.UNDEFINED;
	}

	private void readUntil(ICharacterScanner scanner, int c) {
		int i;
		do {
			i = scanner.read();
		} while (! (i == ICharacterScanner.EOF) && ! (i == c));
	}

	private boolean scanAhead(ICharacterScanner scanner, char [] chars) {
			boolean v = true;
			for (int i = 0; i < chars.length; ++i) {
				if (! (scanner.read() == chars[i])) {
					v = false;
					for (int j = 0; j < i; ++j) {
						scanner.unread();
					}
					break;
				}
			}
			return v;
	}
}