-
Notifications
You must be signed in to change notification settings - Fork 7
/
ArgumentParser.java
164 lines (146 loc) · 6.46 KB
/
ArgumentParser.java
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
/*******************************************************************************
* Copyright (c) 2008, 2020 SAP AG 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:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.mat.internal.snapshot;
import java.math.BigInteger;
import java.text.ParsePosition;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.internal.Messages;
import org.eclipse.mat.query.registry.CommandLine;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.util.MessageUtil;
import org.eclipse.mat.util.PatternUtil;
public final class ArgumentParser
{
private static final Pattern ADDRESS_PATTERN = Pattern.compile("^0x\\p{XDigit}+$"); //$NON-NLS-1$
/** matches definition of INTEGER_LITERAL and FLOATING_POINT_LITERAL in OQLParser.jj */
private static final Pattern OQL_NUMBER_PATTERN = Pattern.compile("^[+-]?((\\p{Digit}+[lL]?)" //$NON-NLS-1$
+ "|(\\p{Digit}+\\.\\p{Digit}*([eE][+-]?\\p{Digit}+)?[fFdD]?)" //$NON-NLS-1$
+ "|(\\.\\p{Digit}*([eE][+-]?\\p{Digit}+)?[fFdD]?)" //$NON-NLS-1$
+ "|(\\p{Digit}+([eE][+-]?\\p{Digit}+)[fFdD]?)" //$NON-NLS-1$
+ "|(\\p{Digit}+([eE][+-]?\\p{Digit}+)?[fFdD])" //$NON-NLS-1$
+ ")$"); //$NON-NLS-1$
public static HeapObjectParamArgument consumeHeapObjects(ISnapshot snapshot, String line) throws SnapshotException
{
String[] args = CommandLine.tokenize(line);
ParsePosition pos = new ParsePosition(0);
HeapObjectParamArgument hopa = consumeHeapObjects(snapshot, args, pos);
if (pos.getIndex() < args.length)
{
StringBuilder buf = new StringBuilder();
while (pos.getIndex() < args.length)
{
buf.append(args[pos.getIndex()]).append(" "); //$NON-NLS-1$
pos.setIndex(pos.getIndex() + 1);
}
throw new SnapshotException(MessageUtil.format(Messages.ArgumentParser_ErrorMsg_Unparsed, new Object[] { buf
.toString() }));
}
return hopa;
}
public static HeapObjectParamArgument consumeHeapObjects(ISnapshot snapshot, String[] args, ParsePosition pos)
throws SnapshotException
{
if (pos.getIndex() >= args.length)
return null;
HeapObjectParamArgument proxy = new HeapObjectParamArgument(snapshot);
while (pos.getIndex() < args.length)
{
String arg = args[pos.getIndex()];
if (arg == null)
{
pos.setIndex(pos.getIndex() + 1);
}
else if (HeapObjectParamArgument.Flags.VERBOSE.equals(arg))
{
pos.setIndex(pos.getIndex() + 1);
proxy.setVerbose(true);
}
else if (HeapObjectParamArgument.Flags.INCLUDE_SUBCLASSES.equals(arg))
{
pos.setIndex(pos.getIndex() + 1);
proxy.setIncludeSubclasses(true);
}
else if (HeapObjectParamArgument.Flags.INCLUDE_CLASS_INSTANCE.equals(arg))
{
pos.setIndex(pos.getIndex() + 1);
proxy.setIncludeClassInstance(true);
}
else if (HeapObjectParamArgument.Flags.INCLUDE_LOADED_INSTANCES.equals(arg))
{
pos.setIndex(pos.getIndex() + 1);
proxy.setIncludeLoadedInstances(true);
}
else if (HeapObjectParamArgument.Flags.RETAINED.equals(arg))
{
pos.setIndex(pos.getIndex() + 1);
proxy.setRetained(true);
}
else if (arg.charAt(0) == '-')
{
break;
}
else if (ADDRESS_PATTERN.matcher(arg).matches())
{
long address = new BigInteger(arg.substring(2), 16).longValue();
proxy.addObjectAddress(address);
pos.setIndex(pos.getIndex() + 1);
}
else if ("select".equalsIgnoreCase(arg)) //$NON-NLS-1$
{
StringBuilder query = new StringBuilder(128);
query.append(arg);
pos.setIndex(pos.getIndex() + 1);
while (pos.getIndex() < args.length)
{
arg = args[pos.getIndex()];
if (arg.length() == 1 && arg.charAt(0) == ';')
{
pos.setIndex(pos.getIndex() + 1);
break;
}
else if (arg.charAt(arg.length() - 1) == ';')
{
query.append(" ").append(arg.substring(0, arg.length() - 1)); //$NON-NLS-1$
pos.setIndex(pos.getIndex() + 1);
break;
}
else if (arg.charAt(0) == '-' && arg.length() > 1)
{
// Already excluded OQL '-' operator
// OQL numbers look a bit like another flag argument
if (!OQL_NUMBER_PATTERN.matcher(arg).matches())
break;
}
query.append(" ").append(arg); //$NON-NLS-1$
pos.setIndex(pos.getIndex() + 1);
}
proxy.addOql(query.toString());
}
else
{
try
{
Pattern pattern = Pattern.compile(PatternUtil.smartFix(arg, false));
proxy.addPattern(pattern);
pos.setIndex(pos.getIndex() + 1);
}
catch (PatternSyntaxException e)
{
throw new SnapshotException(MessageUtil.format(Messages.ArgumentParser_ErrorMsg_ParsingError, arg, e.getMessage()),
e);
}
}
}
return proxy;
}
}