summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/rgens/parser/elements/CaseElement.java
blob: 9c23ef2198fbd5d706a34b8b2e70da3bf97123cd (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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package bjc.rgens.parser.elements;

import bjc.utils.ioutils.LevelSplitter;
import bjc.rgens.parser.GenerationState;
import bjc.rgens.parser.GrammarException;

import java.util.Arrays;

import static bjc.rgens.parser.RGrammarLogging.*;

/**
 * A element in a rule case.
 *
 * @author EVE
 */
public abstract class CaseElement {
	/**
	 * The possible types of an element.
	 *
	 * @author EVE
	 */
	public static enum ElementType {
		/** An element that represents a literal string. */
		LITERAL(true),
		/** An element that represents a rule reference. */
		RULEREF(true),
		/** An element that represents a random range. */
		RANGE(true),
		/** An element that represents a variable that stores a string. */
		VARIABLE(false);

		public final boolean spacing;

		private ElementType(boolean spacing) {
			this.spacing = spacing;
		}
	}

	/** The type of this element. */
	public boolean spacing;

	protected CaseElement() {
		this(true);
	}

	/**
	 * Create a new case element.
	 *
	 * @param typ
	 *            The type of this element.
	 */
	protected CaseElement(boolean spacing) {
		this.spacing = spacing;
	}

	/**
	 * Generate this case element.
	 *
	 * @param state
	 * 	The current state of generation.
	 */
	public abstract void generate(GenerationState state);

	/**
	 * Create a case element from a string.
	 *
	 * @param csepart
	 *            The string to convert.
	 *
	 * @return A case element representing the string.
	 */
	public static CaseElement createElement(String csepart) {
		if (csepart == null) {
			throw new NullPointerException("Case part cannot be null");
		}

		if (csepart.matches("\\(\\S+\\)")) {
			return createElement(csepart.substring(1, csepart.length() - 1));
		} else if (csepart.matches("\\{\\S+\\}")) {
			/*
			 * Handle special case elements.
			 *
			 */
			String specialBody = csepart.substring(1, csepart.length() - 1);

			if (specialBody.matches("\\S+:\\S=\\S+")) {
				String[] parts = LevelSplitter.def.levelSplit(specialBody, "=").toArray(new String[0]);

				if(parts.length != 2) {
					throw new GrammarException("Colon variables must have a name and a definition");
				}

				String varName = parts[0];

				char op = varName.charAt(varName.length() - 1);

				trace("Colon definition w/ op %d", (int)op);

				// Remove the colon, plus any tacked on operator
				varName = varName.substring(0, varName.length() - 2);

				return VariableDefCaseElement.parseVariable(varName, parts[1], op, true);
			} else if (specialBody.matches("\\S+:=\\S+")) {
				String[] parts = LevelSplitter.def.levelSplit(specialBody, "=").toArray(new String[0]);

				if(parts.length != 2) {
					throw new GrammarException("Colon variables must have a name and a definition");
				}

				String varName = parts[0];

				varName = varName.substring(0, varName.length() - 1);

				return VariableDefCaseElement.parseVariable(varName, parts[1], ' ', true);
			} else if (specialBody.matches("\\S+=\\S+")) {
				String[] parts = LevelSplitter.def.levelSplit(specialBody, "=").toArray(new String[0]);
				if(parts.length != 2) {
					throw new GrammarException("Variables must have a name and a definition");
				}

				// Non-colon variables can't take an operator
				return VariableDefCaseElement.parseVariable(parts[0], parts[1], (char)0, false);
			} else if (specialBody.matches("empty")) {
				/* Literal blank, for empty cases. */
				return new BlankCaseElement();
			} else {
				throw new IllegalArgumentException(String.format("Unknown special case part '%s'", specialBody));
			}
		} else if (csepart.matches("\\[\\S+\\]")) {
			String rawCase = csepart.substring(1, csepart.length() - 1);

			if (rawCase.matches("\\d+\\.{2}\\d+")) {
				int firstNum = Integer.parseInt(rawCase.substring(0, rawCase.indexOf('.')));
				int secondNum = Integer.parseInt(rawCase.substring(rawCase.lastIndexOf('.') + 1));

				return new RangeCaseElement(firstNum, secondNum);
			} else if(rawCase.contains("||")) {
				String[] elms = LevelSplitter.def.levelSplit(rawCase, "||").toArray(new String[0]);

				return new InlineRuleCaseElement(elms);
			} else if(rawCase.contains("|")) {
				throw new GrammarException("Inline rule using | found, they use || now");

				// String[] elms = LevelSplitter.def.levelSplit(rawCase, "|").toArray(new String[0]);
				// return new InlineRuleCaseElement(elms);
			} else if (LevelSplitter.def.levelContains(rawCase, ".")) {
				String[] parts = LevelSplitter.def.levelSplit(rawCase, ".").toArray(new String[0]);

				CaseElement base = createElement(parts[0]);

				parts = Arrays.copyOfRange(parts, 1, parts.length);
				return new MethodCaseElement(base, parts);
			} else {
				return new RuleCaseElement(rawCase);
			}
		} else if(csepart.startsWith("%") && !csepart.equals("%")) {
			return new RuleCaseElement(csepart);	
		} else {
			return new LiteralCaseElement(csepart);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (spacing ? 0 : 2);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CaseElement other = (CaseElement) obj;
		if (spacing != other.spacing)
			return false;
		return true;
	}
}