001    // Copyright 2011 Waterken Inc. under the terms of the MIT X license found at
002    // http://www.opensource.org/licenses/mit-license.html
003    package org.waterken.syntax;
004    
005    import org.joe_e.Powerless;
006    import org.ref_send.deserializer;
007    import org.ref_send.name;
008    
009    /**
010     * Signals parsed data did not contain an expected token at a required position.
011     */
012    public class WrongToken extends RuntimeException implements Powerless {
013        static private final long serialVersionUID = 1L;
014    
015        /**
016         * expected token
017         */
018        public final String expected;
019        
020        /**
021         * Constructs an instance.
022         * @param expected  {@link #expected}
023         */
024        public @deserializer
025        WrongToken(@name("expected") final String expected) {
026            super(expected);
027            this.expected = expected;
028        }
029        
030        /**
031         * Requires a token to have an expected value.
032         * @param expected  expected token
033         * @param actual    actual token
034         * @throws WrongToken   {@code actual} is not {@code expected}
035         */
036        static public void
037        require(final String expected, final String actual) throws WrongToken {
038            if (!expected.equals(actual)) { throw new WrongToken(expected); }
039        }
040    }