ast2 test fix for osx, catch parseString errors, handle type field comments

This commit is contained in:
Ganesh Viswanathan 2020-04-02 18:17:36 -05:00
commit 5c20ce7b60
5 changed files with 51 additions and 36 deletions

View file

@ -19,14 +19,17 @@ proc execCmd(cmd: string) =
exec cmd exec cmd
proc execTest(test: string, flags = "") = proc execTest(test: string, flags = "") =
execCmd "nim c -f " & flags & " -r " & test execCmd "nim c --hints:off -f " & flags & " -r " & test
execCmd "nim cpp " & flags & " -r " & test execCmd "nim cpp --hints:off " & flags & " -r " & test
task buildToast, "build toast": task buildToast, "build toast":
execCmd("nim c -f nimterop/toast.nim") execCmd("nim c --hints:off -f nimterop/toast.nim")
task bt, "build toast": task bt, "build toast":
execCmd("nim c -d:danger nimterop/toast.nim") execCmd("nim c --hints:off -d:danger nimterop/toast.nim")
task btd, "build toast":
execCmd("nim c --hints:off nimterop/toast.nim")
task docs, "Generate docs": task docs, "Generate docs":
buildDocs(@["nimterop/all.nim"], "build/htmldocs") buildDocs(@["nimterop/all.nim"], "build/htmldocs")
@ -38,13 +41,16 @@ task test, "Test":
execTest "tests/tast2.nim", "-d:HEADER" execTest "tests/tast2.nim", "-d:HEADER"
execTest "tests/tnimterop_c.nim" execTest "tests/tnimterop_c.nim"
execTest "tests/tnimterop_c.nim", "-d:AST2" execTest "tests/tnimterop_c.nim", "-d:FLAGS=\"-f:ast2\""
execTest "tests/tnimterop_c.nim", "-d:HEADER -d:AST2" execTest "tests/tnimterop_c.nim", "-d:FLAGS=\"-f:ast2 -H\""
execCmd "nim cpp -f -r tests/tnimterop_cpp.nim" execCmd "nim cpp --hints:off -f -r tests/tnimterop_cpp.nim"
execCmd "./nimterop/toast -pnk -E=_ tests/include/toast.h" execCmd "./nimterop/toast -pnk -E=_ tests/include/toast.h"
execCmd "./nimterop/toast -pnk -E=_ -f:ast2 tests/include/toast.h" execCmd "./nimterop/toast -pnk -E=_ -f:ast2 tests/include/toast.h"
execTest "tests/tpcre.nim" execTest "tests/tpcre.nim"
#execTest "tests/tpcre.nim", "-d:FLAGS=\"-f:ast2\""
#execTest "tests/tpcre.nim", "-d:FLAGS=\"-f:ast2 -H\""
# Platform specific tests # Platform specific tests
when defined(Windows): when defined(Windows):

View file

