Package Torello.CSS

Class Num

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.CharSequence, java.lang.Comparable<java.lang.CharSequence>
    Direct Known Subclasses:
    Dimension, Percentage

    public class Num
    extends CSSToken
    implements java.lang.CharSequence, java.io.Serializable, java.lang.Comparable<java.lang.CharSequence>
    This is a Token Data-Class. It is a descendant of the root CSSToken-Class: CSSToken. Instances of the class are usually are produced by the CSSTokenizer class. Many (but not all) of these subclasses maintain a static-method for building instances of this class named 'build'. Any CSSToken-subclass that is neither a singleton-instance, nor an "Error-Subtype" should have such a builder. Singeton instances do not need builders, and the two Error-Subtype Classes can only be generated by the tokenizer.

    All CSSToken subclasses have a CSSToken.str field which contains the exact character data that was extracted and used to construct instances of this class. All sub-casses also have several "Loop Optimization" methods. These are methods that may or may not be useful in light of some of the newer additions to JDK 17 & 21 including the 'instanceof varName' conditional-expression variable-naming features.

    The algorithms used to write this tokenizer were generated based solely on the CSS Working-Group's Syntax-Documentation. This document may be viewed here: CSS Working-Group CSS-Syntax. There is an external site that maintain all thing CSS located at drafts.csswg.org
    CSS-Tokenizer Number-Literal & Number-Token Class.
    See Also:
    Serialized Form


    • Field Detail

      • serialVersionUID

        🡇     🗕  🗗  🗖
        protected static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface java.io.Serializable. Using the Serializable Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         protected static final long serialVersionUID = 1;
        
      • number

        🡅  🡇     🗕  🗗  🗖
        public final java.math.BigDecimal number
        Though Java's BigDecimal may be an "overly ambitious" means of representing CSS-Extracted Number-Literals, for now, this is how it is going to work. After the 'javadoc' stuff is done, maybe I'll change it to java.lang.Number or something else.

        Yes, a "2em" or "10px" would be saved as a BigDecimal 2 and 10. The upside is that Java double and integer primitives are easily extracted using java.math.BigDecimal's exported methods.
      • integerOrNumber

        🡅  🡇     🗕  🗗  🗖
        public final boolean integerOrNumber
        The parser will return true if the parsed Number-Literal had neither a "Decimal Part", nor an "Exponent Part". If either of these were present, then this boolean will contain FALSE.

        Note that even though something like 5e2 - which is actually just the integer 500 - were parsed, this boolean would still evaluate to FALSE.
      • signChar

        🡅  🡇     🗕  🗗  🗖
        public final char signChar
        This shall contain one of three values: '+', '-' or ASCII 0. If the parsed Number-Literal began with a sign-character, then the appropriate sign-character will be stored. If the Number-Literal had no sign-character, then a 0 is stored.
    • Method Detail

      • isNum

        🡅  🡇     🗕  🗗  🗖
        public final boolean isNum()
        Description copied from class: CSSToken
        Loop Optimization: This method only returns TRUE if this is an actual instance of Num.
        Overrides:
        isNum in class CSSToken
        Returns:
        This method returns FALSE for all instances of CSSToken, except when 'this' instance is actually the Num Subclass.

        That class has overridden this method, and returns TRUE.
        See Also:
        isNum()
      • ifNum

        🡅  🡇     🗕  🗗  🗖
        public final Num ifNum()
        Description copied from class: CSSToken
        Loop Optimization: When this method is invoked on an instance of sub-class Num this method produces 'this' instance.
        Overrides:
        ifNum in class CSSToken
        Returns:
        This method shall return null, always, except when 'this' is an actual instance of Num. When so, this method simply returns 'this'. All other sub-classes of (abstract) class CSSToken inherit this method, and therefore return null.
        See Also:
        ifNum()
      • build

        🡅  🡇     🗕  🗗  🗖
        public static Num build​(java.lang.String numStr)
        Static-Builder Method for creating an instance of this class. This Static-Method is a substitute for an actual Constructor. Because many of the 'consume(...)' methods in the Token Classe for Torello.CSS actually generate / spit-out more than CSSToken instance, writing publicly available constructors is largely impossible.

        The upside to this approach is that the build methods and the consume methods share identical code. Furthermore this code is (nearly) perfectly based on the Pseudo-Code on the CSS Working-Group Website.
        Parameters:
        numStr - Any Java-String that can be parsed into an instance of Num
        Returns:
        An instance of Num.

        If the contents of the Input-String parameter 'numStr' cannot be consumed, exactly, by this class' 'consume' method, then an exception shall throw.
        Throws:
        TokenizeException - This exception may be thrown for any number of reasons involving the inability to parse input parameter 'numStr'.
      • is

        🡅  🡇     🗕  🗗  🗖
        public static boolean is​(int[] css,
                                 int sPos)
        Checks whether or not the next token to consume is a number token, or number-subclass token.

        Tokenizer: Number-Literal Check Method, Pseudo-Code

        Making use of the CSS Parser DOES NOT require any knowledge of how the underlying Pass 1 Tokenizer actually works. Browser-War people are usually pretty convincing that parsing CSS is a "Moving Target" type of operation, not to be engaged by mere mortals.

        Below is the CSS Working Group's Number-Literal Pseudo-Code. You may review it if you are at wit's end, and have nothing better to do. There is no need to actually invoke this method, it is here solely for informational purposes.

        These Parsing Pseudo-Code Instructions and Rail-Road Diagrams have been copied from the CSS-Working-Group Web-Site:
        https://drafts.csswg.org/css-syntax/#check-if-three-code-points-would-start-a-number

        Check if three code points would start a number

        This section describes how to check if three code points would start a number. The algorithm described here can be called explicitly with three code points, or can be called with the input stream itself. In the latter case, the three code points in question are the current input code point and the next two input code points, in that order.

        Note: This algorithm will not consume any additional code points.

        Look at the first code point:

        U+002B PLUS SIGN (+)
        U+002D HYPHEN-MINUS (-)
        If the second code point is a digit, return true.

        Otherwise, if the second code point is a U+002E FULL STOP (.) and the third code point is a digit, return true.

        Otherwise, return false.

        U+002E FULL STOP (.)
        If the second code point is a digit, return true. Otherwise, return false.
        digit
        Return true.
        anything else
        Return false.
        Parameters:
        css - CSS-String as an array of code-points.
        sPos - The array-index where the tokenizer is to consume its next token
        Returns:
        TRUE if and only if the next token in the array is a number
      • consume

        🡅  🡇     🗕  🗗  🗖
        protected static void consume​
                    (int[] css,
                     ByRef<java.lang.Integer> POS,
                     java.util.function.Consumer<CSSToken> returnParsedToken,
                     java.util.function.Consumer<TokenizeError> errorEncountered)
        
        This is a tokenizer method which "consumes" the next Number-Token (or Number-Token Subclass) from the input Code-Point Array.

        Tokenizer: Numeric-Token Consume Method, Pseudo-Code

        Making use of the CSS Parser DOES NOT require any knowledge of how the underlying Pass 1 Tokenizer actually works. Browser-War people are usually pretty convincing that parsing CSS is a "Moving Target" type of operation, not to be engaged by mere mortals.

        Below is the CSS Working Group's Numeric-Token Pseudo-Code. You may review it if you are at wit's end, and have nothing better to do. There is no need to actually invoke this method, it is here solely for informational purposes.

        These Parsing Pseudo-Code Instructions and Rail-Road Diagrams have been copied from the CSS-Working-Group Web-Site:
        https://drafts.csswg.org/css-syntax/#consume-a-numeric-token

        Consume a numeric token

        This section describes how to consume a numeric token from a stream of code points. It returns either a <number-token>, <percentage-token>, or <dimension-token>.

        Consume a number and let number be the result.

        If the next 3 input code points would start an ident sequence, then:

        1. Create a <dimension-token> with the same value, type flag, and sign character as number, and a unit set initially to the empty string.
        2. Consume an ident sequence. Set the <dimension-token>’s unit to the returned value.
        3. Return the <dimension-token>.

        Otherwise, if the next input code point is U+0025 PERCENTAGE SIGN (%), consume it. Create a <percentage-token> with the same value and sign character as number, and return it.

        Otherwise, create a <number-token> with the same value, type flag, and sign character as number, and return it.

      • consumeNumber

        🡅     🗕  🗗  🗖
        protected static int consumeNumber​(int[] css,
                                           int sPos,
                                           ByRef<Num> outNum)
        This is a tokenizer method which "consumes" the next Number-Literal from the input Code-Point Array.

        Tokenizer: Number-Literal Consume Method, Pseudo-Code

        Making use of the CSS Parser DOES NOT require any knowledge of how the underlying Pass 1 Tokenizer actually works. Browser-War people are usually pretty convincing that parsing CSS is a "Moving Target" type of operation, not to be engaged by mere mortals.

        Below is the CSS Working Group's Number-Literal Pseudo-Code. You may review it if you are at wit's end, and have nothing better to do. There is no need to actually invoke this method, it is here solely for informational purposes.

        These Parsing Pseudo-Code Instructions and Rail-Road Diagrams have been copied from the CSS-Working-Group Web-Site:
        https://drafts.csswg.org/css-syntax/#consume-a-number

        Consume a number

        This section describes how to consume a number from a stream of code points. It returns a numeric value, a string type which is either "integer" or "number", and an optional sign character which is either "+", "-", or missing.

        Note: This algorithm does not do the verification of the first few code points that are necessary to ensure a number can be obtained from the stream. Ensure that the stream starts with a number before calling this algorithm.

        Execute the following steps in order:

        1. Let type be the string "integer". Let number part and exponent part be the empty string.
        2. If the next input code point is U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-), consume it. Append it to number part and set sign character to it.
        3. While the next input code point is a digit, consume it and append it to number part.
        4. If the next 2 input code points are U+002E FULL STOP (.) followed by a digit, then:
          1. Consume the next input code point and append it to number part.
          2. While the next input code point is a digit, consume it and append it to number part.
          3. Set type to "number".
        5. If the next 2 or 3 input code points are U+0045 LATIN CAPITAL LETTER E (E) or U+0065 LATIN SMALL LETTER E (e), optionally followed by U+002D HYPHEN-MINUS (-) or U+002B PLUS SIGN (+), followed by a digit, then:
          1. Consume the next input code point.
          2. If the next input code point is "+" or "-", consume it and append it to exponent part.
          3. While the next input code point is a digit, consume it and append it to exponent part.
          4. Set type to "number".
        6. Let value be the result of interpreting number part as a base-10 number.

          If exponent part is non-empty, interpret it as a base-10 integer, then raise 10 to the power of the result, multiply it by value, and set value to that result.

        7. Return value, type, and sign character.
        <number-token>
        + - digit . digit digit . digit e E + - digit