Example usage for com.google.common.css.compiler.ast StringCharStream backup

List of usage examples for com.google.common.css.compiler.ast StringCharStream backup

Introduction

In this page you can find the example usage for com.google.common.css.compiler.ast StringCharStream backup.

Prototype

@Override
public void backup(int amount) 

Source Link

Usage

From source file:io.bazel.rules.closure.webfiles.compiler.CssParser.java

/**
 * Skips current statement (a ruleset or an at-rule) and gets back to the "top level" so that it
 * can continue to parse. See <a href="http://www.w3.org/TR/css-syntax-3/#error-handling">W3C CSS3
 * Error Handling</a>./*from  w ww .  j av a  2 s.  c  om*/
 */
private static int skipCurrentStatement(GssParserCC parser, StringCharStream stream, int begin) {
    // Back up to the beginning of the current statement
    // NOTE: StringCharStream points 0-based index of the last read char, that's like (-1)-based
    stream.backup(stream.getCharIndex() - begin + 1);
    // Clear prefetched token
    parser.token.next = null;
    // Skip until the end of current statement
    Token t = parser.getToken(1);
    // At the "top level" of a stylesheet, an <at-keyword-token> starts an at-rule. Anything else
    // starts a qualified rule.
    if (t.kind == GssParserCCConstants.ATKEYWORD || t.kind == GssParserCCConstants.ATLIST
            || t.kind == GssParserCCConstants.ATRULESWITHDECLBLOCK) {
        // An at-rule ends with a semicolon or a block
        t = skipUntil(parser, GssParserCCConstants.LEFTBRACE, GssParserCCConstants.SEMICOLON);
        if (t.kind == GssParserCCConstants.LEFTBRACE) {
            // An opening curly-brace starts the at-rules body. The at-rule seeks forward, matching
            // blocks (content surrounded by (), {}, or []) until it finds a closing curly-brace that
            // isnt matched by anything else or inside of another block.
            t = skipUntil(parser, GssParserCCConstants.RIGHTBRACE);
        } else {
            // A semicolon ends the at-rule immediately
        }
    } else {
        // A qualified ruleset ends with a block
        t = skipUntil(parser, GssParserCCConstants.LEFTBRACE);
        if (t.kind == GssParserCCConstants.LEFTBRACE) {
            t = skipUntil(parser, GssParserCCConstants.RIGHTBRACE);
        }
    }
    // NOTE: +1 for end-index because a location is represented as a closed range
    return stream.convertToCharacterIndex(t.endLine, t.endColumn) + 1;
}