Class MessagePatternParser
- java.lang.Object
-
- com.squarespace.cldrengine.message.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 Summary
Constructors Constructor Description MessagePatternParser(java.util.Collection<java.lang.String> formatters, java.lang.String str, boolean disableEscapes)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected MessageCode
flatten(java.util.List<MessageCode> n)
MessageCode
inner(MessageMatcher.State r)
MessageCode
outer(MessageMatcher.State r, java.lang.Object argsub)
MessageCode
parse()
protected MessageCode
plural(java.util.List<java.lang.Object> args, PluralNumberType type, MessageMatcher.State r)
Parse a plural instruction.protected int
seek(int i, int j)
Seek to the matching '}' character at the same nesting level, skipping over any apostrophes.protected MessageCode
select(java.util.List<java.lang.Object> args, MessageMatcher.State r)
Parse a select instruction.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.protected MessageCode
tag(MessageMatcher.State r, java.lang.Object argsub)
Parse a nested tag sequence '{' ...protected MessageCode
textarg(java.lang.String s, java.lang.Object argsub)
Emit a text node, performing argument substitution for all occurrences of the '#' character.
-
-
-
Method Detail
-
parse
public MessageCode parse()
-
outer
public MessageCode outer(MessageMatcher.State r, java.lang.Object argsub)
-
inner
public MessageCode inner(MessageMatcher.State r)
-
tag
protected MessageCode tag(MessageMatcher.State r, java.lang.Object argsub)
Parse a nested tag sequence '{' ... '}'
-
plural
protected MessageCode plural(java.util.List<java.lang.Object> args, PluralNumberType type, MessageMatcher.State r)
Parse a plural instruction.
-
select
protected MessageCode select(java.util.List<java.lang.Object> args, MessageMatcher.State r)
Parse a select instruction.
-
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.
-
flatten
protected MessageCode flatten(java.util.List<MessageCode> n)
-
-