@ -2,7 +2,7 @@ import macros, os, sequtils, sets, strformat, strutils, tables, times
import regex import regex
import compiler/[ast, idents, modulegraphs, options, parser, renderer] import compiler/[ast, idents, lineinfos, modulegraphs, options, parser, renderer]
import "."/treesitter/api import "."/treesitter/api
@ -17,9 +17,19 @@ proc getPtrType*(str: string): string =
else: else:
str str
proc handleError*(conf: ConfigRef, info: TLineInfo, msg: TMsgKind, arg: string) =
# Raise exception in parseString() instead of exiting
raise newException(Exception, "")
proc parseString(nimState: NimState, str: string): PNode = proc parseString(nimState: NimState, str: string): PNode =
# Parse a string into Nim AST # Parse a string into Nim AST - use custom error handler that raises
result = parseString(str, nimState.identCache, nimState.config) # an exception rather than exiting on failure
try:
result = parseString(
str, nimState.identCache, nimState.config, errorHandler = handleError
)
except:
discard
proc getLit*(nimState: NimState, str: string): PNode = proc getLit*(nimState: NimState, str: string): PNode =
# Used to convert #define literals into const and expressions # Used to convert #define literals into const and expressions
@ -33,9 +43,8 @@ proc getLit*(nimState: NimState, str: string): PNode =
elif str.contains(re"^[\-]?[\d]*[.]?[\d]+$"): # float elif str.contains(re"^[\-]?[\d]*[.]?[\d]+$"): # float
result = newFloatNode(nkFloatLit, parseFloat(str)) result = newFloatNode(nkFloatLit, parseFloat(str))
# # TODO - hex becomes int on render elif str.contains(re"^0x[\da-fA-F]+$"): # hexadecimal
# elif str.contains(re"^0x[\da-fA-F]+$"): # hexadecimal result = nimState.parseString(str)
# result = newIntNode(nkIntLit, parseHexInt(str))
elif str.contains(re"^'[[:ascii:]]'$"): # char elif str.contains(re"^'[[:ascii:]]'$"): # char
result = newNode(nkCharLit) result = newNode(nkCharLit)
@ -46,8 +55,9 @@ proc getLit*(nimState: NimState, str: string): PNode =
else: else:
result = nimState.parseString(nimState.getNimExpression(str)) result = nimState.parseString(nimState.getNimExpression(str))
if result.isNil:
result = newNode(nkNilLit) if result.isNil:
result = newNode(nkNilLit)
proc getOverrideOrSkip(nimState: NimState, node: TSNode, origname: string, kind: NimSymKind): PNode = proc getOverrideOrSkip(nimState: NimState, node: TSNode, origname: string, kind: NimSymKind): PNode =
# Check if symbol `origname` of `kind` and `origname` has any cOverride defined # Check if symbol `origname` of `kind` and `origname` has any cOverride defined
@ -132,7 +142,9 @@ proc newConstDef(nimState: NimState, node: TSNode, fname = "", fval = ""): PNode
if name.Bl: if name.Bl:
# Name skipped or overridden since blank # Name skipped or overridden since blank
result = nimState.getOverrideOrSkip(node, origname, nskConst) result = nimState.getOverrideOrSkip(node, origname, nskConst)
elif valident.kind != nkNilLit: elif valident.kind in {nkCharLit .. nkStrLit} or
(valident.kind == nkStmtList and valident.len > 0 and
valident[0].kind in {nkCharLit .. nkStrLit}):
if nimState.addNewIdentifer(name): if nimState.addNewIdentifer(name):
# const X* = Y # const X* = Y
# #
@ -147,7 +159,11 @@ proc newConstDef(nimState: NimState, node: TSNode, fname = "", fval = ""): PNode
result = newNode(nkConstDef) result = newNode(nkConstDef)
result.add ident result.add ident
result.add newNode(nkEmpty) result.add newNode(nkEmpty)
result.add valident if valident.kind == nkStmtList and valident.len == 1:
# Collapse single line statement
result.add valident[0]
else:
result.add valident
else: else:
necho &"# const '{origname}' is duplicate, skipped" necho &"# const '{origname}' is duplicate, skipped"
else: else:
@ -576,11 +592,12 @@ proc newRecListTree(nimState: NimState, name: string, node: TSNode): PNode =
result = newNode(nkRecList) result = newNode(nkRecList)
for i in 0 ..< node.len: for i in 0 ..< node.len:
# Add nkIdentDefs for each field if node[i].getName() == "field_declaration":
let # Add nkIdentDefs for each field
field = nimState.newIdentDefs(name, node[i], i, exported = true) let
if not field.isNil: field = nimState.newIdentDefs(name, node[i], i, exported = true)
result.add field if not field.isNil:
result.add field
proc addTypeObject(nimState: NimState, node: TSNode, typeDef: PNode = nil, fname = "", istype = false, union = false) = proc addTypeObject(nimState: NimState, node: TSNode, typeDef: PNode = nil, fname = "", istype = false, union = false) =
# Add a type of object # Add a type of object

View file

@ -157,11 +157,11 @@ typedef struct { char *a1; int *a2[1]; } A19, *A19p;
typedef struct A20 { char a1; } A20, A21, *A21p; typedef struct A20 { char a1; } A20, A21, *A21p;
//Expression //Expression
typedef struct A22 { int **f1; int *f2[123+132]; } A22; typedef struct A22 { const int **f1; int *f2[123+132]; } A22;
//Unions //Unions
union U1 {int f1; float f2; }; union U1 {int f1; float f2; };
typedef union U2 { int **f1; int abc[123+132]; } U2; typedef union U2 { const int **f1; int abc[123+132]; } U2;
// Enums // Enums

View file

@ -41,17 +41,8 @@ cOverride:
proc weirdfunc(apple: ptr ptr ptr cchar): int {.importc.} proc weirdfunc(apple: ptr ptr ptr cchar): int {.importc.}
proc weirdfunc2(mango: ptr ptr cchar): int {.importc.} proc weirdfunc2(mango: ptr ptr cchar): int {.importc.}
# includeHeader const FLAGS {.strdefine.} = ""
const header = cImport(cSearchPath("test.h"), flags = FLAGS)
when defined(HEADER): " -H"
else: ""
# Test AST2
const mode =
when defined(AST2): " -f:ast2"
else: ""
cImport(cSearchPath("test.h"), flags = header & mode)
check TEST_INT == 512 check TEST_INT == 512
check TEST_FLOAT == 5.12 check TEST_FLOAT == 5.12

View file

@ -30,7 +30,8 @@ cPlugin:
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} = proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
sym.name = sym.name.replace("pcre_", "") sym.name = sym.name.replace("pcre_", "")
cImport(pcreH, dynlib="dynpcre") const FLAGS {.strdefine.} = ""
cImport(pcreH, dynlib="dynpcre", flags = FLAGS)
echo version() echo version()