Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d26de463225c573233a27336d9a9e521f7aedae (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.core.scripting.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.protocol.JSON;

/**
 * Script parser implementation.
 */
public class Parser {
	// Reference to the script to parse
	private final String script;

	// Define some patterns to match the lines against
	private static final Pattern EMPTY_LINE = Pattern.compile("\\s*"); //$NON-NLS-1$
	private static final Pattern COMMENT_LINE = Pattern.compile("\\s*#.*"); //$NON-NLS-1$
	private static final Pattern CONNECT_LINE = Pattern.compile("\\s*connect\\s+.*"); //$NON-NLS-1$
	private static final Pattern COMMAND_LINE = Pattern.compile("\\s*tcf\\s+(\\w+)\\s+(\\w+)(.*)"); //$NON-NLS-1$

	/**
     * Constructor.
     */
    public Parser(String script) {
    	Assert.isNotNull(script);
    	this.script = script;
    }

    /**
     * Parse the given script and returns the extracted command tokens.
     *
     * @return The list of command tokens found in the script, or an empty list.
     * @throws IOException - if the script parsing fails.
     */
    public Token[] parse() throws IOException {
    	List<Token> tokens = new ArrayList<Token>();

    	BufferedReader reader = new BufferedReader(new StringReader(script));

    	String line;
    	while ((line = reader.readLine()) != null) {
    		// All the following lines are ignored if matched
    		if (EMPTY_LINE.matcher(line).matches()) continue;
    		if (COMMENT_LINE.matcher(line).matches()) continue;
    		if (CONNECT_LINE.matcher(line).matches()) continue;

    		// If it is a command line, get the groups from it
    		Matcher matcher = COMMAND_LINE.matcher(line);
    		if (matcher.matches()) {
    			String serviceName = matcher.group(1).trim();
    			String commandName = matcher.group(2).trim();
    			String arguments = matcher.group(3);

    			// Create a new token
    			Token token = new Token();
    			token.setServiceName(serviceName);
    			token.setCommandName(commandName);

    			// Parse the arguments
    			parseArguments(token, arguments);

    			// Add the token to the list
    			tokens.add(token);
    		}
    	}

    	reader.close();

    	return tokens.toArray(new Token[tokens.size()]);
    }

    /**
     * Parse the arguments string and add the extracted arguments
     * to the given token.
     *
     * @param token The token. Must not be <code>null</code>.
     * @param arguments The arguments string or <code>null</code>.
     */
    protected void parseArguments(Token token, String arguments) {
    	Assert.isNotNull(token);

    	if (arguments == null || "".equals(arguments.trim())) { //$NON-NLS-1$
    		return;
    	}

    	// Tokenize by space, but do special handling for maps and lists
    	StringTokenizer tokenizer = new StringTokenizer(arguments, " "); //$NON-NLS-1$
    	while (tokenizer.hasMoreTokens()) {
    		String tok = tokenizer.nextToken();
    		if (tok == null || "".equals(tok.trim())) continue; //$NON-NLS-1$

    		if (tok.equals("null")) { //$NON-NLS-1$
    			token.addArgument(null);
    			continue;
    		}

    		if (tok.startsWith("\"")) { //$NON-NLS-1$
    			// String type

    			String fullTok = tok;
    			boolean complete = isComplete(fullTok, '"', '"');
    			while (!complete && tokenizer.hasMoreTokens()) {
    				fullTok = fullTok + " " + tokenizer.nextToken(); //$NON-NLS-1$
    				complete = isComplete(fullTok, '"', '"');
    			}

    			if (complete) {
    				fullTok = fullTok.trim();
    				if (fullTok.startsWith("\"")) fullTok = fullTok.substring(1); //$NON-NLS-1$
    				if (fullTok.endsWith("\"")) fullTok = fullTok.substring(0, fullTok.length() - 1); //$NON-NLS-1$
    				token.addArgument(fullTok);
    				continue;
    			}
    		}

    		if ("true".equalsIgnoreCase(tok) || "false".equalsIgnoreCase(tok)) { //$NON-NLS-1$ //$NON-NLS-2$
    			token.addArgument(Boolean.valueOf(tok));
    			continue;
    		}

    		try {
    			Integer i = Integer.decode(tok);
    			token.addArgument(i);
    			continue;
    		} catch (NumberFormatException e) { /* ignored on purpose */ }

    		try {
    			Long l = Long.decode(tok);
    			token.addArgument(l);
    			continue;
    		} catch (NumberFormatException e) { /* ignored on purpose */ }

    		try {
    			Float f = Float.valueOf(tok);
    			token.addArgument(f);
    			continue;
    		} catch (NumberFormatException e) { /* ignored on purpose */ }

    		try {
    			Double d = Double.valueOf(tok);
    			token.addArgument(d);
    			continue;
    		} catch (NumberFormatException e) { /* ignored on purpose */ }

    		// If it starts with '{' or '[', it's a map or list type
    		if (tok.startsWith("{")) { //$NON-NLS-1$
    			// Map type

    			String fullTok = tok;
    			boolean complete = isComplete(fullTok, '{', '}');
    			while (!complete && tokenizer.hasMoreTokens()) {
    				fullTok = fullTok + " " + tokenizer.nextToken(); //$NON-NLS-1$
    				complete = isComplete(fullTok, '{', '}');
    			}

    			if (complete) {
    				fullTok = fullTok + "\0"; //$NON-NLS-1$
    				try {
    					Object[] args = JSON.parseSequence(fullTok.getBytes());
    					if (args != null) {
    						for (Object arg : args) {
    							if (arg != null) token.addArgument(arg);
    						}
    						continue;
    					}
    				} catch (IOException e) { /* ignored on purpose */ e.printStackTrace(); }
    			}
    		}

    		if (tok.startsWith("[")) { //$NON-NLS-1$
    			// List type

    			String fullTok = tok;
    			boolean complete = isComplete(fullTok, '[', ']');
    			while (!complete && tokenizer.hasMoreTokens()) {
    				fullTok = fullTok + " " + tokenizer.nextToken(); //$NON-NLS-1$
    				complete = isComplete(fullTok, '[', ']');
    			}

    			if (complete) {
    				fullTok = fullTok + "\0"; //$NON-NLS-1$
    				try {
    					Object[] args = JSON.parseSequence(fullTok.getBytes());
    					if (args != null) {
    						for (Object arg : args) {
    							if (arg != null) token.addArgument(arg);
    						}
    						continue;
    					}
    				} catch (IOException e) { /* ignored on purpose */ }
    			}
    		}

    		// Add the argument token as is
    		token.addArgument(tok);
    	}
    }

    /**
     * Counts the number of opening and closing characters inside the given
     * string and returns <code>true</code> if the number matches.
     *
     * @param tok The arguments token. Must not be <code>null</code>.
     * @param opening The opening character.
     * @param closing The closing character.
     *
     * @return <code>True</code> if the number of opening characters matches the number of closing characters, <code>false</code> otherwise.
     */
    protected boolean isComplete(String tok, char opening, char closing) {
    	Assert.isNotNull(tok);

    	int countOpening = 0;
    	int countClosing = 0;

    	boolean same = opening == closing;

    	for (int i = 0; i < tok.length(); i++) {
    		char c = tok.charAt(i);

    		if (c == opening && same) {
    			if (countOpening > countClosing) countClosing++;
    			else countOpening++;
    		} else {
    			if (c == opening) { countOpening++; continue; }
    			if (c == closing) { countClosing++; continue; }
    		}
    	}

    	return countOpening > 0 && countOpening == countClosing;
    }
}

Back to the top