1 # Copyright (c) 2002-2016 International Business Machines Corporation and
2 # others. All Rights Reserved.
7 # Implement default line breaking as defined by
8 # Unicode Standard Annex #14 Revision 35 for Unicode 8.0
9 # http://www.unicode.org/reports/tr14/
11 # Includes the Emoji breaking proposals from Unicode L2/16-011R3.
12 # http://www.unicode.org/L2/L2016/16011r3-break-prop-emoji.pdf
14 # TODO: Rule LB 8 remains as it was in Unicode 5.2
15 # This is only because of a limitation of ICU break engine implementation,
16 # not because the older behavior is desirable.
18 # This corresponds to CSS line-break=strict (BCP47 -u-lb-strict).
19 # It sets characters of class CJ to behave like NS.
22 # Character Classes defined by TR 14.
29 # !!lookAheadHardBreak Described here because it is (as yet) undocumented elsewhere
30 # and only used for the line break rules.
32 # It is used in the implementation of rule LB 10
33 # which says to treat any combining mark that is not attached to a base
34 # character as if it were of class AL (alphabetic).
36 # The problem occurs in the reverse rules.
38 # Consider a sequence like, with correct breaks as shown
41 # Then consider the sequence without the initial ID (ideographic)
44 # Our CM, which in the first example was attached to the ideograph,
45 # is now unattached, becomes an alpha, and joins in with the other
48 # When iterating forwards, these sequences do not present any problems
49 # When iterating backwards, we need to look ahead when encountering
50 # a CM to see whether it attaches to something further on or not.
51 # (Look-ahead in a reverse rule is looking towards the start)
53 # If the CM is unattached, we need to force a break.
55 # !!lookAheadHardBreak forces the run time state machine to
56 # stop immediately when a look ahead rule ( '/' operator) matches,
57 # and set the match position to that of the look-ahead operator,
58 # no matter what other rules may be in play at the time.
60 # See rule LB 19 for an example.
63 # Temporary definitions of Emoji Base and Emoji Modifiers, until properties are available.
65 # Apple $EmojiForMods becomes $EB here (deletes \U0001F46F, should not have been in $EmojiForMods)
66 $EB = [\u261D\u26F9\u270A-\u270D\U0001F385\U0001F3C2-\U0001F3C4\U0001F3C7\U0001F3CA-\U0001F3CC\U0001F442-\U0001F443\U0001F446-\U0001F450\U0001F466-\U0001F478\U0001F47C\U0001F481-\U0001F483\U0001F485-\U0001F487\U0001F4AA\U0001F574-\U0001F575\U0001F57A\U0001F590\U0001F595-\U0001F596\U0001F645-\U0001F647\U0001F64B-\U0001F64F\U0001F6A3\U0001F6B4-\U0001F6B6\U0001F6C0\U0001F6CC\U0001F918-\U0001F91E\U0001F926\U0001F930\U0001F933-\U0001F939\U0001F93C-\U0001F93E];
67 # Apple $EmojiMods becomes $EM here, same set
68 $EM = [\U0001F3FB-\U0001F3FF];
70 $AI = [[:LineBreak = Ambiguous:] - [\u2640\u2642]];
71 $AL = [[:LineBreak = Alphabetic:] - [$EM\u2695\u2696\u2764]];
72 $BA = [:LineBreak = Break_After:];
73 $BB = [:LineBreak = Break_Before:];
74 $BK = [:LineBreak = Mandatory_Break:];
75 $B2 = [:LineBreak = Break_Both:];
76 $CB = [:LineBreak = Contingent_Break:];
77 $CJ = [:LineBreak = Conditional_Japanese_Starter:];
78 $CL = [:LineBreak = Close_Punctuation:];
79 $CM = [[:LineBreak = Combining_Mark:] \u200d];
80 $CP = [:LineBreak = Close_Parenthesis:];
81 $CR = [:LineBreak = Carriage_Return:];
82 $EX = [:LineBreak = Exclamation:];
83 $GL = [:LineBreak = Glue:];
84 $HL = [:LineBreak = Hebrew_Letter:];
85 $HY = [:LineBreak = Hyphen:];
86 $H2 = [:LineBreak = H2:];
87 $H3 = [:LineBreak = H3:];
88 $ID = [[:LineBreak = Ideographic:][\u2640\u2642\u2695\u2696\u2764] - $EB];
89 $IN = [:LineBreak = Inseperable:];
90 $IS = [:LineBreak = Infix_Numeric:];
91 $JL = [:LineBreak = JL:];
92 $JV = [:LineBreak = JV:];
93 $JT = [:LineBreak = JT:];
94 $LF = [:LineBreak = Line_Feed:];
95 $NL = [:LineBreak = Next_Line:];
96 $NS = [[:LineBreak = Nonstarter:] $CJ];
97 $NU = [:LineBreak = Numeric:];
98 $OP = [:LineBreak = Open_Punctuation:];
99 $PO = [:LineBreak = Postfix_Numeric:];
100 $PR = [:LineBreak = Prefix_Numeric:];
101 $QU = [:LineBreak = Quotation:];
102 $RI = [:LineBreak = Regional_Indicator:];
103 $SA = [:LineBreak = Complex_Context:];
104 $SG = [:LineBreak = Surrogate:];
105 $SP = [:LineBreak = Space:];
106 $SY = [:LineBreak = Break_Symbols:];
107 $WJ = [:LineBreak = Word_Joiner:];
108 $XX = [:LineBreak = Unknown:];
109 $ZW = [:LineBreak = ZWSpace:];
112 # Dictionary character set, for triggering language-based break engines. Currently
113 # limited to LineBreak=Complex_Context. Note that this set only works in Unicode
114 # 5.0 or later as the definition of Complex_Context was corrected to include all
115 # characters requiring dictionary break.
117 $dictionary = [:LineBreak = Complex_Context:];
120 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
121 # SA (South East Asian: Thai, Lao, Khmer)
122 # SG (Unpaired Surrogates)
123 # XX (Unknown, unassigned)
124 # as $AL (Alphabetic)
126 $ALPlus = [$AL $AI $SA $SG $XX];
129 # Combining Marks. X $CM* behaves as if it were X. Rule LB6.
131 $ALcm = $ALPlus $CM*;
158 ## -------------------------------------------------
163 # Each class of character can stand by itself as an unbroken token, with trailing combining stuff
196 # CAN_CM is the set of characters that may combine with CM combining chars.
197 # Note that Linebreak UAX 14's concept of a combining char and the rules
198 # for what they can combine with are _very_ different from the rest of Unicode.
200 # Note that $CM itself is left out of this set. If CM is needed as a base
201 # it must be listed separately in the rule.
203 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
204 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
207 # AL_FOLLOW set of chars that can unconditionally follow an AL
208 # Needed in rules where stand-alone $CM s are treated as AL.
209 # Chaining is disabled with CM because it causes other failures,
210 # so for this one case we need to manually list out longer sequences.
212 $AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
213 $AL_FOLLOW_CM = [$CL $CP $EX $HL $IS $SY $WJ $GL $OP $QU $BA $HY $NS $IN $NU $ALPlus];
214 $AL_FOLLOW = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
218 # Rule LB 4, 5 Mandatory (Hard) breaks.
220 $LB4Breaks = [$BK $CR $LF $NL];
221 $LB4NonBreaks = [^$BK $CR $LF $NL $CM];
225 # LB 6 Do not break before hard line breaks.
227 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
228 $CAN_CM $CM* $LB4Breaks {100};
229 ^$CM+ $LB4Breaks {100};
233 $LB4NonBreaks [$SP $ZW];
234 $CAN_CM $CM* [$SP $ZW];
238 # LB 8 Break after zero width space
239 # TODO: ZW SP* <break>
240 # An engine change is required to write the reverse rule for this.
241 # For now, leave the Unicode 5.2 rule, ZW <break>
243 $LB8Breaks = [$LB4Breaks $ZW];
244 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
246 # LB 8a ZWJ x ID Emoji proposal.
248 $ZWJ ($ID | $EB | $EM);
250 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
251 # $CM not covered by the above needs to behave like $AL
252 # See definition of $CAN_CM.
254 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
258 # LB 11 Do not break before or after WORD JOINER & related characters.
268 # LB 12 Do not break after NBSP and related characters.
275 # LB 12a Do not break before NBSP and related characters ...
278 [[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GLcm;
284 # LB 13 Don't break before ']' or '!' or ';' or '/', even after spaces.
288 ^$CM+ $CL; # by rule 10, stand-alone CM behaves as AL
292 ^$CM+ $CP; # by rule 10, stand-alone CM behaves as AL
296 ^$CM+ $EX; # by rule 10, stand-alone CM behaves as AL
300 ^$CM+ $IS; # by rule 10, stand-alone CM behaves as AL
304 ^$CM+ $SY; # by rule 10, stand-alone CM behaves as AL
308 # LB 14 Do not break after OP, even after spaces
310 $OPcm $SP* $CAN_CM $CM*;
313 $OPcm $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
319 ($CLcm | $CPcm) $SP* $NScm;
325 # LB 18 Break after spaces.
327 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
328 $LB18Breaks = [$LB8Breaks $SP];
333 $LB18NonBreaks $CM* $QUcm;
344 $LB20NonBreaks = [$LB18NonBreaks - $CB];
346 # LB 21 x (BA | HY | NS)
349 $LB20NonBreaks $CM* ($BAcm | $HYcm | $NScm);
350 ^$CM+ ($BAcm | $HYcm | $NScm);
352 $BBcm [^$CB]; # $BB x
353 $BBcm $LB20NonBreaks $CM*;
355 # LB 21a Don't break after Hebrew + Hyphen
358 $HLcm ($HYcm | $BAcm) [^$CB]?;
360 # LB 21b (forward) Don't break between SY and HL
361 # (break between HL and SY already disallowed by LB 13 above)
365 ($ALcm | $HLcm) $INcm;
366 ^$CM+ $INcm; # by rule 10, any otherwise unattached CM behaves as AL
368 ($ID | $EB | $EM) $CM* $INcm;
374 ($ID | $EB | $EM) $CM* $POcm;
375 $ALcm $NUcm; # includes $LB19
377 ^$CM+ $NUcm; # Rule 10, any otherwise unattached CM behaves as AL
384 $PRcm ($ID | $EB | $EM);
385 $PRcm ($ALcm | $HLcm);
386 $POcm ($ALcm | $HLcm);
387 ($ALcm | $HLcm) ($PRcm | $POcm);
388 ^$CM+ ($PRcm | $POcm); # Rule 10, any otherwise unattached CM behaves as AL
393 ($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* ($CLcm | $CPcm)? ($PRcm | $POcm)?;
395 # LB 26 Do not break a Korean syllable
397 $JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
398 ($JVcm | $H2cm) ($JVcm | $JTcm);
399 ($JTcm | $H3cm) $JTcm;
401 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
402 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
403 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
404 $PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
407 # LB 28 Do not break between alphabetics
409 ($ALcm | $HLcm) ($ALcm | $HLcm);
410 ^$CM+ ($ALcm | $HLcm); # The $CM+ is from rule 10, an unattached CM is treated as AL
413 $IScm ($ALcm | $HLcm);
416 ($ALcm | $HLcm | $NUcm) $OPcm;
417 ^$CM+ $OPcm; # The $CM+ is from rule 10, an unattached CM is treated as AL.
418 $CPcm ($ALcm | $HLcm | $NUcm);
420 # LB 30a Do not break between regional indicators. Break after pairs of them.
421 # Tricky interaction with LB8a: ZWJ x ID
422 $RIcm $RI / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM] {eof}];
423 $RIcm $RI $CM* $ZWJ / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM $ID $EB $EM] {eof}];
424 $RIcm $RI $CM* [$CM-$ZWJ] / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM] {eof}];
426 $RIcm $RIcm [$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS {eof}];
427 $RIcm $RIcm $ZWJ ($ID | $EB | $EM);
429 # LB 30b Do not break between an Emoji Base and an Emoji Modifier
435 ## -------------------------------------------------
472 # Sequences of the form (shown forwards)
473 # [CANT_CM] <break> [CM] [whatever]
474 # The CM needs to behave as an AL
477 [$BK $CR $LF $NL $ZW {eof}] |
479 $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}])); # if LB 14 will match, need to surpress this break.
480 # LB14 says OP SP* x .
481 # becomes OP SP* x AL
482 # becomes OP SP* x CM+ AL_FOLLOW
484 # Further note: the $AL in [$AL {eof}] is only to work around
485 # a rule compiler bug which complains about
486 # empty sets otherwise.
489 # Sequences of the form (shown forwards)
490 # [CANT_CM] <break> [CM] <break> [PR]
491 # The CM needs to behave as an AL
492 # This rule is concerned about getting the second of the two <breaks> in place.
495 # Apple early addition, remove this, superseded by LB24
496 # [$PR ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
502 $LB4Breaks [$LB4NonBreaks-$CM];
503 $LB4Breaks $CM+ $CAN_CM;
509 [$SP $ZW] [$LB4NonBreaks-$CM];
510 [$SP $ZW] $CM+ $CAN_CM;
512 # LB 8 ZW SP* <break>
513 # TODO: to implement this, we need more than one look-ahead hard break in play at a time.
514 # Requires an engine enhancement.
517 # LB 8a ZWJ x ID Unicode Emoji proposal L2/16-011R3
518 # The ZWJ will look like a CM to whatever precedes it.
520 ($ID | $EB | $EM) $ZWJ $CM* $CAN_CM?;
523 # LB 9,10 Combining marks.
524 # X $CM needs to behave like X, where X is not $SP or controls.
525 # $CM not covered by the above needs to behave like $AL
526 # Stick together any combining sequences that don't match other rules.
533 $WJ [$LB8NonBreaks-$CM];
541 $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HY]];
557 $CL [$LB8NonBreaks-$CM];
558 $CP [$LB8NonBreaks-$CM];
559 $EX [$LB8NonBreaks-$CM];
560 $IS [$LB8NonBreaks-$CM];
561 $SY [$LB8NonBreaks-$CM];
563 # Rule 13 & 14 taken together for an edge case.
564 # Match this, shown forward
565 # OP SP+ ($CM+ behaving as $AL) (CL | CP | EX | IS | IY)
566 # This really wants to chain at the $CM+ (which is acting as an $AL)
567 # except for $CM chaining being disabled.
568 [$CL $CP $EX $IS $SY] $CM+ $SP+ $CM* $OP;
572 $CAN_CM $SP* $CM* $OP;
573 $CANT_CM $SP* $CM* $OP;
574 $AL_FOLLOW? $CM+ $SP $SP* $CM* $OP; # by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
576 $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
577 $AL_FOLLOW_CM $CM+ $SP+ $CM* $OP;
584 $NS $SP* $CM* ($CL | $CP);
589 # LB 18 break after spaces
590 # Nothing explicit needed here.
596 $QU $CM* $CAN_CM; # . x QU
600 $CAN_CM $CM* $QU; # QU x .
604 # LB 20 Break before and after CB.
605 # nothing needed here.
609 ($BA | $HY | $NS) $CM* [$LB20NonBreaks-$CM]; # . x (BA | HY | NS)
611 [$LB20NonBreaks-$CM] $CM* $BB; # BB x .
615 [^$CB] $CM* ($HY | $BA) $CM* $HL;
621 $IN $CM* ($ALPlus | $HL);
623 $IN $CM* ($ID | $EB | $EM);
628 $PO $CM* ($ID | $EB | $EM);
629 $NU $CM* ($ALPlus | $HL);
630 ($ALPlus | $HL) $CM* $NU;
633 ($ID | $EB | $EM) $CM* $PR;
634 ($ALPlus | $HL) $CM* $PR;
635 ($ALPlus | $HL) $CM* $PO;
636 $CM* ($PR | $PO) $CM* ($ALPlus | $HL);
637 $CM* ($PR | $PO) $CM+ / [$BK $CR $LF $NL $ZW $SP {eof}];
640 ($CM* ($PR | $PO))? ($CM* ($CL | $CP))? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
643 ($H3 | $H2 | $JV | $JL) $CM* $JL;
644 ($JT | $JV) $CM* ($H2 | $JV);
645 $JT $CM* ($H3 | $JT);
648 $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
649 $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
650 ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
653 ($ALPlus | $HL) $CM* ($ALPlus | $HL);
657 ($ALPlus | $HL) $CM* $IS;
660 $OP $CM* ($ALPlus | $HL | $NU);
661 ($ALPlus | $HL | $NU) $CM* $CP;
664 # Pairs of Regional Indicators.
665 # The following two rules are nearly identical. The first matches only sequences with an odd number of adjacent RIs,
666 # the second with an even number. Stripping away the cruft they look like
667 # [^RI] RI / (RI RI)+ ^RI;
668 # [^RI] RI RI / (RI RI)+ ^RI;
670 [{bof} $NS $HY $BA $QU $CL $CP $EX $IS $SY $WJ $GL $ZW $SP $BK $CR $LF $NL $ZWJ] $CM* $RI / ($CM* $RI $CM* $RI)+ $CM* [{eof}[^$RI $CM]];
671 [{bof} $NS $HY $BA $QU $CL $CP $EX $IS $SY $WJ $GL $ZW $SP $BK $CR $LF $NL $ZWJ] $CM* $RI $CM* $RI / ($CM* $RI $CM* $RI)+ $CM* [{eof}[^$RI $CM]];
673 # In general, adjacent RIs stay together. The hard-break rules, above, overide this, forcing in the boundaries between pairs.
676 # WJ, GL, QU, etc. are classes with rules like "WJ x " which includes "WJ x RI".
677 $RI $CM* ([$WJ $GL $QU $BB] | (($HY | $BA)$CM* $HL));
680 # LB 30b Do not break between an Emoji Base and an Emoji Modifier
684 ## -------------------------------------------------
689 ^$CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
699 $SP+ $CM* ($CL | $CP);
705 $CM* ($HY | $BA) $CM* $HL;
708 ($CM* ($IS | $SY))+ $CM* $NU;
709 ($CL | $CP) $CM* ($NU | $IS | $SY);
714 # For dictionary-based break
715 $dictionary $dictionary;
717 ## -------------------------------------------------
721 # Skip forward over all character classes that are involved in
722 # rules containing patterns with possibly more than one char
725 # It might be slightly more efficient to have specific rules
726 # instead of one generic one, but only if we could
727 # turn off rule chaining. We don't want to move more
730 ^[$CM $OP $QU $CL $CP $B2 $PR $HY $BA $SP $RI $ZWJ $dictionary]+ [^$CM $OP $QU $CL $CP $B2 $PR $HY $BA $RI $ZWJ $dictionary];
731 $dictionary $dictionary;