expression(int)   F
last analyzed

Complexity

Conditions 16

Size

Total Lines 133
Code Lines 112

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 112
dl 0
loc 133
rs 1.68
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like yalep.antlr.generated.LogicalExpressionParser.expression(int) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// Generated from /home/dimakl/IdeaProjects/inflate-my-mind/src/main/java/com/inflatemymind/antlr/LogicalExpression.g4 by ANTLR 4.7.2
2
package yalep.antlr.generated;
3
import org.antlr.v4.runtime.atn.*;
4
import org.antlr.v4.runtime.dfa.DFA;
5
import org.antlr.v4.runtime.*;
6
import org.antlr.v4.runtime.tree.*;
7
import java.util.List;
8
9
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
10
public class LogicalExpressionParser extends Parser {
11
	static { RuntimeMetaData.checkVersion("4.7.2", RuntimeMetaData.VERSION); }
12
13
	protected static final DFA[] _decisionToDFA;
14
	protected static final PredictionContextCache _sharedContextCache =
15
		new PredictionContextCache();
16
	public static final int
17
		LPAREN=1, RPAREN=2, NOT=3, AND=4, OR=5, XOR=6, VARIABLE=7;
18
	public static final int
19
		RULE_eval = 0, RULE_expression = 1;
20
	private static String[] makeRuleNames() {
21
		return new String[] {
22
			"eval", "expression"
23
		};
24
	}
25
	public static final String[] ruleNames = makeRuleNames();
26
27
	private static String[] makeLiteralNames() {
28
		return new String[] {
29
			null, "'('", "')'", "'!'", "'&'", "'|'", "'^'"
30
		};
31
	}
32
	private static final String[] _LITERAL_NAMES = makeLiteralNames();
33
	private static String[] makeSymbolicNames() {
34
		return new String[] {
35
			null, "LPAREN", "RPAREN", "NOT", "AND", "OR", "XOR", "VARIABLE"
36
		};
37
	}
38
	private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
39
	public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
40
41
	/**
42
	 * @deprecated Use {@link #VOCABULARY} instead.
43
	 */
44
	@Deprecated
45
	public static final String[] tokenNames;
46
	static {
47
		tokenNames = new String[_SYMBOLIC_NAMES.length];
48 View Code Duplication
		for (int i = 0; i < tokenNames.length; i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
49
			tokenNames[i] = VOCABULARY.getLiteralName(i);
50
			if (tokenNames[i] == null) {
51
				tokenNames[i] = VOCABULARY.getSymbolicName(i);
52
			}
53
54
			if (tokenNames[i] == null) {
55
				tokenNames[i] = "<INVALID>";
56
			}
57
		}
58
	}
59
60
	@Override
61
	@Deprecated
62
	public String[] getTokenNames() {
63
		return tokenNames;
64
	}
65
66
	@Override
67
68
	public Vocabulary getVocabulary() {
69
		return VOCABULARY;
70
	}
71
72
	@Override
73
	public String getGrammarFileName() { return "LogicalExpression.g4"; }
74
75
	@Override
76
	public String[] getRuleNames() { return ruleNames; }
77
78
	@Override
79
	public String getSerializedATN() { return _serializedATN; }
80
81
	@Override
82
	public ATN getATN() { return _ATN; }
83
84
	public LogicalExpressionParser(TokenStream input) {
85
		super(input);
86
		_interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
87
	}
88
89
	public static class EvalContext extends ParserRuleContext {
90
		public ExpressionContext expression() {
91
			return getRuleContext(ExpressionContext.class,0);
92
		}
93
		public TerminalNode EOF() { return getToken(LogicalExpressionParser.EOF, 0); }
94
		public EvalContext(ParserRuleContext parent, int invokingState) {
95
			super(parent, invokingState);
96
		}
97
		@Override public int getRuleIndex() { return RULE_eval; }
98
		@Override
99
		public void enterRule(ParseTreeListener listener) {
100
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).enterEval(this);
101
		}
102
		@Override
103
		public void exitRule(ParseTreeListener listener) {
104
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).exitEval(this);
105
		}
106
	}
