Skip to content

Commit d573578

Browse files
repl: support eof, define object with fields (#24784)
For `nim secret`: - **fix(repl): eof(ctrl-D/Z) and ctrl-C were ignored** - **feat(repl): continueLine figures section, constr, bool ops** --------- Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
1 parent 909f3b8 commit d573578

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

compiler/llstream.nim

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import
1313
pathutils
14-
14+
import std/strutils
1515
when 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

90131
proc endsWithOpr*(x: string): bool =
91132
result = x.endsWith(LineContinuationOprs)
92133

93134
proc 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

98141
proc 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

Comments
 (0)