Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Stieber2011-12-14 09:11:03 +0000
committerUwe Stieber2011-12-14 09:11:03 +0000
commit3feceeebed629568a597abc1a2526fa3b0b8ecff (patch)
tree5c1eb2f87b9e847eab92c16219eb2fe5d1fca8d8 /target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser
parentdd980d901b6e2501640d1c732776d4baed021752 (diff)
downloadorg.eclipse.tcf-3feceeebed629568a597abc1a2526fa3b0b8ecff.tar.gz
org.eclipse.tcf-3feceeebed629568a597abc1a2526fa3b0b8ecff.tar.xz
org.eclipse.tcf-3feceeebed629568a597abc1a2526fa3b0b8ecff.zip
Target Explorer: Added scripting core plugin and updated features
Diffstat (limited to 'target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser')
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Parser.java171
-rw-r--r--target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Token.java91
2 files changed, 262 insertions, 0 deletions
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Parser.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Parser.java
new file mode 100644
index 000000000..409c9e1ca
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Parser.java
@@ -0,0 +1,171 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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;
+
+/**
+ * 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);
+ }
+ }
+
+ 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 ("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) {}
+
+ try {
+ Long l = Long.decode(tok);
+ token.addArgument(l);
+ continue;
+ } catch (NumberFormatException e) {}
+
+ try {
+ Float f = Float.valueOf(tok);
+ token.addArgument(f);
+ continue;
+ } catch (NumberFormatException e) {}
+
+ try {
+ Double d = Double.valueOf(tok);
+ token.addArgument(d);
+ continue;
+ } catch (NumberFormatException e) {}
+
+ // If it starts with '{' or '[', it's a map or list type
+ if (tok.startsWith("{")) { //$NON-NLS-1$
+ // Map type
+ }
+
+ if (tok.startsWith("[")) { //$NON-NLS-1$
+ // List type
+ }
+
+ // 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;
+
+ for (int i = 0; i < tok.length(); i++) {
+ char c = tok.charAt(i);
+ if (c == opening) { countOpening++; continue; }
+ if (c == closing) { countClosing++; continue; }
+ }
+
+ return countOpening > 0 && countOpening == countClosing;
+ }
+}
diff --git a/target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Token.java b/target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Token.java
new file mode 100644
index 000000000..18c838c43
--- /dev/null
+++ b/target_explorer/plugins/org.eclipse.tcf.te.tcf.core.scripting/src/org/eclipse/tcf/te/tcf/core/scripting/parser/Token.java
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.Assert;
+
+/**
+ * Script token. Created by the script parser on parsing the script.
+ */
+public final class Token {
+ // The service name
+ private String serviceName;
+ // The command name
+ private String commandName;
+ // The command arguments
+ private List<Object> arguments;
+
+ /**
+ * Constructor.
+ */
+ public Token() {
+ }
+
+ /**
+ * Sets the service name.
+ *
+ * @param serviceName The service name. Must not be <code>null</code>.
+ */
+ public void setServiceName(String serviceName) {
+ Assert.isNotNull(serviceName);
+ this.serviceName = serviceName;
+ }
+
+ /**
+ * Gets the service name.
+ *
+ * @return The service name or <code>null</code>.
+ */
+ public String getServiceName() {
+ return serviceName;
+ }
+
+ /**
+ * Sets the command name.
+ *
+ * @param commandName The command name. Must not be <code>null</code>.
+ */
+ public void setCommandName(String commandName) {
+ Assert.isNotNull(commandName);
+ this.commandName = commandName;
+ }
+
+ /**
+ * Returns the command name.
+ *
+ * @return The command name or <code>null</code>.
+ */
+ public String getCommandName() {
+ return commandName;
+ }
+
+ /**
+ * Adds an argument to the command arguments list.
+ *
+ * @param arg The argument. Must not be <code>null</code>.
+ */
+ public void addArgument(Object arg) {
+ Assert.isNotNull(arg);
+ if (arguments == null) arguments = new ArrayList<Object>();
+ arguments.add(arg);
+ }
+
+ /**
+ * Returns the command arguments.
+ *
+ * @return The command arguments or an empty array.
+ */
+ public Object[] getArguments() {
+ return arguments != null ? arguments.toArray() : new Object[0];
+ }
+}

Back to the top