107
108
	public final EvalContext eval() throws RecognitionException {
109
		EvalContext _localctx = new EvalContext(_ctx, getState());
110
		enterRule(_localctx, 0, RULE_eval);
111
		try {
112
			enterOuterAlt(_localctx, 1);
113
			{
114
			setState(4);
115
			expression(0);
116
			setState(5);
117
			match(EOF);
118
			}
119
		}
120
		catch (RecognitionException re) {
121
			_localctx.exception = re;
122
			_errHandler.reportError(this, re);
123
			_errHandler.recover(this, re);
124
		}
125
		finally {
126
			exitRule();
127
		}
128
		return _localctx;
129
	}
130
131
	public static class ExpressionContext extends ParserRuleContext {
132
		public ExpressionContext(ParserRuleContext parent, int invokingState) {
133
			super(parent, invokingState);
134
		}
135
		@Override public int getRuleIndex() { return RULE_expression; }
136
	 
137
		public ExpressionContext() { }
138
		public void copyFrom(ExpressionContext ctx) {
139
			super.copyFrom(ctx);
140
		}
141
	}
142
	public static class ORContext extends ExpressionContext {
143
		public List<ExpressionContext> expression() {
144
			return getRuleContexts(ExpressionContext.class);
145
		}
146
		public ExpressionContext expression(int i) {
147
			return getRuleContext(ExpressionContext.class,i);
148
		}
149
		public TerminalNode OR() { return getToken(LogicalExpressionParser.OR, 0); }
150
		public ORContext(ExpressionContext ctx) { copyFrom(ctx); }
151
		@Override
152
		public void enterRule(ParseTreeListener listener) {
153
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).enterOR(this);
154
		}
155
		@Override
156
		public void exitRule(ParseTreeListener listener) {
157
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).exitOR(this);
158
		}
159
	}
160
	public static class PARENSContext extends ExpressionContext {
161
		public TerminalNode LPAREN() { return getToken(LogicalExpressionParser.LPAREN, 0); }
162
		public ExpressionContext expression() {
163
			return getRuleContext(ExpressionContext.class,0);
164
		}
165
		public TerminalNode RPAREN() { return getToken(LogicalExpressionParser.RPAREN, 0); }
166
		public List<TerminalNode> NOT() { return getTokens(LogicalExpressionParser.NOT); }
167
		public TerminalNode NOT(int i) {
168
			return getToken(LogicalExpressionParser.NOT, i);
169
		}
170
		public PARENSContext(ExpressionContext ctx) { copyFrom(ctx); }
171
		@Override
172
		public void enterRule(ParseTreeListener listener) {
173
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).enterPARENS(this);
174
		}
175
		@Override
176
		public void exitRule(ParseTreeListener listener) {
177
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).exitPARENS(this);
178
		}
179
	}
180
	public static class VARIABLEContext extends ExpressionContext {
181
		public TerminalNode VARIABLE() { return getToken(LogicalExpressionParser.VARIABLE, 0); }
182
		public List<TerminalNode> NOT() { return getTokens(LogicalExpressionParser.NOT); }
183
		public TerminalNode NOT(int i) {
184
			return getToken(LogicalExpressionParser.NOT, i);
185
		}
186
		public VARIABLEContext(ExpressionContext ctx) { copyFrom(ctx); }
187
		@Override
188
		public void enterRule(ParseTreeListener listener) {
189
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).enterVARIABLE(this);
190
		}
191
		@Override
192
		public void exitRule(ParseTreeListener listener) {
193
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).exitVARIABLE(this);
194
		}
195
	}
196
	public static class ANDContext extends ExpressionContext {
197
		public List<ExpressionContext> expression() {
198
			return getRuleContexts(ExpressionContext.class);
199
		}
200
		public ExpressionContext expression(int i) {
201
			return getRuleContext(ExpressionContext.class,i);
202
		}
203
		public TerminalNode AND() { return getToken(LogicalExpressionParser.AND, 0); }
204
		public ANDContext(ExpressionContext ctx) { copyFrom(ctx); }
205
		@Override
206
		public void enterRule(ParseTreeListener listener) {
207
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).enterAND(this);
208
		}
209
		@Override
