All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.vutbr.web.csskit.antlr4.CSSLexerState Maven / Gradle / Ivy

Go to download

jStyleParser is a CSS parser written in Java. It has its own application interface that is designed to allow an efficient CSS processing in Java and mapping the values to the Java data types. It parses CSS 2.1 style sheets into structures that can be efficiently assigned to DOM elements. It is intended be the primary CSS parser for the CSSBox library. While handling errors, it is user agent conforming according to the CSS specification.

There is a newer version: 4.0.1
Show newest version
package cz.vutbr.web.csskit.antlr4;

public class CSSLexerState {

    public enum RecoveryMode {
        BALANCED,
        FUNCTION,
        RULE,
        DECL,
        NOBALANCE
    }

    public short curlyNest;
    public short parenNest;
    public short sqNest;
    public boolean quotOpen;
    public boolean aposOpen;

    public CSSLexerState() {
        this.curlyNest = 0;
        this.parenNest = 0;
        this.sqNest = 0;
        this.quotOpen = false;
        this.aposOpen = false;
    }

    public CSSLexerState(CSSLexerState clone) {
        this.curlyNest = clone.curlyNest;
        this.parenNest = clone.parenNest;
        this.sqNest = clone.sqNest;
        this.quotOpen = clone.quotOpen;
        this.aposOpen = clone.aposOpen;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof CSSLexerState) {
            CSSLexerState that = (CSSLexerState) o;
            return (this.curlyNest == that.curlyNest &&
                    this.parenNest == that.parenNest &&
                    this.sqNest == that.sqNest &&
                    this.quotOpen == that.quotOpen &&
                    this.aposOpen == that.aposOpen);
        }
        return false;
    }

    /**
     * Checks whether all pair characters (single and double quotatation marks,
     * curly braces) are balanced
     */
    public boolean isBalanced() {
        return !aposOpen && !quotOpen && curlyNest == 0 && parenNest == 0 && this.sqNest == 0; 
    }

    /**
     * Checks whether some pair characters are balanced. Modes are:
     * 
    *
  • BALANCED - everything must be balanced: single and double quotatation marks, curly braces, parentheses *
  • FUNCTION - within the function arguments: parentheses must be balanced *
  • RULE - within the CSS rule: all but curly braces *
  • DECL - within declaration: all, keep curly braces at desired state *
* * @param mode the desired recovery node * @param state the required lexer state (used for DECL mode) * @param t the token that is being processed (used for DECL mode) */ public boolean isBalanced(RecoveryMode mode, CSSLexerState state, CSSToken t) { if (mode == RecoveryMode.BALANCED) return !aposOpen && !quotOpen && curlyNest == 0 && parenNest == 0 && sqNest == 0; else if (mode == RecoveryMode.FUNCTION) return parenNest == 0 && sqNest == 0; else if (mode == RecoveryMode.RULE) return !aposOpen && !quotOpen && parenNest == 0 && sqNest == 0; else if (mode == RecoveryMode.DECL) { if (t.getType() == CSSLexer.RCURLY) //if '}' is processed the curlyNest has been already decreased return !aposOpen && !quotOpen && parenNest == 0 && sqNest == 0 && curlyNest == state.curlyNest - 1; else return !aposOpen && !quotOpen && parenNest == 0 && sqNest == 0 && curlyNest == state.curlyNest; } else return true; } @Override public String toString() { return "{=" + curlyNest + ", (=" + parenNest + ", [=" + sqNest + ", '=" + (aposOpen ? "1" : "0") + ", \"=" + (quotOpen ? "1" : "0"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy