-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
192 lines (178 loc) · 5.65 KB
/
strings.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
/*******************************************************************************
* Copyright (c) 2009-2022 Crater Dog Technologies™. All Rights Reserved. *
*******************************************************************************
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. *
* *
* This code is free software; you can redistribute it and/or modify it under *
* the terms of The MIT License (MIT), as published by the Open Source *
* Initiative. (See http://opensource.org/licenses/MIT) *
*******************************************************************************/
package bali
import (
abs "github.com/craterdog-bali/go-bali-document-notation/abstractions"
str "github.com/craterdog-bali/go-bali-document-notation/strings"
stc "strconv"
sts "strings"
uni "unicode"
)
// This method attempts to parse a binary string. It returns the binary
// string and whether or not the binary string was successfully parsed.
func (v *parser) parseBinary() (str.Binary, *Token, bool) {
var token *Token
var binary str.Binary
token = v.nextToken()
if token.Type != TokenBinary {
v.backupOne()
return binary, token, false
}
var matches = scanBinary([]byte(token.Value))
// Remove all whitespace and the "'" delimiters.
binary = str.Binary(sts.Map(func(r rune) rune {
if uni.IsSpace(r) {
return -1
}
return r
}, matches[1]))
return binary, token, true
}
const lineLength = 60 // 60 base 64 characters encode 45 bytes per line.
// This method adds the canonical format for the specified string to the state
// of the formatter.
func (v *formatter) formatBinary(binary str.Binary) {
v.AppendString("'")
var s = string(binary)
var length = len(s)
if length > lineLength {
// Spans multiple lines.
v.depth++
for index := 0; index < length; {
v.AppendNewline()
var next = index + lineLength
if next > length {
next = length
}
v.AppendString(s[index:next])
index = next
}
v.depth--
v.AppendNewline()
} else {
v.AppendString(s)
}
v.AppendString("'")
}
// This method attempts to parse a moniker string. It returns the moniker string
// and whether or not the moniker string was successfully parsed.
func (v *parser) parseMoniker() (str.Moniker, *Token, bool) {
var token *Token
var moniker str.Moniker
token = v.nextToken()
if token.Type != TokenMoniker {
v.backupOne()
return moniker, token, false
}
var matches = scanMoniker([]byte(token.Value))
moniker = str.Moniker(matches[0])
return moniker, token, true
}
// This method adds the canonical format for the specified string to the state
// of the formatter.
func (v *formatter) formatMoniker(moniker str.Moniker) {
var s = string(moniker)
v.AppendString(s)
}
// This method attempts to parse a narrative string. It returns the narrative
// string and whether or not the narrative string was successfully parsed.
func (v *parser) parseNarrative() (str.Narrative, *Token, bool) {
var token *Token
var narrative str.Narrative
token = v.nextToken()
if token.Type != TokenNarrative {
v.backupOne()
return narrative, token, false
}
narrative = str.Narrative(trimIndentation(token.Value))
return narrative, token, true
}
// This method adds the canonical format for the specified string to the state
// of the formatter.
func (v *formatter) formatNarrative(narrative str.Narrative) {
var s = string(narrative)
var lines = sts.Split(s, "\n")
v.AppendString(`">`)
for _, line := range lines {
v.AppendNewline()
v.AppendString(line)
}
v.AppendNewline()
v.AppendString(`<"`)
}
// This method attempts to parse a quote string. It returns the quote string
// and whether or not the quote string was successfully parsed.
func (v *parser) parseQuote() (str.Quote, *Token, bool) {
var token *Token
var quote str.Quote
token = v.nextToken()
if token.Type != TokenQuote {
v.backupOne()
return quote, token, false
}
var matches = scanQuote([]byte(token.Value))
// We must unquote the full token string properly.
var unquoted, _ = stc.Unquote(matches[0])
quote = str.Quote(unquoted)
return quote, token, true
}
// This method adds the canonical format for the specified string to the state
// of the formatter.
func (v *formatter) formatQuote(quote str.Quote) {
// We must requote the string string properly.
var s = stc.Quote(string(quote))
v.AppendString(s)
}
// This method attempts to parse a string sequence. It returns the
// string sequence and whether or not the string sequence was
// successfully parsed.
func (v *parser) parseString() (abs.String, *Token, bool) {
var ok bool
var token *Token
var s abs.String
s, token, ok = v.parseQuote()
if !ok {
s, token, ok = v.parseMoniker()
}
if !ok {
s, token, ok = v.parseVersion()
}
if !ok {
s, token, ok = v.parseBinary()
}
if !ok {
s, token, ok = v.parseNarrative()
}
if !ok {
// Override any empty strings returned from failed parsing attempts.
s = nil
}
return s, token, ok
}
// This method attempts to parse a version string. It returns the version
// string and whether or not the version string was successfully parsed.
func (v *parser) parseVersion() (str.Version, *Token, bool) {
var token *Token
var version str.Version
token = v.nextToken()
if token.Type != TokenVersion {
v.backupOne()
return version, token, false
}
var matches = scanVersion([]byte(token.Value))
version = str.Version(matches[1]) // Remove the leading "v".
return version, token, true
}
// This method adds the canonical format for the specified string to the state
// of the formatter.
func (v *formatter) formatVersion(version str.Version) {
var s = "v" + string(version)
v.AppendString(s)
}