-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlisting.nim
More file actions
100 lines (89 loc) · 2.29 KB
/
listing.nim
File metadata and controls
100 lines (89 loc) · 2.29 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
#
#
# Lexim - The Lexer Generator for Nim
# (c) Copyright 2015 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## this modules contains utility functions for list generating routines:
import strutils
proc nchars*(cc: set[char]): int =
result = 0
for c in countup('\0', '\xFF'):
if c in cc: inc(result)
proc charStr*(c: char; reserved: set[char]): string =
case c
of '\b':
result = "\\b"
of '\t':
result = "\\t"
of '\C':
result = "\\r"
of '\L':
result = "\\l"
of '\v':
result = "\\v"
of '\f':
result = "\\f"
of '\e':
result = "\\e"
of '\a':
result = "\\a"
of '\\':
result = "\\\\"
else:
if c < ' ':
result = '\\' & $ord(c)
elif c in reserved:
result = '\\' & $c
else:
result = $c
proc singleQuoteStr*(str: string): string =
result = "'"
for c in str: result.add charStr(c, {'\''})
result.add '\''
proc doubleQuoteStr*(str: string): string =
result = "\""
for c in str: result.add charStr(c, {'\"'})
result.add '\"'
proc charSetStrAux(cc: set[char]): string =
const
reserved = {'^', '-', ']'}
MaxChar = '\xFF'
result = ""
var c1 = '\0'
while true:
if c1 in cc:
var c2 = c1
while (c2 < MaxChar) and (succ(c2) in cc): c2 = succ(c2)
if c1 == c2:
result.add charStr(c1, reserved)
elif c2 == succ(c1):
result.add charStr(c1, reserved) & charStr(c2, reserved)
else:
result.add charStr(c1, reserved) & '-' & charStr(c2, reserved)
c1 = c2
if c1 >= MaxChar: break
inc(c1)
proc charSetStr*(cc: set[char]): string =
if cc == {'\x01'..'\xFF'} - {'\L'}:
result = "."
else:
if nchars(cc) > 128:
result = "[^" & charSetStrAux({'\0'..'\xFF'} - cc) & ']'
else:
result = '[' & charSetStrAux(cc) & ']'
proc charSetOrCharStr*(cc: set[char]): string =
var count = 0
var c1 = '\0' # to avoid warnings
for c in countup('\0', '\xFF'):
if c in cc:
c1 = c
inc(count)
if count > 1:
result = charSetStr(cc)
elif count == 1:
result = charStr(c1, {'.'}) # was: singleQuoteStr(c1)
else:
result = "[]"