1111
1212import
1313 pathutils
14-
14+ import std / strutils
1515when defined (nimPreviewSlimSystem):
1616 import std/ syncio
1717
@@ -86,14 +86,57 @@ const
8686 LineContinuationOprs = {'+' , '-' , '*' , '/' , '\\ ' , '<' , '>' , '!' , '?' , '^' ,
8787 '|' , '%' , '&' , '$' , '@' , '~' , ',' }
8888 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
89130
90131proc endsWithOpr * (x: string ): bool =
91132 result = x.endsWith (LineContinuationOprs )
92133
93134proc continueLine (line: string , inTripleString: bool ): bool {.inline .} =
94135 result = inTripleString or line.len > 0 and (
95136 line[0 ] == ' ' or
96- line.endsWith (LineContinuationOprs + AdditionalLineContinuationOprs ))
137+ line.endsWith (LineContinuationOprs + AdditionalLineContinuationOprs ) or
138+ line.endsWithLineContinuationToken ()
139+ )
97140
98141proc countTriples (s: string ): int =
99142 result = 0
@@ -109,7 +152,10 @@ proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int =
109152 s.rd = 0
110153 var line = newStringOfCap (120 )
111154 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 ()
113159 s.s.add (line)
114160 s.s.add (" \n " )
115161 inc triples, countTriples (line)
0 commit comments