Class MessagePatternParser


  • public class MessagePatternParser
    extends java.lang.Object
    Hand-implemented parser for ICU message format. Designed to be compact and fast vs. other implementations. The parser produces an instruction tree which can be cached for repeated use, and is intended to be evaluated by a separate engine. Note: The 'choice' formatter is not implemented since it is deprecated. See ICU docs for details on syntax: https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/MessageFormat.html Rationale The decision to hand-implement the parser was for 2 reasons: 1. Avoiding extra runtime dependencies (parser generators, e.g. pegjs, etc) 2. Provide control over the memory allocation, garbage generation, and other aspects that impact parser performance. A major consideration is size of the resulting parser code. The OpenJS project 'messageformat-parser' which is generated by Peg.js is 30kB minified. It also requires other dependencies for the plural calculations, where this is already supported in our library via @phensley/plurals See: https://unpkg.com/messageformat-parser/parser.js
    • Constructor Detail

      • MessagePatternParser

        public MessagePatternParser​(java.util.Collection<java.lang.String> formatters,
                                    java.lang.String str,
                                    boolean disableEscapes)
    • Method Detail

      • simple

        protected MessageCode simple​(java.util.List<java.lang.Object> args,
                                     java.lang.String name,
                                     MessageMatcher.State r)
        Simple single-argument formatter with zero or more options.
      • seek

        protected int seek​(int i,
                           int j)
        Seek to the matching '}' character at the same nesting level, skipping over any apostrophes. This adds some redundant scanning of the string but simplifies some of the parsing logic in other areas. It ensures we're always dealing with a well-formed tag where all '{' have a corresponding '}'.
      • textarg

        protected MessageCode textarg​(java.lang.String s,
                                      java.lang.Object argsub)
        Emit a text node, performing argument substitution for all occurrences of the '#' character.