开发者

Checking if an item in a String[] appears in a stream

开发者 https://www.devze.com 2023-03-05 06:05 出处:网络
I\'m currently writing a parser for a language that I\'m creating, which needs to check if the current part of the stream fits one of the items in the passed array. A short version of the code is:

I'm currently writing a parser for a language that I'm creating, which needs to check if the current part of the stream fits one of the items in the passed array. A short version of the code is:

public abstract class Parser {
    private StringReader reader; //This is a BufferedReader with rollback

    //A single string lookahead method
    public boolean lookahead(String toMatch, boolean rollback) throws ParseException {
        char c;
        //Mark the current position in the stream, so we can come back to it if needed
        MarkToken currentMark = reader.mark();

        //Iterate through the toMatch and check if each character matches
        for(int i = 0; i < toMatch.length(); ++i) {
            c = reader.nextChar();

            if(toMatch.charAt(i) != c) {
                break;
            }
        }

        //Get the current image
        String got = reader.currentImage(currentMark);

        //If we don't have a match, rollback if necessary and return false
        if(!got.equals(toMatch)) {
            if(rollback) {
                reader.rollBack();
            }

            return false;
        }

        return true;
    }

    //The String[] lookahead method
    public int lookahead(String[] toMatch, boolean rollback) throws ParseException {
        if(toMatch.length == 1) {
            //If there is only one element in toMatch, send it to a cheaper function
            if(lookahead(toMatch[0]))
                return 0;
            else return 1;
        } else {
            int maxLength = toMatch[0].length();
            //We use this variable to keep track of how many valid choices are left
            int choices开发者_JAVA技巧Left = toMatch.length;
            int i, j;
            char current;
            //Mark the current position in the stream, so we can come back to it if needed
            MarkToken mark = s().mark();

            //Get the length of the longest string in toMatch
            for(i = 1; i < toMatch.length; ++i) {
                maxLength = Math.max(maxLength, toMatch[i].length());
            }

            //Go up to the length of the longest string
            for(i = 0; i < maxLength; ++i) {
                //Get the next character from the stream
                current = reader.nextChar();

                //If we've reached the end of the stream:
                if(current == -1 || current == '\uffff') {
                    //Get back a character in the stream
                    reader.rollbackChar();

                    //And check to see if we have a match
                    return ArrayUtils.indexOf(toMatch, reader.currentImage(mark));
                }

                //Go through each item in toMatch
                for(j = 0; j < toMatch.length; ++j) {
                    if(toMatch[j] != null) {
                        //Check to see if the character matches or not
                        if(toMatch[j].charAt(i) != current) {
                            //We null an item in toMatch if it doesn't apply any more
                            toMatch[j] = null;
                            --choicesLeft;
                        }
                    }
                }

                //If we only have one choice left, see if there is a match (will return -1 if not)
                if(choicesLeft == 1) {
                    return ArrayUtils.indexOf(toMatch, reader.currentImage(mark));
                }
            }

            //If there is no 
            if(rollback) {
                reader.rollBackTo(mark);
            }
        }

        return -1;
    }
}

This function would be called to check if the stream contained certain symbols (. .* $@ // " ' """ ''' etc.) and consume them greedily.

I would only be providing an array of maximum 10-15 items at once, so removing items from the array may not be the best optimisation.

Is there a more efficient way of doing this, certain methods or loops that I should be using?

0

精彩评论

暂无评论...
验证码 换一张
取 消