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