-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringStream.cls
More file actions
139 lines (114 loc) · 4.86 KB
/
StringStream.cls
File metadata and controls
139 lines (114 loc) · 4.86 KB
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "StringStream"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'@Exposed
'@Folder("JSON.DataReaders")
Option Explicit
Private Const ModuleName As String = "StringStream"
Private Type TData
Value As String
End Type
Private This As TData
'@Description "Constructor"
Friend Sub Create(ByVal Data As String)
Attribute Create.VB_Description = "Constructor"
Const FunctionName As String = "Create"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
This.Value = Data
End Sub
'@Description "Eat any possible spaces"
Public Sub EatSpaces()
Attribute EatSpaces.VB_Description = "Eat any possible spaces"
Const FunctionName As String = "EatSpaces"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Rx As VBScript_RegExp_55.RegExp
Set Rx = CreateObject("VBScript.RegExp")
Rx.Pattern = "^[\s]*([\s\S]+)"
This.Value = Rx.Replace(This.Value, "$1")
End Sub
'@Description "Eat a single character"
Public Sub EatCharacter(ByVal Character As String)
Attribute EatCharacter.VB_Description = "Eat a single character"
Const FunctionName As String = "EatCharacter"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Debug.Assert Len(Character) = 1 '// Expected single character
If (PeekCharacter = Character) Then
This.Value = Right$(This.Value, Len(This.Value) - 1)
Else
Err.Raise JsonExceptionUnexpectedCharacter, ModuleName & "." & FunctionName, "Character """ & Character & """ excpected."
End If
End Sub
'@Description "Lookup the next character in the stream"
Public Function PeekCharacter() As String
Attribute PeekCharacter.VB_Description = "Lookup the next character in the stream"
Const FunctionName As String = "PeekCharacter"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
PeekCharacter = Left$(This.Value, 1)
End Function
'@Description "Get the stream's current value"
Public Function Value() As String
Attribute Value.VB_Description = "Get the stream's current value"
Const FunctionName As String = "Value"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Value = This.Value
End Function
'@Description "End of stream indicator"
Public Function EOF() As Boolean
Attribute EOF.VB_Description = "End of stream indicator"
Const FunctionName As String = "EOF"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
EOF = This.Value = vbNullString
End Function
'@Description "Check wether the stream's value match the given regular expression"
Public Function Match(ByVal RegEx As String) As Boolean
Attribute Match.VB_Description = "Check wether the stream's value match the given regular expression"
Const FunctionName As String = "Match"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Rx As VBScript_RegExp_55.RegExp
Set Rx = CreateObject("VBScript.RegExp")
Rx.Global = True
Rx.IgnoreCase = False
Rx.Pattern = RegEx
Match = Rx.test(This.Value)
End Function
'@Description "Eat a string"
Public Sub EatString(ByVal Data As String)
Attribute EatString.VB_Description = "Eat a string"
Const FunctionName As String = "EatString"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
If (InStr(This.Value, Data) = 1) Then
This.Value = Right$(This.Value, Len(This.Value) - Len(Data))
Else
Err.Raise JsonExceptionUnexpectedCharacter, ModuleName & "." & FunctionName, "String like """ & Left$(This.Value, 20) & """ expected."
End If
End Sub
'@Description "Return the 1st value matching the given regular expression"
Public Function PeekString(ByVal RegEx As String) As String
Attribute PeekString.VB_Description = "Return the 1st value matching the given regular expression"
Const FunctionName As String = "EatString"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Rx As VBScript_RegExp_55.RegExp
Set Rx = CreateObject("VBScript.RegExp")
Rx.Global = True
Rx.IgnoreCase = False
Rx.Pattern = RegEx
Dim Matchs As VBScript_RegExp_55.MatchCollection
Set Matchs = Rx.Execute(This.Value)
Debug.Assert Matchs.Count > 0 '// Provided regex did not match
PeekString = Matchs.Item(0).Value
End Function