Applied Araq's suggestions for c2nim
This commit is contained in:
parent
5395347582
commit
aec9195c95
2 changed files with 11 additions and 9 deletions
|
|
@ -49,8 +49,8 @@ proc parse(infile: string, options: PParserOptions): PNode =
|
||||||
|
|
||||||
proc main(infile, outfile: string, options: PParserOptions, spliceHeader: bool) =
|
proc main(infile, outfile: string, options: PParserOptions, spliceHeader: bool) =
|
||||||
var start = getTime()
|
var start = getTime()
|
||||||
if spliceHeader and infile[infile.len-2 .. infile.len] == ".c" and existsFile(infile[0 .. infile.len-2] & "h"):
|
if spliceHeader and infile.splitFile.ext == ".c" and existsFile(infile.changeFileExt(".h")):
|
||||||
var header_module = parse(infile[0 .. infile.len-2] & "h", options)
|
var header_module = parse(infile.changeFileExt(".h"), options)
|
||||||
var source_module = parse(infile, options)
|
var source_module = parse(infile, options)
|
||||||
for n in source_module:
|
for n in source_module:
|
||||||
addson(header_module, n)
|
addson(header_module, n)
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,8 @@ type
|
||||||
|
|
||||||
TReplaceTuple* = array[0..1, string]
|
TReplaceTuple* = array[0..1, string]
|
||||||
|
|
||||||
|
ERetryParsing = object of ESynch
|
||||||
|
|
||||||
proc newParserOptions*(): PParserOptions =
|
proc newParserOptions*(): PParserOptions =
|
||||||
new(result)
|
new(result)
|
||||||
result.prefixes = @[]
|
result.prefixes = @[]
|
||||||
|
|
@ -1181,7 +1183,7 @@ proc startExpression(p : var TParser, tok : TToken) : PNode =
|
||||||
try:
|
try:
|
||||||
addSon(result, expression(p, 139))
|
addSon(result, expression(p, 139))
|
||||||
closeContext(p)
|
closeContext(p)
|
||||||
except:
|
except ERetryParsing:
|
||||||
backtrackContext(p)
|
backtrackContext(p)
|
||||||
eat(p, pxParLe)
|
eat(p, pxParLe)
|
||||||
addSon(result, typeName(p))
|
addSon(result, typeName(p))
|
||||||
|
|
@ -1226,12 +1228,12 @@ proc startExpression(p : var TParser, tok : TToken) : PNode =
|
||||||
result = newNodeP(nkPar, p)
|
result = newNodeP(nkPar, p)
|
||||||
addSon(result, expression(p, 0))
|
addSon(result, expression(p, 0))
|
||||||
if p.tok.xkind != pxParRi:
|
if p.tok.xkind != pxParRi:
|
||||||
raise
|
raise newException(ERetryParsing, "expected a ')'")
|
||||||
getTok(p, result)
|
getTok(p, result)
|
||||||
if p.tok.xkind in {pxSymbol, pxIntLit, pxFloatLit, pxStrLit, pxCharLit}:
|
if p.tok.xkind in {pxSymbol, pxIntLit, pxFloatLit, pxStrLit, pxCharLit}:
|
||||||
raise
|
raise newException(ERetryParsing, "expected a non literal token")
|
||||||
closeContext(p)
|
closeContext(p)
|
||||||
except:
|
except ERetryParsing:
|
||||||
backtrackContext(p)
|
backtrackContext(p)
|
||||||
result = newNodeP(nkCast, p)
|
result = newNodeP(nkCast, p)
|
||||||
addSon(result, typeName(p))
|
addSon(result, typeName(p))
|
||||||
|
|
@ -1269,7 +1271,7 @@ proc startExpression(p : var TParser, tok : TToken) : PNode =
|
||||||
addSon(result, expression(p, 139))
|
addSon(result, expression(p, 139))
|
||||||
else:
|
else:
|
||||||
# probably from a failed sub expression attempt, try a type cast
|
# probably from a failed sub expression attempt, try a type cast
|
||||||
raise newException(E_Base, "not " & $tok)
|
raise newException(ERetryParsing, "did not expect " & $tok)
|
||||||
|
|
||||||
proc leftBindingPower(p : var TParser, tok : ref TToken) : int =
|
proc leftBindingPower(p : var TParser, tok : ref TToken) : int =
|
||||||
#echo "lbp ", $tok[]
|
#echo "lbp ", $tok[]
|
||||||
|
|
@ -2134,6 +2136,6 @@ proc parseUnit(p: var TParser): PNode =
|
||||||
while p.tok.xkind != pxEof:
|
while p.tok.xkind != pxEof:
|
||||||
var s = statement(p)
|
var s = statement(p)
|
||||||
if s.kind != nkEmpty: embedStmts(result, s)
|
if s.kind != nkEmpty: embedStmts(result, s)
|
||||||
except:
|
except ERetryParsing:
|
||||||
parMessage(p, errGenerated, "Uncaught exception raised during parsing")
|
parMessage(p, errGenerated, "Uncaught parsing exception raised")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue