-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
nfp.go
836 lines (776 loc) · 24.1 KB
/
nfp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// Copyright 2022 - 2024 The nfp Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// This package NFP (Number Format Parser) produce syntax trees for number
// format expression. Excel Number format controls options such the number of
// decimal digits, the currency sign, commas to separate thousands, and
// display of negative numbers. The number format of an index applies wherever
// that index is used, including row or column headers of a table, or graph
// axis that uses that index.
//
// Implementation with Go language by Ri Xu: https://xuri.me
package nfp
import "strings"
// Asterisk, At and other's constants are token definitions.
const (
// Character constants
Asterisk = "*"
At = "@"
BackSlash = "\\"
BlockDelimiter = ";"
BracketClose = "]"
BracketOpen = "["
Colon = ":"
Comma = ","
Dash = "-"
Dollar = "$"
Dot = "."
Hash = "#"
ParenClose = ")"
ParenOpen = "("
Percent = "%"
Plus = "+"
Question = "?"
QuoteDouble = "\""
QuoteSingle = "'"
Slash = "/"
Underscore = "_"
Whitespace = " "
Zero = "0"
// DatesTimesCodeChars defined dates and times control codes in upper case
DatesTimesCodeChars = "AEYMDHSG"
// NumCodeChars defined numeric code character
NumCodeChars = "0123456789"
// Token section types
TokenSectionNegative = "Negative"
TokenSectionPositive = "Positive"
TokenSectionText = "Text"
TokenSectionZero = "Zero"
// Token subtypes
TokenSubTypeCurrencyString = "CurrencyString"
TokenSubTypeLanguageInfo = "LanguageInfo"
TokenTypeColor = "Color"
// Token types
TokenTypeAlignment = "Alignment"
TokenTypeCondition = "Condition"
TokenTypeCurrencyLanguage = "CurrencyLanguage"
TokenTypeDateTimes = "DateTimes"
TokenTypeDecimalPoint = "DecimalPoint"
TokenTypeDenominator = "Denominator"
TokenTypeDigitalPlaceHolder = "DigitalPlaceHolder"
TokenTypeElapsedDateTimes = "ElapsedDateTimes"
TokenTypeExponential = "Exponential"
TokenTypeFraction = "Fraction"
TokenTypeGeneral = "General"
TokenTypeHashPlaceHolder = "HashPlaceHolder"
TokenTypeLiteral = "Literal"
TokenTypeOperand = "Operand"
TokenTypeOperator = "Operator"
TokenTypePercent = "Percent"
TokenTypeRepeatsChar = "RepeatsChar"
TokenTypeSwitchArgument = "SwitchArgument"
TokenTypeTextPlaceHolder = "TextPlaceHolder"
TokenTypeThousandsSeparator = "ThousandsSeparator"
TokenTypeUnknown = "Unknown"
TokenTypeZeroPlaceHolder = "ZeroPlaceHolder"
)
// ColorNames defined colors name used in for a section of the format, use the
// name of one of the following eight colors in square brackets in the
// section. The color code shall be the first item in the section.
var ColorNames = []string{
"black",
"blue",
"cyan",
"green",
"magenta",
"red",
"white",
"yellow",
}
// GeneralFormattingSwitchArguments defined switch-arguments apply to fields
// whose field result is a numeric value. If the result type of the field is
// not numeric, then these switches have no effect.
var GeneralFormattingSwitchArguments = []string{
"AIUEO",
"ALPHABETIC",
"alphabetic",
"Arabic",
"ARABICABJAD",
"ARABICALPHA",
"ArabicDash",
"BAHTTEXT",
"CardText",
"CHINESENUM1",
"CHINESENUM2",
"CHINESENUM3",
"CHOSUNG",
"CIRCLENUM",
"DBCHAR",
"DBNUM1",
"DBNUM2",
"DBNUM3",
"DBNUM4",
"DollarText",
"GANADA",
"GB1",
"GB2",
"GB3",
"GB4",
"HEBREW1",
"HEBREW2",
"Hex",
"HINDIARABIC",
"HINDICARDTEXT",
"HINDILETTER1",
"HINDILETTER2",
"IROHA",
"KANJINUM1",
"KANJINUM2",
"KANJINUM3",
"Ordinal",
"OrdText",
"Roman",
"roman",
"SBCHAR",
"THAIARABIC",
"THAICARDTEXT",
"THAILETTER",
"VIETCARDTEXT",
"ZODIAC1",
"ZODIAC2",
"ZODIAC3",
}
// AmPm defined the AM and PM with international considerations.
var AmPm = []string{"AM/PM", "A/P", "上午/下午"}
// ConditionOperators defined the condition operators.
var ConditionOperators = []string{"<", "<=", ">", ">=", "<>", "="}
// Part directly maps the sub part of the token.
type Part struct {
Token Token
Value string
}
// Token encapsulate a number format token.
type Token struct {
TValue string
TType string
Parts []Part
}
// Section directly maps sections of the number format. Up to four sections of
// format codes can be specified. The format codes, separated by semicolons,
// define the formats for positive numbers, negative numbers, zero values, and
// text, in that order. If only two sections are specified, the first is used
// for positive numbers and zeros, and the second is used for negative
// numbers. If only one section is specified, it is used for all numbers. To
// skip a section, the ending semicolon for that section shall be written.
type Section struct {
Type string
Items []Token
}
// Tokens directly maps the ordered list of tokens.
// Attributes:
//
// Index - Current position in the number format expression
// SectionIndex - Current position in section
// Sections - Ordered section of token sequences
type Tokens struct {
Index int
SectionIndex int
Sections []Section
}
// fTokens provides function to handle an ordered list of tokens.
func fTokens() Tokens {
return Tokens{
Index: -1,
}
}
// fToken provides function to encapsulate a number format token.
func fToken(value, tokenType string, parts []Part) Token {
return Token{
TValue: value,
TType: tokenType,
Parts: parts,
}
}
// add provides function to add a token to the end of the list.
func (tk *Tokens) add(value, tokenType string, parts []Part) Token {
token := fToken(value, tokenType, parts)
tk.addRef(token)
return token
}
// addRef provides function to add a token to the end of the list.
func (tk *Tokens) addRef(token Token) {
if len(tk.Sections) <= tk.SectionIndex {
sectionType := []string{TokenSectionPositive, TokenSectionNegative, TokenSectionZero, TokenSectionText}[tk.SectionIndex]
for i := len(tk.Sections) - 1; i < tk.SectionIndex; i++ {
tk.Sections = append(tk.Sections, Section{Type: sectionType})
}
}
tk.Sections[tk.SectionIndex].Items = append(tk.Sections[tk.SectionIndex].Items, token)
}
// reset provides function to reset the index to -1.
func (tk *Tokens) reset() {
tk.Index = -1
}
// Parser inheritable container.
type Parser struct {
InBracket bool
InString bool
InPlaceholder bool
NumFmt string
// Runes is a copy of the number format string as a rune slice. It's stored here to avoid
// allocating a new slice every time we need to access it.
Runes []rune
Offset int
Tokens Tokens
Token Token
}
// NumberFormatParser provides function to parse an Excel number format into a
// stream of tokens.
func NumberFormatParser() Parser {
return Parser{}
}
// EOF provides function to check whether end of tokens stack.
func (ps *Parser) EOF() bool {
return ps.Offset >= len(ps.Runes)
}
// getTokens return a token stream (list).
func (ps *Parser) getTokens() Tokens {
// state-dependent character evaluation (order is important)
for !ps.EOF() {
if ps.InBracket {
if ps.Token.TType == TokenTypeCurrencyLanguage {
if ps.currentChar() != Dash && ps.currentChar() != BracketClose {
if len(ps.Token.Parts) == 0 {
ps.Token.Parts = append(ps.Token.Parts, Part{Token: Token{TType: TokenSubTypeCurrencyString}})
}
ps.Token.Parts[len(ps.Token.Parts)-1].Token.TValue += ps.currentChar()
}
if ps.currentChar() == Dash && ps.nextChar() != BracketClose {
if l := len(ps.Token.Parts); l == 0 {
ps.Token.Parts = append(ps.Token.Parts, Part{Token: Token{TType: TokenSubTypeLanguageInfo}})
} else {
if ps.Token.Parts[0].Token.TType != TokenSubTypeLanguageInfo && ps.Token.Parts[0].Token.TValue != "" {
ps.Token.Parts = append(ps.Token.Parts, Part{Token: Token{TType: TokenSubTypeLanguageInfo}})
} else {
ps.Token.Parts[l-1].Token.TValue += ps.currentChar()
}
}
}
if ps.currentChar() == Comma {
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
}
if len(ps.Token.TValue) > 1 && inStrSlice(ConditionOperators, ps.Token.TValue[1:], true) != -1 {
if ps.currentChar() == Dash || strings.ContainsAny(NumCodeChars, ps.currentChar()) {
ps.Token.TType = TokenTypeCondition
ps.Token.Parts = []Part{
{Token: Token{TType: TokenTypeOperator, TValue: ps.Token.TValue[1:]}},
{Token: Token{TType: TokenTypeOperand}},
}
ps.Token.TValue = ""
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
}
if ps.currentChar() == BracketClose {
ps.InBracket = false
if ps.Token.TType == TokenTypeCondition && len(ps.Token.Parts) == 2 {
ps.Token.Parts[1].Token.TValue = ps.Token.TValue
ps.Tokens.add(ps.Token.Parts[0].Token.TValue+ps.Token.Parts[1].Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
ps.Token.TValue += ps.currentChar()
if l := len(ps.Token.TValue); l > 2 {
lit := ps.Token.TValue[1 : l-1]
if idx := inStrSlice(ColorNames, lit, false); idx != -1 {
ps.Tokens.add(lit, TokenTypeColor, nil)
ps.Token = Token{}
ps.Offset++
continue
}
if idx := inStrSlice(GeneralFormattingSwitchArguments, lit, false); idx != -1 {
ps.Tokens.add(ps.Token.TValue, TokenTypeSwitchArgument, nil)
ps.Token = Token{}
ps.Offset++
continue
}
if ps.Token.TType == TokenTypeCurrencyLanguage {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
ps.Token.TType, ps.Token.TValue = TokenTypeUnknown, lit
isDateTime := true
for _, ch := range lit {
if !strings.ContainsAny(DatesTimesCodeChars, strings.ToUpper(string(ch))) {
isDateTime = false
}
}
if isDateTime {
ps.Token.TType = TokenTypeElapsedDateTimes
}
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
}
}
if !ps.InBracket {
if strings.ContainsAny(NumCodeChars, ps.currentChar()) {
if ps.Token.TType == TokenTypeZeroPlaceHolder || ps.Token.TType == TokenTypeDenominator {
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.Token.TType == TokenTypeFraction {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{TType: TokenTypeDenominator, TValue: ps.currentChar()}
ps.Offset++
continue
}
if ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypeZeroPlaceHolder
if ps.currentChar() != Zero {
ps.Token.TType = TokenTypeLiteral
}
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.currentChar() == Hash {
if ps.Token.TType != TokenTypeHashPlaceHolder && ps.Token.TType != "" {
if ps.Token.TValue == Dot {
ps.Token.TType = TokenTypeDecimalPoint
}
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypeHashPlaceHolder
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.currentChar() == Dot {
if ps.Token.TType == TokenTypeZeroPlaceHolder || ps.Token.TType == TokenTypeHashPlaceHolder {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Tokens.add(ps.currentChar(), TokenTypeDecimalPoint, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
if ps.Token.TType == TokenTypeDateTimes && !strings.ContainsAny(NumCodeChars, ps.nextChar()) {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Tokens.add(ps.currentChar(), TokenTypeLiteral, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
if !ps.InString {
if ps.Token.TType != "" && strings.ContainsAny(NumCodeChars, ps.nextChar()) {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypeDecimalPoint
}
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
}
if strings.ContainsAny(Dollar+Dash+Plus+ParenOpen+ParenClose+Colon+Whitespace, ps.currentChar()) {
if ps.InBracket {
ps.Token.TValue += ps.currentChar()
ps.Token.TType = TokenTypeCurrencyLanguage
ps.Offset++
continue
}
if ps.Token.TType != TokenTypeLiteral && ps.Token.TType != TokenTypeDateTimes && ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{TType: TokenTypeLiteral, TValue: ps.currentChar()}
ps.Offset++
continue
}
if ps.Token.TValue != BackSlash && ps.Token.TType == "" && inStrSlice(AmPm, ps.Token.TValue, false) == -1 {
ps.Token.TType = TokenTypeLiteral
}
if ps.Token.TType == TokenTypeLiteral {
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
}
if ps.currentChar() == Underscore {
if ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
}
ps.Token.TValue = Whitespace
ps.Token.TType = TokenTypeAlignment
ps.Offset += 2
continue
}
if ps.currentChar() == Asterisk {
if ps.Token.TValue != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Tokens.add(ps.nextChar(), TokenTypeRepeatsChar, ps.Token.Parts)
ps.Token = Token{}
ps.Offset += 2
continue
}
if ps.currentChar() == BackSlash {
if ps.Token.TValue != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Tokens.add(ps.nextChar(), TokenTypeLiteral, ps.Token.Parts)
ps.Token = Token{}
ps.Offset += 2
continue
}
if ps.currentChar() == Dash {
if ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
}
ps.Token.TType = TokenTypeLiteral
if ps.currentChar() != ps.nextChar() {
ps.Tokens.add(ps.currentChar(), ps.Token.TType, ps.Token.Parts)
}
ps.Token = Token{}
ps.Offset++
continue
}
if ps.currentChar() == Comma {
if ps.Token.TType == TokenTypeZeroPlaceHolder || ps.Token.TType == TokenTypeHashPlaceHolder {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Tokens.add(ps.currentChar(), TokenTypeThousandsSeparator, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
if !ps.InString {
if ps.Token.TType == TokenTypeLiteral {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{TType: TokenTypeThousandsSeparator}
}
if ps.Token.TType == TokenTypeDateTimes {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{TType: TokenTypeLiteral}
}
if ps.currentChar() != ps.nextChar() {
if ps.Token.TType == "" {
ps.Token.TType = TokenTypeLiteral
}
ps.Tokens.add(ps.currentChar(), ps.Token.TType, ps.Token.Parts)
}
ps.Token = Token{}
ps.Offset++
continue
}
ps.Token.TType = TokenTypeLiteral
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.currentChar() == Whitespace {
if ps.Token.TType != "" && ps.Token.TType != TokenTypeLiteral {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
}
ps.Token.TType = TokenTypeLiteral
ps.Tokens.add(ps.currentChar(), ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
if ps.currentChar() == Slash {
if ps.Token.TType == TokenTypeDateTimes {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Tokens.add(ps.currentChar(), TokenTypeLiteral, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
if ps.Token.TType == TokenTypeDigitalPlaceHolder {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{TType: TokenTypeFraction, TValue: ps.currentChar()}
ps.Offset++
continue
}
}
if ps.currentChar() == Colon && ps.Token.TType == TokenTypeDateTimes {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Tokens.add(ps.currentChar(), TokenTypeLiteral, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
if ps.currentChar() == QuoteDouble {
ps.Offset++
if ps.InString && len(ps.Token.TValue) > 0 {
ps.Tokens.add(ps.Token.TValue, TokenTypeLiteral, ps.Token.Parts)
ps.Token = Token{}
ps.InString = false
continue
}
if ps.Token.TValue != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
}
ps.InString = true
ps.Token = Token{TType: TokenTypeLiteral}
continue
}
if ps.currentChar() == At {
if len(ps.Tokens.Sections) <= ps.Tokens.SectionIndex {
ps.Tokens.Sections = append(ps.Tokens.Sections, Section{Type: TokenSectionText})
}
ps.Tokens.Sections[ps.Tokens.SectionIndex].Type = TokenSectionText
if ps.Token.TType != "" && !ps.InBracket {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
}
ps.Token = Token{TType: TokenTypeTextPlaceHolder, TValue: ps.currentChar()}
ps.Offset++
continue
}
if ps.currentChar() == BracketOpen {
if ps.Token.TType != "" && !ps.InBracket {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.InBracket = true
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.currentChar() == Question {
if ps.Token.TType != "" && ps.Token.TType != TokenTypeDigitalPlaceHolder {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypeDigitalPlaceHolder
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.currentChar() == Percent {
if ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypePercent
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if ps.currentChar() == BlockDelimiter {
sectionTypes := []string{TokenSectionPositive, TokenSectionNegative, TokenSectionZero, TokenSectionText}
if ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
}
if len(ps.Tokens.Sections) <= ps.Tokens.SectionIndex {
ps.Tokens.Sections = append(ps.Tokens.Sections, Section{Type: sectionTypes[ps.Tokens.SectionIndex]})
}
ps.Tokens.SectionIndex++
if ps.Tokens.SectionIndex > 3 {
tokens := fTokens()
tokens.reset()
return Tokens{}
}
ps.Token = Token{}
ps.Tokens.Sections = append(ps.Tokens.Sections, Section{Type: sectionTypes[ps.Tokens.SectionIndex]})
ps.Offset++
continue
}
if strings.EqualFold("E+", ps.doubleChar()) {
if ps.Token.TType != "" {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypeExponential
ps.Token.TValue += ps.doubleChar()
ps.Offset += 2
continue
}
if ap, matched := ps.apPattern(); ap != -1 {
ps.Tokens.add(matched, TokenTypeDateTimes, ps.Token.Parts)
ps.Token = Token{}
ps.Offset += len(matched)
continue
}
if general, matched := ps.generalPattern(); general != -1 {
ps.Tokens.add(matched, TokenTypeGeneral, ps.Token.Parts)
ps.Token = Token{}
ps.Offset += len(matched)
continue
}
// token accumulation
if !ps.InBracket && !ps.InString {
if strings.ContainsAny(DatesTimesCodeChars, strings.ToUpper(ps.currentChar())) {
if inStrSlice(AmPm, ps.Token.TValue, false) != -1 {
ps.Token.TType = TokenTypeDateTimes
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
if ps.Token.TType == TokenTypeLiteral || ps.Token.TType == TokenTypeDateTimes && !strings.ContainsAny(ps.Token.TValue, ps.currentChar()) {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
if ps.Token.TType != "" && ps.Token.TType != TokenTypeDateTimes {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
}
ps.Token.TType = TokenTypeDateTimes
ps.Token.TValue += ps.currentChar()
ps.Offset++
continue
}
if strings.ContainsAny(DatesTimesCodeChars, strings.ToUpper(ps.Token.TValue)) {
ps.Tokens.add(ps.Token.TValue, TokenTypeDateTimes, ps.Token.Parts)
ps.Token = Token{TType: TokenTypeLiteral, TValue: ps.currentChar()}
ps.Offset++
continue
}
if strings.ContainsAny(DatesTimesCodeChars, strings.ToUpper(ps.nextChar())) {
ps.Token.TValue += ps.currentChar()
ps.Token.TType = TokenTypeLiteral
ps.Offset++
continue
}
if ps.currentChar() == QuoteSingle {
ps.Offset++
continue
}
if !strings.ContainsAny(NumCodeChars, ps.currentChar()) && ps.Token.TType == TokenTypeZeroPlaceHolder {
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
continue
}
ps.Token.TType = TokenTypeLiteral
}
ps.Token.TValue += ps.currentChar()
if inStrSlice(AmPm, ps.Token.TValue, false) != -1 {
ps.Token.TType = TokenTypeDateTimes
ps.Tokens.add(ps.Token.TValue, ps.Token.TType, ps.Token.Parts)
ps.Token = Token{}
ps.Offset++
continue
}
ps.Offset++
}
// dump remaining accumulation
if len(ps.Token.TValue) > 0 {
tokenType := TokenTypeLiteral
if ps.Token.TType != "" {
tokenType = ps.Token.TType
}
ps.Tokens.add(ps.Token.TValue, tokenType, nil)
}
tokens := fTokens()
tokens.reset()
return ps.Tokens
}
// Parse provides function to parse number format as a token stream (list).
func (ps *Parser) Parse(numFmt string) []Section {
ps.NumFmt = strings.TrimSpace(numFmt)
ps.Runes = []rune(ps.NumFmt)
ps.Tokens = ps.getTokens()
return ps.Tokens.Sections
}
// doubleChar provides function to get two characters after the current
// position.
func (ps *Parser) doubleChar() string {
if len(ps.Runes) >= ps.Offset+2 {
return string(ps.Runes[ps.Offset : ps.Offset+2])
}
return ""
}
// currentChar provides function to get the character of the current position.
func (ps *Parser) currentChar() string {
return string(ps.Runes[ps.Offset])
}
// nextChar provides function to get the next character of the current
// position.
func (ps *Parser) nextChar() string {
if len(ps.Runes) >= ps.Offset+2 {
return string(ps.Runes[ps.Offset+1 : ps.Offset+2])
}
return ""
}
// apPattern infers whether the subsequent characters match the AM/PM pattern,
// it will be returned matched index and result.
func (ps *Parser) apPattern() (int, string) {
for i, pattern := range AmPm {
l := len(pattern)
if len(ps.Runes) >= ps.Offset+l {
matched := string(ps.Runes[ps.Offset : ps.Offset+l])
if strings.EqualFold(matched, pattern) {
return i, matched
}
}
}
return -1, ""
}
// generalPattern infers whether the subsequent characters match the
// general pattern, it will be returned matched result and result.
func (ps *Parser) generalPattern() (int, string) {
l := len(TokenTypeGeneral)
if len(ps.Runes) >= ps.Offset+l {
matched := string(ps.Runes[ps.Offset : ps.Offset+l])
if strings.EqualFold(matched, TokenTypeGeneral) {
return 0, matched
}
}
return -1, ""
}
// inStrSlice provides a method to check if an element is present in an array,
// and return the index of its location, otherwise return -1.
func inStrSlice(a []string, x string, caseSensitive bool) int {
for idx, n := range a {
if !caseSensitive && strings.EqualFold(x, n) {
return idx
}
if x == n {
return idx
}
}
return -1
}
// PrettyPrint provides function to pretty the parsed result with the indented
// format.
func (ps *Parser) PrettyPrint() string {
indent, output := 0, ""
for _, section := range ps.Tokens.Sections {
output += "<" + section.Type + ">" + "\n"
for _, item := range section.Items {
indent++
for i := 0; i < indent; i++ {
output += "\t"
}
if len(item.Parts) == 0 {
output += item.TValue + " <" + item.TType + ">" + "\n"
} else {
output += "<" + item.TType + ">" + "\n"
}
for _, part := range item.Parts {
indent++
for i := 0; i < indent; i++ {
output += "\t"
}
output += part.Token.TValue + " <" + part.Token.TType + ">" + "\n"
indent--
}
indent--
}
}
return output
}