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/
10 # tailored as noted in 2nd paragraph below..
12 # Includes the Emoji breaking proposals from Unicode L2/16-011R3.
13 # http://www.unicode.org/L2/L2016/16011r3-break-prop-emoji.pdf
15 # TODO: Rule LB 8 remains as it was in Unicode 5.2
16 # This is only because of a limitation of ICU break engine implementation,
17 # not because the older behavior is desirable.
19 # This tailors the line break behavior for Finnish, while otherwise behaving
20 # per UAX 14 which corresponds to CSS line-break=strict (BCP47 -u-lb-strict).
21 # It sets characters of class CJ to behave like NS.
24 # Character Classes defined by TR 14.
31 # !!lookAheadHardBreak Described here because it is (as yet) undocumented elsewhere
32 # and only used for the line break rules.
34 # It is used in the implementation of rule LB 10
35 # which says to treat any combining mark that is not attached to a base
36 # character as if it were of class AL (alphabetic).
38 # The problem occurs in the reverse rules.
40 # Consider a sequence like, with correct breaks as shown
43 # Then consider the sequence without the initial ID (ideographic)
46 # Our CM, which in the first example was attached to the ideograph,
47 # is now unattached, becomes an alpha, and joins in with the other
50 # When iterating forwards, these sequences do not present any problems
51 # When iterating backwards, we need to look ahead when encountering
52 # a CM to see whether it attaches to something further on or not.
53 # (Look-ahead in a reverse rule is looking towards the start)
55 # If the CM is unattached, we need to force a break.
57 # !!lookAheadHardBreak forces the run time state machine to
58 # stop immediately when a look ahead rule ( '/' operator) matches,
59 # and set the match position to that of the look-ahead operator,
60 # no matter what other rules may be in play at the time.
62 # See rule LB 19 for an example.
65 # Temporary definitions of Emoji Base and Emoji Modifiers, until properties are available.
67 $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];
68 $EM = [\U0001F3FB-\U0001F3FF];
70 $AI = [[:LineBreak = Ambiguous:] - [\u2640\u2642]];
71 $AL = [[:LineBreak = Alphabetic:] - [$EM\u2695\u2696\u2764]];
72 $BA = [:LineBreak = Break_After:];
74 $BB = [:LineBreak = Break_Before:];
75 $BK = [:LineBreak = Mandatory_Break:];
76 $B2 = [:LineBreak = Break_Both:];
77 $CB = [:LineBreak = Contingent_Break:];
78 $CJ = [:LineBreak = Conditional_Japanese_Starter:];
79 $CL = [:LineBreak = Close_Punctuation:];
80 $CM = [[:LineBreak = Combining_Mark:] \u200d];
81 $CP = [:LineBreak = Close_Parenthesis:];
82 $CR = [:LineBreak = Carriage_Return:];
83 $EX = [:LineBreak = Exclamation:];
84 $GL = [:LineBreak = Glue:];
85 $HL = [:LineBreak = Hebrew_Letter:];
86 $HY = [:LineBreak = Hyphen:];
87 $H2 = [:LineBreak = H2:];
88 $H3 = [:LineBreak = H3:];
89 $ID = [[:LineBreak = Ideographic:][\u2640\u2642\u2695\u2696\u2764] - $EB];
90 $IN = [:LineBreak = Inseperable:];
91 $IS = [:LineBreak = Infix_Numeric:];
92 $JL = [:LineBreak = JL:];
93 $JV = [:LineBreak = JV:];
94 $JT = [:LineBreak = JT:];
95 $LF = [:LineBreak = Line_Feed:];
96 $NL = [:LineBreak = Next_Line:];
97 $NS = [[:LineBreak = Nonstarter:] $CJ];
98 $NU = [:LineBreak = Numeric:];
99 $OP = [:LineBreak = Open_Punctuation:];
100 $PO = [:LineBreak = Postfix_Numeric:];
101 $PR = [:LineBreak = Prefix_Numeric:];
102 $QU = [:LineBreak = Quotation:];
103 $RI = [:LineBreak = Regional_Indicator:];
104 $SA = [:LineBreak = Complex_Context:];
105 $SG = [:LineBreak = Surrogate:];
106 $SP = [:LineBreak = Space:];
107 $SY = [:LineBreak = Break_Symbols:];
108 $WJ = [:LineBreak = Word_Joiner:];
109 $XX = [:LineBreak = Unknown:];
110 $ZW = [:LineBreak = ZWSpace:];
113 # Dictionary character set, for triggering language-based break engines. Currently
114 # limited to LineBreak=Complex_Context. Note that this set only works in Unicode
115 # 5.0 or later as the definition of Complex_Context was corrected to include all
116 # characters requiring dictionary break.
118 $dictionary = [:LineBreak = Complex_Context:];
121 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
122 # SA (South East Asian: Thai, Lao, Khmer)
123 # SG (Unpaired Surrogates)
124 # XX (Unknown, unassigned)
125 # as $AL (Alphabetic)
127 $ALPlus = [$AL $AI $SA $SG $XX];
130 # Combining Marks. X $CM* behaves as if it were X. Rule LB6.
132 $ALcm = $ALPlus $CM*;
160 ## -------------------------------------------------
165 # Each class of character can stand by itself as an unbroken token, with trailing combining stuff
199 # CAN_CM is the set of characters that may combine with CM combining chars.
200 # Note that Linebreak UAX 14's concept of a combining char and the rules
201 # for what they can combine with are _very_ different from the rest of Unicode.
203 # Note that $CM itself is left out of this set. If CM is needed as a base
204 # it must be listed separately in the rule.
206 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
207 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
210 # AL_FOLLOW set of chars that can unconditionally follow an AL
211 # Needed in rules where stand-alone $CM s are treated as AL.
212 # Chaining is disabled with CM because it causes other failures,
213 # so for this one case we need to manually list out longer sequences.
215 $AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
216 $AL_FOLLOW_CM = [$CL $CP $EX $HL $IS $SY $WJ $GL $OP $QU $BA $HH $HY $NS $IN $NU $ALPlus];
217 $AL_FOLLOW = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
221 # Rule LB 4, 5 Mandatory (Hard) breaks.
223 $LB4Breaks = [$BK $CR $LF $NL];
224 $LB4NonBreaks = [^$BK $CR $LF $NL $CM];
228 # LB 6 Do not break before hard line breaks.
230 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
231 $CAN_CM $CM* $LB4Breaks {100};
232 ^$CM+ $LB4Breaks {100};
236 $LB4NonBreaks [$SP $ZW];
237 $CAN_CM $CM* [$SP $ZW];
241 # LB 8 Break after zero width space
242 # TODO: ZW SP* <break>
243 # An engine change is required to write the reverse rule for this.
244 # For now, leave the Unicode 5.2 rule, ZW <break>
246 $LB8Breaks = [$LB4Breaks $ZW];
247 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
249 # LB 8a ZWJ x ID Emoji proposal.
251 $ZWJ ($ID | $EB | $EM);
253 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
254 # $CM not covered by the above needs to behave like $AL
255 # See definition of $CAN_CM.
257 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
261 # LB 11 Do not break before or after WORD JOINER & related characters.
271 # LB 12 Do not break after NBSP and related characters.
278 # LB 12a Do not break before NBSP and related characters ...
281 [[$LB8NonBreaks] - [$SP $BA $HH $HY]] $CM* $GLcm;
287 # LB 13 Don't break before ']' or '!' or ';' or '/', even after spaces.
291 ^$CM+ $CL; # by rule 10, stand-alone CM behaves as AL
295 ^$CM+ $CP; # by rule 10, stand-alone CM behaves as AL
299 ^$CM+ $EX; # by rule 10, stand-alone CM behaves as AL
303 ^$CM+ $IS; # by rule 10, stand-alone CM behaves as AL
307 ^$CM+ $SY; # by rule 10, stand-alone CM behaves as AL
311 # LB 14 Do not break after OP, even after spaces
313 $OPcm $SP* $CAN_CM $CM*;
316 $OPcm $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
322 ($CLcm | $CPcm) $SP* $NScm;
328 # LB 18 Break after spaces.
330 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
331 $LB18Breaks = [$LB8Breaks $SP];
336 $LB18NonBreaks $CM* $QUcm;
347 $LB20NonBreaks = [$LB18NonBreaks - $CB];
349 # LB 20.09 added rule for Finnish tailoring
350 # LB 21 x (BA | HY | NS)
353 $LB20NonBreaks $CM* ($BAcm | $HHcm | $HYcm | $NScm) / $AL;
354 $LB20NonBreaks $CM* ($BAcm | $HHcm | $HYcm | $NScm);
356 ^$CM+ ($BAcm | $HYcm | $HHcm | $NScm);
358 $BBcm [^$CB]; # $BB x
359 $BBcm $LB20NonBreaks $CM*;
361 # LB 21a Don't break after Hebrew + Hyphen
364 $HLcm ($HYcm | $BAcm | $HHcm) [^$CB]?;
366 # LB 21b (forward) Don't break between SY and HL
367 # (break between HL and SY already disallowed by LB 13 above)
371 ($ALcm | $HLcm) $INcm;
372 ^$CM+ $INcm; # by rule 10, any otherwise unattached CM behaves as AL
374 ($ID | $EB | $EM) $CM* $INcm;
380 ($ID | $EB | $EM) $CM* $POcm;
381 $ALcm $NUcm; # includes $LB19
383 ^$CM+ $NUcm; # Rule 10, any otherwise unattached CM behaves as AL
390 $PRcm ($ID | $EB | $EM);
391 $PRcm ($ALcm | $HLcm);
392 $POcm ($ALcm | $HLcm);
393 ($ALcm | $HLcm) ($PRcm | $POcm);
394 ^$CM+ ($PRcm | $POcm); # Rule 10, any otherwise unattached CM behaves as AL
399 ($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* ($CLcm | $CPcm)? ($PRcm | $POcm)?;
401 # LB 26 Do not break a Korean syllable
403 $JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
404 ($JVcm | $H2cm) ($JVcm | $JTcm);
405 ($JTcm | $H3cm) $JTcm;
407 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
408 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
409 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
410 $PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
413 # LB 28 Do not break between alphabetics
415 ($ALcm | $HLcm) ($ALcm | $HLcm);
416 ^$CM+ ($ALcm | $HLcm); # The $CM+ is from rule 10, an unattached CM is treated as AL
419 $IScm ($ALcm | $HLcm);
422 ($ALcm | $HLcm | $NUcm) $OPcm;
423 ^$CM+ $OPcm; # The $CM+ is from rule 10, an unattached CM is treated as AL.
424 $CPcm ($ALcm | $HLcm | $NUcm);
426 # LB 30a Do not break between regional indicators. Break after pairs of them.
427 # Tricky interaction with LB8a: ZWJ x ID
428 $RIcm $RI / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM] {eof}];
429 $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}];
430 $RIcm $RI $CM* [$CM-$ZWJ] / [[^$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS $CM] {eof}];
432 $RIcm $RIcm [$BK $CR $LF $NL $SP $ZW $WJ $CL $CP $EX $IS $SY $GL $QU $BA $HY $NS {eof}];
433 $RIcm $RIcm $ZWJ ($ID | $EB | $EM);
435 # LB 30b Do not break between an Emoji Base and an Emoji Modifier
441 ## -------------------------------------------------
479 # Sequences of the form (shown forwards)
480 # [CANT_CM] <break> [CM] [whatever]
481 # The CM needs to behave as an AL
484 [$BK $CR $LF $NL $ZW {eof}] |
486 $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}])); # if LB 14 will match, need to surpress this break.
487 # LB14 says OP SP* x .
488 # becomes OP SP* x AL
489 # becomes OP SP* x CM+ AL_FOLLOW
491 # Further note: the $AL in [$AL {eof}] is only to work around
492 # a rule compiler bug which complains about
493 # empty sets otherwise.
496 # Sequences of the form (shown forwards)
497 # [CANT_CM] <break> [CM] <break> [PR]
498 # The CM needs to behave as an AL
499 # This rule is concerned about getting the second of the two <breaks> in place.
502 # Apple early addition, remove this, superseded by LB24
503 # [$PR ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
509 $LB4Breaks [$LB4NonBreaks-$CM];
510 $LB4Breaks $CM+ $CAN_CM;
516 [$SP $ZW] [$LB4NonBreaks-$CM];
517 [$SP $ZW] $CM+ $CAN_CM;
519 # LB 8 ZW SP* <break>
520 # TODO: to implement this, we need more than one look-ahead hard break in play at a time.
521 # Requires an engine enhancement.
524 # LB 8a ZWJ x ID Unicode Emoji proposal L2/16-011R3
525 # The ZWJ will look like a CM to whatever precedes it.
527 ($ID | $EB | $EM) $ZWJ $CM* $CAN_CM?;
530 # LB 9,10 Combining marks.
531 # X $CM needs to behave like X, where X is not $SP or controls.
532 # $CM not covered by the above needs to behave like $AL
533 # Stick together any combining sequences that don't match other rules.
540 $WJ [$LB8NonBreaks-$CM];
548 $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HH $HY]];
564 $CL [$LB8NonBreaks-$CM];
565 $CP [$LB8NonBreaks-$CM];
566 $EX [$LB8NonBreaks-$CM];
567 $IS [$LB8NonBreaks-$CM];
568 $SY [$LB8NonBreaks-$CM];
570 # Rule 13 & 14 taken together for an edge case.
571 # Match this, shown forward
572 # OP SP+ ($CM+ behaving as $AL) (CL | CP | EX | IS | IY)
573 # This really wants to chain at the $CM+ (which is acting as an $AL)
574 # except for $CM chaining being disabled.
575 [$CL $CP $EX $IS $SY] $CM+ $SP+ $CM* $OP;
579 $CAN_CM $SP* $CM* $OP;
580 $CANT_CM $SP* $CM* $OP;
581 $AL_FOLLOW? $CM+ $SP $SP* $CM* $OP; # by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
583 $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
584 $AL_FOLLOW_CM $CM+ $SP+ $CM* $OP;
591 $NS $SP* $CM* ($CL | $CP);
596 # LB 18 break after spaces
597 # Nothing explicit needed here.
603 $QU $CM* $CAN_CM; # . x QU
607 $CAN_CM $CM* $QU; # QU x .
611 # LB 20 Break before and after CB.
612 # nothing needed here.
615 # LB 20.09 added rule for Finnish tailoring
616 $AL ($HY | $HH) / $SP;
619 ($BA | $HH | $HY | $NS) $CM* [$LB20NonBreaks-$CM]; # . x (BA | HY | NS)
621 [$LB20NonBreaks-$CM] $CM* $BB; # BB x .
625 [^$CB] $CM* ($HY | $BA | $HH) $CM* $HL;
631 $IN $CM* ($ALPlus | $HL);
633 $IN $CM* ($ID | $EB | $EM);
638 $PO $CM* ($ID | $EB | $EM);
639 $NU $CM* ($ALPlus | $HL);
640 ($ALPlus | $HL) $CM* $NU;
643 ($ID | $EB | $EM) $CM* $PR;
644 ($ALPlus | $HL) $CM* $PR;
645 ($ALPlus | $HL) $CM* $PO;
646 $CM* ($PR | $PO) $CM* ($ALPlus | $HL);
647 $CM* ($PR | $PO) $CM+ / [$BK $CR $LF $NL $ZW $SP {eof}];
650 ($CM* ($PR | $PO))? ($CM* ($CL | $CP))? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
653 ($H3 | $H2 | $JV | $JL) $CM* $JL;
654 ($JT | $JV) $CM* ($H2 | $JV);
655 $JT $CM* ($H3 | $JT);
658 $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
659 $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
660 ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
663 ($ALPlus | $HL) $CM* ($ALPlus | $HL);
667 ($ALPlus | $HL) $CM* $IS;
670 $OP $CM* ($ALPlus | $HL | $NU);
671 ($ALPlus | $HL | $NU) $CM* $CP;
674 # Pairs of Regional Indicators.
675 # The following two rules are nearly identical. The first matches only sequences with an odd number of adjacent RIs,
676 # the second with an even number. Stripping away the cruft they look like
677 # [^RI] RI / (RI RI)+ ^RI;
678 # [^RI] RI RI / (RI RI)+ ^RI;
680 [{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]];
681 [{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]];
683 # In general, adjacent RIs stay together. The hard-break rules, above, overide this, forcing in the boundaries between pairs.
686 # WJ, GL, QU, etc. are classes with rules like "WJ x " which includes "WJ x RI".
687 $RI $CM* ([$WJ $GL $QU $BB] | (($HY | $BA)$CM* $HL));
690 # LB 30b Do not break between an Emoji Base and an Emoji Modifier
694 ## -------------------------------------------------
699 ^$CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
709 $SP+ $CM* ($CL | $CP);
715 $CM* ($HY | $BA | $HH) $CM* $HL;
718 ($CM* ($IS | $SY))+ $CM* $NU;
719 ($CL | $CP) $CM* ($NU | $IS | $SY);
724 # For dictionary-based break
725 $dictionary $dictionary;
727 ## -------------------------------------------------
731 # Skip forward over all character classes that are involved in
732 # rules containing patterns with possibly more than one char
735 # It might be slightly more efficient to have specific rules
736 # instead of one generic one, but only if we could
737 # turn off rule chaining. We don't want to move more
740 ^[$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];
741 $dictionary $dictionary;