1# Copyright (c) 2002-2015  International Business Machines Corporation and
2# others. All Rights Reserved.
3#
4#  file:  line_normal.txt
5#
6#         Line Breaking Rules
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/
10#         tailored as noted in 2nd paragraph below..
11#
12#         TODO:  Rule LB 8 remains as it was in Unicode 5.2
13#         This is only because of a limitation of ICU break engine implementation,
14#         not because the older behavior is desirable.
15#
16#         This tailors the line break behavior to correspond to CSS
17#         line-break=normal (BCP47 -u-lb-normal) as defined for languages other than
18#         Chinese & Japanese.
19#         It sets characters of class CJ to behave like ID.
20
21#
22#  Character Classes defined by TR 14.
23#
24
25!!chain;
26!!LBCMNoChain;
27
28
29!!lookAheadHardBreak;
30#
31#  !!lookAheadHardBreak    Described here because it is (as yet) undocumented elsewhere
32#                          and only used for the line break rules.
33#
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).
37#
38#           The problem occurs in the reverse rules.
39#
40#           Consider a sequence like, with correct breaks as shown
41#               LF  ID  CM  AL  AL
42#                  ^       ^       ^
43#           Then consider the sequence without the initial ID (ideographic)
44#                 LF  CM  AL  AL
45#                    ^           ^
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
48#           alphas.
49#
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)
54#
55#           If the CM is unattached, we need to force a break.
56#
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.
61#
62#           See rule LB 19 for an example.
63#
64
65$AI = [:LineBreak =  Ambiguous:];
66$AL = [:LineBreak =  Alphabetic:];
67$BA = [:LineBreak =  Break_After:];
68$BB = [:LineBreak =  Break_Before:];
69$BK = [:LineBreak =  Mandatory_Break:];
70$B2 = [:LineBreak =  Break_Both:];
71$CB = [:LineBreak =  Contingent_Break:];
72$CJ = [:LineBreak =  Conditional_Japanese_Starter:];
73$CL = [:LineBreak =  Close_Punctuation:];
74$CM = [:LineBreak =  Combining_Mark:];
75$CP = [:LineBreak =  Close_Parenthesis:];
76$CR = [:LineBreak =  Carriage_Return:];
77$EX = [:LineBreak =  Exclamation:];
78$GL = [:LineBreak =  Glue:];
79$HL = [:LineBreak =  Hebrew_Letter:];
80$HY = [:LineBreak =  Hyphen:];
81$H2 = [:LineBreak =  H2:];
82$H3 = [:LineBreak =  H3:];
83$ID = [[:LineBreak =  Ideographic:] $CJ];
84$IN = [:LineBreak =  Inseperable:];
85$IS = [:LineBreak =  Infix_Numeric:];
86$JL = [:LineBreak =  JL:];
87$JV = [:LineBreak =  JV:];
88$JT = [:LineBreak =  JT:];
89$LF = [:LineBreak =  Line_Feed:];
90$NL = [:LineBreak =  Next_Line:];
91$NS = [:LineBreak =  Nonstarter:];
92$NU = [:LineBreak =  Numeric:];
93$OP = [:LineBreak =  Open_Punctuation:];
94$PO = [:LineBreak =  Postfix_Numeric:];
95$PR = [:LineBreak =  Prefix_Numeric:];
96$QU = [:LineBreak =  Quotation:];
97$RI = [:LineBreak =  Regional_Indicator:];
98$SA = [:LineBreak =  Complex_Context:];
99$SG = [:LineBreak =  Surrogate:];
100$SP = [:LineBreak =  Space:];
101$SY = [:LineBreak =  Break_Symbols:];
102$WJ = [:LineBreak =  Word_Joiner:];
103$XX = [:LineBreak =  Unknown:];
104$ZW = [:LineBreak =  ZWSpace:];
105
106#   Dictionary character set, for triggering language-based break engines. Currently
107#   limited to LineBreak=Complex_Context. Note that this set only works in Unicode
108#   5.0 or later as the definition of Complex_Context was corrected to include all
109#   characters requiring dictionary break.
110
111$dictionary = [:LineBreak = Complex_Context:];
112
113#
114#  Rule LB1.  By default, treat AI  (characters with ambiguous east Asian width),
115#                               SA  (South East Asian: Thai, Lao, Khmer)
116#                               SG  (Unpaired Surrogates)
117#                               XX  (Unknown, unassigned)
118#                         as $AL  (Alphabetic)
119#
120$ALPlus = [$AL $AI $SA $SG $XX];
121
122#
123#  Combining Marks.   X $CM*  behaves as if it were X.  Rule LB6.
124#
125$ALcm = $ALPlus $CM*;
126$BAcm = $BA $CM*;
127$BBcm = $BB $CM*;
128$B2cm = $B2 $CM*;
129$CLcm = $CL $CM*;
130$CPcm = $CP $CM*;
131$EXcm = $EX $CM*;
132$GLcm = $GL $CM*;
133$HLcm = $HL $CM*;
134$HYcm = $HY $CM*;
135$H2cm = $H2 $CM*;
136$H3cm = $H3 $CM*;
137$IDcm = $ID $CM*;
138$INcm = $IN $CM*;
139$IScm = $IS $CM*;
140$JLcm = $JL $CM*;
141$JVcm = $JV $CM*;
142$JTcm = $JT $CM*;
143$NScm = $NS $CM*;
144$NUcm = $NU $CM*;
145$OPcm = $OP $CM*;
146$POcm = $PO $CM*;
147$PRcm = $PR $CM*;
148$QUcm = $QU $CM*;
149$RIcm = $RI $CM*;
150$SYcm = $SY $CM*;
151$WJcm = $WJ $CM*;
152
153## -------------------------------------------------
154
155!!forward;
156
157#
158#  Each class of character can stand by itself as an unbroken token, with trailing combining stuff
159#
160$ALPlus $CM+;
161$BA $CM+;
162$BB $CM+;
163$B2 $CM+;
164$CL $CM+;
165$CP $CM+;
166$EX $CM+;
167$GL $CM+;
168$HL $CM+;
169$HY $CM+;
170$H2 $CM+;
171$H3 $CM+;
172$ID $CM+;
173$IN $CM+;
174$IS $CM+;
175$JL $CM+;
176$JV $CM+;
177$JT $CM+;
178$NS $CM+;
179$NU $CM+;
180$OP $CM+;
181$PO $CM+;
182$PR $CM+;
183$QU $CM+;
184$RI $CM+;
185$SY $CM+;
186$WJ $CM+;
187
188#
189# CAN_CM  is the set of characters that may combine with CM combining chars.
190#         Note that Linebreak UAX 14's concept of a combining char and the rules
191#         for what they can combine with are _very_ different from the rest of Unicode.
192#
193#         Note that $CM itself is left out of this set.  If CM is needed as a base
194#         it must be listed separately in the rule.
195#
196$CAN_CM  = [^$SP $BK $CR $LF $NL $ZW $CM];       # Bases that can   take CMs
197$CANT_CM = [ $SP $BK $CR $LF $NL $ZW $CM];       # Bases that can't take CMs
198
199#
200# AL_FOLLOW  set of chars that can unconditionally follow an AL
201#            Needed in rules where stand-alone $CM s are treated as AL.
202#            Chaining is disabled with CM because it causes other failures,
203#            so for this one case we need to manually list out longer sequences.
204#
205$AL_FOLLOW_NOCM = [$BK $CR $LF $NL $ZW $SP];
206$AL_FOLLOW_CM   = [$CL $CP $EX $HL $IS $SY $WJ $GL $OP $QU $BA $HY $NS $IN $NU $ALPlus];
207$AL_FOLLOW      = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
208
209
210#
211#  Rule LB 4, 5    Mandatory (Hard) breaks.
212#
213$LB4Breaks    = [$BK $CR $LF $NL];
214$LB4NonBreaks = [^$BK $CR $LF $NL];
215$CR $LF {100};
216
217#
218#  LB 6    Do not break before hard line breaks.
219#
220$LB4NonBreaks?  $LB4Breaks {100};    # LB 5  do not break before hard breaks.
221$CAN_CM $CM*    $LB4Breaks {100};
222$CM+            $LB4Breaks {100};
223
224# LB 7         x SP
225#              x ZW
226$LB4NonBreaks [$SP $ZW];
227$CAN_CM $CM*  [$SP $ZW];
228$CM+          [$SP $ZW];
229
230#
231# LB 8         Break after zero width space
232#              TODO:  ZW SP* <break>
233#              An engine change is required to write the reverse rule for this.
234#              For now, leave the Unicode 5.2 rule, ZW <break>
235#
236$LB8Breaks    = [$LB4Breaks $ZW];
237$LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
238
239
240# LB 9     Combining marks.      X   $CM needs to behave like X, where X is not $SP, $BK $CR $LF $NL
241#                                $CM not covered by the above needs to behave like $AL
242#                                See definition of $CAN_CM.
243
244$CAN_CM $CM+;                   #  Stick together any combining sequences that don't match other rules.
245$CM+;
246
247#
248# LB 11  Do not break before or after WORD JOINER & related characters.
249#
250$CAN_CM $CM*  $WJcm;
251$LB8NonBreaks $WJcm;
252$CM+          $WJcm;
253
254$WJcm $CANT_CM;
255$WJcm $CAN_CM $CM*;
256
257#
258# LB 12  Do not break after NBSP and related characters.
259#         GL  x
260#
261$GLcm $CAN_CM $CM*;
262$GLcm $CANT_CM;
263
264#
265# LB 12a  Do not break before NBSP and related characters ...
266#            [^SP BA HY] x GL
267#
268[[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GLcm;
269$CM+ GLcm;
270
271
272
273#
274# LB 13   Don't break before ']' or '!' or ';' or '/', even after spaces.
275#
276$LB8NonBreaks $CL;
277$CAN_CM $CM*  $CL;
278$CM+          $CL;              # by rule 10, stand-alone CM behaves as AL
279
280$LB8NonBreaks $CP;
281$CAN_CM $CM*  $CP;
282$CM+          $CP;              # by rule 10, stand-alone CM behaves as AL
283
284$LB8NonBreaks $EX;
285$CAN_CM $CM*  $EX;
286$CM+          $EX;              # by rule 10, stand-alone CM behaves as AL
287
288$LB8NonBreaks $IS;
289$CAN_CM $CM*  $IS;
290$CM+          $IS;              # by rule 10, stand-alone CM behaves as AL
291
292$LB8NonBreaks $SY;
293$CAN_CM $CM*  $SY;
294$CM+          $SY;              # by rule 10, stand-alone CM behaves as AL
295
296
297#
298# LB 14  Do not break after OP, even after spaces
299#
300$OPcm $SP* $CAN_CM $CM*;
301$OPcm $SP* $CANT_CM;
302
303$OPcm $SP+ $CM+ $AL_FOLLOW?;    # by rule 10, stand-alone CM behaves as AL
304
305# LB 15
306$QUcm $SP* $OPcm;
307
308# LB 16
309($CLcm | $CPcm) $SP* $NScm;
310
311# LB 17
312$B2cm $SP* $B2cm;
313
314#
315# LB 18  Break after spaces.
316#
317$LB18NonBreaks = [$LB8NonBreaks - [$SP]];
318$LB18Breaks    = [$LB8Breaks $SP];
319
320
321# LB 19
322#         x QU
323$LB18NonBreaks $CM* $QUcm;
324$CM+                $QUcm;
325
326#         QU  x
327$QUcm .?;
328$QUcm $LB18NonBreaks $CM*;    # Don't let a combining mark go onto $CR, $BK, etc.
329                              #  TODO:  I don't think this rule is needed.
330
331
332# LB 20
333#        <break>  $CB
334#        $CB   <break>
335
336$LB20NonBreaks = [$LB18NonBreaks - $CB];
337
338# LB 21        x   (BA | HY | NS)
339#           BB x
340#
341$LB20NonBreaks $CM* ($BAcm | $HYcm | $NScm);
342
343$BBcm [^$CB];                                  #  $BB  x
344$BBcm $LB20NonBreaks $CM*;
345
346# LB 21a Don't break after Hebrew + Hyphen
347#   HL (HY | BA) x
348#
349$HLcm ($HYcm | $BAcm) [^$CB]?;
350
351# LB 21b (forward) Don't break between SY and HL
352# (break between HL and SY already disallowed by LB 13 above)
353$SYcm $HLcm;
354
355# LB 22
356($ALcm | $HLcm) $INcm;
357$CM+     $INcm;     #  by rule 10, any otherwise unattached CM behaves as AL
358$IDcm    $INcm;
359$INcm    $INcm;
360$NUcm    $INcm;
361
362
363# $LB 23
364$IDcm  $POcm;
365$ALcm  $NUcm;       # includes $LB19
366$HLcm  $NUcm;
367$CM+   $NUcm;       # Rule 10, any otherwise unattached CM behaves as AL
368$NUcm  $ALcm;
369$NUcm  $HLcm;
370
371#
372# LB 24
373#
374$PRcm $IDcm;
375$PRcm ($ALcm | $HLcm);
376$POcm ($ALcm | $HLcm);
377
378#
379# LB 25   Numbers.
380#
381($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* ($CLcm | $CPcm)? ($PRcm | $POcm)?;
382
383# LB 26  Do not break a Korean syllable
384#
385$JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
386($JVcm | $H2cm) ($JVcm | $JTcm);
387($JTcm | $H3cm) $JTcm;
388
389# LB 27  Treat korean Syllable Block the same as ID  (don't break it)
390($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
391($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
392$PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
393
394
395# LB 28   Do not break between alphabetics
396#
397($ALcm | $HLcm) ($ALcm | $HLcm);
398$CM+ ($ALcm | $HLcm);      # The $CM+ is from rule 10, an unattached CM is treated as AL
399
400# LB 29
401$IScm ($ALcm | $HLcm);
402
403# LB 30
404($ALcm | $HLcm | $NUcm) $OPcm;
405$CM+ $OPcm;         # The $CM+ is from rule 10, an unattached CM is treated as AL.
406$CPcm ($ALcm | $HLcm | $NUcm);
407
408# LB 30a  Do not break between regional indicators.
409$RIcm $RIcm;
410
411#
412#  Reverse Rules.
413#
414## -------------------------------------------------
415
416!!reverse;
417
418$CM+ $ALPlus;
419$CM+ $BA;
420$CM+ $BB;
421$CM+ $B2;
422$CM+ $CL;
423$CM+ $CP;
424$CM+ $EX;
425$CM+ $GL;
426$CM+ $HL;
427$CM+ $HY;
428$CM+ $H2;
429$CM+ $H3;
430$CM+ $ID;
431$CM+ $IN;
432$CM+ $IS;
433$CM+ $JL;
434$CM+ $JV;
435$CM+ $JT;
436$CM+ $NS;
437$CM+ $NU;
438$CM+ $OP;
439$CM+ $PO;
440$CM+ $PR;
441$CM+ $QU;
442$CM+ $RI;
443$CM+ $SY;
444$CM+ $WJ;
445$CM+;
446
447
448#
449#  Sequences of the form  (shown forwards)
450#      [CANT_CM]  <break>  [CM]  [whatever]
451#  The CM needs to behave as an AL
452#
453$AL_FOLLOW $CM+ / (
454          [$BK $CR $LF $NL $ZW {eof}] |
455          $SP+ $CM+ $SP |
456          $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}]));   # if LB 14 will match, need to surpress this break.
457                                               #  LB14 says    OP SP* x .
458                                               #    becomes    OP SP* x AL
459                                               #    becomes    OP SP* x CM+ AL_FOLLOW
460                                               #
461                                               # Further note:  the $AL in [$AL {eof}] is only to work around
462                                               #                a rule compiler bug which complains about
463                                               #                empty sets otherwise.
464
465#
466#  Sequences of the form  (shown forwards)
467#      [CANT_CM]  <break> [CM]  <break>  [PR]
468#  The CM needs to behave as an AL
469#  This rule is concerned about getting the second of the two <breaks> in place.
470#
471
472[$PR   ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
473
474
475
476# LB 4, 5, 5
477
478$LB4Breaks [$LB4NonBreaks-$CM];
479$LB4Breaks $CM+ $CAN_CM;
480$LF $CR;
481
482
483# LB 7         x SP
484#              x ZW
485[$SP $ZW] [$LB4NonBreaks-$CM];
486[$SP $ZW] $CM+ $CAN_CM;
487
488# LB 8 ZW SP* <break>
489#     TODO: to implement this, we need more than one look-ahead hard break in play at a time.
490#           Requires an engine enhancement.
491#   / $SP* $ZW
492
493# LB 9,10  Combining marks.
494#    X   $CM needs to behave like X, where X is not $SP or controls.
495#    $CM not covered by the above needs to behave like $AL
496# Stick together any combining sequences that don't match other rules.
497$CM+ $CAN_CM;
498
499
500# LB 11
501$CM* $WJ $CM* $CAN_CM;
502$CM* $WJ      [$LB8NonBreaks-$CM];
503
504     $CANT_CM $CM* $WJ;
505$CM* $CAN_CM  $CM* $WJ;
506
507# LB 12a
508#      [^SP BA HY] x GL
509#
510$CM* $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HY]];
511
512# LB 12
513#     GL  x
514#
515$CANT_CM $CM* $GL;
516$CM* $CAN_CM $CM* $GL;
517
518
519# LB 13
520$CL $CM+ $CAN_CM;
521$CP $CM+ $CAN_CM;
522$EX $CM+ $CAN_CM;
523$IS $CM+ $CAN_CM;
524$SY $CM+ $CAN_CM;
525
526$CL [$LB8NonBreaks-$CM];
527$CP [$LB8NonBreaks-$CM];
528$EX [$LB8NonBreaks-$CM];
529$IS [$LB8NonBreaks-$CM];
530$SY [$LB8NonBreaks-$CM];
531
532# Rule 13 & 14 taken together for an edge case.
533#   Match this, shown forward
534#     OP SP+  ($CM+ behaving as $AL) (CL | CP | EX | IS | IY)
535#   This really wants to chain at the $CM+ (which is acting as an $AL)
536#   except for $CM chaining being disabled.
537[$CL $CP $EX $IS $SY] $CM+ $SP+ $CM* $OP;
538
539# LB 14    OP SP* x
540#
541$CM* $CAN_CM    $SP* $CM* $OP;
542     $CANT_CM   $SP* $CM* $OP;
543$AL_FOLLOW? $CM+  $SP $SP* $CM* $OP;     #  by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
544
545     $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
546$CM* $AL_FOLLOW_CM   $CM+ $SP+ $CM* $OP;
547$SY $CM $SP+ $OP;   # TODO:  Experiment.  Remove.
548
549
550
551# LB 15
552$CM* $OP $SP* $CM* $QU;
553
554# LB 16
555$CM* $NS $SP* $CM* ($CL | $CP);
556
557# LB 17
558$CM* $B2 $SP* $CM* $B2;
559
560# LB 18  break after spaces
561#        Nothing explicit needed here.
562
563
564#
565# LB 19
566#
567$CM* $QU $CM* $CAN_CM;                                #   . x QU
568$CM* $QU      $LB18NonBreaks;
569
570
571$CM* $CAN_CM  $CM* $QU;                               #   QU x .
572     $CANT_CM $CM* $QU;
573
574#
575#  LB 20  Break before and after CB.
576#         nothing needed here.
577#
578
579# LB 21
580$CM* ($BA | $HY | $NS) $CM* [$LB20NonBreaks-$CM];     #  . x (BA | HY | NS)
581
582$CM* [$LB20NonBreaks-$CM] $CM* $BB;                   #  BB x .
583[^$CB] $CM* $BB;                                      #
584
585# LB21a
586[^$CB] $CM* ($HY | $BA) $CM* $HL;
587
588# LB21b (reverse)
589$CM* $HL $CM* $SY;
590
591# LB 22
592$CM* $IN $CM* ($ALPlus | $HL);
593$CM* $IN $CM* $ID;
594$CM* $IN $CM* $IN;
595$CM* $IN $CM* $NU;
596
597# LB 23
598$CM* $PO $CM* $ID;
599$CM* $NU $CM* ($ALPlus | $HL);
600$CM* ($ALPlus | $HL) $CM* $NU;
601
602# LB 24
603$CM* $ID $CM* $PR;
604$CM* ($ALPlus | $HL) $CM* $PR;
605$CM* ($ALPlus | $HL) $CM* $PO;
606
607
608# LB 25
609($CM* ($PR | $PO))? ($CM* ($CL | $CP))? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
610
611# LB 26
612$CM* ($H3 | $H2 | $JV | $JL) $CM* $JL;
613$CM* ($JT | $JV) $CM* ($H2 | $JV);
614$CM* $JT $CM* ($H3 | $JT);
615
616# LB 27
617$CM* $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
618$CM* $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
619$CM* ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
620
621# LB 28
622$CM* ($ALPlus | $HL) $CM* ($ALPlus | $HL);
623
624
625# LB 29
626$CM* ($ALPlus | $HL) $CM* $IS;
627
628# LB 30
629$CM* $OP $CM* ($ALPlus | $HL | $NU);
630$CM* ($ALPlus | $HL | $NU) $CM* $CP;
631
632# LB 30a
633$CM* $RI $CM* $RI;
634
635## -------------------------------------------------
636
637!!safe_reverse;
638
639# LB 9
640$CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
641$CM+ $SP / .;
642
643# LB 14
644$SP+ $CM* $OP;
645
646# LB 15
647$SP+ $CM* $QU;
648
649# LB 16
650$SP+ $CM* ($CL | $CP);
651
652# LB 17
653$SP+ $CM* $B2;
654
655# LB 21
656$CM* ($HY | $BA) $CM* $HL;
657
658# LB 25
659($CM* ($IS | $SY))+ $CM* $NU;
660($CL | $CP) $CM* ($NU | $IS | $SY);
661
662# For dictionary-based break
663$dictionary $dictionary;
664
665## -------------------------------------------------
666
667!!safe_forward;
668
669# Skip forward over all character classes that are involved in
670#   rules containing patterns with possibly more than one char
671#   of context.
672#
673#  It might be slightly more efficient to have specific rules
674#  instead of one generic one, but only if we could
675#  turn off rule chaining.  We don't want to move more
676#  than necessary.
677#
678[$CM $OP $QU $CL $CP $B2 $PR $HY $BA $SP $dictionary]+ [^$CM $OP $QU $CL $CP $B2 $PR $HY $BA $dictionary];
679$dictionary $dictionary;
680
681