1 # Copyright (c) 2002-2006 International Business Machines Corporation and
2 # others. All Rights Reserved.
7 # Implement default line breaking as defined by Unicode Standard Annex #14 version 5.0.0
8 # http://www.unicode.org/reports/tr14/
13 # Character Classes defined by TR 14.
22 # !!lookAheadHardBreak Described here because it is (as yet) undocumented elsewhere
23 # and only used for the line break rules.
25 # It is used in the implementation of the incredibly annoying rule LB 10
26 # which says to treat any combining mark that is not attached to a base
27 # character as if it were of class AL (alphabetic).
29 # The problem occurs in the reverse rules.
31 # Consider a sequence like, with correct breaks as shown
34 # Then consider the sequence without the initial ID (ideographic)
37 # Our CM, which in the first example was attached to the ideograph,
38 # is now unattached, becomes an alpha, and joins in with the other
41 # When iterating forwards, these sequences do not present any problems
42 # When iterating backwards, we need to look ahead when encountering
43 # a CM to see whether it attaches to something further on or not.
44 # (Look-ahead in a reverse rule is looking towards the start)
46 # If the CM is unattached, we need to force a break.
48 # !!lookAheadHardBreak forces the run time state machine to
49 # stop immediately when a look ahead rule ( '/' operator) matches,
50 # and set the match position to that of the look-ahead operator,
51 # no matter what other rules may be in play at the time.
53 # See rule LB 19 for an example.
56 $AI = [:LineBreak = Ambiguous:];
58 $AL = [[:LineBreak = Alphabetic:] $DG];
59 $BA = [:LineBreak = Break_After:];
60 $BB = [:LineBreak = Break_Before:];
61 $BK = [:LineBreak = Mandatory_Break:];
62 $B2 = [:LineBreak = Break_Both:];
63 $CB = [:LineBreak = Contingent_Break:];
64 $CL = [:LineBreak = Close_Punctuation:] ;
65 $CM = [:LineBreak = Combining_Mark:];
66 $CR = [:LineBreak = Carriage_Return:];
67 $EX = [:LineBreak = Exclamation:];
68 $GL = [:LineBreak = Glue:];
69 $HY = [:LineBreak = Hyphen:];
70 $H2 = [:LineBreak = H2:];
71 $H3 = [:LineBreak = H3:];
72 $ID = [[:LineBreak = Ideographic:] - [\ufe30]];
73 $IN = [:LineBreak = Inseperable:];
74 $IS = [[:LineBreak = Infix_Numeric:] [\ufe30]];
75 $JL = [:LineBreak = JL:];
76 $JV = [:LineBreak = JV:];
77 $JT = [:LineBreak = JT:];
78 $LF = [:LineBreak = Line_Feed:];
79 $NL = [:LineBreak = Next_Line:];
80 $NS = [:LineBreak = Nonstarter:];
81 $NU = [:LineBreak = Numeric:];
82 $OP = [[:LineBreak = Open_Punctuation:] - $DG];
83 $PO = [:LineBreak = Postfix_Numeric:];
85 $PR = [[:LineBreak = Prefix_Numeric:] - $BS];
86 $QU = [:LineBreak = Quotation:];
87 $SA = [:LineBreak = Complex_Context:];
88 $SG = [:LineBreak = Surrogate:];
89 $SP = [:LineBreak = Space:];
90 $SY = [[:LineBreak = Break_Symbols:] $BS];
91 $WJ = [:LineBreak = Word_Joiner:];
92 $XX = [:LineBreak = Unknown:];
93 $ZW = [:LineBreak = ZWSpace:];
95 # Dictionary character set, for triggering language-based break engines. Currently
96 # limited to LineBreak=Complex_Context. Note that this set only works in Unicode
97 # 5.0 or later as the definition of Complex_Context was corrected to include all
98 # characters requiring dictionary break.
100 $dictionary = [:LineBreak = Complex_Context:];
103 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
104 # SA (South East Asian: Thai, Lao, Khmer)
105 # SG (Unpaired Surrogates)
106 # XX (Unknown, unassigned)
107 # as $AL (Alphabetic)
109 $ALPlus = [$AL $AI $SA $SG $XX];
112 # Combining Marks. X $CM* behaves as if it were X. Rule LB6.
114 $ALcm = $ALPlus $CM*;
139 ## -------------------------------------------------
144 # Each class of character can stand by itself as an unbroken token, with trailing combining stuff
172 # CAN_CM is the set of characters that may combine with CM combining chars.
173 # Note that Linebreak UAX 14's concept of a combining char and the rules
174 # for what they can combine with are _very_ different from the rest of Unicode.
176 # Note that $CM itself is left out of this set. If CM is needed as a base
177 # it must be listed separately in the rule.
179 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
180 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
183 # AL_FOLLOW set of chars that can unconditionally follow an AL
184 # Needed in rules where stand-alone $CM s are treated as AL.
185 # Chaining is disabled with CM because it causes other failures,
186 # so for this one case we need to manually list out longer sequences.
188 $AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
189 $AL_FOLLOW_CM = [$CL $EX $IS $SY $WJ $GL $QU $BA $HY $NS $IN $NU $ALPlus $OP];
190 $AL_FOLLOW = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
194 # Rule LB 4, 5 Mandatory (Hard) breaks.
196 $LB4Breaks = [$BK $CR $LF $NL];
197 $LB4NonBreaks = [^$BK $CR $LF $NL];
201 # LB 6 Do not break before hard line breaks.
203 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
204 $CAN_CM $CM* $LB4Breaks {100};
205 $CM+ $LB4Breaks {100};
209 $LB4NonBreaks [$SP $ZW];
210 $CAN_CM $CM* [$SP $ZW];
214 # LB 8 Break after zero width space
216 $LB8Breaks = [$LB4Breaks $ZW];
217 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
220 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
221 # $CM not covered by the above needs to behave like $AL
222 # See definition of $CAN_CM.
224 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
228 # LB 11 Do not break before or after WORD JOINER & related characters.
238 # LB 12 Do not break before or after NBSP and related characters.
241 [$LB8NonBreaks-$SP] $CM* $GLcm;
245 $GLcm ($LB8Breaks | $SP);
246 $GLcm [$LB8NonBreaks-$SP] $CM*; # Don't let a combining mark go onto $CR, $BK, etc.
247 # TODO: I don't think we need this rule.
248 # All but $CM will chain off of preceding rule.
249 # $GLcm will pick up the CM case by itself.
255 # LB 13 Don't break before ']' or '!' or ';' or '/', even after spaces.
259 $CM+ $CL; # by rule 10, stand-alone CM behaves as AL
263 $CM+ $EX; # by rule 10, stand-alone CM behaves as AL
267 $CM+ $IS; # by rule 10, stand-alone CM behaves as AL
271 $CM+ $SY; # by rule 10, stand-alone CM behaves as AL
275 # LB 14 Do not break after OP, even after spaced
277 $OPcm $SP* $CAN_CM $CM*;
280 $OPcm $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
292 # LB 18 Break after spaces.
294 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
295 $LB18Breaks = [$LB8Breaks $SP];
300 $LB18NonBreaks $CM* $QUcm;
305 $QUcm $LB18NonBreaks $CM*; # Don't let a combining mark go onto $CR, $BK, etc.
306 # TODO: I don't think this rule is needed.
313 $LB20NonBreaks = [$LB18NonBreaks - $CB];
315 # LB 21 x (BA | HY | NS)
318 $LB20NonBreaks $CM* ($BAcm | $HYcm | $NScm);
320 $BBcm [^$CB]; # $BB x
321 $BBcm $LB20NonBreaks $CM*;
325 $CM+ $INcm; # by rule 10, any otherwise unattached CM behaves as AL
333 $ALcm $NUcm; # includes $LB19
334 $CM+ $NUcm; # Rule 10, any otherwise unattached CM behaves as AL
348 ($PRcm | $POcm)? ($OPcm)? $NUcm ($NUcm | $SYcm | $IScm)* $CLcm? ($PRcm | $POcm)?;
350 # LB 26 Do not break a Korean syllable
352 $JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
353 ($JVcm | $H2cm) ($JVcm | $JTcm);
354 ($JTcm | $H3cm) $JTcm;
356 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
357 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
358 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
359 $PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
362 # LB 28 Do not break between alphabetics
365 $CM+ $ALcm; # The $CM+ is from rule 10, and unattached CM is treated as AL
368 $IScm ($ALcm | $NUcm);
371 # Rule 30 Do not break between letters, numbers or ordinary symbols
372 # and opening or closing punctuation
374 ($ALcm | $NUcm) $OPcm;
376 $CLcm ($ALcm | $NUcm);
383 ## -------------------------------------------------
415 # Sequences of the form (shown forwards)
416 # [CANT_CM] <break> [CM] [whatever]
417 # The CM needs to behave as an AL
420 [$BK $CR $LF $NL $ZW {eof}] |
422 $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}])); # if LB 14 will match, need to surpress this break.
423 # LB14 says OP SP* x .
424 # becomes OP SP* x AL
425 # becomes OP SP* x CM+ AL_FOLLOW
427 # Further note: the $AL in [$AL {eof}] is only to work around
428 # a rule compiler bug which complains about
429 # empty sets otherwise.
432 # Sequences of the form (shown forwards)
433 # [CANT_CM] <break> [CM] <break> [PR]
434 # The CM needs to behave as an AL
435 # This rule is concerned about getting the second of the two <breaks> in place.
438 [$PR ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
444 $LB4Breaks [$LB4NonBreaks-$CM];
445 $LB4Breaks $CM+ $CAN_CM;
451 [$SP $ZW] [$LB4NonBreaks-$CM];
452 [$SP $ZW] $CM+ $CAN_CM;
454 # LB 8 Break after zero width space
457 # LB 9,10 Combining marks.
458 # X $CM needs to behave like X, where X is not $SP or controls.
459 # $CM not covered by the above needs to behave like $AL
460 # Stick together any combining sequences that don't match other rules.
465 $CM* $WJ $CM* $CAN_CM;
466 $CM* $WJ [$LB8NonBreaks-$CM];
469 $CM* $CAN_CM $CM* $WJ;
474 $CM* $GL $CM* [$LB8NonBreaks-$CM-$SP];
480 $CM* $CAN_CM $CM* $GL;
489 $CL [$LB8NonBreaks-$CM];
490 $EX [$LB8NonBreaks-$CM];
491 $IS [$LB8NonBreaks-$CM];
492 $SY [$LB8NonBreaks-$CM];
494 # Rule 13 & 14 taken together for an edge case.
495 # Match this, shown forward
496 # OP SP+ ($CM+ behaving as $AL) (CL | EX | IS | IY)
497 # This really wants to chain at the $CM+ (which is acting as an $AL)
498 # except for $CM chaining being disabled.
499 [$CL $EX $IS $SY] $CM+ $SP+ $CM* $OP;
503 $CM* $CAN_CM $SP* $CM* $OP;
504 $CANT_CM $SP* $CM* $OP;
505 $AL_FOLLOW? $CM+ $SP $SP* $CM* $OP; # by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
507 $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
508 $CM* $AL_FOLLOW_CM $CM+ $SP+ $CM* $OP;
509 $SY $CM $SP+ $OP; # TODO: Experiment. Remove.
514 # $CM* $OP $SP* $CM* $QU;
517 $CM* $NS $SP* $CM* $CL;
520 $CM* $B2 $SP* $CM* $B2;
522 # LB 18 break after spaces
523 # Nothing explicit needed here.
529 $CM* $QU $CM* $CAN_CM; # . x QU
530 $CM* $QU $LB18NonBreaks;
533 $CM* $CAN_CM $CM* $QU; # QU x .
537 # LB 20 Break before and after CB.
538 # nothing needed here.
542 $CM* ($BA | $HY | $NS) $CM* [$LB20NonBreaks-$CM]; # . x (BA | HY | NS)
544 $CM* [$LB20NonBreaks-$CM] $CM* $BB; # BB x .
550 $CM* $IN $CM* $ALPlus;
557 $CM* $NU $CM* $ALPlus;
558 $CM* $ALPlus $CM* $NU;
562 $CM* $PR $CM* $ALPlus;
563 $CM* $ALPlus $CM* $PR;
564 $CM* $ALPlus $CM* $PO;
566 $CM* $ALPlus $CM* ($IS | $SY | $HY)+ / $SP;
567 $CM* $NU+ $CM* $HY+ / $SP;
570 ($CM* ($PR | $PO))? ($CM* $CL)? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP))? ($CM* ($PR | $PO))?;
573 $CM* ($H3 | $H2 | $JV | $JL) $CM* $JL;
574 $CM* ($JT | $JV) $CM* ($H2 | $JV);
575 $CM* $JT $CM* ($H3 | $JT);
578 $CM* $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
579 $CM* $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
580 $CM* ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
583 $CM* $ALPlus $CM* $ALPlus;
587 $CM* ($NU | $ALPlus) $CM* $IS+ [^$SP];
590 $CM* $OP $CM* ($NU | $ALPlus);
591 $CM* ($NU | $ALPlus) $CM* ($CL | $SY)+ [^$SP];
594 ## -------------------------------------------------
599 $CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
613 ($CM* ($IS | $SY))+ $CM* $NU;
614 $CL $CM* ($NU | $IS | $SY);
616 # For dictionary-based break
617 $dictionary $dictionary;
619 ## -------------------------------------------------
623 # Skip forward over all character classes that are involved in
624 # rules containing patterns with possibly more than one char
627 # It might be slightly more efficient to have specific rules
628 # instead of one generic one, but only if we could
629 # turn off rule chaining. We don't want to move more
632 [$CM $OP $QU $CL $B2 $PR $HY $SP $dictionary]+ [^$CM $OP $QU $CL $B2 $PR $HY $dictionary];
633 $dictionary $dictionary;