| 1 | /* |
| 2 | * This file is part of JON. |
| 3 | * |
| 4 | * JON is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * JON is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright 2007 Michael Doberenz |
| 18 | */ |
| 19 | package org.fuwjax.jon; |
| 20 | |
| 21 | import java.util.HashMap; |
| 22 | import java.util.Map; |
| 23 | import java.util.regex.Matcher; |
| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | /** |
| 27 | * A default instance of {@link CachedLexable} that allows for JON input to be |
| 28 | * supplied in a string. |
| 29 | * @author michaeldoberenz |
| 30 | */ |
| 31 | public class DefaultCachedLexer implements CachedLexable{ |
| 32 | private CharSequence seq; |
| 33 | private Map<String, Reference> references; |
| 34 | private int space; |
| 35 | private int position; |
| 36 | |
| 37 | /** |
| 38 | * Creates a new instance using <code>input</code> as the source |
| 39 | * JON-formatted text. |
| 40 | * @param input the JON-formatted text to use as input for a read operation |
| 41 | */ |
| 42 | public DefaultCachedLexer(final CharSequence input){ |
| 43 | seq = input; |
| 44 | references = new HashMap<String, Reference>(); |
| 45 | } |
| 46 | |
| 47 | public void read(final char token) throws SerialFormatException{ |
| 48 | skipWhitespace(); |
| 49 | if(current() != token){ |
| 50 | throw SerialFormatException.Messages.UnexpectedToken.exception(getPosition()); |
| 51 | } |
| 52 | next(); |
| 53 | } |
| 54 | |
| 55 | public Reference reserve(final String referenceId) throws ReferenceExistsException{ |
| 56 | Reference ref = references.get(referenceId); |
| 57 | if(ref == null){ |
| 58 | ref = new Reference(referenceId); |
| 59 | references.put(referenceId, ref); |
| 60 | } |
| 61 | return ref; |
| 62 | } |
| 63 | |
| 64 | public CharSequence read(final Pattern pattern) throws SerialFormatException{ |
| 65 | skipWhitespace(); |
| 66 | final Matcher matcher = pattern.matcher(seq); |
| 67 | matcher.region(position, matcher.regionEnd()); |
| 68 | if(!matcher.lookingAt()){ |
| 69 | throw SerialFormatException.Messages.UnexpectedIdentifier.exception(getPosition()); |
| 70 | } |
| 71 | final String match = getMatch(matcher); |
| 72 | if(match.length() == 0){ |
| 73 | throw SerialFormatException.Messages.UnexpectedIdentifier.exception(getPosition()); |
| 74 | } |
| 75 | position = matcher.end(); |
| 76 | return match; |
| 77 | } |
| 78 | |
| 79 | private String getMatch(final Matcher matcher){ |
| 80 | String match = null; |
| 81 | if(matcher.groupCount() > 0){ |
| 82 | match = matcher.group(1); |
| 83 | } |
| 84 | if(match == null){ |
| 85 | match = matcher.group(); |
| 86 | } |
| 87 | return match; |
| 88 | } |
| 89 | |
| 90 | private void next(){ |
| 91 | ++position; |
| 92 | } |
| 93 | |
| 94 | private char current() throws SerialFormatException{ |
| 95 | try{ |
| 96 | return seq.charAt(position); |
| 97 | }catch(RuntimeException e){ |
| 98 | throw SerialFormatException.Messages.UnexpectedEndOfInput.exception(getPosition()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private void skipWhitespace() throws SerialFormatException{ |
| 103 | while(Character.isWhitespace(current())){ |
| 104 | ++space; |
| 105 | next(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | public int getPosition(){ |
| 110 | return position - space; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString(){ |
| 115 | return seq.subSequence(position, seq.length()).toString(); |
| 116 | } |
| 117 | } |