aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Yakimowich-Payne <jyapayne@gmail.com>2020-04-23 19:21:57 -0600
committerJoey Yakimowich-Payne <jyapayne@gmail.com>2020-04-26 09:12:17 -0600
commit073dc5d35a70270c49234842bfbb7f3e608c539e (patch)
tree9c125f269f7f36dcb5b0558cdba465d585506924
parent9e799ee3b19ac152d1c2403c6af1e3806e1a31fe (diff)
downloadnimterop-073dc5d35a70270c49234842bfbb7f3e608c539e.tar.gz
nimterop-073dc5d35a70270c49234842bfbb7f3e608c539e.zip
Add hack for skipping types as root nodes
-rw-r--r--nimterop/ast2.nim30
-rw-r--r--nimterop/exprparser.nim21
2 files changed, 39 insertions, 12 deletions
diff --git a/nimterop/ast2.nim b/nimterop/ast2.nim
index 4fec237..bb41ebe 100644
--- a/nimterop/ast2.nim
+++ b/nimterop/ast2.nim
@@ -4,9 +4,9 @@ import options as opts
import compiler/[ast, idents, lineinfos, modulegraphs, msgs, options, renderer]
-import "."/treesitter/api
+import "."/treesitter/[api, c, cpp]
-import "."/[globals, getters, exprparser, comphelp]
+import "."/[globals, getters, exprparser, comphelp, tshelp]
proc getPtrType*(str: string): string =
result = case str:
@@ -99,8 +99,30 @@ proc newConstDef(gState: State, node: TSNode, fname = "", fval = ""): PNode =
fval
else:
gState.getNodeVal(node[1])
- valident =
- gState.parseCExpression(val)
+ var valident = newNode(nkNone)
+
+ withCodeAst(val, gState.mode):
+ # This section is a hack for determining that the first
+ # node is a type, which shouldn't be accepted by a const
+ # def section. Need to replace this with some other mechanism
+ # to handle type aliases
+ var maybeTyNode: TSNode
+ # Take the very first node, which may be 2 levels
+ # down if there is an error node
+ if root.len > 0 and root[0].getName() == "ERROR":
+ maybeTyNode = root[0][0]
+ elif root.len > 0:
+ maybeTyNode = root[0]
+
+ if not maybeTyNode.isNil:
+ let name = maybeTyNode.getName()
+ case name
+ of "type_descriptor", "sized_type_specifier":
+ discard
+ else:
+ # Can't do gState.parseCExpression(root) here for some reason?
+ # get a SEGFAULT if we use root
+ valident = gState.parseCExpression(val)
if name.Bl:
# Name skipped or overridden since blank
diff --git a/nimterop/exprparser.nim b/nimterop/exprparser.nim
index a59a546..6e102ea 100644
--- a/nimterop/exprparser.nim
+++ b/nimterop/exprparser.nim
@@ -547,18 +547,15 @@ proc processTSNode(gState: State, node: TSNode, typeofNode: var PNode): PNode =
decho "NODE RESULT: ", result
-proc parseCExpression*(gState: State, code: string, name = ""): PNode =
- ## Convert the C string to a nim PNode tree
- gState.currentExpr = code
- gState.currentTyCastName = name
+proc parseCExpression*(gState: State, codeRoot: TSNode, name = ""): PNode =
+ ## Parse a c expression from a root ts node
- result = newNode(nkNone)
- # This is used for keeping track of the type of the first
+ # This var is used for keeping track of the type of the first
# symbol used for type casting
var tnode: PNode = nil
+ result = newNode(nkNone)
try:
- withCodeAst(gState.currentExpr, gState.mode):
- result = gState.processTSNode(root, tnode)
+ result = gState.processTSNode(codeRoot, tnode)
except ExprParseError as e:
decho e.msg
result = newNode(nkNone)
@@ -566,6 +563,14 @@ proc parseCExpression*(gState: State, code: string, name = ""): PNode =
decho "UNEXPECTED EXCEPTION: ", e.msg
result = newNode(nkNone)
+proc parseCExpression*(gState: State, code: string, name = ""): PNode =
+ ## Convert the C string to a nim PNode tree
+ gState.currentExpr = code
+ gState.currentTyCastName = name
+
+ withCodeAst(gState.currentExpr, gState.mode):
+ result = gState.parseCExpression(root, name)
+
# Clear the state
gState.currentExpr = ""
gState.currentTyCastName = "" \ No newline at end of file