1 # Copyright (c) 2002-2012 International Business Machines Corporation and
2 # others. All Rights Reserved.
7 # Implement default line breaking as defined by
8 # Unicode Standard Annex #14 Revision 29 for Unicode 6.2
9 # http://www.unicode.org/reports/tr14/
11 # TODO: Rule LB 8 remains as it was in Unicode 5.2
12 # This is only because of a limitation of ICU break engine implementation,
13 # not because the older behavior is desirable.
16 # Character Classes defined by TR 14.
25 # !!lookAheadHardBreak Described here because it is (as yet) undocumented elsewhere
26 # and only used for the line break rules.
28 # It is used in the implementation of rule LB 10
29 # which says to treat any combining mark that is not attached to a base
30 # character as if it were of class AL (alphabetic).
32 # The problem occurs in the reverse rules.
34 # Consider a sequence like, with correct breaks as shown
37 # Then consider the sequence without the initial ID (ideographic)
40 # Our CM, which in the first example was attached to the ideograph,
41 # is now unattached, becomes an alpha, and joins in with the other
44 # When iterating forwards, these sequences do not present any problems
45 # When iterating backwards, we need to look ahead when encountering
46 # a CM to see whether it attaches to something further on or not.
47 # (Look-ahead in a reverse rule is looking towards the start)
49 # If the CM is unattached, we need to force a break.
51 # !!lookAheadHardBreak forces the run time state machine to
52 # stop immediately when a look ahead rule ( '/' operator) matches,
53 # and set the match position to that of the look-ahead operator,
54 # no matter what other rules may be in play at the time.
56 # See rule LB 19 for an example.
59 $AI = [:LineBreak = Ambiguous:];
60 $AL = [:LineBreak = Alphabetic:];
61 $BA = [:LineBreak = Break_After:];
62 $BB = [:LineBreak = Break_Before:];
63 $BK = [:LineBreak = Mandatory_Break:];
64 $B2 = [:LineBreak = Break_Both:];
65 $CB = [:LineBreak = Contingent_Break:];
66 $CJ = [:LineBreak = Conditional_Japanese_Starter:];
67 $CL = [:LineBreak = Close_Punctuation:];
68 $CM = [:LineBreak = Combining_Mark:];
69 $CP = [:LineBreak = Close_Parenthesis:];
70 $CR = [:LineBreak = Carriage_Return:];
71 $EX = [:LineBreak = Exclamation:];
72 $GL = [:LineBreak = Glue:];
73 $HL = [:LineBreak = Hebrew_Letter:];
74 $HY = [:LineBreak = Hyphen:];
75 $H2 = [:LineBreak = H2:];
76 $H3 = [:LineBreak = H3:];
77 $ID = [:LineBreak = Ideographic:];
78 $IN = [:LineBreak = Inseperable:];
79 $IS = [:LineBreak = Infix_Numeric:];
80 $JL = [:LineBreak = JL:];
81 $JV = [:LineBreak = JV:];
82 $JT = [:LineBreak = JT:];
83 $LF = [:LineBreak = Line_Feed:];
84 $NL = [:LineBreak = Next_Line:];
85 $NS = [[:LineBreak = Nonstarter:] $CJ];
86 $NU = [:LineBreak = Numeric:];
87 $OP = [:LineBreak = Open_Punctuation:];
88 $PO = [:LineBreak = Postfix_Numeric:];
89 $PR = [:LineBreak = Prefix_Numeric:];
90 $QU = [:LineBreak = Quotation:];
91 $RI = [:LineBreak = Regional_Indicator:];
92 $SA = [:LineBreak = Complex_Context:];
93 $SG = [:LineBreak = Surrogate:];
94 $SP = [:LineBreak = Space:];
95 $SY = [:LineBreak = Break_Symbols:];
96 $WJ = [:LineBreak = Word_Joiner:];
97 $XX = [:LineBreak = Unknown:];
98 $ZW = [:LineBreak = ZWSpace:];
100 # Dictionary character set, for triggering language-based break engines. Currently
101 # limited to LineBreak=Complex_Context. Note that this set only works in Unicode
102 # 5.0 or later as the definition of Complex_Context was corrected to include all
103 # characters requiring dictionary break.
105 $dictionary = [:LineBreak = Complex_Context:];
108 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
109 # SA (South East Asian: Thai, Lao, Khmer)
110 # SG (Unpaired Surrogates)
111 # XX (Unknown, unassigned)
112 # as $AL (Alphabetic)
114 $ALPlus = [$AL $AI $SA $SG $XX];
117 # Combining Marks. X $CM* behaves as if it were X. Rule LB6.
119 $ALcm = $ALPlus $CM*;
147 ## -------------------------------------------------
152 # Each class of character can stand by itself as an unbroken token, with trailing combining stuff
183 # CAN_CM is the set of characters that may combine with CM combining chars.
184 # Note that Linebreak UAX 14's concept of a combining char and the rules
185 # for what they can combine with are _very_ different from the rest of Unicode.
187 # Note that $CM itself is left out of this set. If CM is needed as a base
188 # it must be listed separately in the rule.
190 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
191 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
194 # AL_FOLLOW set of chars that can unconditionally follow an AL
195 # Needed in rules where stand-alone $CM s are treated as AL.
196 # Chaining is disabled with CM because it causes other failures,
197 # so for this one case we need to manually list out longer sequences.
199 $AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
200 $AL_FOLLOW_CM = [$CL $CP $EX $HL $IS $SY $WJ $GL $OP $QU $BA $HY $NS $IN $NU $ALPlus];
201 $AL_FOLLOW = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
205 # Rule LB 4, 5 Mandatory (Hard) breaks.
207 $LB4Breaks = [$BK $CR $LF $NL];
208 $LB4NonBreaks = [^$BK $CR $LF $NL];
212 # LB 6 Do not break before hard line breaks.
214 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
215 $CAN_CM $CM* $LB4Breaks {100};
216 $CM+ $LB4Breaks {100};
220 $LB4NonBreaks [$SP $ZW];
221 $CAN_CM $CM* [$SP $ZW];
225 # LB 8 Break after zero width space
226 # TODO: ZW SP* <break>
227 # An engine change is required to write the reverse rule for this.
228 # For now, leave the Unicode 5.2 rule, ZW <break>
230 $LB8Breaks = [$LB4Breaks $ZW];
231 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
234 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
235 # $CM not covered by the above needs to behave like $AL
236 # See definition of $CAN_CM.
238 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
242 # LB 11 Do not break before or after WORD JOINER & related characters.
252 # LB 12 Do not break after NBSP and related characters.
259 # LB 12a Do not break before NBSP and related characters ...
262 [[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GLcm;
268 # LB 13 Don't break before ']' or '!' or ';' or '/', even after spaces.
272 $CM+ $CL; # by rule 10, stand-alone CM behaves as AL
276 $CM+ $CP; # by rule 10, stand-alone CM behaves as AL
280 $CM+ $EX; # by rule 10, stand-alone CM behaves as AL
284 $CM+ $IS; # by rule 10, stand-alone CM behaves as AL
288 $CM+ $SY; # by rule 10, stand-alone CM behaves as AL
292 # LB 14 Do not break after OP, even after spaces
294 $OPcm $SP* $CAN_CM $CM*;
297 $OPcm $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
303 ($CLcm | $CPcm) $SP* $NScm;
309 # LB 18 Break after spaces.
311 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
312 $LB18Breaks = [$LB8Breaks $SP];
317 $LB18NonBreaks $CM* $QUcm;
322 $QUcm $LB18NonBreaks $CM*; # Don't let a combining mark go onto $CR, $BK, etc.
323 # TODO: I don't think this rule is needed.
330 $LB20NonBreaks = [$LB18NonBreaks - $CB];
332 # LB 21 x (BA | HY | NS)
335 $LB20NonBreaks $CM* ($BAcm | $HYcm | $NScm);
337 $BBcm [^$CB]; # $BB x
338 $BBcm $LB20NonBreaks $CM*;
340 # LB 21a Don't break after Hebrew + Hyphen
343 $HLcm ($HYcm | $BAcm) [^$CB]?;
345 # LB 21b (forward) Don't break between SY and HL
346 # (break between HL and SY already disallowed by LB 13 above)
350 ($ALcm | $HLcm) $INcm;
351 $CM+ $INcm; # by rule 10, any otherwise unattached CM behaves as AL
359 $ALcm $NUcm; # includes $LB19
361 $CM+ $NUcm; # Rule 10, any otherwise unattached CM behaves as AL
369 $PRcm ($ALcm | $HLcm);
370 $POcm ($ALcm | $HLcm);
375 ($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* ($CLcm | $CPcm)? ($PRcm | $POcm)?;
377 # LB 26 Do not break a Korean syllable
379 $JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
380 ($JVcm | $H2cm) ($JVcm | $JTcm);
381 ($JTcm | $H3cm) $JTcm;
383 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
384 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
385 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
386 $PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
389 # LB 28 Do not break between alphabetics
391 ($ALcm | $HLcm) ($ALcm | $HLcm);
392 $CM+ ($ALcm | $HLcm); # The $CM+ is from rule 10, an unattached CM is treated as AL
395 $IScm ($ALcm | $HLcm);
398 ($ALcm | $HLcm | $NUcm) $OPcm;
399 $CM+ $OPcm; # The $CM+ is from rule 10, an unattached CM is treated as AL.
400 $CPcm ($ALcm | $HLcm | $NUcm);
402 # LB 30a Do not break between regional indicators.
408 ## -------------------------------------------------
443 # Sequences of the form (shown forwards)
444 # [CANT_CM] <break> [CM] [whatever]
445 # The CM needs to behave as an AL
448 [$BK $CR $LF $NL $ZW {eof}] |
450 $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}])); # if LB 14 will match, need to surpress this break.
451 # LB14 says OP SP* x .
452 # becomes OP SP* x AL
453 # becomes OP SP* x CM+ AL_FOLLOW
455 # Further note: the $AL in [$AL {eof}] is only to work around
456 # a rule compiler bug which complains about
457 # empty sets otherwise.
460 # Sequences of the form (shown forwards)
461 # [CANT_CM] <break> [CM] <break> [PR]
462 # The CM needs to behave as an AL
463 # This rule is concerned about getting the second of the two <breaks> in place.
466 [$PR ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
472 $LB4Breaks [$LB4NonBreaks-$CM];
473 $LB4Breaks $CM+ $CAN_CM;
479 [$SP $ZW] [$LB4NonBreaks-$CM];
480 [$SP $ZW] $CM+ $CAN_CM;
482 # LB 8 ZW SP* <break>
483 # TODO: to implement this, we need more than one look-ahead hard break in play at a time.
484 # Requires an engine enhancement.
487 # LB 9,10 Combining marks.
488 # X $CM needs to behave like X, where X is not $SP or controls.
489 # $CM not covered by the above needs to behave like $AL
490 # Stick together any combining sequences that don't match other rules.
495 $CM* $WJ $CM* $CAN_CM;
496 $CM* $WJ [$LB8NonBreaks-$CM];
499 $CM* $CAN_CM $CM* $WJ;
504 $CM* $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HY]];
510 $CM* $CAN_CM $CM* $GL;
520 $CL [$LB8NonBreaks-$CM];
521 $CP [$LB8NonBreaks-$CM];
522 $EX [$LB8NonBreaks-$CM];
523 $IS [$LB8NonBreaks-$CM];
524 $SY [$LB8NonBreaks-$CM];
526 # Rule 13 & 14 taken together for an edge case.
527 # Match this, shown forward
528 # OP SP+ ($CM+ behaving as $AL) (CL | CP | EX | IS | IY)
529 # This really wants to chain at the $CM+ (which is acting as an $AL)
530 # except for $CM chaining being disabled.
531 [$CL $CP $EX $IS $SY] $CM+ $SP+ $CM* $OP;
535 $CM* $CAN_CM $SP* $CM* $OP;
536 $CANT_CM $SP* $CM* $OP;
537 $AL_FOLLOW? $CM+ $SP $SP* $CM* $OP; # by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
539 $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
540 $CM* $AL_FOLLOW_CM $CM+ $SP+ $CM* $OP;
541 $SY $CM $SP+ $OP; # TODO: Experiment. Remove.
546 $CM* $OP $SP* $CM* $QU;
549 $CM* $NS $SP* $CM* ($CL | $CP);
552 $CM* $B2 $SP* $CM* $B2;
554 # LB 18 break after spaces
555 # Nothing explicit needed here.
561 $CM* $QU $CM* $CAN_CM; # . x QU
562 $CM* $QU $LB18NonBreaks;
565 $CM* $CAN_CM $CM* $QU; # QU x .
569 # LB 20 Break before and after CB.
570 # nothing needed here.
574 $CM* ($BA | $HY | $NS) $CM* [$LB20NonBreaks-$CM]; # . x (BA | HY | NS)
576 $CM* [$LB20NonBreaks-$CM] $CM* $BB; # BB x .
580 [^$CB] $CM* ($HY | $BA) $CM* $HL;
586 $CM* $IN $CM* ($ALPlus | $HL);
593 $CM* $NU $CM* ($ALPlus | $HL);
594 $CM* ($ALPlus | $HL) $CM* $NU;
598 $CM* ($ALPlus | $HL) $CM* $PR;
599 $CM* ($ALPlus | $HL) $CM* $PO;
603 ($CM* ($PR | $PO))? ($CM* ($CL | $CP))? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
606 $CM* ($H3 | $H2 | $JV | $JL) $CM* $JL;
607 $CM* ($JT | $JV) $CM* ($H2 | $JV);
608 $CM* $JT $CM* ($H3 | $JT);
611 $CM* $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
612 $CM* $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
613 $CM* ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
616 $CM* ($ALPlus | $HL) $CM* ($ALPlus | $HL);
620 $CM* ($ALPlus | $HL) $CM* $IS;
623 $CM* $OP $CM* ($ALPlus | $HL | $NU);
624 $CM* ($ALPlus | $HL | $NU) $CM* $CP;
629 ## -------------------------------------------------
634 $CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
644 $SP+ $CM* ($CL | $CP);
650 $CM* ($HY | $BA) $CM* $HL;
653 ($CM* ($IS | $SY))+ $CM* $NU;
654 ($CL | $CP) $CM* ($NU | $IS | $SY);
656 # For dictionary-based break
657 $dictionary $dictionary;
659 ## -------------------------------------------------
663 # Skip forward over all character classes that are involved in
664 # rules containing patterns with possibly more than one char
667 # It might be slightly more efficient to have specific rules
668 # instead of one generic one, but only if we could
669 # turn off rule chaining. We don't want to move more
672 [$CM $OP $QU $CL $CP $B2 $PR $HY $BA $SP $dictionary]+ [^$CM $OP $QU $CL $CP $B2 $PR $HY $BA $dictionary];
673 $dictionary $dictionary;