proc execute / return-error handling

This commit is contained in:
Simon Hafner 2015-04-26 09:20:55 +05:00
commit 7b7188a8ad

View file

@ -45,6 +45,9 @@ const
"type 'debug' to toggle debug mode on/off\n" & "type 'debug' to toggle debug mode on/off\n" &
"type 'terse' to toggle terse mode on/off" "type 'terse' to toggle terse mode on/off"
type
EUnexpectedCommand = object of Exception
proc parseQuoted(cmd: string; outp: var string; start: int): int = proc parseQuoted(cmd: string; outp: var string; start: int): int =
var i = start var i = start
i += skipWhitespace(cmd, i) i += skipWhitespace(cmd, i)
@ -80,16 +83,38 @@ proc sexp(s: seq[Suggest]): SexpNode =
proc listEPC(): SexpNode = proc listEPC(): SexpNode =
discard discard
proc execute(cmd: IdeCmd, file, dirtyfile: string, line, col: int) =
gIdeCmd = cmd
if cmd == ideUse:
modules.resetAllModules()
var isKnownFile = true
let dirtyIdx = file.fileInfoIdx(isKnownFile)
if dirtyfile.len != 0: msgs.setDirtyFile(dirtyIdx, dirtyfile)
else: msgs.setDirtyFile(dirtyIdx, nil)
resetModule dirtyIdx
if dirtyIdx != gProjectMainIdx:
resetModule gProjectMainIdx
gTrackPos = newLineInfo(dirtyIdx, line, col)
#echo dirtyfile, gDirtyBufferIdx, " project ", gProjectMainIdx
gErrorCounter = 0
if not isKnownFile:
compileProject(dirtyIdx)
else:
compileProject()
proc executeEPC(cmd: IdeCmd, args: SexpNode) = proc executeEPC(cmd: IdeCmd, args: SexpNode) =
let let
file = args[0].getStr file = args[0].getStr
line = args[1].getNum line = args[1].getNum
column = args[2].getNum column = args[2].getNum
dirtyfile = args[3].getStr(nil) dirtyfile = args[3].getStr(nil)
discard execute(cmd, file, dirtyfile, int(line), int(column))
proc returnEPC(socket: var Socket, uid: string, s: SexpNode) = proc returnEPC(socket: var Socket, uid: string, s: SexpNode, return_symbol = "return") =
let response = $convertSexp([newSSymbol("return"), [uid, s]]) let response = $convertSexp([newSSymbol(return_symbol), [uid, s]])
socket.send(toHex(len(response), 6)) socket.send(toHex(len(response), 6))
socket.send(response) socket.send(response)
socket.close() socket.close()
@ -97,7 +122,7 @@ proc returnEPC(socket: var Socket, uid: string, s: SexpNode) =
proc findEPCPort(): int = proc findEPCPort(): int =
98294 # guaranteed to be random 98294 # guaranteed to be random
proc action(cmd: string) = proc parseCmdLine(cmd: string) =
template toggle(sw) = template toggle(sw) =
if sw in gGlobalOptions: if sw in gGlobalOptions:
excl(gGlobalOptions, sw) excl(gGlobalOptions, sw)
@ -115,9 +140,7 @@ proc action(cmd: string) =
of "sug": gIdeCmd = ideSug of "sug": gIdeCmd = ideSug
of "con": gIdeCmd = ideCon of "con": gIdeCmd = ideCon
of "def": gIdeCmd = ideDef of "def": gIdeCmd = ideDef
of "use": of "use": gIdeCmd = ideUse
modules.resetAllModules()
gIdeCmd = ideUse
of "quit": quit() of "quit": quit()
of "debug": toggle optIdeDebug of "debug": toggle optIdeDebug
of "terse": toggle optIdeTerse of "terse": toggle optIdeTerse
@ -133,23 +156,7 @@ proc action(cmd: string) =
i += skipWhile(cmd, seps, i) i += skipWhile(cmd, seps, i)
i += parseInt(cmd, col, i) i += parseInt(cmd, col, i)
var isKnownFile = true execute(gIdeCmd, orig, dirtyfile, line, col)
if orig.len == 0: err()
let dirtyIdx = orig.fileInfoIdx(isKnownFile)
if dirtyfile.len != 0: msgs.setDirtyFile(dirtyIdx, dirtyfile)
else: msgs.setDirtyFile(dirtyIdx, nil)
resetModule dirtyIdx
if dirtyIdx != gProjectMainIdx:
resetModule gProjectMainIdx
gTrackPos = newLineInfo(dirtyIdx, line, col)
#echo dirtyfile, gDirtyBufferIdx, " project ", gProjectMainIdx
gErrorCounter = 0
if not isKnownFile:
compileProject(dirtyIdx)
else:
compileProject()
proc serve() = proc serve() =
# do not stop after the first error: # do not stop after the first error:
@ -159,7 +166,7 @@ proc serve() =
echo Help echo Help
var line = "" var line = ""
while readLineFromStdin("> ", line): while readLineFromStdin("> ", line):
action line parseCmdLine line
echo "" echo ""
flushFile(stdout) flushFile(stdout)
of mtcp: of mtcp:
@ -176,7 +183,7 @@ proc serve() =
accept(server, stdoutSocket) accept(server, stdoutSocket)
stdoutSocket.readLine(inp) stdoutSocket.readLine(inp)
action inp.string parseCmdLine inp.string
stdoutSocket.send("\c\L") stdoutSocket.send("\c\L")
stdoutSocket.close() stdoutSocket.close()
@ -189,11 +196,7 @@ proc serve() =
server.listen() server.listen()
echo(port) echo(port)
while true: while true:
var results: seq[Suggest] = @[]
var client = newSocket() var client = newSocket()
suggest.suggestionResultHook = proc (s: Suggest) =
results.add(s)
accept(server, client) accept(server, client)
var sizeHex = "" var sizeHex = ""
if client.recv(sizeHex, 6, 1000) != 6: if client.recv(sizeHex, 6, 1000) != 6:
@ -207,8 +210,13 @@ proc serve() =
let message = parseSexp($messageBuffer) let message = parseSexp($messageBuffer)
let messageType = message[0].getSymbol let messageType = message[0].getSymbol
let body = message[1] let body = message[1]
try:
case messageType: case messageType:
of "call": of "call":
var results: seq[Suggest] = @[]
suggest.suggestionResultHook = proc (s: Suggest) =
results.add(s)
let let
uid = body[0].getStr uid = body[0].getStr
cmd = parseIdeCmd(body[1].getStr) cmd = parseIdeCmd(body[1].getStr)
@ -216,16 +224,18 @@ proc serve() =
executeEPC(cmd, args) executeEPC(cmd, args)
returnEPC(client, uid, sexp(results)) returnEPC(client, uid, sexp(results))
of "return": of "return":
raise newException(ValueError, "no return expected") raise newException(EUnexpectedCommand, "no return expected")
of "return-error": of "return-error":
raise newException(ValueError, "no return expected") raise newException(EUnexpectedCommand, "no return expected")
of "epc-error": of "epc-error":
stderr.writeln("recieved epc error: " & $messageBuffer) stderr.writeln("recieved epc error: " & $messageBuffer)
raise newException(ValueError, "epc error") raise newException(IOError, "epc error")
of "methods": of "methods":
returnEPC(client, body[0].getStr, listEPC()) returnEPC(client, body[0].getStr, listEPC())
else: else:
raise newException(ValueError, "unexpected call: " & messageType) raise newException(EUnexpectedCommand, "unexpected call: " & messageType)
except:
returnEPC(client, body[0].getStr, sexp(getCurrentExceptionMsg()), "return-error")
proc mainCommand = proc mainCommand =
registerPass verbosePass registerPass verbosePass