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:] - [\u2010]];
63 $BB = [:LineBreak = Break_Before:];
64 $BK = [:LineBreak = Mandatory_Break:];
65 $B2 = [:LineBreak = Break_Both:];
66 $CB = [:LineBreak = Contingent_Break:];
67 $CJ = [:LineBreak = Conditional_Japanese_Starter:];
68 $CL = [:LineBreak = Close_Punctuation:];
69 $CM = [:LineBreak = Combining_Mark:];
70 $CP = [:LineBreak = Close_Parenthesis:];
71 $CR = [:LineBreak = Carriage_Return:];
72 $EX = [:LineBreak = Exclamation:];
73 $GL = [:LineBreak = Glue:];
74 $HL = [:LineBreak = Hebrew_Letter:];
75 $HY = [:LineBreak = Hyphen:];
76 $H2 = [:LineBreak = H2:];
77 $H3 = [:LineBreak = H3:];
78 $ID = [:LineBreak = Ideographic:];
79 $IN = [:LineBreak = Inseperable:];
80 $IS = [:LineBreak = Infix_Numeric:];
81 $JL = [:LineBreak = JL:];
82 $JV = [:LineBreak = JV:];
83 $JT = [:LineBreak = JT:];
84 $LF = [:LineBreak = Line_Feed:];
85 $NL = [:LineBreak = Next_Line:];
86 $NS = [[:LineBreak = Nonstarter:] $CJ];
87 $NU = [:LineBreak = Numeric:];
88 $OP = [:LineBreak = Open_Punctuation:];
89 $PO = [:LineBreak = Postfix_Numeric:];
90 $PR = [:LineBreak = Prefix_Numeric:];
91 $QU = [:LineBreak = Quotation:];
92 $RI = [:LineBreak = Regional_Indicator:];
93 $SA = [:LineBreak = Complex_Context:];
94 $SG = [:LineBreak = Surrogate:];
95 $SP = [:LineBreak = Space:];
96 $SY = [:LineBreak = Break_Symbols:];
97 $WJ = [:LineBreak = Word_Joiner:];
98 $XX = [:LineBreak = Unknown:];
99 $ZW = [:LineBreak = ZWSpace:];
101 # Dictionary character set, for triggering language-based break engines. Currently
102 # limited to LineBreak=Complex_Context. Note that this set only works in Unicode
103 # 5.0 or later as the definition of Complex_Context was corrected to include all
104 # characters requiring dictionary break.
106 $dictionary = [:LineBreak = Complex_Context:];
109 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
110 # SA (South East Asian: Thai, Lao, Khmer)
111 # SG (Unpaired Surrogates)
112 # XX (Unknown, unassigned)
113 # as $AL (Alphabetic)
115 $ALPlus = [$AL $AI $SA $SG $XX];
118 # Combining Marks. X $CM* behaves as if it were X. Rule LB6.
120 $ALcm = $ALPlus $CM*;
149 ## -------------------------------------------------
154 # Each class of character can stand by itself as an unbroken token, with trailing combining stuff
186 # CAN_CM is the set of characters that may combine with CM combining chars.
187 # Note that Linebreak UAX 14's concept of a combining char and the rules
188 # for what they can combine with are _very_ different from the rest of Unicode.
190 # Note that $CM itself is left out of this set. If CM is needed as a base
191 # it must be listed separately in the rule.
193 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
194 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
197 # AL_FOLLOW set of chars that can unconditionally follow an AL
198 # Needed in rules where stand-alone $CM s are treated as AL.
199 # Chaining is disabled with CM because it causes other failures,
200 # so for this one case we need to manually list out longer sequences.
202 $AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
203 $AL_FOLLOW_CM = [$CL $CP $EX $HL $IS $SY $WJ $GL $OP $QU $BA $HH $HY $NS $IN $NU $ALPlus];
204 $AL_FOLLOW = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
208 # Rule LB 4, 5 Mandatory (Hard) breaks.
210 $LB4Breaks = [$BK $CR $LF $NL];
211 $LB4NonBreaks = [^$BK $CR $LF $NL];
215 # LB 6 Do not break before hard line breaks.
217 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
218 $CAN_CM $CM* $LB4Breaks {100};
219 $CM+ $LB4Breaks {100};
223 $LB4NonBreaks [$SP $ZW];
224 $CAN_CM $CM* [$SP $ZW];
228 # LB 8 Break after zero width space
229 # TODO: ZW SP* <break>
230 # An engine change is required to write the reverse rule for this.
231 # For now, leave the Unicode 5.2 rule, ZW <break>
233 $LB8Breaks = [$LB4Breaks $ZW];
234 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
237 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
238 # $CM not covered by the above needs to behave like $AL
239 # See definition of $CAN_CM.
241 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
245 # LB 11 Do not break before or after WORD JOINER & related characters.
255 # LB 12 Do not break after NBSP and related characters.
262 # LB 12a Do not break before NBSP and related characters ...
265 [[$LB8NonBreaks] - [$SP $BA $HH $HY]] $CM* $GLcm;
271 # LB 13 Don't break before ']' or '!' or ';' or '/', even after spaces.
275 $CM+ $CL; # by rule 10, stand-alone CM behaves as AL
279 $CM+ $CP; # by rule 10, stand-alone CM behaves as AL
283 $CM+ $EX; # by rule 10, stand-alone CM behaves as AL
287 $CM+ $IS; # by rule 10, stand-alone CM behaves as AL
291 $CM+ $SY; # by rule 10, stand-alone CM behaves as AL
295 # LB 14 Do not break after OP, even after spaces
297 $OPcm $SP* $CAN_CM $CM*;
300 $OPcm $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
306 ($CLcm | $CPcm) $SP* $NScm;
312 # LB 18 Break after spaces.
314 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
315 $LB18Breaks = [$LB8Breaks $SP];
320 $LB18NonBreaks $CM* $QUcm;
325 $QUcm $LB18NonBreaks $CM*; # Don't let a combining mark go onto $CR, $BK, etc.
326 # TODO: I don't think this rule is needed.
333 $LB20NonBreaks = [$LB18NonBreaks - $CB];
335 # LB 20.09 added rule for Finnish tailoring
336 # LB 21 x (BA | HY | NS)
339 $LB20NonBreaks $CM* ($BAcm | $HHcm | $HYcm | $NScm) / $AL;
340 $LB20NonBreaks $CM* ($BAcm | $HHcm | $HYcm | $NScm);
343 $BBcm [^$CB]; # $BB x
344 $BBcm $LB20NonBreaks $CM*;
346 # LB 21a Don't break after Hebrew + Hyphen
349 $HLcm ($HYcm | $BAcm | $HHcm) [^$CB]?;
352 ($ALcm | $HLcm) $INcm;
353 $CM+ $INcm; # by rule 10, any otherwise unattached CM behaves as AL
361 $ALcm $NUcm; # includes $LB19
363 $CM+ $NUcm; # Rule 10, any otherwise unattached CM behaves as AL
371 $PRcm ($ALcm | $HLcm);
372 $POcm ($ALcm | $HLcm);
377 ($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* ($CLcm | $CPcm)? ($PRcm | $POcm)?;
379 # LB 26 Do not break a Korean syllable
381 $JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
382 ($JVcm | $H2cm) ($JVcm | $JTcm);
383 ($JTcm | $H3cm) $JTcm;
385 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
386 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
387 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
388 $PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
391 # LB 28 Do not break between alphabetics
393 ($ALcm | $HLcm) ($ALcm | $HLcm);
394 $CM+ ($ALcm | $HLcm); # The $CM+ is from rule 10, an unattached CM is treated as AL
397 $IScm ($ALcm | $HLcm);
400 ($ALcm | $HLcm | $NUcm) $OPcm;
401 $CM+ $OPcm; # The $CM+ is from rule 10, an unattached CM is treated as AL.
402 $CPcm ($ALcm | $HLcm | $NUcm);
404 # LB 30a Do not break between regional indicators.
410 ## -------------------------------------------------
446 # Sequences of the form (shown forwards)
447 # [CANT_CM] <break> [CM] [whatever]
448 # The CM needs to behave as an AL
451 [$BK $CR $LF $NL $ZW {eof}] |
453 $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}])); # if LB 14 will match, need to surpress this break.
454 # LB14 says OP SP* x .
455 # becomes OP SP* x AL
456 # becomes OP SP* x CM+ AL_FOLLOW
458 # Further note: the $AL in [$AL {eof}] is only to work around
459 # a rule compiler bug which complains about
460 # empty sets otherwise.
463 # Sequences of the form (shown forwards)
464 # [CANT_CM] <break> [CM] <break> [PR]
465 # The CM needs to behave as an AL
466 # This rule is concerned about getting the second of the two <breaks> in place.
469 [$PR ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
475 $LB4Breaks [$LB4NonBreaks-$CM];
476 $LB4Breaks $CM+ $CAN_CM;
482 [$SP $ZW] [$LB4NonBreaks-$CM];
483 [$SP $ZW] $CM+ $CAN_CM;
485 # LB 8 ZW SP* <break>
486 # TODO: to implement this, we need more than one look-ahead hard break in play at a time.
487 # Requires an engine enhancement.
490 # LB 9,10 Combining marks.
491 # X $CM needs to behave like X, where X is not $SP or controls.
492 # $CM not covered by the above needs to behave like $AL
493 # Stick together any combining sequences that don't match other rules.
498 $CM* $WJ $CM* $CAN_CM;
499 $CM* $WJ [$LB8NonBreaks-$CM];
502 $CM* $CAN_CM $CM* $WJ;
507 $CM* $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HH $HY]];
513 $CM* $CAN_CM $CM* $GL;
523 $CL [$LB8NonBreaks-$CM];
524 $CP [$LB8NonBreaks-$CM];
525 $EX [$LB8NonBreaks-$CM];
526 $IS [$LB8NonBreaks-$CM];
527 $SY [$LB8NonBreaks-$CM];
529 # Rule 13 & 14 taken together for an edge case.
530 # Match this, shown forward
531 # OP SP+ ($CM+ behaving as $AL) (CL | CP | EX | IS | IY)
532 # This really wants to chain at the $CM+ (which is acting as an $AL)
533 # except for $CM chaining being disabled.
534 [$CL $CP $EX $IS $SY] $CM+ $SP+ $CM* $OP;
538 $CM* $CAN_CM $SP* $CM* $OP;
539 $CANT_CM $SP* $CM* $OP;
540 $AL_FOLLOW? $CM+ $SP $SP* $CM* $OP; # by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
542 $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
543 $CM* $AL_FOLLOW_CM $CM+ $SP+ $CM* $OP;
544 $SY $CM $SP+ $OP; # TODO: Experiment. Remove.
549 $CM* $OP $SP* $CM* $QU;
552 $CM* $NS $SP* $CM* ($CL | $CP);
555 $CM* $B2 $SP* $CM* $B2;
557 # LB 18 break after spaces
558 # Nothing explicit needed here.
564 $CM* $QU $CM* $CAN_CM; # . x QU
565 $CM* $QU $LB18NonBreaks;
568 $CM* $CAN_CM $CM* $QU; # QU x .
572 # LB 20 Break before and after CB.
573 # nothing needed here.
576 # LB 20.09 added rule for Finnish tailoring
577 $AL ($HY | $HH) / $SP;
580 $CM* ($BA | $HH | $HY | $NS) $CM* [$LB20NonBreaks-$CM]; # . x (BA | HY | NS)
582 $CM* [$LB20NonBreaks-$CM] $CM* $BB; # BB x .
586 [^$CB] $CM* ($HY | $BA | $HH) $CM* $HL;
590 $CM* $IN $CM* ($ALPlus | $HL);
597 $CM* $NU $CM* ($ALPlus | $HL);
598 $CM* ($ALPlus | $HL) $CM* $NU;
602 $CM* ($ALPlus | $HL) $CM* $PR;
603 $CM* ($ALPlus | $HL) $CM* $PO;
607 ($CM* ($PR | $PO))? ($CM* ($CL | $CP))? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
610 $CM* ($H3 | $H2 | $JV | $JL) $CM* $JL;
611 $CM* ($JT | $JV) $CM* ($H2 | $JV);
612 $CM* $JT $CM* ($H3 | $JT);
615 $CM* $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
616 $CM* $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
617 $CM* ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
620 $CM* ($ALPlus | $HL) $CM* ($ALPlus | $HL);
624 $CM* ($ALPlus | $HL) $CM* $IS;
627 $CM* $OP $CM* ($ALPlus | $HL | $NU);
628 $CM* ($ALPlus | $HL | $NU) $CM* $CP;
633 ## -------------------------------------------------
638 $CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
648 $SP+ $CM* ($CL | $CP);
654 $CM* ($HY | $BA | $HH) $CM* $HL;
657 ($CM* ($IS | $SY))+ $CM* $NU;
658 ($CL | $CP) $CM* ($NU | $IS | $SY);
660 # For dictionary-based break
661 $dictionary $dictionary;
663 ## -------------------------------------------------
667 # Skip forward over all character classes that are involved in
668 # rules containing patterns with possibly more than one char
671 # It might be slightly more efficient to have specific rules
672 # instead of one generic one, but only if we could
673 # turn off rule chaining. We don't want to move more
676 [$CM $OP $QU $CL $CP $B2 $PR $HY $BA $SP $dictionary]+ [^$CM $OP $QU $CL $CP $B2 $PR $HY $BA $dictionary];
677 $dictionary $dictionary;