210
		public void exitRule(ParseTreeListener listener) {
211
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).exitAND(this);
212
		}
213
	}
214
	public static class XORContext extends ExpressionContext {
215
		public List<ExpressionContext> expression() {
216
			return getRuleContexts(ExpressionContext.class);
217
		}
218
		public ExpressionContext expression(int i) {
219
			return getRuleContext(ExpressionContext.class,i);
220
		}
221
		public TerminalNode XOR() { return getToken(LogicalExpressionParser.XOR, 0); }
222
		public XORContext(ExpressionContext ctx) { copyFrom(ctx); }
223
		@Override
224
		public void enterRule(ParseTreeListener listener) {
225
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).enterXOR(this);
226
		}
227
		@Override
228
		public void exitRule(ParseTreeListener listener) {
229
			if ( listener instanceof LogicalExpressionListener ) ((LogicalExpressionListener)listener).exitXOR(this);
230
		}
231
	}
232
233
	public final ExpressionContext expression() throws RecognitionException {
234
		return expression(0);
235
	}
236
237
	private ExpressionContext expression(int _p) throws RecognitionException {
238
		ParserRuleContext _parentctx = _ctx;
239
		int _parentState = getState();
240
		ExpressionContext _localctx = new ExpressionContext(_ctx, _parentState);
241
		ExpressionContext _prevctx = _localctx;
242
		int _startState = 2;
243
		enterRecursionRule(_localctx, 2, RULE_expression, _p);
244
		int _la;
245
		try {
246
			int _alt;
247
			enterOuterAlt(_localctx, 1);
248
			{
249
			setState(25);
250
			_errHandler.sync(this);
251
			switch ( getInterpreter().adaptivePredict(_input,2,_ctx) ) {
252
			case 1:
253
				{
254
				_localctx = new PARENSContext(_localctx);
255
				_ctx = _localctx;
256
				_prevctx = _localctx;
257
258
				setState(11);
259
				_errHandler.sync(this);
260
				_la = _input.LA(1);
261
				while (_la==NOT) {
262
					{
263
					{
264
					setState(8);
265
					match(NOT);
266
					}
267
					}
268
					setState(13);
269
					_errHandler.sync(this);
270
					_la = _input.LA(1);
271
				}
272
				setState(14);
273
				match(LPAREN);
274
				setState(15);
275
				expression(0);
276
				setState(16);
277
				match(RPAREN);
278
				}
279
				break;
280
			case 2:
281
				{
282
				_localctx = new VARIABLEContext(_localctx);
283
				_ctx = _localctx;
284
				_prevctx = _localctx;
285
				setState(21);
286
				_errHandler.sync(this);
287
				_la = _input.LA(1);
288
				while (_la==NOT) {
289
					{
290
					{
291
					setState(18);
292
					match(NOT);
293
					}
294
					}
295
					setState(23);
296
					_errHandler.sync(this);
297
					_la = _input.LA(1);
298
				}
299
				setState(24);
300
				match(VARIABLE);
301
				}
302
				break;
303
			}
304
			_ctx.stop = _input.LT(-1);
305
			setState(38);
306
			_errHandler.sync(this);
307
			_alt = getInterpreter().adaptivePredict(_input,4,_ctx);
308
			while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) {
309
				if ( _alt==1 ) {
310
					if ( _parseListeners!=null ) triggerExitRuleEvent();
311
					_prevctx = _localctx;
312
					{
313
					setState(36);
314
					_errHandler.sync(this);
315
					switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) {
316
					case 1:
317
						{
318
						_localctx = new XORContext(new ExpressionContext(_parentctx, _parentState));
319
						pushNewRecursionContext(_localctx, _startState, RULE_expression);
320
						setState(27);
321
						if (!(precpred(_ctx, 5))) throw new FailedPredicateException(this, "precpred(_ctx, 5)");
322
						setState(28);
323
						match(XOR);
324
						setState(29);
325
						expression(6);
326
						}
327
						break;
328
					case 2:
329
						{
330
						_localctx = new ANDContext(new ExpressionContext(_parentctx, _parentState));
331
						pushNewRecursionContext(_localctx, _startState, RULE_expression);
332
						setState(30);
333
						if (!(precpred(_ctx, 4))) throw new FailedPredicateException(this, "precpred(_ctx, 4)");
334
						setState(31);
335
						match(AND);
336
						setState(32);
337
						expression(5);
338
						}
339
						break;
340
					case 3:
341
						{
342
						_localctx = new ORContext(new ExpressionContext(_parentctx, _parentState));
343
						pushNewRecursionContext(_localctx, _startState, RULE_expression);
344
						setState(33);
345
						if (!(precpred(_ctx, 3))) throw new FailedPredicateException(this, "precpred(_ctx, 3)");
346
						setState(34);
347
						match(OR);
348
						setState(35);
349
						expression(4);
350
						}
351
						break;
352
					}
353
					} 
354
				}
355
				setState(40);
356
				_errHandler.sync(this);
357
				_alt = getInterpreter().adaptivePredict(_input,4,_ctx);
358
			}
359
			}
