summaryrefslogtreecommitdiff
path: root/JPratt/src/main/java/com/ashardalon/pratt/blocks/SimpleParseBlock.java
blob: 8c4a046d842500857563efcf72f4128fa3b14b6c (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
package com.ashardalon.pratt.blocks;

import java.util.function.Predicate;

import com.ashardalon.pratt.ParserContext;
import com.ashardalon.pratt.commands.CommandResult;
import com.ashardalon.pratt.commands.CommandResult.Status;
import com.ashardalon.pratt.tokens.Token;

import bjc.data.Tree;
import bjc.utils.parserutils.ParserException;

/**
 * Simple implementation of {@link ParseBlock}
 *
 * @author bjculkin
 *
 * @param <K>
 *        The key type of the tokens.
 *
 * @param <V>
 *        The value type of the tokens.
 *
 * @param <C>
 *        The state type of the parser.
 */
public class SimpleParseBlock<K, V, C> implements ParseBlock<K, V, C> {
	private final int pow;

	private final K term;

	private final Predicate<Tree<Token<K, V>>> validatr;

	/**
	 * Create a new block.
	 *
	 * @param precedence
	 *        The precedence of this block.
	 * @param validator
	 *        The predicate to apply to blocks.
	 * @param terminator
	 *        The token type that terminates the block. If this is null,
	 *        don't check for a terminator.
	 */
	public SimpleParseBlock(final int precedence, final Predicate<Tree<Token<K, V>>> validator,
			final K terminator) {
		if(precedence < 0) throw new IllegalArgumentException("Precedence must be non-negative");

		pow = precedence;
		term = terminator;
		validatr = validator;
	}

	@Override
	public CommandResult<K, V> parse(final ParserContext<K, V, C> ctx) throws ParserException {
		final CommandResult<K,V> resBlock = ctx.parse.parseExpression(pow, ctx.tokens, ctx.state, false);
		if (resBlock.status != Status.SUCCESS) return resBlock;
		
		Tree<Token<K, V>> res = resBlock.success();
		if(term != null) {
			ctx.tokens.expect(term);
		}

		if(validatr == null || validatr.test(res)) return CommandResult.success(res);

		// TODO: Figure out the right way to handle error context w/ CommandResult
		throw new ParserException("Block failed validation");
	}

	@Override
	public int hashCode() {
		final int prime = 31;

		int result = 1;

		result = prime * result + pow;
		result = prime * result + (term == null ? 0 : term.hashCode());

		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if(this == obj) return true;
		if(obj == null) return false;
		if(!(obj instanceof SimpleParseBlock)) return false;

		final SimpleParseBlock<?, ?, ?> other = (SimpleParseBlock<?, ?, ?>) obj;

		if(pow != other.pow) return false;

		if(term == null) {
			if(other.term != null) return false;
		} else if(!term.equals(other.term)) return false;

		return true;
	}
}