1 # Copyright (c) 2002-2008 International Business Machines Corporation and
2 # others. All Rights Reserved.
7 # Implement default line breaking as defined by
8 # Unicode Standard Annex #14 Revision 21 for Unicode 5.1
9 # http://www.unicode.org/reports/tr14/
14 # Character Classes defined by TR 14.
23 # !!lookAheadHardBreak Described here because it is (as yet) undocumented elsewhere
24 # and only used for the line break rules.
26 # It is used in the implementation of the incredibly annoying rule LB 10
27 # which says to treat any combining mark that is not attached to a base
28 # character as if it were of class AL (alphabetic).
30 # The problem occurs in the reverse rules.
32 # Consider a sequence like, with correct breaks as shown
35 # Then consider the sequence without the initial ID (ideographic)
38 # Our CM, which in the first example was attached to the ideograph,
39 # is now unattached, becomes an alpha, and joins in with the other
42 # When iterating forwards, these sequences do not present any problems
43 # When iterating backwards, we need to look ahead when encountering
44 # a CM to see whether it attaches to something further on or not.
45 # (Look-ahead in a reverse rule is looking towards the start)
47 # If the CM is unattached, we need to force a break.
49 # !!lookAheadHardBreak forces the run time state machine to
50 # stop immediately when a look ahead rule ( '/' operator) matches,
51 # and set the match position to that of the look-ahead operator,
52 # no matter what other rules may be in play at the time.
54 # See rule LB 19 for an example.
57 $AI = [:LineBreak = Ambiguous:];
58 $AL = [:LineBreak = Alphabetic:];
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:];
73 $IN = [:LineBreak = Inseperable:];
74 $IS = [:LineBreak = Infix_Numeric:];
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:];
83 $PO = [:LineBreak = Postfix_Numeric:];
84 $PR = [:LineBreak = Prefix_Numeric:];
85 $QU = [:LineBreak = Quotation:];
86 $SA = [:LineBreak = Complex_Context:];
87 $SG = [:LineBreak = Surrogate:];
88 $SP = [:LineBreak = Space:];
89 $SY = [:LineBreak = Break_Symbols:];
90 $WJ = [:LineBreak = Word_Joiner:];
91 $XX = [:LineBreak = Unknown:];
92 $ZW = [:LineBreak = ZWSpace:];
94 # Dictionary character set, for triggering language-based break engines. Currently
95 # limited to LineBreak=Complex_Context. Note that this set only works in Unicode
96 # 5.0 or later as the definition of Complex_Context was corrected to include all
97 # characters requiring dictionary break.
99 $dictionary = [:LineBreak = Complex_Context:];
102 # Rule LB1. By default, treat AI (characters with ambiguous east Asian width),
103 # SA (South East Asian: Thai, Lao, Khmer)
104 # SG (Unpaired Surrogates)
105 # XX (Unknown, unassigned)
106 # as $AL (Alphabetic)
108 $ALPlus = [$AL $AI $SA $SG $XX];
111 # Combining Marks. X $CM* behaves as if it were X. Rule LB6.
113 $ALcm = $ALPlus $CM*;
138 ## -------------------------------------------------
143 # Each class of character can stand by itself as an unbroken token, with trailing combining stuff
171 # CAN_CM is the set of characters that may combine with CM combining chars.
172 # Note that Linebreak UAX 14's concept of a combining char and the rules
173 # for what they can combine with are _very_ different from the rest of Unicode.
175 # Note that $CM itself is left out of this set. If CM is needed as a base
176 # it must be listed separately in the rule.
178 $CAN_CM = [^$SP $BK $CR $LF $NL $ZW $CM]; # Bases that can take CMs
179 $CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM]; # Bases that can't take CMs
182 # AL_FOLLOW set of chars that can unconditionally follow an AL
183 # Needed in rules where stand-alone $CM s are treated as AL.
184 # Chaining is disabled with CM because it causes other failures,
185 # so for this one case we need to manually list out longer sequences.
187 $AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
188 $AL_FOLLOW_CM = [$CL $EX $IS $SY $WJ $GL $QU $BA $HY $NS $IN $NU $ALPlus];
189 $AL_FOLLOW = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
193 # Rule LB 4, 5 Mandatory (Hard) breaks.
195 $LB4Breaks = [$BK $CR $LF $NL];
196 $LB4NonBreaks = [^$BK $CR $LF $NL];
200 # LB 6 Do not break before hard line breaks.
202 $LB4NonBreaks? $LB4Breaks {100}; # LB 5 do not break before hard breaks.
203 $CAN_CM $CM* $LB4Breaks {100};
204 $CM+ $LB4Breaks {100};
208 $LB4NonBreaks [$SP $ZW];
209 $CAN_CM $CM* [$SP $ZW];
213 # LB 8 Break after zero width space
215 $LB8Breaks = [$LB4Breaks $ZW];
216 $LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
219 # LB 9 Combining marks. X $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
220 # $CM not covered by the above needs to behave like $AL
221 # See definition of $CAN_CM.
223 $CAN_CM $CM+; # Stick together any combining sequences that don't match other rules.
227 # LB 11 Do not break before or after WORD JOINER & related characters.
237 # LB 12 Do not break after NBSP and related characters.
244 # LB 12a Do not break before NBSP and related characters ...
247 [[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GLcm;
253 # LB 13 Don't break before ']' or '!' or ';' or '/', even after spaces.
257 $CM+ $CL; # by rule 10, stand-alone CM behaves as AL
261 $CM+ $EX; # by rule 10, stand-alone CM behaves as AL
265 $CM+ $IS; # by rule 10, stand-alone CM behaves as AL
269 $CM+ $SY; # by rule 10, stand-alone CM behaves as AL
273 # LB 14 Do not break after OP, even after spaces
275 $OPcm $SP* $CAN_CM $CM*;
278 $OPcm $SP+ $CM+ $AL_FOLLOW?; # by rule 10, stand-alone CM behaves as AL
290 # LB 18 Break after spaces.
292 $LB18NonBreaks = [$LB8NonBreaks - [$SP]];
293 $LB18Breaks = [$LB8Breaks $SP];
298 $LB18NonBreaks $CM* $QUcm;
303 $QUcm $LB18NonBreaks $CM*; # Don't let a combining mark go onto $CR, $BK, etc.
304 # TODO: I don't think this rule is needed.
311 $LB20NonBreaks = [$LB18NonBreaks - $CB];
313 # LB 21 x (BA | HY | NS)
316 $LB20NonBreaks $CM* ($BAcm | $HYcm | $NScm);
318 $BBcm [^$CB]; # $BB x
319 $BBcm $LB20NonBreaks $CM*;
323 $CM+ $INcm; # by rule 10, any otherwise unattached CM behaves as AL
331 $ALcm $NUcm; # includes $LB19
332 $CM+ $NUcm; # Rule 10, any otherwise unattached CM behaves as AL
345 ($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* $CLcm? ($PRcm | $POcm)?;
347 # LB 26 Do not break a Korean syllable
349 $JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
350 ($JVcm | $H2cm) ($JVcm | $JTcm);
351 ($JTcm | $H3cm) $JTcm;
353 # LB 27 Treat korean Syllable Block the same as ID (don't break it)
354 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
355 ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
356 $PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
359 # LB 28 Do not break between alphabetics
362 $CM+ $ALcm; # The $CM+ is from rule 10, and unattached CM is treated as AL
371 ## -------------------------------------------------
403 # Sequences of the form (shown forwards)
404 # [CANT_CM] <break> [CM] [whatever]
405 # The CM needs to behave as an AL
408 [$BK $CR $LF $NL $ZW {eof}] |
410 $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}])); # if LB 14 will match, need to surpress this break.
411 # LB14 says OP SP* x .
412 # becomes OP SP* x AL
413 # becomes OP SP* x CM+ AL_FOLLOW
415 # Further note: the $AL in [$AL {eof}] is only to work around
416 # a rule compiler bug which complains about
417 # empty sets otherwise.
420 # Sequences of the form (shown forwards)
421 # [CANT_CM] <break> [CM] <break> [PR]
422 # The CM needs to behave as an AL
423 # This rule is concerned about getting the second of the two <breaks> in place.
426 [$PR ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
432 $LB4Breaks [$LB4NonBreaks-$CM];
433 $LB4Breaks $CM+ $CAN_CM;
439 [$SP $ZW] [$LB4NonBreaks-$CM];
440 [$SP $ZW] $CM+ $CAN_CM;
442 # LB 8 Break after zero width space
445 # LB 9,10 Combining marks.
446 # X $CM needs to behave like X, where X is not $SP or controls.
447 # $CM not covered by the above needs to behave like $AL
448 # Stick together any combining sequences that don't match other rules.
453 $CM* $WJ $CM* $CAN_CM;
454 $CM* $WJ [$LB8NonBreaks-$CM];
457 $CM* $CAN_CM $CM* $WJ;
462 $CM* $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HY]];
468 $CM* $CAN_CM $CM* $GL;
477 $CL [$LB8NonBreaks-$CM];
478 $EX [$LB8NonBreaks-$CM];
479 $IS [$LB8NonBreaks-$CM];
480 $SY [$LB8NonBreaks-$CM];
482 # Rule 13 & 14 taken together for an edge case.
483 # Match this, shown forward
484 # OP SP+ ($CM+ behaving as $AL) (CL | EX | IS | IY)
485 # This really wants to chain at the $CM+ (which is acting as an $AL)
486 # except for $CM chaining being disabled.
487 [$CL $EX $IS $SY] $CM+ $SP+ $CM* $OP;
491 $CM* $CAN_CM $SP* $CM* $OP;
492 $CANT_CM $SP* $CM* $OP;
493 $AL_FOLLOW? $CM+ $SP $SP* $CM* $OP; # by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
495 $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
496 $CM* $AL_FOLLOW_CM $CM+ $SP+ $CM* $OP;
497 $SY $CM $SP+ $OP; # TODO: Experiment. Remove.
502 $CM* $OP $SP* $CM* $QU;
505 $CM* $NS $SP* $CM* $CL;
508 $CM* $B2 $SP* $CM* $B2;
510 # LB 18 break after spaces
511 # Nothing explicit needed here.
517 $CM* $QU $CM* $CAN_CM; # . x QU
518 $CM* $QU $LB18NonBreaks;
521 $CM* $CAN_CM $CM* $QU; # QU x .
525 # LB 20 Break before and after CB.
526 # nothing needed here.
530 $CM* ($BA | $HY | $NS) $CM* [$LB20NonBreaks-$CM]; # . x (BA | HY | NS)
532 $CM* [$LB20NonBreaks-$CM] $CM* $BB; # BB x .
538 $CM* $IN $CM* $ALPlus;
545 $CM* $NU $CM* $ALPlus;
546 $CM* $ALPlus $CM* $NU;
550 $CM* $ALPlus $CM* $PR;
551 $CM* $ALPlus $CM* $PO;
555 ($CM* ($PR | $PO))? ($CM* $CL)? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
558 $CM* ($H3 | $H2 | $JV | $JL) $CM* $JL;
559 $CM* ($JT | $JV) $CM* ($H2 | $JV);
560 $CM* $JT $CM* ($H3 | $JT);
563 $CM* $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
564 $CM* $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
565 $CM* ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
568 $CM* $ALPlus $CM* $ALPlus;
572 $CM* $ALPlus $CM* $IS;
575 ## -------------------------------------------------
580 $CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
596 ($CM* ($IS | $SY))+ $CM* $NU;
597 $CL $CM* ($NU | $IS | $SY);
599 # For dictionary-based break
600 $dictionary $dictionary;
602 ## -------------------------------------------------
606 # Skip forward over all character classes that are involved in
607 # rules containing patterns with possibly more than one char
610 # It might be slightly more efficient to have specific rules
611 # instead of one generic one, but only if we could
612 # turn off rule chaining. We don't want to move more
615 [$CM $OP $QU $CL $B2 $PR $HY $SP $dictionary]+ [^$CM $OP $QU $CL $B2 $PR $HY $dictionary];
616 $dictionary $dictionary;