-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlex.go
1008 lines (933 loc) · 22.1 KB
/
lex.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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rdf
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
)
type tokenType int
const (
// special tokens
tokenEOF tokenType = iota // end of input
tokenEOL // end of line
tokenError // an illegal token
// turtle tokens
tokenIRIAbs // RDF IRI reference (absolute)
tokenIRIRel // RDF IRI reference (relative)
tokenBNode // RDF blank node
tokenLiteral // RDF literal
tokenLiteral3 // RDF literal (triple-quoted string)
tokenLiteralInteger // RDF literal (integer)
tokenLiteralDouble // RDF literal (double-precision floating point)
tokenLiteralDecimal // RDF literal (arbritary-precision decimal)
tokenLiteralBoolean // RDF literal (boolean)
tokenLangMarker // '@''
tokenLang // literal language tag
tokenDataTypeMarker // '^^'
tokenDot // '.'
tokenSemicolon // ';'
tokenComma // ','
tokenRDFType // 'a' => <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
tokenPrefix // @prefix
tokenPrefixLabel // @prefix tokenPrefixLabel: IRI
tokenIRISuffix // prefixLabel:IRISuffix
tokenBase // Base marker
tokenSparqlPrefix // PREFIX
tokenSparqlBase // BASE
tokenAnonBNode // [ws*]
tokenPropertyListStart // '['
tokenPropertyListEnd // ']'
tokenCollectionStart // '('
tokenCollectionEnd // ')'
)
const eof = -1
func min(a, b int) int {
if a < b {
return a
}
return b
}
// token represents a token emitted by the lexer.
type token struct {
typ tokenType // type of token
line int // line number
col int // column number (NB measured in bytes, not runes)
text string // the value of the token
}
// stateFn represents the state of the lexer as a function that returns the next state.
type stateFn func(*lexer) stateFn
// lexer for trig/turtle (and their line-based subsets n-triples & n-quads).
//
// Tokens for whitespace and comments are not not emitted.
//
// The design of the lexer and indeed much of the implementation is lifted from
// the template lexer in Go's standard library, and is governed by a BSD licence
// and Copyright 2011 The Go Authors.
type lexer struct {
rdr *bufio.Reader
input []byte // the input being scanned (should not inlcude newlines)
lineMode bool // true when lexing line-based formats (N-Triples & N-Quads)
unEsc bool // true when current token needs to be unescaped
state stateFn // the next lexing function to enter
line int // the current line number
pos int // the current position in input
width int // width of the last rune read from input
start int // start of current token
tokens chan token // channel of scanned tokens
}
func newLexer(r io.Reader) *lexer {
l := lexer{
rdr: bufio.NewReader(r),
tokens: make(chan token),
}
go l.run()
return &l
}
func newLineLexer(r io.Reader) *lexer {
l := lexer{
rdr: bufio.NewReader(r),
tokens: make(chan token),
lineMode: true,
}
go l.run()
return &l
}
// next returns the next rune in the input.
func (l *lexer) next() rune {
if l.pos >= len(l.input) {
l.width = 0
return eof
}
r, w := decodeRune(l.input[l.pos:])
l.width = w
l.pos += l.width
return r
}
// peek returns but does not consume the next rune in the input.
func (l *lexer) peek() rune {
r := l.next()
l.backup()
return r
}
// backup steps back one rune. Can only be called once per call of next.
func (l *lexer) backup() {
l.pos -= l.width
}
func (l *lexer) unescape(s string, t tokenType) string {
if !l.unEsc {
return s
}
l.unEsc = false
if t == tokenIRISuffix {
return unescapeReservedChars(s)
}
return unescapeNumericString(s)
}
func unescapeNumericString(s string) string {
r := []rune(s)
buf := bytes.NewBuffer(make([]byte, 0, len(r)))
for i := 0; i < len(r); {
switch r[i] {
case '\\':
i++
var c byte
switch r[i] {
case 't':
c = '\t'
case 'b':
c = '\b'
case 'n':
c = '\n'
case 'r':
c = '\r'
case 'f':
c = '\f'
case '"':
c = '"'
case '\'':
c = '\''
case '\\':
c = '\\'
case 'u':
rc, _ := strconv.ParseInt(string(r[i+1:i+5]), 16, 32)
// we can safely assume no error, because we allready veryfied
// the escape sequence in the lex state funcitons
buf.WriteRune(rune(rc))
i += 5
continue
case 'U':
rc, _ := strconv.ParseInt(string(r[i+1:i+9]), 16, 32)
// we can safely assume no error, because we allready veryfied
// the escape sequence in the lex state funcitons
buf.WriteRune(rune(rc))
i += 9
continue
}
buf.WriteByte(c)
default:
buf.WriteRune(r[i])
}
i++
}
return buf.String()
}
func unescapeReservedChars(s string) string {
r := []rune(s)
buf := bytes.NewBuffer(make([]byte, 0, len(r)))
for i := 0; i < len(r); {
switch r[i] {
case '\\':
i++
var c rune
switch r[i] {
case '_', '~', '.', '-', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '/', '?', '#', '@', '%':
c = r[i]
}
buf.WriteRune(c)
default:
buf.WriteRune(r[i])
}
i++
}
return buf.String()
}
// emit publishes a token back to the comsumer.
func (l *lexer) emit(typ tokenType) {
if typ == tokenEOL && !l.lineMode {
// Don't emit EOL tokens in linemode
l.start = l.pos
return
}
l.tokens <- token{
typ: typ,
line: l.line,
col: l.start,
text: l.unescape(string(l.input[l.start:l.pos]), typ),
}
l.start = l.pos
}
// ignore skips over the pending input before this point.
func (l *lexer) ignore() {
l.start = l.pos
}
// acceptRunMin consumes a run of runes from the valid set, returning
// true if a minimum number of runes where consumed.
func (l *lexer) acceptRunMin(valid []byte, num int) bool {
c := 0
for bytes.ContainsRune(valid, l.next()) {
c++
}
l.backup()
return c >= num
}
// acceptExact consumes the given string in l.input and returns true,
// or otherwise false if the string is not matched in l.input.
// The string must not contain multi-byte runes.
func (l *lexer) acceptExact(s string) bool {
if len(l.input[l.start:]) < len(s) {
return false
}
if string(l.input[l.start:l.pos+len(s)-1]) == s {
l.pos = l.pos + len(s) - 1
return true
}
return false
}
// acceptCaseInsensitive consumes the given string in l.input, disregarding case,
// and returns true, or otherwise false if the string is not matched in l.input.
// It only works on the ASCII subset.
func (l *lexer) acceptCaseInsensitive(s string) bool {
if len(l.input[l.start:]) < len(s) {
return false
}
for i := 0; i < len(s); i++ {
r := l.input[l.start+i]
if r == s[i] {
continue
}
if r < 'A' {
// r is lowercase, s[i] can be uppercase
if r == s[i]-32 {
continue
}
return false
}
// r is uppercase, s[i] can be lowercase
if r == s[i]+32 {
continue
}
return false
}
l.pos = l.pos + len(s) - 1
return true
}
// nextToken returns the next token from the input.
func (l *lexer) nextToken() token {
tok := <-l.tokens
return tok
}
func (l *lexer) feed(overwrite bool) bool {
again:
line, err := l.rdr.ReadBytes('\n')
if err != nil && len(line) == 0 {
return false
}
l.line++
if len(line) == 0 || line[0] == '#' {
// skip empty lines and lines starting with comment
l.emit(tokenEOL)
goto again
}
if overwrite {
// multi-line literal, concat to current input
l.input = append(l.input, line...)
} else {
l.input = line
l.pos = 0
l.start = 0
}
return true
}
// run runs the state machine for the lexer.
func (l *lexer) run() {
for {
if !l.feed(false) {
break
}
for l.state = lexAny; l.state != nil; {
l.state = l.state(l)
}
}
// No more input to lex, emit final EOF token and terminate.
// The value of the closed tokens channel is tokenEOF.
close(l.tokens)
}
// state functions:
// errorf returns an error token and terminates the scan by passing
// back a nil pointer that will be the next state, terminating l.nextToken.
func (l *lexer) errorf(format string, args ...interface{}) stateFn {
l.tokens <- token{
tokenError,
l.line,
l.pos,
fmt.Sprintf(format, args...),
}
return nil
}
func lexAny(l *lexer) stateFn {
r := l.next()
switch r {
case '@':
n := l.next()
switch n {
case 'p':
l.start++ // consume '@''
return lexPrefix
case 'b':
l.start++ // consume '@''
return lexBase
default:
l.backup()
return l.errorf("unrecognized directive")
}
case '_':
if l.peek() != ':' {
return l.errorf("illegal character %q in blank node identifier", l.peek())
}
// consume & ignore '_:'
l.next()
//l.ignore()
return lexBNode
case '<':
l.ignore()
return lexIRI
case 'a':
p := l.peek()
for _, a := range okAfterRDFType {
if p == a {
l.emit(tokenRDFType)
return lexAny
}
}
// If not 'a' as rdf:type, it can be a prefixed local name starting with 'a'
l.pos-- // undread 'a'
return lexPrefixLabel
case ':':
// default namespace, no prefix
l.backup()
return lexPrefixLabel
case '\'':
l.backup()
return lexLiteral
case '"':
l.backup()
return lexLiteral
case '+', '-':
if !isDigit(l.peek()) {
return l.errorf("bad literal: illegal number syntax: (%q not followed by number)", r)
}
l.backup()
return lexNumber
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
l.backup()
return lexNumber
case ' ', '\t':
// whitespace tokens are not emitted, so we ignore and continue
l.ignore()
return lexAny
case '[':
// Can be either an anonymous blank node: '[' WS* ']',
// or start of blank node property list: '[' verb objectList (';' (verb objectList)?)* ']'
for r = l.next(); r == ' ' || r == '\t'; r = l.next() {
}
if r == ']' {
l.ignore()
l.emit(tokenAnonBNode)
return lexAny
}
l.backup()
l.ignore()
l.emit(tokenPropertyListStart)
return lexAny
case ']':
// This must be closing of a blank node property list,
// since the closing of anonymous blank node is lexed above.
l.ignore()
l.emit(tokenPropertyListEnd)
return lexAny
case '(':
l.ignore()
l.emit(tokenCollectionStart)
return lexAny
case ')':
l.ignore()
l.emit(tokenCollectionEnd)
return lexAny
case '.':
if isDigit(l.peek()) {
l.pos -= 2 // can only backup once with l.backup()
return lexNumber
}
l.ignore()
l.emit(tokenDot)
return lexAny
case '\r':
if l.peek() == '\n' {
l.next()
return lexAny
}
l.ignore()
l.emit(tokenEOL)
return lexAny
case '\n':
l.ignore()
l.emit(tokenEOL)
return nil
case ';':
l.emit(tokenSemicolon)
return lexAny
case ',':
l.emit(tokenComma)
return lexAny
case '#', eof:
// comment tokens are not emitted, so treated as eof
l.ignore()
l.emit(tokenEOL)
return nil // This parks the lexer until it gets more input
case 'P', 'p':
if l.acceptCaseInsensitive("PREFIX") {
l.emit(tokenSparqlPrefix)
// consume and ignore any whitespace before localname
for r := l.next(); r == ' ' || r == '\t'; r = l.next() {
}
l.backup()
l.ignore()
return lexPrefixLabelInDirective
}
l.backup()
return lexPrefixLabel
case 'B', 'b':
if l.acceptCaseInsensitive("BASE") {
l.emit(tokenSparqlBase)
return lexAny
}
l.backup()
return lexPrefixLabel
case 't':
if l.acceptExact("true") {
l.emit(tokenLiteralBoolean)
return lexAny
}
l.backup()
return lexPrefixLabel
case 'f':
if l.acceptExact("false") {
l.emit(tokenLiteralBoolean)
return lexAny
}
l.backup()
return lexPrefixLabel
default:
if isPnCharsBase(r) {
l.backup()
return lexPrefixLabel
}
return l.errorf("unexpected character: %q", r)
}
}
func hasValidScheme(l *lexer) bool {
// RFC 2396: scheme = alpha *( alpha | digit | "+" | "-" | "." )
// decode first rune, must be in set [a-zA-Z]
r, w := decodeRune(l.input[l.start:])
if !isAlpha(r) {
return false
}
// remaining runes must be alphanumeric or '+', '-', '.''
for p := l.start + w; p < l.pos-w; p = p + w {
r, w = decodeRune(l.input[p:])
if isAlphaOrDigit(r) || r == '+' || r == '-' || r == '.' {
continue
}
return false
}
return true
}
func _lexIRI(l *lexer) (stateFn, bool) {
hasScheme := false // does it have a scheme? defines if IRI is absolute or relative
maybeAbsolute := true // false if we reach a non-valid scheme rune before ':'
for {
r := l.next()
if r == eof {
return l.errorf("bad IRI: no closing '>'"), false
}
for _, bad := range badIRIRunes {
if r == bad {
return l.errorf("bad IRI: disallowed character %q", r), false
}
}
if r == '\\' {
// handle numeric escape sequences for unicode points:
esc := l.peek()
switch esc {
case 'u':
l.next() // cosume 'u'
if !l.acceptRunMin(hex, 4) {
return l.errorf("bad IRI: insufficent hex digits in unicode escape"), false
}
// Ensure that escaped character is not in badIRIRunes.
// We can ignore the error, because we know it's a correctly lexed hex value.
i, _ := strconv.ParseInt(string(l.input[l.pos-4:l.pos]), 16, 0)
for _, bad := range badIRIRunesEsc {
if rune(i) == bad {
return l.errorf("bad IRI: disallowed character in unicode escape: %q", string(l.input[l.pos-6:l.pos])), false
}
}
l.unEsc = true
case 'U':
l.next() // cosume 'U'
if !l.acceptRunMin(hex, 8) {
return l.errorf("bad IRI: insufficent hex digits in unicode escape"), false
}
// Ensure that escaped character is not in badIRIRunes.
// We can ignore the error, because we know it's a correctly lexed hex value.
i, _ := strconv.ParseInt(string(l.input[l.pos-4:l.pos]), 16, 0)
for _, bad := range badIRIRunesEsc {
if rune(i) == bad {
return l.errorf("bad IRI: disallowed character in unicode escape: %q", string(l.input[l.pos-9:l.pos])), false
}
}
l.unEsc = true
case eof:
return l.errorf("bad IRI: no closing '>'"), false
default:
return l.errorf("bad IRI: disallowed escape character %q", esc), false
}
}
if maybeAbsolute && r == ':' {
// Check if we have an absolute IRI
if l.pos != l.start && hasValidScheme(l) && l.peek() != eof {
hasScheme = true
maybeAbsolute = false // stop checking for absolute
}
}
if r == '>' {
// reached end of IRI
break
}
}
l.backup()
return nil, hasScheme
}
func lexIRI(l *lexer) stateFn {
res, absolute := _lexIRI(l)
if res != nil {
return res
}
if absolute {
l.emit(tokenIRIAbs)
} else {
l.emit(tokenIRIRel)
}
// ignore '>'
l.pos++
l.ignore()
return lexAny
}
func lexLiteral(l *lexer) stateFn {
quote := l.next()
quoteCount := 1
var r rune
l.ignore()
for quoteCount < 6 {
r = l.next()
if r != quote {
break
}
l.ignore()
quoteCount++
}
if quoteCount == 6 {
// Empty triple-quoted string
l.pos = l.start
goto done
}
if quoteCount == 2 {
// Empty single-quoted string
quoteCount = 0
l.pos = l.start
goto done
}
outer:
for {
switch r {
case '\n':
if quoteCount != 3 {
return l.errorf("bad literal: newline not allowed in single-quoted string")
}
// triple-quoted strings can contain newlines
if !l.feed(true) {
return l.errorf("bad literal: no closing quote: %q", quote)
}
case '\r':
if quoteCount != 3 {
return l.errorf("bad literal: carriage return not allowed in single-quoted string")
}
case eof:
return l.errorf("bad literal: no closing quote: %q", quote)
case '\\':
// handle numeric escape sequences for unicode points:
esc := l.next()
switch esc {
case 't', 'b', 'n', 'r', 'f', '"', '\'', '\\':
l.unEsc = true
case 'u':
if !l.acceptRunMin(hex, 4) {
return l.errorf("bad literal: insufficent hex digits in unicode escape")
}
l.unEsc = true
case 'U':
if !l.acceptRunMin(hex, 8) {
return l.errorf("bad literal: insufficent hex digits in unicode escape")
}
l.unEsc = true
case eof:
return l.errorf("bad literal: no closing quote %q", quote)
default:
return l.errorf("bad literal: disallowed escape character %q", esc)
}
case quote:
// reached end of Literal
if quoteCount == 3 {
// Triple-quoted strings can contain quotes, as long as not 3 in a row.
if l.next() != quote {
break
}
if l.next() != quote {
break
}
}
l.pos -= quoteCount
break outer
}
r = l.next()
}
done:
if quoteCount == 3 || quoteCount == 6 {
l.emit(tokenLiteral3)
} else {
l.emit(tokenLiteral)
}
// ignore quote(s)
if quoteCount != 6 {
l.pos += quoteCount
}
l.ignore()
// check if literal has language tag or datatype IRI:
r = l.next()
switch r {
case '@':
l.emit(tokenLangMarker)
return lexLang
case '^':
if l.next() != '^' {
return l.errorf("bad literal: invalid datatype IRI")
}
l.emit(tokenDataTypeMarker)
/*if l.next() != '<' {
return l.errorf("bad literal: invalid datatype IRI")
}
l.ignore() // ignore '<'
return lexIRI*/
return lexAny
case ' ', '\t':
l.ignore()
return lexAny
default:
l.backup()
return lexAny
}
}
func lexNumber(l *lexer) stateFn {
// integer: [+-]?[0-9]+
// decimal: [+-]?[0-9]*\.[0-9]+
// dobule: (([+-]?[0-9]+\.[0-9]+)|
// ([+-]?\.[0-9]+)|
// ([+-]?[0-9]))
// (e|E)[+-]?[0-9]+
gotDot := false
gotE := false
r := l.next()
switch r {
case '+', '-':
case '.':
// cannot be an integer
gotDot = true
default:
// a digit
outer:
for {
r = l.next()
switch {
case isDigit(r):
continue
case r == '.':
if gotDot {
// done lexing number, next one can be end-of-statement dot.
l.backup()
break outer
}
p := l.peek()
if !isDigit(p) && p != 'E' && p != 'e' {
// integer followed by end-of-statement dot
l.pos-- // backup() may allready be called
break outer
}
gotDot = true
case r == 'e', r == 'E':
if gotE {
return l.errorf("bad literal: illegal number syntax")
}
gotE = true
p := l.peek()
if p == '+' || p == '-' {
l.next()
} else {
if !isDigit(p) {
return l.errorf("bad literal: illegal number syntax: missing exponent")
}
}
default:
if r == ' ' || r == ',' || r == ';' || r == eof || r == ')' || r == ']' {
l.backup()
break outer
}
l.errorf("bad literal: illegal number syntax (number followed by %q)", r)
}
}
switch {
case gotE:
l.emit(tokenLiteralDouble)
case gotDot:
l.emit(tokenLiteralDecimal)
default:
l.emit(tokenLiteralInteger)
}
}
return lexAny
}
func lexBNode(l *lexer) stateFn {
r := l.next()
if r == eof {
return l.errorf("bad blank node: unexpected end of line")
}
if !(isPnCharsU(r) || isDigit(r)) {
return l.errorf("bad blank node: invalid character %q", r)
}
for {
r = l.next()
if r == '.' {
// Blank node labels can include '.', except as the final character
if isPnChars(l.peek()) {
continue
} else {
l.pos-- // backup, '.' has width 1
break
}
}
if !(isPnChars(r)) {
l.backup()
break
}
}
l.emit(tokenBNode)
return lexAny
}
func lexLang(l *lexer) stateFn {
// NOTE this is only a rough check of the language tag's well-formedness.
// TODO: Full conformance with the spec is quite complex:
// http://tools.ietf.org/html/bcp47 [section 2.1]
c := 0
var r rune
for r = l.next(); isAlpha(r); r = l.next() {
c++
}
if c == 0 {
return l.errorf("bad literal: invalid language tag")
}
if r == '-' {
c = 0
for r := l.next(); isAlphaOrDigit(r) || r == '-'; r = l.next() {
c++
}
l.backup()
if c == 0 {
return l.errorf("bad literal: invalid language tag")
}
} else {
l.backup()
}
l.emit(tokenLang)
return lexAny
}
func lexPrefix(l *lexer) stateFn {
if l.acceptExact("prefix") {
l.emit(tokenPrefix)
// consume and ignore any whitespace before localname
for r := l.next(); r == ' ' || r == '\t'; r = l.next() {
}
l.backup()
l.ignore()
return lexPrefixLabelInDirective
}
return l.errorf("invalid character 'p'")
}
func lexPrefixLabelInDirective(l *lexer) stateFn {
r := l.next()
if r == ':' {
//PN_PREFIX can be empty
l.emit(tokenPrefixLabel)
return lexAny
}
if !isPnCharsBase(r) {
return l.errorf("unexpected character: %q", r)
}
for {
r = l.next()
if r == ':' {
l.backup()
break
}
if !(isPnChars(r) || (r == '.' && l.peek() != ':')) {
return l.errorf("illegal token: %q", string(l.input[l.start:l.pos]))
}
}
l.emit(tokenPrefixLabel)
// consume and ignore ':'
l.next()
l.ignore()
return lexAny
}
func lexPrefixLabel(l *lexer) stateFn {
l.ignore() // TODO why is this needed here?
r := l.next()
if r == ':' {
// PN_PREFIX can be empty, in which case use ':' as namespace key.
l.emit(tokenPrefixLabel)
return lexIRISuffix
}
if !isPnCharsBase(r) {
return l.errorf("unexpected character: %q", r)
}
for {
r = l.next()
if r == ':' {
l.backup()
break
}
if !(isPnChars(r) || (r == '.' && l.peek() != ':')) {
return l.errorf("illegal token: %q", string(l.input[l.start:l.pos]))
}
}
l.emit(tokenPrefixLabel)
// consume and ignore ':'
l.next()
l.ignore()
if l.peek() == '#' {
// emit empty IRI suffix, easier than dealing with special case in parser
l.emit(tokenIRISuffix)
return lexAny
}
return lexIRISuffix
}
func lexIRISuffix(l *lexer) stateFn {
//(PN_CHARS_U | ':' | [0-9] | PLX) ((PN_CHARS | '.' | ':' | PLX)* (PN_CHARS | ':' | PLX))?
r := l.next()
if r == ' ' {
// prefix ony IRI
l.ignore()
l.emit(tokenIRISuffix)
return lexAny
}
if !isPnLocalFirst(r) {
return l.errorf("unexpected character: %q", r)
}
if r == '\\' || r == '%' {
// Need to check that escaped char is in pnLocalEsc,
// so we do it outerLoop below.
l.backup()
}
outerLoop:
for r = l.next(); isPnLocalMid(r); r = l.next() {
if r == '\\' {
p := l.next()
for _, esc := range pnLocalEsc {
if esc == p {
l.unEsc = true
continue outerLoop
}
}
return l.errorf("invalid escape charater %q", p)
}
// - ('%' hex hex)
if r == '%' {
if !l.acceptRunMin(hex, 2) {
return l.errorf("invalid hex escape sequence")
}
}
}
l.backup()
if l.input[min(len(l.input)-1, l.pos-1)] == '.' && l.input[min(len(l.input)-2, l.pos-2)] != '\\' {
// last rune cannot be dot, otherwise isPnLocalMid(r) is valid for last position as well
l.pos--
}
l.emit(tokenIRISuffix)
return lexAny
}