• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2002-2009  International Business Machines Corporation and
2# others. All Rights Reserved.
3#
4#  file:  line.txt
5#
6#         Line Breaking Rules
7#         Implement default line breaking as defined by
8#         Unicode Standard Annex #14 Revision 22 for Unicode 5.1
9#         http://www.unicode.org/reports/tr14/
10
11
12
13#
14#  Character Classes defined by TR 14.
15#
16
17!!chain;
18!!LBCMNoChain;
19
20
21!!lookAheadHardBreak;
22#
23#  !!lookAheadHardBreak    Described here because it is (as yet) undocumented elsewhere
24#                          and only used for the line break rules.
25#
26#           It is used in the implementation of 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).
29#
30#           The problem occurs in the reverse rules.
31#
32#           Consider a sequence like, with correct breaks as shown
33#               LF  ID  CM  AL  AL
34#                  ^       ^       ^
35#           Then consider the sequence without the initial ID (ideographic)
36#                 LF  CM  AL  AL
37#                    ^           ^
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
40#           alphas.
41#
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)
46#
47#           If the CM is unattached, we need to force a break.
48#
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.
53#
54#           See rule LB 19 for an example.
55#
56
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$IDDelta = [\ufe45\ufe46\ufe51];
73$ID = [[:LineBreak =  Ideographic:] - $IDDelta];
74$IN = [:LineBreak =  Inseperable:];
75$IS = [:LineBreak =  Infix_Numeric:];
76$JL = [:LineBreak =  JL:];
77$JV = [:LineBreak =  JV:];
78$JT = [:LineBreak =  JT:];
79$LF = [:LineBreak =  Line_Feed:];
80$NL = [:LineBreak =  Next_Line:];
81$NS = [[:LineBreak =  Nonstarter:] $IDDelta \u23b5];
82$NU = [:LineBreak =  Numeric:];
83$OP = [:LineBreak =  Open_Punctuation:];
84$PO = [:LineBreak =  Postfix_Numeric:];
85$PR = [:LineBreak =  Prefix_Numeric:];
86$QU = [:LineBreak =  Quotation:];
87$SA = [:LineBreak =  Complex_Context:];
88$SG = [:LineBreak =  Surrogate:];
89$SP = [:LineBreak =  Space:];
90$SY = [:LineBreak =  Break_Symbols:];
91$WJ = [:LineBreak =  Word_Joiner:];
92$XX = [:LineBreak =  Unknown:];
93$ZW = [:LineBreak =  ZWSpace:];
94
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.
99
100$dictionary = [:LineBreak = Complex_Context:];
101
102#
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)
108#
109$ALPlus = [$AL $AI $SA $SG $XX];
110
111#
112#  Combining Marks.   X $CM*  behaves as if it were X.  Rule LB6.
113#
114$ALcm = $ALPlus $CM*;
115$BAcm = $BA $CM*;
116$BBcm = $BB $CM*;
117$B2cm = $B2 $CM*;
118$CLcm = $CL $CM*;
119$EXcm = $EX $CM*;
120$GLcm = $GL $CM*;
121$HYcm = $HY $CM*;
122$H2cm = $H2 $CM*;
123$H3cm = $H3 $CM*;
124$IDcm = $ID $CM*;
125$INcm = $IN $CM*;
126$IScm = $IS $CM*;
127$JLcm = $JL $CM*;
128$JVcm = $JV $CM*;
129$JTcm = $JT $CM*;
130$NScm = $NS $CM*;
131$NUcm = $NU $CM*;
132$OPcm = $OP $CM*;
133$POcm = $PO $CM*;
134$PRcm = $PR $CM*;
135$QUcm = $QU $CM*;
136$SYcm = $SY $CM*;
137$WJcm = $WJ $CM*;
138
139## -------------------------------------------------
140
141!!forward;
142
143#
144#  Each class of character can stand by itself as an unbroken token, with trailing combining stuff
145#
146$ALPlus $CM+;
147$BA $CM+;
148$BB $CM+;
149$B2 $CM+;
150$CL $CM+;
151$EX $CM+;
152$GL $CM+;
153$HY $CM+;
154$H2 $CM+;
155$H3 $CM+;
156$ID $CM+;
157$IN $CM+;
158$IS $CM+;
159$JL $CM+;
160$JV $CM+;
161$JT $CM+;
162$NS $CM+;
163$NU $CM+;
164$OP $CM+;
165$PO $CM+;
166$PR $CM+;
167$QU $CM+;
168$SY $CM+;
169$WJ $CM+;
170
171#
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.
175#
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.
178#
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
181
182#
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.
187#
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];
190$AL_FOLLOW      = [$AL_FOLLOW_NOCM $AL_FOLLOW_CM];
191
192
193#
194#  Rule LB 4, 5    Mandatory (Hard) breaks.
195#
196$LB4Breaks    = [$BK $CR $LF $NL];
197$LB4NonBreaks = [^$BK $CR $LF $NL];
198$CR $LF {100};
199
200#
201#  LB 6    Do not break before hard line breaks.
202#
203$LB4NonBreaks?  $LB4Breaks {100};    # LB 5  do not break before hard breaks.
204$CAN_CM $CM*    $LB4Breaks {100};
205$CM+            $LB4Breaks {100};
206
207# LB 7         x SP
208#              x ZW
209$LB4NonBreaks [$SP $ZW];
210$CAN_CM $CM*  [$SP $ZW];
211$CM+          [$SP $ZW];
212
213#
214# LB 8         Break after zero width space
215#
216$LB8Breaks    = [$LB4Breaks $ZW];
217$LB8NonBreaks = [[$LB4NonBreaks] - [$ZW]];
218
219
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.
223
224$CAN_CM $CM+;                   #  Stick together any combining sequences that don't match other rules.
225$CM+;
226
227#
228# LB 11  Do not break before or after WORD JOINER & related characters.
229#
230$CAN_CM $CM*  $WJcm;
231$LB8NonBreaks $WJcm;
232$CM+          $WJcm;
233
234$WJcm $CANT_CM;
235$WJcm $CAN_CM $CM*;
236
237#
238# LB 12  Do not break after NBSP and related characters.
239#         GL  x
240#
241$GLcm $CAN_CM $CM*;
242$GLcm $CANT_CM;
243
244#
245# LB 12a  Do not break before NBSP and related characters ...
246#            [^SP BA HY] x GL
247#
248[[$LB8NonBreaks] - [$SP $BA $HY]] $CM* $GLcm;
249$CM+ GLcm;
250
251
252
253#
254# LB 13   Don't break before ']' or '!' or ';' or '/', even after spaces.
255#
256$LB8NonBreaks $CL;
257$CAN_CM $CM*  $CL;
258$CM+          $CL;              # by rule 10, stand-alone CM behaves as AL
259
260$LB8NonBreaks $EX;
261$CAN_CM $CM*  $EX;
262$CM+          $EX;              # by rule 10, stand-alone CM behaves as AL
263
264$LB8NonBreaks $IS;
265$CAN_CM $CM*  $IS;
266$CM+          $IS;              # by rule 10, stand-alone CM behaves as AL
267
268$LB8NonBreaks $SY;
269$CAN_CM $CM*  $SY;
270$CM+          $SY;              # by rule 10, stand-alone CM behaves as AL
271
272
273#
274# LB 14  Do not break after OP, even after spaces
275#
276$OPcm $SP* $CAN_CM $CM*;
277$OPcm $SP* $CANT_CM;
278
279$OPcm $SP+ $CM+ $AL_FOLLOW?;    # by rule 10, stand-alone CM behaves as AL
280
281# LB 15
282$QUcm $SP* $OPcm;
283
284# LB 16
285$CLcm $SP* $NScm;
286
287# LB 17
288$B2cm $SP* $B2cm;
289
290#
291# LB 18  Break after spaces.
292#
293$LB18NonBreaks = [$LB8NonBreaks - [$SP]];
294$LB18Breaks    = [$LB8Breaks $SP];
295
296
297# LB 19
298#         x QU
299$LB18NonBreaks $CM* $QUcm;
300$CM+                $QUcm;
301
302#         QU  x
303$QUcm .?;
304$QUcm $LB18NonBreaks $CM*;    # Don't let a combining mark go onto $CR, $BK, etc.
305                              #  TODO:  I don't think this rule is needed.
306
307
308# LB 20
309#        <break>  $CB
310#        $CB   <break>
311
312$LB20NonBreaks = [$LB18NonBreaks - $CB];
313
314# LB 21        x   (BA | HY | NS)
315#           BB x
316#
317$LB20NonBreaks $CM* ($BAcm | $HYcm | $NScm);
318
319$BBcm [^$CB];                                  #  $BB  x
320$BBcm $LB20NonBreaks $CM*;
321
322# LB 22
323$ALcm    $INcm;
324$CM+     $INcm;     #  by rule 10, any otherwise unattached CM behaves as AL
325$IDcm    $INcm;
326$INcm    $INcm;
327$NUcm    $INcm;
328
329
330# $LB 23
331$IDcm  $POcm;
332$ALcm  $NUcm;       # includes $LB19
333$CM+   $NUcm;       # Rule 10, any otherwise unattached CM behaves as AL
334$NUcm  $ALcm;
335
336#
337# LB 24
338#
339$PRcm $IDcm;
340$PRcm $ALcm;
341$POcm $ALcm;
342
343#
344# LB 25   Numbers.
345#
346($PRcm | $POcm)? ($OPcm | $HYcm)? $NUcm ($NUcm | $SYcm | $IScm)* $CLcm? ($PRcm | $POcm)?;
347
348# LB 26  Do not break a Korean syllable
349#
350$JLcm ($JLcm | $JVcm | $H2cm | $H3cm);
351($JVcm | $H2cm) ($JVcm | $JTcm);
352($JTcm | $H3cm) $JTcm;
353
354# LB 27  Treat korean Syllable Block the same as ID  (don't break it)
355($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $INcm;
356($JLcm | $JVcm | $JTcm | $H2cm | $H3cm) $POcm;
357$PRcm ($JLcm | $JVcm | $JTcm | $H2cm | $H3cm);
358
359
360# LB 28   Do not break between alphabetics
361#
362$ALcm $ALcm;
363$CM+ $ALcm;      # The $CM+ is from rule 10, an unattached CM is treated as AL
364
365# LB 29
366$IScm $ALcm;
367
368
369#
370#  Reverse Rules.
371#
372## -------------------------------------------------
373
374!!reverse;
375
376$CM+ $ALPlus;
377$CM+ $BA;
378$CM+ $BB;
379$CM+ $B2;
380$CM+ $CL;
381$CM+ $EX;
382$CM+ $GL;
383$CM+ $HY;
384$CM+ $H2;
385$CM+ $H3;
386$CM+ $ID;
387$CM+ $IN;
388$CM+ $IS;
389$CM+ $JL;
390$CM+ $JV;
391$CM+ $JT;
392$CM+ $NS;
393$CM+ $NU;
394$CM+ $OP;
395$CM+ $PO;
396$CM+ $PR;
397$CM+ $QU;
398$CM+ $SY;
399$CM+ $WJ;
400$CM+;
401
402
403#
404#  Sequences of the form  (shown forwards)
405#      [CANT_CM]  <break>  [CM]  [whatever]
406#  The CM needs to behave as an AL
407#
408$AL_FOLLOW $CM+ / (
409          [$BK $CR $LF $NL $ZW {eof}] |
410          $SP+ $CM+ $SP |
411          $SP+ $CM* ([^$OP $CM $SP] | [$AL {eof}]));   # if LB 14 will match, need to surpress this break.
412                                               #  LB14 says    OP SP* x .
413                                               #    becomes    OP SP* x AL
414                                               #    becomes    OP SP* x CM+ AL_FOLLOW
415                                               #
416                                               # Further note:  the $AL in [$AL {eof}] is only to work around
417                                               #                a rule compiler bug which complains about
418                                               #                empty sets otherwise.
419
420#
421#  Sequences of the form  (shown forwards)
422#      [CANT_CM]  <break> [CM]  <break>  [PR]
423#  The CM needs to behave as an AL
424#  This rule is concerned about getting the second of the two <breaks> in place.
425#
426
427[$PR   ] / $CM+ [$BK $CR $LF $NL $ZW $SP {eof}];
428
429
430
431# LB 4, 5, 5
432
433$LB4Breaks [$LB4NonBreaks-$CM];
434$LB4Breaks $CM+ $CAN_CM;
435$LF $CR;
436
437
438# LB 7         x SP
439#              x ZW
440[$SP $ZW] [$LB4NonBreaks-$CM];
441[$SP $ZW] $CM+ $CAN_CM;
442
443# LB 8 Break after zero width space
444
445
446# LB 9,10  Combining marks.
447#    X   $CM needs to behave like X, where X is not $SP or controls.
448#    $CM not covered by the above needs to behave like $AL
449# Stick together any combining sequences that don't match other rules.
450$CM+ $CAN_CM;
451
452
453# LB 11
454$CM* $WJ $CM* $CAN_CM;
455$CM* $WJ      [$LB8NonBreaks-$CM];
456
457     $CANT_CM $CM* $WJ;
458$CM* $CAN_CM  $CM* $WJ;
459
460# LB 12a
461#      [^SP BA HY] x GL
462#
463$CM* $GL $CM* [$LB8NonBreaks-[$CM $SP $BA $HY]];
464
465# LB 12
466#     GL  x
467#
468$CANT_CM $CM* $GL;
469$CM* $CAN_CM $CM* $GL;
470
471
472# LB 13
473$CL $CM+ $CAN_CM;
474$EX $CM+ $CAN_CM;
475$IS $CM+ $CAN_CM;
476$SY $CM+ $CAN_CM;
477
478$CL [$LB8NonBreaks-$CM];
479$EX [$LB8NonBreaks-$CM];
480$IS [$LB8NonBreaks-$CM];
481$SY [$LB8NonBreaks-$CM];
482
483# Rule 13 & 14 taken together for an edge case.
484#   Match this, shown forward
485#     OP SP+  ($CM+ behaving as $AL) (CL | EX | IS | IY)
486#   This really wants to chain at the $CM+ (which is acting as an $AL)
487#   except for $CM chaining being disabled.
488[$CL $EX $IS $SY] $CM+ $SP+ $CM* $OP;
489
490# LB 14    OP SP* x
491#
492$CM* $CAN_CM    $SP* $CM* $OP;
493     $CANT_CM   $SP* $CM* $OP;
494$AL_FOLLOW? $CM+  $SP $SP* $CM* $OP;     #  by LB 10, behaves like $AL_FOLLOW? $AL $SP* $CM* $OP
495
496     $AL_FOLLOW_NOCM $CM+ $SP+ $CM* $OP;
497$CM* $AL_FOLLOW_CM   $CM+ $SP+ $CM* $OP;
498$SY $CM $SP+ $OP;   # TODO:  Experiment.  Remove.
499
500
501
502# LB 15
503$CM* $OP $SP* $CM* $QU;
504
505# LB 16
506$CM* $NS $SP* $CM* $CL;
507
508# LB 17
509$CM* $B2 $SP* $CM* $B2;
510
511# LB 18  break after spaces
512#        Nothing explicit needed here.
513
514
515#
516# LB 19
517#
518$CM* $QU $CM* $CAN_CM;                                #   . x QU
519$CM* $QU      $LB18NonBreaks;
520
521
522$CM* $CAN_CM  $CM* $QU;                               #   QU x .
523     $CANT_CM $CM* $QU;
524
525#
526#  LB 20  Break before and after CB.
527#         nothing needed here.
528#
529
530# LB 21
531$CM* ($BA | $HY | $NS) $CM* [$LB20NonBreaks-$CM];     #  . x (BA | HY | NS)
532
533$CM* [$LB20NonBreaks-$CM] $CM* $BB;                   #  BB x .
534[^$CB] $CM* $BB;                                      #
535
536
537
538# LB 22
539$CM* $IN $CM* $ALPlus;
540$CM* $IN $CM* $ID;
541$CM* $IN $CM* $IN;
542$CM* $IN $CM* $NU;
543
544# LB 23
545$CM* $PO $CM* $ID;
546$CM* $NU $CM* $ALPlus;
547$CM* $ALPlus $CM* $NU;
548
549# LB 24
550$CM* $ID $CM* $PR;
551$CM* $ALPlus $CM* $PR;
552$CM* $ALPlus $CM* $PO;
553
554
555# LB 25
556($CM* ($PR | $PO))? ($CM* $CL)? ($CM* ($NU | $IS | $SY))* $CM* $NU ($CM* ($OP | $HY))? ($CM* ($PR | $PO))?;
557
558# LB 26
559$CM* ($H3 | $H2 | $JV | $JL) $CM* $JL;
560$CM* ($JT | $JV) $CM* ($H2 | $JV);
561$CM* $JT $CM* ($H3 | $JT);
562
563# LB 27
564$CM* $IN $CM* ($H3 | $H2 | $JT | $JV | $JL);
565$CM* $PO $CM* ($H3 | $H2 | $JT | $JV | $JL);
566$CM* ($H3 | $H2 | $JT | $JV | $JL) $CM* $PR;
567
568# LB 28
569$CM* $ALPlus $CM* $ALPlus;
570
571
572# LB 29
573$CM* $ALPlus $CM* $IS;
574
575
576## -------------------------------------------------
577
578!!safe_reverse;
579
580# LB 9
581$CM+ [^$CM $BK $CR $LF $NL $ZW $SP];
582$CM+ $SP / .;
583
584# LB 14
585$SP+ $CM* $OP;
586
587# LB 15
588$SP+ $CM* $QU;
589
590# LB 16
591$SP+ $CM* $CL;
592
593# LB 17
594$SP+ $CM* $B2;
595
596# LB 25
597($CM* ($IS | $SY))+ $CM* $NU;
598$CL $CM* ($NU | $IS | $SY);
599
600# For dictionary-based break
601$dictionary $dictionary;
602
603## -------------------------------------------------
604
605!!safe_forward;
606
607# Skip forward over all character classes that are involved in
608#   rules containing patterns with possibly more than one char
609#   of context.
610#
611#  It might be slightly more efficient to have specific rules
612#  instead of one generic one, but only if we could
613#  turn off rule chaining.  We don't want to move more
614#  than necessary.
615#
616[$CM $OP $QU $CL $B2 $PR $HY $SP $dictionary]+ [^$CM $OP $QU $CL $B2 $PR $HY $dictionary];
617$dictionary $dictionary;
618
619