360
		}
361
		catch (RecognitionException re) {
362
			_localctx.exception = re;
363
			_errHandler.reportError(this, re);
364
			_errHandler.recover(this, re);
365
		}
366
		finally {
367
			unrollRecursionContexts(_parentctx);
368
		}
369
		return _localctx;
370
	}
371
372
	public boolean sempred(RuleContext _localctx, int ruleIndex, int predIndex) {
373
		switch (ruleIndex) {
374
		case 1:
375
			return expression_sempred((ExpressionContext)_localctx, predIndex);
376
		}
377
		return true;
378
	}
379
	private boolean expression_sempred(ExpressionContext _localctx, int predIndex) {
380
		switch (predIndex) {
381
		case 0:
382
			return precpred(_ctx, 5);
383
		case 1:
384
			return precpred(_ctx, 4);
385
		case 2:
386
			return precpred(_ctx, 3);
387
		}
388
		return true;
389
	}
390
391
	public static final String _serializedATN =
392
		"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\t,\4\2\t\2\4\3\t"+
393
		"\3\3\2\3\2\3\2\3\3\3\3\7\3\f\n\3\f\3\16\3\17\13\3\3\3\3\3\3\3\3\3\3\3"+
394
		"\7\3\26\n\3\f\3\16\3\31\13\3\3\3\5\3\34\n\3\3\3\3\3\3\3\3\3\3\3\3\3\3"+
395
		"\3\3\3\3\3\7\3\'\n\3\f\3\16\3*\13\3\3\3\2\3\4\4\2\4\2\2\2/\2\6\3\2\2\2"+
396
		"\4\33\3\2\2\2\6\7\5\4\3\2\7\b\7\2\2\3\b\3\3\2\2\2\t\r\b\3\1\2\n\f\7\5"+
397
		"\2\2\13\n\3\2\2\2\f\17\3\2\2\2\r\13\3\2\2\2\r\16\3\2\2\2\16\20\3\2\2\2"+
398
		"\17\r\3\2\2\2\20\21\7\3\2\2\21\22\5\4\3\2\22\23\7\4\2\2\23\34\3\2\2\2"+
399
		"\24\26\7\5\2\2\25\24\3\2\2\2\26\31\3\2\2\2\27\25\3\2\2\2\27\30\3\2\2\2"+
400
		"\30\32\3\2\2\2\31\27\3\2\2\2\32\34\7\t\2\2\33\t\3\2\2\2\33\27\3\2\2\2"+
401
		"\34(\3\2\2\2\35\36\f\7\2\2\36\37\7\b\2\2\37\'\5\4\3\b !\f\6\2\2!\"\7\6"+
402
		"\2\2\"\'\5\4\3\7#$\f\5\2\2$%\7\7\2\2%\'\5\4\3\6&\35\3\2\2\2& \3\2\2\2"+
403
		"&#\3\2\2\2\'*\3\2\2\2(&\3\2\2\2()\3\2\2\2)\5\3\2\2\2*(\3\2\2\2\7\r\27"+
404
		"\33&(";
405
	public static final ATN _ATN =
406
		new ATNDeserializer().deserialize(_serializedATN.toCharArray());
407
	static {
408
		_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
409
		for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
410
			_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
411
		}
412
	}
413
}