aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core.test/src/org/jacoco/core/test/validation/StatementParser.java
blob: 7ee75c7e39704692ce84d232fa5e1e84938dc8d2 (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
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
/*******************************************************************************
 * Copyright (c) 2009, 2022 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.core.test.validation;

import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Simple parser for Java like method invocation statements. For example:
 *
 * <pre>
 * foo("BAR", 42)
 * </pre>
 *
 * Method parameters can only be String or int literals.
 */
class StatementParser {

	/**
	 * Call-back interface for parsed statements.
	 */
	public interface IStatementVisitor {

		/**
		 * Called when a method invocation has been paresed.
		 *
		 * @param ctx
		 *            context information for error messages
		 * @param name
		 *            local method name
		 * @param args
		 *            argument values
		 */
		void visitInvocation(String ctx, String name, Object... args);

	}

	/**
	 * Parses the given source.
	 *
	 * @param source
	 *            source string to parse
	 * @param visitor
	 *            visitor to emit parsed statements
	 * @param ctx
	 *            context information to include in error messages
	 */
	public static void parse(String source, IStatementVisitor visitor,
			String ctx) throws IOException {
		new StatementParser(source, visitor, ctx).parse();
	}

	private final IStatementVisitor visitor;
	private final StreamTokenizer tokenizer;
	private final String ctx;

	private StatementParser(String source, IStatementVisitor visitor,
			String ctx) {
		this.visitor = visitor;
		this.ctx = ctx;
		tokenizer = new StreamTokenizer(new StringReader(source));
		tokenizer.resetSyntax();
		tokenizer.whitespaceChars(' ', ' ');
		tokenizer.whitespaceChars('\t', '\t');
		tokenizer.wordChars('a', 'z');
		tokenizer.wordChars('A', 'Z');
		tokenizer.quoteChar('"');
		tokenizer.parseNumbers();
	}

	private void parse() throws IOException {
		while (!accept(StreamTokenizer.TT_EOF)) {
			invocation();
		}
	}

	private void invocation() throws IOException {
		final String name = expect(StreamTokenizer.TT_WORD).sval;
		final List<Object> args = new ArrayList<Object>();
		expect('(');
		if (!accept(')')) {
			args.add(argument());
			while (!accept(')')) {
				expect(',');
				args.add(argument());
			}
		}
		visitor.visitInvocation(ctx, name, args.toArray());
	}

	private Object argument() throws IOException {
		if (accept(StreamTokenizer.TT_NUMBER)) {
			return Integer.valueOf((int) tokenizer.nval);
		}
		if (accept('"')) {
			return tokenizer.sval;
		}
		throw syntaxError();
	}

	private boolean accept(final int type) throws IOException {
		final boolean match = tokenizer.nextToken() == type;
		if (!match) {
			tokenizer.pushBack();
		}
		return match;
	}

	private StreamTokenizer expect(final int type) throws IOException {
		if (tokenizer.nextToken() != type) {
			throw syntaxError();
		}
		return tokenizer;
	}

	private IOException syntaxError() {
		return new IOException("Invalid syntax (" + ctx + ")");
	}

}