1 /*
2  * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 
7 package org.antlr.v4.runtime.atn;
8 
9 import org.antlr.v4.runtime.misc.IntervalSet;
10 
11 /**
12  * Utility class to create {@link AtomTransition}, {@link RangeTransition},
13  * and {@link SetTransition} appropriately based on the range of the input.
14  *
15  * To keep the serialized ATN size small, we only inline atom and
16  * range transitions for Unicode code points <= U+FFFF.
17  *
18  * Whenever we encounter a Unicode code point > U+FFFF, we represent that
19  * as a set transition (even if it is logically an atom or a range).
20  */
21 public abstract class CodePointTransitions {
22 	/**
23 	 * If {@code codePoint} is <= U+FFFF, returns a new {@link AtomTransition}.
24 	 * Otherwise, returns a new {@link SetTransition}.
25 	 */
createWithCodePoint(ATNState target, int codePoint)26 	public static Transition createWithCodePoint(ATNState target, int codePoint) {
27 		if (Character.isSupplementaryCodePoint(codePoint)) {
28 			return new SetTransition(target, IntervalSet.of(codePoint));
29 		}
30 		else {
31 			return new AtomTransition(target, codePoint);
32 		}
33 	}
34 
35 	/**
36 	 * If {@code codePointFrom} and {@code codePointTo} are both
37 	 * <= U+FFFF, returns a new {@link RangeTransition}.
38 	 * Otherwise, returns a new {@link SetTransition}.
39 	 */
createWithCodePointRange( ATNState target, int codePointFrom, int codePointTo)40 	public static Transition createWithCodePointRange(
41 			ATNState target,
42 			int codePointFrom,
43 			int codePointTo) {
44 		if (Character.isSupplementaryCodePoint(codePointFrom) ||
45 		    Character.isSupplementaryCodePoint(codePointTo)) {
46 			return new SetTransition(target, IntervalSet.of(codePointFrom, codePointTo));
47 		}
48 		else {
49 			return new RangeTransition(target, codePointFrom, codePointTo);
50 		}
51 	}
52 }
53