2626 * The default jamplate parser that parses {@link String}s into {@link Logic}s.
2727 *
2828 * @author LSafer
29- * @version 0.0.4
29+ * @version 0.0.6
3030 * @since 0.0.1 ~2020.09.19
3131 */
3232public class LogicParser implements PollParser <Logic > {
@@ -65,7 +65,7 @@ public class LogicParser implements PollParser<Logic> {
6565 *
6666 * @since 0.0.1 ~2020.09.19
6767 */
68- protected final Pattern PATTERN_WHITESPACES = Pattern .compile ("(\\ s+)|[|]|[&]|(!=)|(==)|(!(?!=))" );
68+ protected final Pattern PATTERN_WHITESPACES = Pattern .compile ("(\\ s+)|[|]|[+]|[ &]|(!=)|(==)|(!(?!=))" );
6969
7070 @ Override
7171 public Logic link (List poll ) {
@@ -101,6 +101,7 @@ public void parse(List poll) {
101101
102102 //clear all negations, equations
103103 this .parseNegations (poll );
104+ this .parseAdditions (poll );
104105 this .parseAnds (poll );
105106 this .parseOrs (poll );
106107 this .parseEquations (poll );
@@ -112,6 +113,55 @@ public List poll(String string) {
112113 return new ArrayList (Collections .singleton (string ));
113114 }
114115
116+ /**
117+ * Parse any possible {@link Addition}s in the given {@code poll}. After calling this method, no
118+ * addition statement in a {@link String} should remain in the given {@code poll}.
119+ * <p>
120+ * Clear these before calling this method:
121+ * <ul>
122+ * <li>{@link #processParenthesis(List)}</li>
123+ * <li>{@link #parseConstants(List)}</li>
124+ * <li>{@link #parseReferences(List)}</li>
125+ * <li>{@link #processWhitespaces(List)}</li>
126+ * <li>{@link #parseNegations(List)}</li>
127+ * <li>any statement that could be before or next to an addition</li>
128+ * </ul>
129+ *
130+ * @param poll the poll to parse any addition in it.
131+ * @throws NullPointerException if the given {@code poll} is null.
132+ * @throws ParseException if any parse exception occurs; if an addition has no element
133+ * before or after it; if an element before or after an addition
134+ * has not been resolved into a {@link Logic}.
135+ * @since 0.0.6 ~2020.09.22
136+ */
137+ protected void parseAdditions (List poll ) {
138+ Objects .requireNonNull (poll , "poll" );
139+
140+ ListIterator iterator = poll .listIterator ();
141+ while (iterator .hasNext ()) {
142+ Object next = iterator .next ();
143+
144+ if (next instanceof String ) {
145+ String string = (String ) next ;
146+
147+ if (string .equals ("+" ))
148+ try {
149+ iterator .remove ();
150+ Logic rightLogic = (Logic ) iterator .next ();
151+ iterator .remove ();
152+ Logic leftLogic = (Logic ) iterator .previous ();
153+ iterator .remove ();
154+
155+ Logic logic = new Addition (leftLogic , rightLogic );
156+
157+ iterator .add (logic );
158+ } catch (NoSuchElementException | ClassCastException e ) {
159+ throw new ParseException ("Invalid Addition" , e );
160+ }
161+ }
162+ }
163+ }
164+
115165 /**
116166 * Parse any possible {@link And}s in the given {@code poll}. After calling this method, no and
117167 * statement in a {@link String} should remain in the given {@code poll}.
0 commit comments