11
11
12
12
import
13
13
pathutils
14
-
14
+ import std / strutils
15
15
when defined(nimPreviewSlimSystem):
16
16
import std/ syncio
17
17
@@ -86,14 +86,57 @@ const
86
86
LineContinuationOprs = {'+' , '-' , '*' , '/' , '\\ ' , '<' , '>' , '!' , '?' , '^' ,
87
87
'|' , '%' , '&' , '$' , '@' , '~' , ',' }
88
88
AdditionalLineContinuationOprs = {'#' , ':' , '=' }
89
+ LineContinuationTokens = [
90
+ " let" , " var" , " const" , " type" , # section
91
+ " object" , " tuple" ,
92
+ # from ./layouter.oprSet
93
+ " div" , " mod" , " shl" , " shr" , " in" , " notin" , " is" ,
94
+ " isnot" , " not" , " of" , " as" , " from" , " .." , " and" , " or" , " xor" ,
95
+ ] # must be all `nimIdentNormalized`-ed
96
+
97
+ proc eqIdent(a, bNormalized: string ): bool =
98
+ a.nimIdentNormalize == bNormalized
99
+
100
+ proc endsWithIdent(s, subs: string ): bool =
101
+ let le = subs.len
102
+ if le > s.len: return false
103
+ s[^ le .. ^ 1 ].eqIdent subs
104
+
105
+ proc continuesWithIdent(s, subs: string , start: int ): bool =
106
+ s.substr(start, start+ subs.high).eqIdent subs
107
+
108
+ proc endsWithIdent(s, subs: string , endIdx: var int ): bool =
109
+ endIdx.dec subs.len
110
+ result = s.continuesWithIdent(subs, endIdx+ 1 )
111
+
112
+ proc containsObjectOf(x: string ): bool =
113
+ const sep = ' '
114
+ var idx = x.rfind(sep)
115
+ if idx == - 1 : return
116
+ template eatWord(word) =
117
+ while x[idx] == sep: idx.dec
118
+ result = x.endsWithIdent(word, idx)
119
+ if not result : return
120
+ eatWord " of"
121
+ eatWord " object"
122
+ result = true
123
+
124
+ proc endsWithLineContinuationToken(x: string ): bool =
125
+ result = false
126
+ for tok in LineContinuationTokens:
127
+ if x.endsWithIdent(tok):
128
+ return true
129
+ result = x.containsObjectOf
89
130
90
131
proc endsWithOpr* (x: string ): bool =
91
132
result = x.endsWith(LineContinuationOprs)
92
133
93
134
proc continueLine(line: string , inTripleString: bool ): bool {.inline.} =
94
135
result = inTripleString or line.len > 0 and (
95
136
line[0 ] == ' ' or
96
- line.endsWith(LineContinuationOprs+ AdditionalLineContinuationOprs))
137
+ line.endsWith(LineContinuationOprs+ AdditionalLineContinuationOprs) or
138
+ line.endsWithLineContinuationToken()
139
+ )
97
140
98
141
proc countTriples(s: string ): int =
99
142
result = 0
@@ -109,7 +152,10 @@ proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int =
109
152
s.rd = 0
110
153
var line = newStringOfCap(120 )
111
154
var triples = 0
112
- while readLineFromStdin(if s.s.len == 0 : " >>> " else : " ... " , line):
155
+ while true :
156
+ if not readLineFromStdin(if s.s.len == 0 : " >>> " else : " ... " , line):
157
+ # now readLineFromStdin meets EOF (ctrl-D/Z) or ctrl-C
158
+ quit()
113
159
s.s.add(line)
114
160
s.s.add(" \n " )
115
161
inc triples, countTriples(line)
0 commit comments