-
Notifications
You must be signed in to change notification settings - Fork 2
/
helperParse.nim
143 lines (110 loc) · 3.34 KB
/
helperParse.nim
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
#
# nimrod-tools
# (c) Copyright 2013 Adrian Veith
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements some routines for parsing
type
TParseString* {.pure, final, shallow.} = object
s*: string
a*: int
template parseLoop* (x, it: expr, loop: stmt) : stmt {.dirty,immediate.} =
block:
var proceed {.noinit.}: bool
var `x` {.inject.} = current(it, proceed)
while proceed :
loop
`x` = next(it, proceed)
proc current* (s: var TParseString, proceed: var bool): char {.inline.} =
proceed = s.a < s.s.len
return s.s[s.a]
proc next* (s: var TParseString, proceed: var bool): char {.inline.} =
proceed = s.a < s.s.len
if proceed : inc(s.a)
result = s.s[s.a]
proc next* (s: var TParseString): char {.inline.} =
if s.a < s.s.len :
inc(s.a)
result = s.s[s.a]
proc prev* (s: var TParseString, proceed: var bool): char {.inline.} =
proceed = s.a > 0
if proceed :
dec(s.a)
result = s.s[s.a]
else: result = '\0'
proc prev* (s: var TParseString): char {.inline.} =
if s.a > 0 :
dec(s.a)
result = s.s[s.a]
else: result = '\0'
proc fromLiteral* (s: string): TParseString {.inline.} =
if not isNil(s) : result.s = s
else: result.s = ""
proc fromLiteral* (s: string, start: int): TParseString {.inline.} =
if not isNil(s) : result.s = s
else: result.s = ""
result.a = min(start, result.s.len)
proc fromString* (s: var string): TParseString {.inline.} =
if not isNil(s) : shallowCopy(result.s, s)
else: result.s = ""
proc fromString* (s: var string, start: int): TParseString {.inline.} =
if not isNil(s) : shallowCopy(result.s, s)
else: result.s = ""
result.a = min(start, result.s.len)
proc `[]`* (s: TParseString, x): char {.inline.} = return s.s[s.a + x]
# proc `!`* (s: TParseString): char {.inline.} = return s.s[s.a -1]
proc len* (s: TParseString): int {.inline.} =
return s.len - s.a
iterator items*(s: TParseString): char {.inline.} =
for i in s.a .. s.s.high :
yield s.s[i]
#iterator mitems*(s: TParseString): var char {.inline.} =
# for i in s.a .. s.s.high :
# yield s.s[i]
proc `$`* (s: TParseString): string {.inline.} =
return s.s.substr(s.a)
proc substr* (s: TParseString, first: int): string {.inline.} =
return s.s.substr(s.a + first)
proc substr* (s: TParseString, first, last: int): string {.inline.} =
return s.s.substr(s.a + first, s.a + last)
proc slice* (s: TParseString, length: int): string {.inline.} =
return s.s.substr(s.a, s.a + length -1)
when isMainModule:
var str = "12345678"
var ps = fromString(str, 2)
var p2 = ps
echo ps.substr(2,3)
echo ps.slice(4)
str[2] = 'c'
echo ps.substr(2,3)
echo ps.slice(4)
str = "abcdefghijk"
echo ps.substr(2,3)
echo ps.slice(4)
echo p2.substr(2,3)
echo p2.slice(4)
p2.a = 3
echo p2.substr(2,3), " <> ? ", ps.substr(2,3)
ps = fromLiteral("abcdef", 2)
echo ps.substr(2,3)
echo ps.slice(4)
parseLoop c, ps :
echo c
ps = fromLiteral("abcdef", 2)
parseLoop c, ps :
echo ps[0]
c = ps.next
echo c
ps = fromLiteral("abcdef", 3)
parseLoop c, ps :
echo ps[0]
c = ps.next
echo c == '\0'
ps = fromLiteral(nil, 3)
parseLoop c, ps :
echo c
ps = fromLiteral("123", 3)
parseLoop c, ps :
echo c